Skip to content

Commit efdf6d0

Browse files
committed
Use std::shared_ptr instead of _CountedRef
1 parent e2be445 commit efdf6d0

5 files changed

Lines changed: 40 additions & 31 deletions

File tree

EScript/Runtime/Runtime.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "../Utils/Logger.h"
2222
#include <algorithm>
2323
#include <iostream>
24+
#include <memory>
2425
#include <sstream>
2526
#include <stack>
2627

@@ -135,12 +136,12 @@ Runtime::Runtime() :
135136
internals(new RuntimeInternals(*this,
136137
EScript::getSGlobals()->clone(),
137138
std::make_shared<RuntimeInternals::SharedRuntimeContext>())),
138-
logger(new LoggerGroup(Logger::LOG_WARNING)){
139+
logger(Logger::LOG_WARNING){
139140

140141
declareConstant(internals->getGlobals(),"GLOBALS",internals->getGlobals());
141142
declareConstant(internals->getGlobals(),"SGLOBALS",EScript::getSGlobals());
142143

143-
logger->addLogger("coutLogger",new StdLogger(std::cout));
144+
logger.addLogger("coutLogger", std::make_shared<StdLogger>(std::cout));
144145
//ctor
145146
}
146147

@@ -155,11 +156,7 @@ Runtime::Runtime(const Runtime& other) :
155156
}
156157

157158
//! (dtor)
158-
Runtime::~Runtime() {
159-
// declareConstant(internals->getGlobals(), "GLOBALS",nullptr); //! \todo threading: Remove this and check the effect.
160-
internals.reset(nullptr);
161-
//dtor
162-
}
159+
Runtime::~Runtime() = default;
163160

