rewrite and reoganize tools functions

* create stdext namespace which contains additional C++ algorithms
* organize stdext in string, math, cast and exception utilities
This commit is contained in:
Eduardo Bart
2012-05-28 10:06:26 -03:00
parent 2676eb4da3
commit 4c80d783d6
75 changed files with 856 additions and 652 deletions

View File

@@ -44,7 +44,7 @@ bool ConfigManager::load(const std::string& file)
if(confsDoc)
m_confsDoc = confsDoc;
return true;
} catch(Exception& e) {
} catch(stdext::exception& e) {
logError("could not load configurations: ", e.what());
return false;
}

View File

@@ -72,7 +72,7 @@ void EventDispatcher::poll()
}
}
ScheduledEventPtr EventDispatcher::scheduleEvent(const SimpleCallback& callback, int delay)
ScheduledEventPtr EventDispatcher::scheduleEvent(const std::function<void()>& callback, int delay)
{
assert(delay >= 0);
ScheduledEventPtr scheduledEvent(new ScheduledEvent(callback, delay));
@@ -80,7 +80,7 @@ ScheduledEventPtr EventDispatcher::scheduleEvent(const SimpleCallback& callback,
return scheduledEvent;
}
EventPtr EventDispatcher::addEvent(const SimpleCallback& callback, bool pushFront)
EventPtr EventDispatcher::addEvent(const std::function<void()>& callback, bool pushFront)
{
EventPtr event(new Event(callback));
// front pushing is a way to execute an event before others

View File

@@ -30,7 +30,7 @@
class Event : public LuaObject
{
public:
Event(const SimpleCallback& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
Event(const std::function<void()>& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
void execute() {
if(!m_canceled && !m_executed && m_callback) {
@@ -44,7 +44,7 @@ public:
bool isExecuted() { return m_executed; }
protected:
SimpleCallback m_callback;
std::function<void()> m_callback;
bool m_canceled;
bool m_executed;
};
@@ -52,7 +52,7 @@ protected:
class ScheduledEvent : public Event
{
public:
ScheduledEvent(const SimpleCallback& callback, int delay) : Event(callback) {
ScheduledEvent(const std::function<void()>& callback, int delay) : Event(callback) {
m_ticks = g_clock.ticksFor(delay);
}
@@ -75,8 +75,8 @@ public:
void flush();
void poll();
EventPtr addEvent(const SimpleCallback& callback, bool pushFront = false);
ScheduledEventPtr scheduleEvent(const SimpleCallback& callback, int delay);
EventPtr addEvent(const std::function<void()>& callback, bool pushFront = false);
ScheduledEventPtr scheduleEvent(const std::function<void()>& callback, int delay);
private:
std::list<EventPtr> m_eventList;

View File

@@ -181,7 +181,7 @@ uint16 FileStream::getU16()
return 0;
}
v = Fw::readLE16(&m_cacheBuffer[m_cacheReadPos]);
v = stdext::readLE16(&m_cacheBuffer[m_cacheReadPos]);
m_cacheReadPos += 2;
}
return v;
@@ -199,7 +199,7 @@ uint32 FileStream::getU32()
return 0;
}
v = Fw::readLE32(&m_cacheBuffer[m_cacheReadPos]);
v = stdext::readLE32(&m_cacheBuffer[m_cacheReadPos]);
m_cacheReadPos += 4;
}
return v;
@@ -217,7 +217,7 @@ uint64 FileStream::getU64()
return 0;
}
v = Fw::readLE64(&m_cacheBuffer[m_cacheReadPos]);
v = stdext::readLE64(&m_cacheBuffer[m_cacheReadPos]);
m_cacheReadPos += 8;
}
return v;

View File

@@ -93,6 +93,6 @@ void Logger::setLogFile(const std::string& file)
return;
}
m_outFile << "\n== application started at " << Fw::dateTimeString() << std::endl;
m_outFile << "\n== application started at " << stdext::date_time_string() << std::endl;
m_outFile.flush();
}

View File

