From 389960211861736ef321df82f6abcb59f6302897 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 17 Dec 2008 21:32:20 +0100 Subject: Sinnlose Klassen SharedPtr und SingletonPtr entfernt --- src/Client/InformationManager.cpp | 6 +-- src/Client/InformationManager.h | 19 ++++--- src/Common/ConfigManager.cpp | 9 ++++ src/Common/ConfigManager.h | 3 +- src/Common/Initializable.cpp | 9 ---- src/Common/Initializable.h | 5 -- src/Common/LogManager.cpp | 35 +++++-------- src/Common/LogManager.h | 25 +++++----- src/Common/Makefile.am | 3 +- src/Common/Makefile.in | 3 +- src/Common/RequestManager.cpp | 2 +- src/Common/RequestManager.h | 9 ++-- src/Common/SharedPtr.h | 101 -------------------------------------- src/Common/SingletonPtr.h | 84 ------------------------------- src/Core/ConnectionManager.cpp | 6 +-- src/Core/ConnectionManager.h | 13 ++--- src/mad.cpp | 2 +- src/madc.cpp | 2 +- 18 files changed, 68 insertions(+), 268 deletions(-) delete mode 100644 src/Common/SharedPtr.h delete mode 100644 src/Common/SingletonPtr.h diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index a2f67cb..cef0ce1 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -28,7 +28,7 @@ namespace Mad { namespace Client { -Common::SingletonPtr InformationManager::informationManager; +InformationManager InformationManager::informationManager; void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection *connection, const Net::Packet &packet) { @@ -56,11 +56,11 @@ void InformationManager::DaemonStateUpdateRequest::handlePacket(Net::Connection } -InformationManager::InformationManager() : updating(false) { +void InformationManager::doInit() { Common::RequestManager::get()->registerPacketType(Net::Packet::DAEMON_STATE_UPDATE); } -InformationManager::~InformationManager() { +void InformationManager::doDeinit() { Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_STATE_UPDATE); } diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index 1464e2f..80dab9f 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -24,8 +24,8 @@ #include #include +#include #include -#include namespace Mad { @@ -40,7 +40,7 @@ class HostListPacket; namespace Client { -class InformationManager { +class InformationManager : public Common::Initializable { private: class DaemonStateUpdateRequest : public Common::RequestHandler { protected: @@ -50,7 +50,7 @@ class InformationManager { DaemonStateUpdateRequest() {} }; - static Common::SingletonPtr informationManager; + static InformationManager informationManager; std::map daemons; @@ -60,16 +60,19 @@ class InformationManager { InformationManager(const InformationManager &o); InformationManager& operator=(const InformationManager &o); + InformationManager() : updating(false) {} + void daemonListRequestFinished(const Common::Request &request); + protected: + virtual void doInit(); + virtual void doDeinit(); + public: - static InformationManager* get() { - return informationManager.get(); + static InformationManager *get() { + return &informationManager; } - InformationManager(); - virtual ~InformationManager(); - void updateDaemonList(Net::Connection *con); bool isUpdating() const { 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 @@ -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 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::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 class SingletonPtr; - class Initializable { private: - template friend class SingletonPtr; - static std::stack 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; 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(new Backends::ConsoleLogger())); + registerLogger(static_cast(&consoleLogger)); return true; } } @@ -49,19 +49,10 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { } void LogManager::configFinished() { - SharedPtr consoleLogger; + if(loggers.empty()) + registerLogger(static_cast(&consoleLogger)); - if(loggers.empty() || remoteLoggers.empty()) { - consoleLogger = SharedPtr(new Backends::ConsoleLogger()); - - if(loggers.empty()) - registerLogger(consoleLogger.cast()); - - if(remoteLoggers.empty()) - registerLogger(consoleLogger.cast()); - } - - std::auto_ptr > queue = messageQueue; + std::auto_ptr > 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 >::iterator it = loggers.begin(); it != loggers.end(); ++it) { - SharedPtr logger = *it; - - if(logger->getLevel() >= level && logger->isCategorySet(category)) - logger->logMessage(category, level, timestamp, message); + for(std::set::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 >::iterator it = remoteLoggers.begin(); it != remoteLoggers.end(); ++it) { - SharedPtr logger = *it; - - if(logger->getLevel() >= level && logger->isCategorySet(category)) - logger->logMessage(category, level, timestamp, message, source); + for(std::set::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 #include @@ -53,42 +52,44 @@ class LogManager : public Configurable { std::string source; }; - static SingletonPtr logManager; + static LogManager logManager; - std::set > loggers; - std::set > remoteLoggers; + Backends::ConsoleLogger consoleLogger; + + std::set loggers; + std::set remoteLoggers; std::auto_ptr > messageQueue; std::auto_ptr > remoteMessageQueue; + LogManager() : messageQueue(new std::queue()), remoteMessageQueue(new std::queue()) {} + protected: virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); virtual void configFinished(); public: - LogManager() : messageQueue(new std::queue()), remoteMessageQueue(new std::queue()) {} - 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) { + void registerLogger(Logger *logger) { loggers.insert(logger); } - void unregisterLogger(SharedPtr logger) { + void unregisterLogger(Logger *logger) { loggers.erase(logger); } - void registerLogger(SharedPtr logger) { + void registerLogger(RemoteLogger *logger) { remoteLoggers.insert(logger); } - void unregisterLogger(SharedPtr 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::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 #include @@ -65,7 +64,7 @@ class RequestManager { } }; - static SingletonPtr requestManager; + static RequestManager requestManager; std::map 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 - * - * 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 . - */ - -#ifndef MAD_COMMON_SHAREDPTR_H_ -#define MAD_COMMON_SHAREDPTR_H_ - -namespace Mad { -namespace Common { - -template -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 - class CastPtr : public SharedPtr { - public: - CastPtr(T *o, unsigned int *cCount) : SharedPtr(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 &c) : counter(c.counter) { - ++counter->refCount; - } - - virtual ~SharedPtr() { - if(--counter->refCount == 0) - delete counter; - } - - SharedPtr& operator=(const SharedPtr& 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 &o) const {return counter->object < o.counter->object;} - bool operator==(const SharedPtr &o) const {return counter->object == o.counter->object;} - - template SharedPtr cast() const { - return CastPtr(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 - * - * 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 . - */ - -#ifndef MAD_COMMON_SINGLETONPTR_H_ -#define MAD_COMMON_SINGLETONPTR_H_ - -#include "Initializable.h" -#include "Configurable.h" -#include "ConfigManager.h" - -namespace Mad { -namespace Common { - -template -class SingletonPtr : public virtual Initializable { - private: - T *ptr; - - protected: - virtual void doInit() { - ptr = new T(); - - Initializable *in = dynamic_cast(ptr); - if(in) { - in->initialized = true; - in->doInit(); - } - - Configurable *c = dynamic_cast(ptr); - if(c) - ConfigManager::get()->registerConfigurable(c); - - if(in) { - initializing = false; - initialized = true; - } - } - - virtual void doDeinit() { - Configurable *c = dynamic_cast(ptr); - if(c) - ConfigManager::get()->unregisterConfigurable(c); - - Initializable *in = dynamic_cast(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_ */ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 63b29e1..c347e29 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -42,7 +42,7 @@ namespace Mad { namespace Core { -Common::SingletonPtr ConnectionManager::connectionManager; +ConnectionManager ConnectionManager::connectionManager; void ConnectionManager::updateState(const std::string &name, Common::HostInfo::State state) { @@ -129,7 +129,7 @@ void ConnectionManager::configFinished() { } } -ConnectionManager::ConnectionManager() { +void ConnectionManager::doInit() { Common::RequestManager::get()->setCore(true); Net::Connection::init(); @@ -146,7 +146,7 @@ ConnectionManager::ConnectionManager() { Common::RequestManager::get()->registerPacketType(Net::Packet::LOG); } -ConnectionManager::~ConnectionManager() { +void ConnectionManager::doDeinit() { for(std::list::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) delete *con; diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index fdc8a46..4623ab6 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -29,7 +29,6 @@ #include #include #include -#include #include @@ -46,7 +45,7 @@ namespace Core { class ConnectionManager : public Common::Configurable, public Common::Initializable { private: - static Common::SingletonPtr connectionManager; + static ConnectionManager connectionManager; std::string x509TrustFile, x509CrlFile, x509CertFile, x509KeyFile; @@ -67,16 +66,18 @@ class ConnectionManager : public Common::Configurable, public Common::Initializa void updateState(const std::string &name, Common::HostInfo::State state); + ConnectionManager() {} + protected: virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool handled); virtual void configFinished(); - public: - ConnectionManager(); - virtual ~ConnectionManager(); + virtual void doInit(); + virtual void doDeinit(); + public: static ConnectionManager* get() { - return connectionManager.get(); + return &connectionManager; } void run(); diff --git a/src/mad.cpp b/src/mad.cpp index ccf4625..f5fcaa6 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -64,7 +64,7 @@ int main() { Common::RequestManager::get()->registerConnection(connection); - Common::SharedPtr networkLogger = new Daemon::Backends::NetworkLogger(connection); + Common::Logger *networkLogger = new Daemon::Backends::NetworkLogger(connection); Common::LogManager::get()->registerLogger(networkLogger); diff --git a/src/madc.cpp b/src/madc.cpp index 5dcb6ad..ac3b3a3 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -79,7 +79,7 @@ int main(int argc, char *argv[]) { Net::Connection::init(); - Common::LogManager::get(); + Client::InformationManager::get()->init(); Common::ConfigManager::get()->finish(); -- cgit v1.2.3