2010-03-19 23:26:00 +00:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest Shared Library (www.glest.org)
|
|
|
|
//
|
2010-04-13 16:00:19 +00:00
|
|
|
// Copyright (C) 2001-2008 Martio Figueroa
|
2010-03-19 23:26:00 +00:00
|
|
|
//
|
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#ifndef _SHARED_UTIL_UTIL_H_
|
|
|
|
#define _SHARED_UTIL_UTIL_H_
|
|
|
|
|
|
|
|
#include <string>
|
2010-03-23 07:59:24 +00:00
|
|
|
#include <fstream>
|
2010-04-27 03:36:36 +00:00
|
|
|
#include <map>
|
2010-05-02 06:21:36 +00:00
|
|
|
#include "thread.h"
|
2010-03-19 23:26:00 +00:00
|
|
|
|
|
|
|
using std::string;
|
2010-05-02 06:21:36 +00:00
|
|
|
using namespace Shared::Platform;
|
2010-03-19 23:26:00 +00:00
|
|
|
|
2010-05-07 06:46:55 +00:00
|
|
|
//#define UNDEF_DEBUG
|
|
|
|
|
2010-03-19 23:26:00 +00:00
|
|
|
namespace Shared{ namespace Util{
|
|
|
|
|
|
|
|
class SystemFlags
|
|
|
|
{
|
2010-04-27 03:36:36 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
enum DebugType {
|
|
|
|
debugSystem,
|
|
|
|
debugNetwork,
|
|
|
|
debugPerformance,
|
|
|
|
debugWorldSynch
|
|
|
|
};
|
|
|
|
|
|
|
|
class SystemFlagsType
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
DebugType debugType;
|
2010-05-01 09:10:52 +00:00
|
|
|
|
2010-04-27 03:36:36 +00:00
|
|
|
public:
|
|
|
|
SystemFlagsType() {
|
|
|
|
this->debugType = debugSystem;
|
|
|
|
this->enabled = false;
|
|
|
|
this->fileStream = NULL;
|
|
|
|
this->debugLogFileName = "";
|
2010-04-28 22:34:10 +00:00
|
|
|
this->fileStreamOwner = false;
|
2010-05-02 06:21:36 +00:00
|
|
|
this->mutex = NULL;
|
2010-04-27 03:36:36 +00:00
|
|
|
}
|
|
|
|
SystemFlagsType(DebugType debugType) {
|
|
|
|
this->debugType = debugType;
|
|
|
|
this->enabled = false;
|
|
|
|
this->fileStream = NULL;
|
|
|
|
this->debugLogFileName = "";
|
2010-04-28 22:34:10 +00:00
|
|
|
this->fileStreamOwner = false;
|
2010-05-02 06:21:36 +00:00
|
|
|
this->mutex = NULL;
|
2010-04-27 03:36:36 +00:00
|
|
|
}
|
2010-05-02 06:21:36 +00:00
|
|
|
~SystemFlagsType() {
|
|
|
|
Close();
|
|
|
|
}
|
2010-04-27 03:36:36 +00:00
|
|
|
SystemFlagsType(DebugType debugType,bool enabled,
|
|
|
|
std::ofstream *fileStream,std::string debugLogFileName) {
|
|
|
|
this->debugType = debugType;
|
|
|
|
this->enabled = enabled;
|
|
|
|
this->fileStream = fileStream;
|
|
|
|
this->debugLogFileName = debugLogFileName;
|
2010-04-28 22:34:10 +00:00
|
|
|
this->fileStreamOwner = false;
|
2010-05-02 06:21:36 +00:00
|
|
|
this->mutex = NULL;
|
2010-04-27 03:36:36 +00:00
|
|
|
}
|
|
|
|
|
2010-05-02 06:21:36 +00:00
|
|
|
void Close() {
|
|
|
|
if(this->fileStreamOwner == true) {
|
|
|
|
if( this->fileStream != NULL &&
|
|
|
|
this->fileStream->is_open() == true) {
|
|
|
|
this->fileStream->close();
|
|
|
|
}
|
|
|
|
delete this->fileStream;
|
|
|
|
delete this->mutex;
|
|
|
|
}
|
|
|
|
this->fileStream = NULL;
|
|
|
|
this->fileStreamOwner = false;
|
|
|
|
this->mutex = NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:36:36 +00:00
|
|
|
bool enabled;
|
|
|
|
std::ofstream *fileStream;
|
|
|
|
std::string debugLogFileName;
|
2010-04-28 22:34:10 +00:00
|
|
|
bool fileStreamOwner;
|
2010-05-02 06:21:36 +00:00
|
|
|
Mutex *mutex;
|
2010-04-27 03:36:36 +00:00
|
|
|
};
|
|
|
|
|
2010-03-23 07:59:24 +00:00
|
|
|
protected:
|
2010-04-27 03:36:36 +00:00
|
|
|
|
2010-04-13 16:00:19 +00:00
|
|
|
static int lockFile;
|
|
|
|
static string lockfilename;
|
|
|
|
|
2010-04-27 03:36:36 +00:00
|
|
|
static std::map<DebugType,SystemFlagsType> debugLogFileList;
|
|
|
|
|
2010-03-19 23:26:00 +00:00
|
|
|
public:
|
|
|
|
|
2010-04-13 16:00:19 +00:00
|
|
|
SystemFlags();
|
|
|
|
~SystemFlags();
|
|
|
|
|
2010-04-27 03:36:36 +00:00
|
|
|
static void init();
|
|
|
|
static SystemFlagsType & getSystemSettingType(DebugType type) { return debugLogFileList[type]; }
|
2010-05-07 06:46:55 +00:00
|
|
|
|
|
|
|
// Let the macro call into this when require.. NEVER call it automatically.
|
|
|
|
static void handleDebug(DebugType type, const char *fmt, ...);
|
|
|
|
|
|
|
|
#ifndef UNDEF_DEBUG
|
|
|
|
|
|
|
|
#define OutputDebug(type, fmt, ...) SystemFlags::handleDebug (type, fmt, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// stub out debugging completely
|
|
|
|
#define OutputDebug(type, fmt, ...) type
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-03-23 07:59:24 +00:00
|
|
|
static void Close();
|
2010-03-19 23:26:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const string sharedLibVersionString= "v0.4.1";
|
|
|
|
|
|
|
|
//string fcs
|
|
|
|
string lastDir(const string &s);
|
|
|
|
string lastFile(const string &s);
|
|
|
|
string cutLastFile(const string &s);
|
|
|
|
string cutLastExt(const string &s);
|
|
|
|
string ext(const string &s);
|
|
|
|
string replaceBy(const string &s, char c1, char c2);
|
|
|
|
string toLower(const string &s);
|
|
|
|
void copyStringToBuffer(char *buffer, int bufferSize, const string& s);
|
|
|
|
|
|
|
|
//numeric fcs
|
|
|
|
int clamp(int value, int min, int max);
|
|
|
|
float clamp(float value, float min, float max);
|
|
|
|
float saturate(float value);
|
|
|
|
int round(float f);
|
|
|
|
|
|
|
|
//misc
|
|
|
|
bool fileExists(const string &path);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void deleteValues(T beginIt, T endIt){
|
|
|
|
for(T it= beginIt; it!=endIt; ++it){
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void deleteMapValues(T beginIt, T endIt){
|
|
|
|
for(T it= beginIt; it!=endIt; ++it){
|
|
|
|
delete it->second;
|
2010-05-01 20:14:25 +00:00
|
|
|
it->second = NULL;
|
2010-03-19 23:26:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-04 02:32:43 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
class create_map
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::map<T, U> m_map;
|
|
|
|
public:
|
|
|
|
create_map(const T& key, const U& val)
|
|
|
|
{
|
|
|
|
m_map[key] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
create_map<T, U>& operator()(const T& key, const U& val)
|
|
|
|
{
|
|
|
|
m_map[key] = val;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::map<T, U>()
|
|
|
|
{
|
|
|
|
return m_map;
|
|
|
|
}
|
|
|
|
};
|
2010-03-19 23:26:00 +00:00
|
|
|
}}//end namespace
|
|
|
|
|
|
|
|
#endif
|