@@ -57,17 +57,17 @@ private:
extern Logger g_logger;
// specialized logging
#define logDebug(...) g_logger.log(Fw::LogDebug, Fw::mkstr(__VA_ARGS__))
#define logInfo(...) g_logger.log(Fw::LogInfo, Fw::mkstr(__VA_ARGS__))
#define logWarning(...) g_logger.log(Fw::LogWarning, Fw::mkstr(__VA_ARGS__))
#define logError(...) g_logger.log(Fw::LogError, Fw::mkstr(__VA_ARGS__))
#define logFatal(...) g_logger.log(Fw::LogFatal, Fw::mkstr(__VA_ARGS__))
#define logDebug(...) g_logger.log(Fw::LogDebug, stdext::mkstr(__VA_ARGS__))
#define logInfo(...) g_logger.log(Fw::LogInfo, stdext::mkstr(__VA_ARGS__))
#define logWarning(...) g_logger.log(Fw::LogWarning, stdext::mkstr(__VA_ARGS__))
#define logError(...) g_logger.log(Fw::LogError, stdext::mkstr(__VA_ARGS__))
#define logFatal(...) g_logger.log(Fw::LogFatal, stdext::mkstr(__VA_ARGS__))
#define logTrace() g_logger.logFunc(Fw::LogDebug, "", __PRETTY_FUNCTION__)
#define logTraceDebug(...) g_logger.logFunc(Fw::LogDebug, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) g_logger.logFunc(Fw::LogInfo, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) g_logger.logFunc(Fw::LogError, Fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceDebug(...) g_logger.logFunc(Fw::LogDebug, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) g_logger.logFunc(Fw::LogInfo, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) g_logger.logFunc(Fw::LogWarning, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) g_logger.logFunc(Fw::LogError, stdext::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceCounter() { \
static int __count = 0; \

View File

@@ -120,14 +120,14 @@ void Module::discover(const OTMLNodePtr& moduleNode)
if(OTMLNodePtr node = moduleNode->get("@onLoad")) {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
g_lua.useValue();
m_loadCallback = g_lua.polymorphicPop<SimpleCallback>();
m_loadCallback = g_lua.polymorphicPop<std::function<void()>>();
}
// set onUnload callback
if(OTMLNodePtr node = moduleNode->get("@onUnload")) {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
g_lua.useValue();
m_unloadCallback = g_lua.polymorphicPop<SimpleCallback>();
m_unloadCallback = g_lua.polymorphicPop<std::function<void()>>();
}
if(OTMLNodePtr node = moduleNode->get("load-later")) {

View File

@@ -68,8 +68,8 @@ private:
std::string m_author;
std::string m_website;
std::string m_version;
SimpleCallback m_loadCallback;
SimpleCallback m_unloadCallback;
std::function<void()> m_loadCallback;
std::function<void()> m_unloadCallback;
std::list<std::string> m_dependencies;
std::list<std::string> m_loadLaterModules;
};

View File

@@ -104,7 +104,7 @@ ModulePtr ModuleManager::discoverModule(const std::string& moduleFile)
std::string name = moduleNode->valueAt("name");
//if(getModule(name))
// Fw::throwException("module '", name, "' already exists, cannot have duplicate module names");
// stdext::throw_exception("module '", name, "' already exists, cannot have duplicate module names");
bool push = false;
module = getModule(name);
@@ -117,7 +117,7 @@ ModulePtr ModuleManager::discoverModule(const std::string& moduleFile)
// not loaded modules are always in back
if(push)
m_modules.push_back(module);
} catch(Exception& e) {
} catch(stdext::exception& e) {
logError("Unable to discover module from file '", moduleFile, "': ", e.what());
}
return module;

View File

@@ -43,7 +43,7 @@ void ResourceManager::terminate()
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName)
{
std::string userDir = PHYSFS_getUserDir();
std::string dirName = Fw::mkstr(".", appWriteDirName);
std::string dirName = stdext::mkstr(".", appWriteDirName);
std::string writeDir = userDir + dirName;
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str()))
@@ -92,7 +92,7 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
out.clear(std::ios::failbit);
Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError());
stdext::throw_exception(stdext::format("failed to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
} else {
int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) {

View File

@@ -23,7 +23,7 @@
#ifndef TIMER_H
#define TIMER_H
#include <framework/util/types.h>
#include <framework/global.h>
class Timer
{