164161
ERef<Runtime> Runtime::_fork()const{
165162
return new Runtime(*this);
@@ -247,7 +244,7 @@ size_t Runtime::getStackSize()const { return internals->getStackSize(); }
247244

248245
size_t Runtime::_getStackSizeLimit()const { return internals->_getStackSizeLimit(); }
249246

250-
void Runtime::info(const std::string & s) { logger->info(s); }
247+
void Runtime::info(const std::string & s) { logger.info(s); }
251248

252249
void Runtime::setAddStackInfoToExceptions(bool b) { internals->setAddStackInfoToExceptions(b); }
253250

@@ -261,7 +258,7 @@ void Runtime::_setStackSizeLimit(const size_t s) { internals->_setStackSizeLimit
261258

262259
void Runtime::setTreatWarningsAsError(bool b){
263260
if(b){ // --> disable coutLogger and add throwLogger
264-
Logger * coutLogger = logger->getLogger("coutLogger");
261+
Logger * coutLogger = logger.getLogger("coutLogger");
265262
if(coutLogger!=nullptr)
266263
coutLogger->setMinLevel(Logger::LOG_ERROR);
267264

@@ -272,12 +269,12 @@ void Runtime::setTreatWarningsAsError(bool b){
272269
public:
273270
ThrowLogger(Runtime & _rt) : Logger(LOG_PEDANTIC_WARNING,LOG_WARNING), rt(_rt){}
274271
};
275-
logger->addLogger("throwLogger",new ThrowLogger(*this));
272+
logger.addLogger("throwLogger", std::make_shared<ThrowLogger>(*this));
276273
}else{
277-
Logger * coutLogger = logger->getLogger("coutLogger");
274+
Logger * coutLogger = logger.getLogger("coutLogger");
278275
if(coutLogger!=nullptr)
279276
coutLogger->setMinLevel(Logger::LOG_ALL);
280-
logger->removeLogger("throwLogger");
277+
logger.removeLogger("throwLogger");
281278
}
282279
}
283280
void Runtime::throwException(const std::string & s,Object * obj){ internals->throwException(s,obj); }
@@ -330,22 +327,22 @@ class CountingLogger : public Logger{
330327
};
331328

332329
void Runtime::enableLogCounting(){
333-
if(logger->getLogger("countingLogger")==nullptr)
334-
logger->addLogger("countingLogger",new CountingLogger);
330+
if(logger.getLogger("countingLogger")==nullptr)
331+
logger.addLogger("countingLogger", std::make_shared<CountingLogger>());
335332
}
336333

337334
void Runtime::disableLogCounting(){
338-
logger->removeLogger("countingLogger");
335+
logger.removeLogger("countingLogger");
339336
}
340337

341338
void Runtime::resetLogCounter(Logger::level_t level){
342-
CountingLogger * l = dynamic_cast<CountingLogger*>(logger->getLogger("countingLogger"));
339+
CountingLogger * l = dynamic_cast<CountingLogger*>(logger.getLogger("countingLogger"));
343340
if(l!=nullptr)
344341
l->reset(level);
345342
}
346343

347344
uint32_t Runtime::getLogCounter(Logger::level_t level)const{
348-
CountingLogger * l = dynamic_cast<CountingLogger*>(logger->getLogger("countingLogger"));
345+
auto l = dynamic_cast<const CountingLogger*>(logger.getLogger("countingLogger"));
349346
return l==nullptr ? 0 : l->get(level);
350347
}
351348

EScript/Runtime/Runtime.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,21 @@ class Runtime : public ExtObject {
122122
std::string getCurrentFile()const;
123123
int getCurrentLine()const;
124124
uint32_t getLogCounter(Logger::level_t level)const;
125-
LoggerGroup * getLogger()const { return logger.get(); }
126-
Logger::level_t getLoggingLevel() { return logger->getMinLevel(); }
125+
LoggerGroup & getLogger() { return logger; }
126+
const LoggerGroup & getLogger() const { return logger; }
127+
Logger::level_t getLoggingLevel() { return logger.getMinLevel(); }
127128
std::string getStackInfo();
128129
std::string getLocalStackInfo();
129130

130-
void log(Logger::level_t l,const std::string & s) { logger->log(l,s); }
131+
void log(Logger::level_t l,const std::string & s) { logger.log(l,s); }
131132
void resetLogCounter(Logger::level_t level);
132133

133134
void setAddStackInfoToExceptions(bool b);
134-
void setLoggingLevel(Logger::level_t level) { logger->setMinLevel(level); }
135+
void setLoggingLevel(Logger::level_t level) { logger.setMinLevel(level); }
135136
void setTreatWarningsAsError(bool b);
136137

137138
private:
138-
_CountedRef<LoggerGroup> logger;
139+
LoggerGroup logger;
139140
// @}
140141

141142
};

EScript/Runtime/RuntimeInternals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ void RuntimeInternals::warn(const std::string & s)const {
12261226
if(getActiveFCC()){
12271227
os<<" ('" << getActiveFCC()->getUserFunction()->getCode().getFilename() << "':~"<<getCurrentLine()<<")";
12281228
}
1229-
runtime.getLogger()->warn(os.str());
1229+
runtime.getLogger().warn(os.str());
12301230
}
12311231

12321232
// -------------------------------------------------------------

EScript/Utils/Logger.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
// Licensed under the MIT License. See LICENSE file for details.
88
// ---------------------------------------------------------------------------------
99
#include "Logger.h"
10-
#include <stdexcept>
1110
#include <iostream>
11+
#include <memory>
12+
#include <stdexcept>
1213

1314
namespace EScript{
1415

1516

1617

1718
// ------------------------------------------------
1819
// LoggerGroup
19-
void LoggerGroup::addLogger(const std::string & name,Logger * logger){
20-
if(logger==nullptr)
20+
void LoggerGroup::addLogger(const std::string & name, const std::shared_ptr<Logger> & logger){
21+
if (!logger) {
2122
throw std::invalid_argument("addLogger(nullptr)");
23+
}
2224
loggerRegistry[name] = logger;
2325
}
2426

@@ -35,6 +37,13 @@ Logger * LoggerGroup::getLogger(const std::string & name){
3537
}
3638
return nullptr;
3739
}
40+
const Logger * LoggerGroup::getLogger(const std::string & name) const{
41+
const auto lbIt = loggerRegistry.lower_bound(name);
42+
if(lbIt!=loggerRegistry.cend() && !(loggerRegistry.key_comp()(name, lbIt->first)) ){
43+
return lbIt->second.get();
44+
}
45+
return nullptr;
46+
}
3847

3948
//! ---|> Logger
4049
void LoggerGroup::doLog(level_t l,const std::string & msg){

EScript/Utils/Logger.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
#include "ObjRef.h"
1414

1515
#include <map>
16-
#include <string>
16+
#include <memory>
1717
#include <ostream>
18+
#include <string>
1819

1920
namespace EScript {
2021

2122
//! [Logger]
22-
class Logger : public EReferenceCounter<Logger> {
23+
class Logger {
2324
public:
2425
enum level_t{
2526
LOG_ALL = 0,
@@ -65,16 +66,17 @@ class Logger : public EReferenceCounter<Logger> {
6566
class LoggerGroup : public Logger {
6667
public:
6768
LoggerGroup(level_t _minLevel = LOG_ALL,level_t _maxLevel = LOG_NONE) : Logger(_minLevel,_maxLevel){}
68-
virtual ~LoggerGroup(){}
69+
virtual ~LoggerGroup() = default;
6970

70-
void addLogger(const std::string & name,Logger * logger);
71+
void addLogger(const std::string & name, const std::shared_ptr<Logger> & logger);
7172
bool removeLogger(const std::string & name);
7273
void clearLoggers();
7374
Logger * getLogger(const std::string & name);
75+
const Logger * getLogger(const std::string & name) const;
7476
private:
7577
//! ---|> Logger
7678
void doLog(level_t l,const std::string & msg) override;
77-
typedef std::map<std::string, _CountedRef<Logger> > loggerRegistry_t;
79+
using loggerRegistry_t = std::map<std::string, std::shared_ptr<Logger>>;
7880
loggerRegistry_t loggerRegistry;
7981
};
8082

0 commit comments

Comments
 (0)