From 0a1df98c0420e9ac097622f2bddcd73a6488ed52 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 20 Nov 2008 00:33:28 +0100 Subject: SingletonPtr hinzugefuegt --- src/Common/Backends/SystemBackendPosix.cpp | 4 +- src/Common/Initializable.cpp | 12 ++++-- src/Common/Initializable.h | 1 + src/Common/LogManager.cpp | 2 +- src/Common/LogManager.h | 9 +++-- src/Common/Makefile.am | 2 +- src/Common/Makefile.in | 2 +- src/Common/RequestManager.cpp | 4 +- src/Common/RequestManager.h | 21 ++++++---- src/Common/SingletonPtr.h | 63 ++++++++++++++++++++++++++++++ 10 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 src/Common/SingletonPtr.h (limited to 'src/Common') diff --git a/src/Common/Backends/SystemBackendPosix.cpp b/src/Common/Backends/SystemBackendPosix.cpp index 62067c2..bff72db 100644 --- a/src/Common/Backends/SystemBackendPosix.cpp +++ b/src/Common/Backends/SystemBackendPosix.cpp @@ -131,7 +131,7 @@ void SystemBackendPosix::childHandler(int) { while((n = read(handle, buffer, sizeof(buffer))) > 0) output += std::string(buffer, n); - Net::FdManager::getFdManager()->unregisterFd(handle); + Net::FdManager::get()->unregisterFd(handle); close(handle); it2->second(status, output); @@ -266,7 +266,7 @@ bool SystemBackendPosix::execWithOutput(const sigc::slotregisterFd(pipeHandles[0], sigc::bind(sigc::ptr_fun(&SystemBackendPosix::outputHandler), pid), POLLIN); + Net::FdManager::get()->registerFd(pipeHandles[0], sigc::bind(sigc::ptr_fun(&SystemBackendPosix::outputHandler), pid), POLLIN); } dup2(saveStdout, STDOUT_FILENO); // restore old stdout diff --git a/src/Common/Initializable.cpp b/src/Common/Initializable.cpp index 9e7a49e..65893a1 100644 --- a/src/Common/Initializable.cpp +++ b/src/Common/Initializable.cpp @@ -28,12 +28,17 @@ 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(); @@ -41,9 +46,6 @@ void Initializable::init() { initializing = true; - if(!(LogManager::get()->isInitialized() || LogManager::get()->isInitializing())) - LogManager::get()->init(); - doInit(); Configurable *c = dynamic_cast(this); @@ -68,6 +70,8 @@ void Initializable::deinit() { initializedObjects.pop(); } + + logInit = false; } } diff --git a/src/Common/Initializable.h b/src/Common/Initializable.h index 6f67678..27ab5bf 100644 --- a/src/Common/Initializable.h +++ b/src/Common/Initializable.h @@ -28,6 +28,7 @@ namespace Common { class Initializable { private: static std::stack initializedObjects; + static bool logInit; bool initializing; bool initialized; diff --git a/src/Common/LogManager.cpp b/src/Common/LogManager.cpp index d974d7c..09cdd10 100644 --- a/src/Common/LogManager.cpp +++ b/src/Common/LogManager.cpp @@ -26,7 +26,7 @@ namespace Mad { namespace Common { -LogManager LogManager::logManager; +SingletonPtr LogManager::logManager; bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) { diff --git a/src/Common/LogManager.h b/src/Common/LogManager.h index 22a8c75..7c0f1e0 100644 --- a/src/Common/LogManager.h +++ b/src/Common/LogManager.h @@ -23,6 +23,7 @@ #include "Configurable.h" #include "Logger.h" #include "RemoteLogger.h" +#include "SingletonPtr.h" #include "SharedPtr.h" #include @@ -52,7 +53,7 @@ class LogManager : public Configurable { std::string source; }; - static LogManager logManager; + static SingletonPtr logManager; std::set > loggers; std::set > remoteLoggers; @@ -60,13 +61,13 @@ class LogManager : public Configurable { 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); @@ -87,7 +88,7 @@ class LogManager : public Configurable { } static LogManager *get() { - return &logManager; + return logManager.get(); } }; diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 20daebb..7375c64 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -7,4 +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 RemoteLogger.h Request.h RequestBase.h \ - RequestHandler.h RequestManager.h SharedPtr.h SystemBackend.h Tokenizer.h + RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h SystemBackend.h Tokenizer.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 3bd9f4a..b011031 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -207,7 +207,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 RemoteLogger.h Request.h RequestBase.h \ - RequestHandler.h RequestManager.h SharedPtr.h SystemBackend.h Tokenizer.h + RequestHandler.h RequestManager.h SharedPtr.h SingletonPtr.h SystemBackend.h Tokenizer.h all: all-recursive diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index e15ff67..3dfd1cf 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -30,7 +30,7 @@ namespace Mad { namespace Common { -RequestManager RequestManager::requestManager; +SingletonPtr RequestManager::requestManager; RequestManager::RequestMap::~RequestMap() { @@ -149,7 +149,7 @@ void RequestManager::unregisterPacketType(Net::Packet::Type type) { requestHandlerFactories.erase(it); } -RequestManager::RequestManager() : core(false) { +RequestManager::RequestManager() : core(false), requestId(-1) { registerPacketType(Net::Packet::DISCONNECT); } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index 723e619..0bc8080 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -20,7 +20,7 @@ #ifndef MAD_COMMON_REQUESTMANAGER_H_ #define MAD_COMMON_REQUESTMANAGER_H_ -#include "Initializable.h" +#include "SingletonPtr.h" #include #include @@ -32,7 +32,7 @@ namespace Common { class RequestBase; class RequestHandler; -class RequestManager : public Initializable { +class RequestManager { private: class RequestMap : private std::map { private: @@ -65,7 +65,7 @@ class RequestManager : public Initializable { } }; - static RequestManager requestManager; + static SingletonPtr requestManager; std::map requestMaps; bool core; @@ -81,19 +81,24 @@ class RequestManager : public Initializable { RequestManager(const RequestManager &o); RequestManager& operator=(const RequestManager &o); - RequestManager(); - void receiveHandler(Net::Connection *connection, const Net::Packet &packet); public: static RequestManager* get() { - return &requestManager; + return requestManager.get(); } + RequestManager(); + bool isCore() const {return core;} - void setCore(bool newCore) {core = newCore;} + void setCore(bool newCore) { + core = newCore; - virtual void doInit() {requestId = core ? -2 : -1;} + if(core) + requestId &= ~0x01; + else + requestId |= 0x01; + } void registerConnection(Net::Connection *connection); void unregisterConnection(Net::Connection *connection); diff --git a/src/Common/SingletonPtr.h b/src/Common/SingletonPtr.h new file mode 100644 index 0000000..ea4eda1 --- /dev/null +++ b/src/Common/SingletonPtr.h @@ -0,0 +1,63 @@ +/* + * 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" + +namespace Mad { +namespace Common { + +template +class SingletonPtr : public virtual Initializable { + private: + T *ptr; + + protected: + virtual void doInit() { + if(ptr) + delete ptr; + + ptr = new T(); + + Initializable *in = dynamic_cast(ptr); + if(in) + in->init(); + } + + public: + SingletonPtr() : ptr(0) {} + + virtual ~SingletonPtr() { + delete ptr; + } + + T *get() { + if(!ptr) + init(); + + return ptr; + } +}; + +} +} + +#endif /* MAD_COMMON_SINGLETONPTR_H_ */ -- cgit v1.2.3