summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/ConfigManager.cpp9
-rw-r--r--src/Common/ConfigManager.h3
-rw-r--r--src/Common/Initializable.cpp9
-rw-r--r--src/Common/Initializable.h5
-rw-r--r--src/Common/LogManager.cpp35
-rw-r--r--src/Common/LogManager.h25
-rw-r--r--src/Common/Makefile.am3
-rw-r--r--src/Common/Makefile.in3
-rw-r--r--src/Common/RequestManager.cpp2
-rw-r--r--src/Common/RequestManager.h9
-rw-r--r--src/Common/SharedPtr.h101
-rw-r--r--src/Common/SingletonPtr.h84
12 files changed, 42 insertions, 246 deletions
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp
index 6727cc3..3bf0c85 100644
--- a/src/Common/ConfigManager.cpp
+++ b/src/Common/ConfigManager.cpp
@@ -21,6 +21,7 @@
#include "ConfigEntry.h"
#include "Configurable.h"
#include "Logger.h"
+#include "LogManager.h"
#include "Tokenizer.h"
#include <fstream>
@@ -128,5 +129,13 @@ void ConfigManager::finish() {
finished = true;
}
+ConfigManager::ConfigManager() : finished(false) {
+ registerConfigurable(LogManager::get());
+}
+
+ConfigManager::~ConfigManager() {
+ unregisterConfigurable(LogManager::get());
+}
+
}
}
diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h
index 036eed7..e7885b8 100644
--- a/src/Common/ConfigManager.h
+++ b/src/Common/ConfigManager.h
@@ -41,7 +41,8 @@ class ConfigManager {
std::set<Configurable*, Compare> configurables;
bool finished;
- ConfigManager() : finished(false) {}
+ ConfigManager();
+ ~ConfigManager();
void handleConfigEntry(const ConfigEntry &entry);
diff --git a/src/Common/Initializable.cpp b/src/Common/Initializable.cpp
index 65893a1..2a9aef3 100644
--- a/src/Common/Initializable.cpp
+++ b/src/Common/Initializable.cpp
@@ -21,24 +21,17 @@
#include "ConfigManager.h"
#include "Configurable.h"
-#include "LogManager.h"
#include "Logger.h"
namespace Mad {
namespace Common {
std::stack<Initializable*> Initializable::initializedObjects;
-bool Initializable::logInit = false;
void Initializable::init() {
if(initialized)
return;
- if(!logInit) {
- logInit = true;
- LogManager::get();
- }
-
if(initializing) {
Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies.");
std::terminate();
@@ -70,8 +63,6 @@ void Initializable::deinit() {
initializedObjects.pop();
}
-
- logInit = false;
}
}
diff --git a/src/Common/Initializable.h b/src/Common/Initializable.h
index 9f177da..6f67678 100644
--- a/src/Common/Initializable.h
+++ b/src/Common/Initializable.h
@@ -25,14 +25,9 @@
namespace Mad {
namespace Common {
-template <typename T> class SingletonPtr;
-
class Initializable {
private:
- template <typename T> friend class SingletonPtr;
-
static std::stack<Initializable*> initializedObjects;
- static bool logInit;
bool initializing;
bool initialized;
diff --git a/src/Common/LogManager.cpp b/src/Common/LogManager.cpp
index 5e902fa..5182a97 100644
--- a/src/Common/LogManager.cpp
+++ b/src/Common/LogManager.cpp
@@ -25,7 +25,7 @@
namespace Mad {
namespace Common {
-SingletonPtr<LogManager> LogManager::logManager;
+LogManager LogManager::logManager;
bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
@@ -35,7 +35,7 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
if(entry[0].getKey().matches("Logger")) {
if(entry[0][0].matches("Console")) {
if(entry[1].empty()) {
- registerLogger(SharedPtr<Logger>(new Backends::ConsoleLogger()));
+ registerLogger(static_cast<Logger*>(&consoleLogger));
return true;
}
}
@@ -49,19 +49,10 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
}
void LogManager::configFinished() {
- SharedPtr<Backends::ConsoleLogger> consoleLogger;
+ if(loggers.empty())
+ registerLogger(static_cast<Logger*>(&consoleLogger));
- if(loggers.empty() || remoteLoggers.empty()) {
- consoleLogger = SharedPtr<Backends::ConsoleLogger>(new Backends::ConsoleLogger());
-
- if(loggers.empty())
- registerLogger(consoleLogger.cast<Logger>());
-
- if(remoteLoggers.empty())
- registerLogger(consoleLogger.cast<RemoteLogger>());
- }
-
- std::auto_ptr<std::queue<Message> > queue = messageQueue;
+ std::auto_ptr<std::queue<Message> > queue = messageQueue;
while(!queue->empty()) {
const Message &message = queue->front();
@@ -85,11 +76,9 @@ void LogManager::log(MessageCategory category, MessageLevel level, time_t timest
return;
}
- for(std::set<SharedPtr<Logger> >::iterator it = loggers.begin(); it != loggers.end(); ++it) {
- SharedPtr<Logger> logger = *it;
-
- if(logger->getLevel() >= level && logger->isCategorySet(category))
- logger->logMessage(category, level, timestamp, message);
+ for(std::set<Logger*>::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) {
+ if((*logger)->getLevel() >= level && (*logger)->isCategorySet(category))
+ (*logger)->logMessage(category, level, timestamp, message);
}
}
@@ -100,11 +89,9 @@ void LogManager::log(MessageCategory category, MessageLevel level, time_t timest
return;
}
- for(std::set<SharedPtr<RemoteLogger> >::iterator it = remoteLoggers.begin(); it != remoteLoggers.end(); ++it) {
- SharedPtr<RemoteLogger> logger = *it;
-
- if(logger->getLevel() >= level && logger->isCategorySet(category))
- logger->logMessage(category, level, timestamp, message, source);
+ for(std::set<RemoteLogger*>::iterator logger = remoteLoggers.begin(); logger != remoteLoggers.end(); ++logger) {
+ if((*logger)->getLevel() >= level && (*logger)->isCategorySet(category))
+ (*logger)->logMessage(category, level, timestamp, message, source);
}
}
diff --git a/src/Common/LogManager.h b/src/Common/LogManager.h
index 7c0f1e0..5fc3824 100644
--- a/src/Common/LogManager.h
+++ b/src/Common/LogManager.h
@@ -23,8 +23,7 @@
#include "Configurable.h"
#include "Logger.h"
#include "RemoteLogger.h"
-#include "SingletonPtr.h"
-#include "SharedPtr.h"
+#include "Backends/ConsoleLogger.h"
#include <memory>
#include <queue>
@@ -53,42 +52,44 @@ class LogManager : public Configurable {
std::string source;
};
- static SingletonPtr<LogManager> logManager;
+ static LogManager logManager;
- std::set<SharedPtr<Logger> > loggers;
- std::set<SharedPtr<RemoteLogger> > remoteLoggers;
+ Backends::ConsoleLogger consoleLogger;
+
+ std::set<Logger*> loggers;
+ std::set<RemoteLogger*> remoteLoggers;
std::auto_ptr<std::queue<Message> > messageQueue;
std::auto_ptr<std::queue<RemoteMessage> > remoteMessageQueue;
+ LogManager() : messageQueue(new std::queue<Message>()), remoteMessageQueue(new std::queue<RemoteMessage>()) {}
+
protected:
virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled);
virtual void configFinished();
public:
- LogManager() : messageQueue(new std::queue<Message>()), remoteMessageQueue(new std::queue<RemoteMessage>()) {}
-
void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message);
void log(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message, const std::string &source);
- void registerLogger(SharedPtr<Logger> logger) {
+ void registerLogger(Logger *logger) {
loggers.insert(logger);
}
- void unregisterLogger(SharedPtr<Logger> logger) {
+ void unregisterLogger(Logger *logger) {
loggers.erase(logger);
}
- void registerLogger(SharedPtr<RemoteLogger> logger) {
+ void registerLogger(RemoteLogger *logger) {
remoteLoggers.insert(logger);
}
- void unregisterLogger(SharedPtr<RemoteLogger> logger) {
+ void unregisterLogger(RemoteLogger *logger) {
remoteLoggers.erase(logger);
}
static LogManager *get() {
- return logManager.get();
+ return &logManager;
}
};
diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am
index 755ab79..4494745 100644
--- a/src/Common/Makefile.am
+++ b/src/Common/Makefile.am
@@ -7,5 +7,4 @@ libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHa
noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \
Logger.h LoggerBase.h LogManager.h ModuleManager.h RemoteLogger.h Request.h \
- RequestBase.h RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h \
- SystemBackend.h Tokenizer.h
+ RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Tokenizer.h
diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in
index 6f8de60..ed307b0 100644
--- a/src/Common/Makefile.in
+++ b/src/Common/Makefile.in
@@ -233,8 +233,7 @@ libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Exception.cpp Initializ
libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la
noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Initializable.h \
Logger.h LoggerBase.h LogManager.h ModuleManager.h RemoteLogger.h Request.h \
- RequestBase.h RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h \
- SystemBackend.h Tokenizer.h
+ RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Tokenizer.h
all: all-recursive
diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp
index cc846c6..d852f5d 100644
--- a/src/Common/RequestManager.cpp
+++ b/src/Common/RequestManager.cpp
@@ -30,7 +30,7 @@
namespace Mad {
namespace Common {
-SingletonPtr<RequestManager> RequestManager::requestManager;
+RequestManager RequestManager::requestManager;
RequestManager::RequestMap::~RequestMap() {
diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h
index 0bc8080..c2f58d2 100644
--- a/src/Common/RequestManager.h
+++ b/src/Common/RequestManager.h
@@ -20,7 +20,6 @@
#ifndef MAD_COMMON_REQUESTMANAGER_H_
#define MAD_COMMON_REQUESTMANAGER_H_
-#include "SingletonPtr.h"
#include <Net/Connection.h>
#include <map>
@@ -65,7 +64,7 @@ class RequestManager {
}
};
- static SingletonPtr<RequestManager> requestManager;
+ static RequestManager requestManager;
std::map<Net::Connection*,RequestMap*> requestMaps;
bool core;
@@ -83,13 +82,13 @@ class RequestManager {
void receiveHandler(Net::Connection *connection, const Net::Packet &packet);
+ RequestManager();
+
public:
static RequestManager* get() {
- return requestManager.get();
+ return &requestManager;
}
- RequestManager();
-
bool isCore() const {return core;}
void setCore(bool newCore) {
core = newCore;
diff --git a/src/Common/SharedPtr.h b/src/Common/SharedPtr.h
deleted file mode 100644
index 3696344..0000000
--- a/src/Common/SharedPtr.h
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * SharedPtr.h
- *
- * Copyright (C) 2008 Johannes Thorn <dante@g4t3.de>
- *
- * This program is free software: you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef MAD_COMMON_SHAREDPTR_H_
-#define MAD_COMMON_SHAREDPTR_H_
-
-namespace Mad {
-namespace Common {
-
-template <typename T>
-class SharedPtr {
- protected:
- class Counter {
- public:
- Counter(T *o) : object(o), refCount(1), cCount(new unsigned int(1)) {}
- Counter(T *o, unsigned int *count) : object(o), refCount(1), cCount(count) {++*cCount;}
-
- virtual ~Counter() {
- if(--cCount)
- return;
-
- if(object)
- delete object;
-
- delete cCount;
- }
-
- T *object;
- unsigned int refCount;
- unsigned int *cCount;
- };
-
- Counter *counter;
-
- template <typename T2>
- class CastPtr : public SharedPtr<T2> {
- public:
- CastPtr(T *o, unsigned int *cCount) : SharedPtr<T2>(o, cCount) {}
- };
-
- SharedPtr(T *o, unsigned int *cCount) : counter(new Counter(o, cCount)) {}
-
- public:
- SharedPtr(T *o = 0) : counter(new Counter(o)) {}
-
- SharedPtr(const SharedPtr<T> &c) : counter(c.counter) {
- ++counter->refCount;
- }
-
- virtual ~SharedPtr() {
- if(--counter->refCount == 0)
- delete counter;
- }
-
- SharedPtr<T>& operator=(const SharedPtr<T>& c) {
- ++c.counter->refCount;
- if(--counter->refCount == 0)
- delete counter;
-
- counter = c.counter;
- return *this;
- }
-
- bool empty() const {
- return counter->object;
- }
-
- T* operator->() {return counter->object;}
- T& operator*() {return *counter->object;}
-
- const T* operator->() const {return counter->object;}
- const T& operator*() const {return *counter->object;}
-
- bool operator<(const SharedPtr<T> &o) const {return counter->object < o.counter->object;}
- bool operator==(const SharedPtr<T> &o) const {return counter->object == o.counter->object;}
-
- template <typename T2> SharedPtr<T2> cast() const {
- return CastPtr<T2>(counter->object, counter->cCount);
- }
-};
-
-}
-}
-
-#endif /*MAD_COMMON_SHAREDPTR_H_*/
diff --git a/src/Common/SingletonPtr.h b/src/Common/SingletonPtr.h
deleted file mode 100644
index 03d0040..0000000
--- a/src/Common/SingletonPtr.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * SingletonPtr.h
- *
- * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
- *
- * This program is free software: you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef MAD_COMMON_SINGLETONPTR_H_
-#define MAD_COMMON_SINGLETONPTR_H_
-
-#include "Initializable.h"
-#include "Configurable.h"
-#include "ConfigManager.h"
-
-namespace Mad {
-namespace Common {
-
-template <typename T>
-class SingletonPtr : public virtual Initializable {
- private:
- T *ptr;
-
- protected:
- virtual void doInit() {
- ptr = new T();
-
- Initializable *in = dynamic_cast<Initializable*>(ptr);
- if(in) {
- in->initialized = true;
- in->doInit();
- }
-
- Configurable *c = dynamic_cast<Configurable*>(ptr);
- if(c)
- ConfigManager::get()->registerConfigurable(c);
-
- if(in) {
- initializing = false;
- initialized = true;
- }
- }
-
- virtual void doDeinit() {
- Configurable *c = dynamic_cast<Configurable*>(ptr);
- if(c)
- ConfigManager::get()->unregisterConfigurable(c);
-
- Initializable *in = dynamic_cast<Initializable*>(ptr);
- if(in) {
- in->doDeinit();
- in->initialized = false;
- }
-
- delete ptr;
- ptr = 0;
- }
-
- public:
- SingletonPtr() : ptr(0) {}
-
- T *get() {
- if(!ptr)
- init();
-
- return ptr;
- }
-};
-
-}
-}
-
-#endif /* MAD_COMMON_SINGLETONPTR_H_ */