summaryrefslogtreecommitdiffstats
path: root/src/Core
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-06-18 22:03:02 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-06-18 22:03:02 +0200
commit7234fe326d16d6bf9f4374a09ddc6ef790e6723f (patch)
tree437d4c40eeb1e9b34b369e4b82064a1572c7dac9 /src/Core
parentbf561f8226e97f4ace4f04bddf198175e91ee7f0 (diff)
downloadmad-7234fe326d16d6bf9f4374a09ddc6ef790e6723f.tar
mad-7234fe326d16d6bf9f4374a09ddc6ef790e6723f.zip
Globale Variablen durch Application-Klasse ersetzt
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/Application.cpp (renamed from src/Core/Logger.cpp)39
-rw-r--r--src/Core/Application.h94
-rw-r--r--src/Core/CMakeLists.txt4
-rw-r--r--src/Core/ConfigManager.cpp16
-rw-r--r--src/Core/ConfigManager.h12
-rw-r--r--src/Core/Initializable.cpp69
-rw-r--r--src/Core/Initializable.h54
-rw-r--r--src/Core/LogManager.cpp16
-rw-r--r--src/Core/LogManager.h11
-rw-r--r--src/Core/Logger.h26
-rw-r--r--src/Core/RemoteLogger.h1
-rw-r--r--src/Core/Signals/GenericSignal.h2
-rw-r--r--src/Core/Signals/Signal0.h5
-rw-r--r--src/Core/Signals/Signal1.h5
-rw-r--r--src/Core/Signals/Signal2.h5
-rw-r--r--src/Core/Signals/SignalBase.h11
-rw-r--r--src/Core/ThreadManager.cpp23
-rw-r--r--src/Core/ThreadManager.h29
18 files changed, 189 insertions, 233 deletions
diff --git a/src/Core/Logger.cpp b/src/Core/Application.cpp
index e4e0341..7c92ed6 100644
--- a/src/Core/Logger.cpp
+++ b/src/Core/Application.cpp
@@ -1,7 +1,7 @@
/*
- * Logger.cpp
+ * Application.cpp
*
- * Copyright (C) 2008 Johannes Thorn <dante@g4t3.de>
+ * Copyright (C) 2009 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
@@ -17,15 +17,26 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "Logger.h"
+#include "Application.h"
+#include "ConfigManager.h"
#include "LogManager.h"
+#include "ThreadManager.h"
#include <cstdlib>
namespace Mad {
namespace Core {
-void Logger::logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap) {
+Application::Application() : configManager(new ConfigManager(this)), logManager(new LogManager(this)), threadManager(new ThreadManager(this)) {}
+
+Application::~Application() {
+ delete threadManager;
+ delete logManager;
+ delete configManager;
+}
+
+
+void Application::logfv(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, va_list ap) {
int size = 100;
char *buf = (char*)std::malloc(size);
@@ -38,7 +49,7 @@ void Logger::logfv(MessageCategory category, MessageLevel level, const char *for
va_end(ap2);
if(n > -1 && n < size) {
- log(category, level, buf);
+ logManager->log(category, level, std::time(0), buf);
std::free(buf);
return;
}
@@ -53,35 +64,35 @@ void Logger::logfv(MessageCategory category, MessageLevel level, const char *for
}
-void Logger::log(MessageCategory category, MessageLevel level, const std::string &message) {
- LogManager::get()->log(category, level, std::time(0), message);
+void Application::log(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const std::string &message) {
+ logManager->log(category, level, std::time(0), message);
}
-void Logger::logf(MessageCategory category, MessageLevel level, const char *format, ...) {
+void Application::logf(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, ...) {
va_list ap;
va_start(ap, format);
logfv(category, level, format, ap);
va_end(ap);
}
-void Logger::logf(MessageCategory category, const char *format, ...) {
+void Application::logf(LoggerBase::MessageCategory category, const char *format, ...) {
va_list ap;
va_start(ap, format);
- logfv(category, DEFAULT, format, ap);
+ logfv(category, LoggerBase::DEFAULT, format, ap);
va_end(ap);
}
-void Logger::logf(MessageLevel level, const char *format, ...) {
+void Application::logf(LoggerBase::MessageLevel level, const char *format, ...) {
va_list ap;
va_start(ap, format);
- logfv(GENERAL, level, format, ap);
+ logfv(LoggerBase::GENERAL, level, format, ap);
va_end(ap);
}
-void Logger::logf(const char *format, ...) {
+void Application::logf(const char *format, ...) {
va_list ap;
va_start(ap, format);
- logfv(GENERAL, DEFAULT, format, ap);
+ logfv(LoggerBase::GENERAL, LoggerBase::DEFAULT, format, ap);
va_end(ap);
}
diff --git a/src/Core/Application.h b/src/Core/Application.h
new file mode 100644
index 0000000..bfc23db
--- /dev/null
+++ b/src/Core/Application.h
@@ -0,0 +1,94 @@
+/*
+ * Application.h
+ *
+ * Copyright (C) 2009 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_CORE_APPLICATION_H_
+#define MAD_CORE_APPLICATION_H_
+
+#include "LoggerBase.h"
+
+#include <cstdarg>
+#include <string>
+
+#include <boost/asio/io_service.hpp>
+#include <boost/noncopyable.hpp>
+
+namespace Mad {
+namespace Core {
+
+class ConfigManager;
+class LogManager;
+class ThreadManager;
+
+class Application : private boost::noncopyable {
+ private:
+ boost::asio::io_service ioService;
+
+ ConfigManager *configManager;
+ LogManager *logManager;
+ ThreadManager *threadManager;
+
+ void logfv(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, va_list ap);
+
+ protected:
+ Application();
+ virtual ~Application();
+
+ public:
+ boost::asio::io_service& getIOService() {
+ return ioService;
+ }
+
+ ConfigManager* getConfigManager() const {
+ return configManager;
+ }
+
+ LogManager* getLogManager() const {
+ return logManager;
+ }
+
+ ThreadManager* getThreadManager() const {
+ return threadManager;
+ }
+
+
+ void log(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const std::string &message);
+
+ void log(LoggerBase::MessageCategory category, const std::string &message) {
+ log(category, LoggerBase::DEFAULT, message);
+ }
+
+ void log(LoggerBase::MessageLevel level, const std::string &message) {
+ log(LoggerBase::GENERAL, level, message);
+ }
+
+ void log(const std::string &message) {
+ log(LoggerBase::GENERAL, LoggerBase::DEFAULT, message);
+ }
+
+
+ void logf(LoggerBase::MessageCategory category, LoggerBase::MessageLevel level, const char *format, ...);
+ void logf(LoggerBase::MessageCategory category, const char *format, ...);
+ void logf(LoggerBase::MessageLevel level, const char *format, ...);
+ void logf(const char *format, ...);
+};
+
+}
+}
+
+#endif /* MAD_CORE_APPLICATION_H_ */
diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt
index 30947e3..9693460 100644
--- a/src/Core/CMakeLists.txt
+++ b/src/Core/CMakeLists.txt
@@ -9,12 +9,12 @@ add_library(Core
Signals/Signal2.h
Signals/SignalBase.h
+ Application.cpp Application.h
ConfigEntry.cpp ConfigEntry.h
ConfigManager.cpp ConfigManager.h
Configurable.h
Exception.cpp Exception.h
- Initializable.cpp Initializable.h
- Logger.cpp Logger.h
+ Logger.h
LoggerBase.h
LogManager.cpp LogManager.h
RemoteLogger.h
diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp
index 8b1ebd4..9e0dada 100644
--- a/src/Core/ConfigManager.cpp
+++ b/src/Core/ConfigManager.cpp
@@ -17,11 +17,10 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "Application.h"
#include "ConfigManager.h"
#include "ConfigEntry.h"
#include "Configurable.h"
-#include "Logger.h"
-#include "LogManager.h"
#include "Tokenizer.h"
#include <fstream>
@@ -30,9 +29,6 @@
namespace Mad {
namespace Core {
-ConfigManager ConfigManager::configManager;
-
-
bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) {
if(c1->getPriority() != c2->getPriority())
return c1->getPriority() > c2->getPriority();
@@ -50,7 +46,7 @@ void ConfigManager::handleConfigEntry(const ConfigEntry &entry) {
}
if(!handled)
- Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str());
+ application->logf(LoggerBase::WARNING, "Invalid config option '%s'.", entry[entry.getSize()-1].getKey().c_str());
}
bool ConfigManager::loadFile(const std::string &filename) {
@@ -129,13 +125,5 @@ void ConfigManager::finish() {
finished = true;
}
-ConfigManager::ConfigManager() : finished(false) {
- registerConfigurable(LogManager::get());
-}
-
-ConfigManager::~ConfigManager() {
- unregisterConfigurable(LogManager::get());
-}
-
}
}
diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h
index 2d49a97..d7d7d01 100644
--- a/src/Core/ConfigManager.h
+++ b/src/Core/ConfigManager.h
@@ -27,6 +27,7 @@
namespace Mad {
namespace Core {
+class Application;
class ConfigEntry;
class Configurable;
@@ -36,13 +37,14 @@ class ConfigManager {
bool operator() (const Configurable *c1, const Configurable *c2);
};
- static ConfigManager configManager;
+ friend class Application;
+
+ Application *application;
std::set<Configurable*, Compare> configurables;
bool finished;
- ConfigManager();
- ~ConfigManager();
+ ConfigManager(Application *application0) : application(application0), finished(false) {}
void handleConfigEntry(const ConfigEntry &entry);
@@ -57,10 +59,6 @@ class ConfigManager {
void unregisterConfigurable(Configurable *c) {
configurables.erase(c);
}
-
- static ConfigManager *get() {
- return &configManager;
- }
};
}
diff --git a/src/Core/Initializable.cpp b/src/Core/Initializable.cpp
deleted file mode 100644
index 95d527f..0000000
--- a/src/Core/Initializable.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Initializable.cpp
- *
- * 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/>.
- */
-
-#include "Initializable.h"
-
-#include "ConfigManager.h"
-#include "Configurable.h"
-#include "Logger.h"
-
-namespace Mad {
-namespace Core {
-
-std::stack<Initializable*> Initializable::initializedObjects;
-
-void Initializable::init() {
- if(initialized)
- return;
-
- if(initializing) {
- Logger::log(Logger::CRITICAL, "Fatal initialization error: cyclic dependencies.");
- std::terminate();
- }
-
- initializing = true;
-
- doInit();
-
- Configurable *c = dynamic_cast<Configurable*>(this);
- if(c)
- ConfigManager::get()->registerConfigurable(c);
-
- initializing = false;
- initialized = true;
- initializedObjects.push(this);
-}
-
-void Initializable::deinit() {
- while(!initializedObjects.empty()) {
- Initializable *in = initializedObjects.top();
-
- Configurable *c = dynamic_cast<Configurable*>(in);
- if(c)
- ConfigManager::get()->unregisterConfigurable(c);
-
- in->doDeinit();
- in->initialized = false;
-
- initializedObjects.pop();
- }
-}
-
-}
-}
diff --git a/src/Core/Initializable.h b/src/Core/Initializable.h
deleted file mode 100644
index e7d329d..0000000
--- a/src/Core/Initializable.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Initializable.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_CORE_INITIALIZABLE_H_
-#define MAD_CORE_INITIALIZABLE_H_
-
-#include <stack>
-
-namespace Mad {
-namespace Core {
-
-class Initializable {
- private:
- static std::stack<Initializable*> initializedObjects;
-
- bool initializing;
- bool initialized;
-
- protected:
- Initializable() : initializing(false), initialized(false) {}
- virtual void doInit() {}
- virtual void doDeinit() {}
-
- public:
- virtual ~Initializable() {}
-
- void init();
-
- bool isInitialized() const {return initialized;}
- bool isInitializing() const {return initializing;}
-
- static void deinit();
-};
-
-}
-}
-
-#endif /* MAD_CORE_INITIALIZABLE_H_ */
diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp
index 91149f9..28b1f6f 100644
--- a/src/Core/LogManager.cpp
+++ b/src/Core/LogManager.cpp
@@ -18,16 +18,15 @@
*/
#include "LogManager.h"
+#include "Application.h"
#include "ConfigEntry.h"
+#include "ConfigManager.h"
#include <iostream>
namespace Mad {
namespace Core {
-LogManager LogManager::logManager;
-
-
void LogManager::ConsoleLogger::logMessage(MessageCategory category _UNUSED_PARAMETER_, MessageLevel level, time_t timestamp _UNUSED_PARAMETER_, const std::string &message) {
if(level != CRITICAL) {// Critical messages are printed to cerr directly, so don't print them a second time
cerrLock.lock();
@@ -49,6 +48,15 @@ void LogManager::ConsoleLogger::logMessageDirect(MessageCategory category _UNUSE
}
+LogManager::LogManager(Application *application0) : application(application0), consoleLogger(new ConsoleLogger), configured(false), running(false) {
+ application->getConfigManager()->registerConfigurable(this);
+}
+
+LogManager::~LogManager() {
+ application->getConfigManager()->unregisterConfigurable(this);
+}
+
+
bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
if(handled)
return false;
@@ -61,7 +69,7 @@ bool LogManager::handleConfigEntry(const ConfigEntry &entry, bool handled) {
}
}
else if(entry[1].empty()) {
- Logger::logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str());
+ application->logf(Logger::WARNING, "Unknown logger '%s'.", entry[0][0].c_str());
return true;
}
}
diff --git a/src/Core/LogManager.h b/src/Core/LogManager.h
index 8c8d091..0ed21b8 100644
--- a/src/Core/LogManager.h
+++ b/src/Core/LogManager.h
@@ -35,10 +35,12 @@
namespace Mad {
namespace Core {
+class Application;
class ThreadManager;
class LogManager : public Configurable {
private:
+ friend class Application;
friend class ThreadManager;
typedef LoggerBase::MessageCategory MessageCategory;
@@ -76,7 +78,7 @@ class LogManager : public Configurable {
};
- static LogManager logManager;
+ Application *application;
boost::shared_ptr<ConsoleLogger> consoleLogger;
@@ -101,7 +103,8 @@ class LogManager : public Configurable {
queueCond.notify_one();
}
- LogManager() : consoleLogger(new ConsoleLogger), configured(false), running(false) {}
+ LogManager(Application *application0);
+ ~LogManager();
protected:
virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled);
@@ -134,10 +137,6 @@ class LogManager : public Configurable {
remoteLoggers.erase(logger);
remoteLoggerLock.unlock();
}
-
- static LogManager *get() {
- return &logManager;
- }
};
}
diff --git a/src/Core/Logger.h b/src/Core/Logger.h
index 3de22d2..6a8e752 100644
--- a/src/Core/Logger.h
+++ b/src/Core/Logger.h
@@ -22,7 +22,6 @@
#include "LoggerBase.h"
-#include <cstdarg>
#include <ctime>
#include <string>
@@ -32,34 +31,11 @@ namespace Core {
class LogManager;
class Logger : public LoggerBase {
- private:
+ protected:
friend class LogManager;
- static void logfv(MessageCategory category, MessageLevel level, const char *format, va_list ap);
-
- protected:
virtual void logMessage(MessageCategory category, MessageLevel level, time_t timestamp, const std::string &message) = 0;
- public:
- static void log(MessageCategory category, MessageLevel level, const std::string &message);
-
- static void log(MessageCategory category, const std::string &message) {
- log(category, DEFAULT, message);
- }
-
- static void log(MessageLevel level, const std::string &message) {
- log(GENERAL, level, message);
- }
-
- static void log(const std::string &message) {
- log(GENERAL, DEFAULT, message);
- }
-
-
- static void logf(MessageCategory category, MessageLevel level, const char *format, ...);
- static void logf(MessageCategory category, const char *format, ...);
- static void logf(MessageLevel level, const char *format, ...);
- static void logf(const char *format, ...);
};
}
diff --git a/src/Core/RemoteLogger.h b/src/Core/RemoteLogger.h
index f5c8a6b..ca982a9 100644
--- a/src/Core/RemoteLogger.h
+++ b/src/Core/RemoteLogger.h
@@ -22,6 +22,7 @@
#include "LoggerBase.h"
+#include <ctime>
#include <string>
namespace Mad {
diff --git a/src/Core/Signals/GenericSignal.h b/src/Core/Signals/GenericSignal.h
index 641f575..af5bae9 100644
--- a/src/Core/Signals/GenericSignal.h
+++ b/src/Core/Signals/GenericSignal.h
@@ -37,6 +37,8 @@ class GenericSignal : protected SignalBase {
protected:
std::map<Connection, slot_type> handlers;
+ GenericSignal(Application *application) : SignalBase(application) {}
+
public:
Connection connect(const slot_type &slot) {
boost::lock_guard<boost::mutex> lock(mutex);
diff --git a/src/Core/Signals/Signal0.h b/src/Core/Signals/Signal0.h
index ccfb548..c01a82a 100644
--- a/src/Core/Signals/Signal0.h
+++ b/src/Core/Signals/Signal0.h
@@ -21,6 +21,7 @@
#define MAD_CORE_SIGNALS_SIGNAL0_H_
#include "GenericSignal.h"
+#include "../Application.h"
#include "../ThreadManager.h"
#include <boost/function.hpp>
@@ -31,11 +32,13 @@ namespace Signals {
class Signal0 : public GenericSignal<boost::function0<void> > {
public:
+ Signal0(Application *application) : GenericSignal<boost::function0<void> >(application) {}
+
void emit() {
boost::lock_guard<boost::mutex> lock(mutex);
for(std::map<Connection, slot_type>::iterator handler = handlers.begin(); handler != handlers.end(); ++handler)
- ThreadManager::get()->pushWork(handler->second);
+ getApplication()->getThreadManager()->pushWork(handler->second);
}
};
diff --git a/src/Core/Signals/Signal1.h b/src/Core/Signals/Signal1.h
index e4a946c..0a03c39 100644
--- a/src/Core/Signals/Signal1.h
+++ b/src/Core/Signals/Signal1.h
@@ -21,6 +21,7 @@
#define MAD_CORE_SIGNALS_SIGNAL1_H_
#include "GenericSignal.h"
+#include "../Application.h"
#include "../ThreadManager.h"
#include <boost/function.hpp>
@@ -32,11 +33,13 @@ namespace Signals {
template <typename T1>
class Signal1 : public GenericSignal<boost::function1<void, T1> > {
public:
+ Signal1(Application *application) : GenericSignal<boost::function1<void, T1> >(application) {}
+
void emit(T1 arg1) {
boost::lock_guard<boost::mutex> lock(this->mutex);
for(typename std::map<Connection, typename GenericSignal<boost::function1<void, T1> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler)
- ThreadManager::get()->pushWork(boost::bind(handler->second, arg1));
+ this->getApplication()->getThreadManager()->pushWork(boost::bind(handler->second, arg1));
}
};
diff --git a/src/Core/Signals/Signal2.h b/src/Core/Signals/Signal2.h
index 41045d7..0966258 100644
--- a/src/Core/Signals/Signal2.h
+++ b/src/Core/Signals/Signal2.h
@@ -21,6 +21,7 @@
#define MAD_CORE_SIGNALS_SIGNAL2_H_
#include "GenericSignal.h"
+#include "../Application.h"
#include "../ThreadManager.h"
#include <boost/function.hpp>
@@ -32,11 +33,13 @@ namespace Signals {
template <typename T1, typename T2>
class Signal2 : public GenericSignal<boost::function2<void, T1, T2> > {
public:
+ Signal2(Application *application) : GenericSignal<boost::function2<void, T1, T2> >(application) {}
+
void emit(T1 arg1, T2 arg2) {
boost::lock_guard<boost::mutex> lock(this->mutex);
for(typename std::map<Connection, typename GenericSignal<boost::function2<void, T1, T2> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler)
- ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2));
+ this->getApplication()->getThreadManager()->pushWork(boost::bind(handler->second, arg1, arg2));
}
};
diff --git a/src/Core/Signals/SignalBase.h b/src/Core/Signals/SignalBase.h
index 1a5d5a3..8ec8483 100644
--- a/src/Core/Signals/SignalBase.h
+++ b/src/Core/Signals/SignalBase.h
@@ -27,12 +27,17 @@
namespace Mad {
namespace Core {
+
+class Application;
+
namespace Signals {
class SignalBase : private boost::noncopyable {
private:
friend class Connection;
+ Application *application;
+
unsigned long connectionId;
protected:
@@ -42,8 +47,12 @@ class SignalBase : private boost::noncopyable {
return Connection(this, connectionId++);
}
- SignalBase() : connectionId(0) {}
+ SignalBase(Application *application0) : application(application0), connectionId(0) {}
virtual ~SignalBase() {}
+
+ Application* getApplication() {
+ return application;
+ }
};
}
diff --git a/src/Core/ThreadManager.cpp b/src/Core/ThreadManager.cpp
index abc0bc6..c34a734 100644
--- a/src/Core/ThreadManager.cpp
+++ b/src/Core/ThreadManager.cpp
@@ -19,7 +19,7 @@
#include "ThreadManager.h"
-#include "Logger.h"
+#include "Application.h"
#include "LogManager.h"
#include <boost/bind.hpp>
@@ -27,9 +27,6 @@
namespace Mad {
namespace Core {
-ThreadManager ThreadManager::threadManager;
-
-
void ThreadManager::workerFunc() {
while(true) {
boost::unique_lock<boost::mutex> lock(runLock);
@@ -61,7 +58,7 @@ void ThreadManager::workerFunc() {
void ThreadManager::detach() {
if(isThisMainThread()) {
- Logger::log(Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!");
+ application->log(Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!");
return;
}
@@ -91,22 +88,20 @@ void ThreadManager::pushWork(const boost::function0<void> &newWork) {
}
-void ThreadManager::doInit() {
- running = true;
-
+ThreadManager::ThreadManager(Application *application0) : application(application0), running(true) {
threadLock.lock();
- ioWorker.reset(new boost::asio::io_service::work(ioService));
+ ioWorker.reset(new boost::asio::io_service::work(application->getIOService()));
mainThreadId = boost::this_thread::get_id();
workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this));
- loggerThread.reset(new boost::thread(&LogManager::loggerThread, LogManager::get()));
- ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &ioService));
+ loggerThread.reset(new boost::thread(&LogManager::loggerThread, application->getLogManager()));
+ ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &application->getIOService()));
threadLock.unlock();
}
-void ThreadManager::doDeinit() {
+ThreadManager::~ThreadManager() {
if(!isThisMainThread()) {
// TODO Critical error!!!
return;
@@ -131,11 +126,11 @@ void ThreadManager::doDeinit() {
// IO thread is next
ioWorker.reset();
- ioService.stop();
+ application->getIOService().stop();
ioThread->join();
// Finally, the logger thread has to die
- LogManager::get()->stopLoggerThread();
+ application->getLogManager()->stopLoggerThread();
loggerThread->join();
}
diff --git a/src/Core/ThreadManager.h b/src/Core/ThreadManager.h
index b512ad4..c20cb1f 100644
--- a/src/Core/ThreadManager.h
+++ b/src/Core/ThreadManager.h
@@ -22,8 +22,6 @@
#include <config.h>
-#include "Initializable.h"
-
#include <queue>
#include <map>
@@ -36,8 +34,14 @@
namespace Mad {
namespace Core {
-class ThreadManager : public Initializable {
+class Application;
+
+class ThreadManager : private boost::noncopyable {
private:
+ friend class Application;
+
+ Application *application;
+
boost::thread::id mainThreadId;
boost::shared_ptr<boost::thread> workerThread, loggerThread, ioThread;
std::map<boost::thread::id, boost::shared_ptr<boost::thread> > threads;
@@ -53,11 +57,8 @@ class ThreadManager : public Initializable {
boost::scoped_ptr<boost::asio::io_service::work> ioWorker;
- boost::asio::io_service ioService;
-
- static ThreadManager threadManager;
-
- ThreadManager() {}
+ ThreadManager(Application *application0);
+ ~ThreadManager();
void workerFunc();
void ioFunc();
@@ -80,10 +81,6 @@ class ThreadManager : public Initializable {
thread->join();
}
- protected:
- virtual void doInit();
- virtual void doDeinit();
-
public:
bool isThisMainThread() {
return (mainThreadId == boost::this_thread::get_id());
@@ -94,17 +91,9 @@ class ThreadManager : public Initializable {
return (workerThread->get_id() == boost::this_thread::get_id());
}
- boost::asio::io_service& getIOService() {
- return ioService;
- }
-
void detach();
void pushWork(const boost::function0<void> &newWork);
-
- static ThreadManager* get() {
- return &threadManager;
- }
};
}