mirror of
https://github.com/glest/glest-source.git
synced 2025-08-08 17:36:30 +02:00
Code Restructuring to make mega-glest more standard
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_CHECKSUM_H_
|
||||
#define _SHARED_UTIL_CHECKSUM_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
using std::string;
|
||||
using Shared::Platform::int32;
|
||||
using Shared::Platform::int8;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
// =====================================================
|
||||
// class Checksum
|
||||
// =====================================================
|
||||
|
||||
class Checksum{
|
||||
private:
|
||||
int32 sum;
|
||||
int32 r;
|
||||
int32 c1;
|
||||
int32 c2;
|
||||
|
||||
public:
|
||||
Checksum();
|
||||
|
||||
int32 getSum() const {return sum;}
|
||||
|
||||
void addByte(int8 value);
|
||||
void addString(const string &value);
|
||||
void addFile(const string &path);
|
||||
};
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,37 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_CONVERSION_H_
|
||||
#define _SHARED_UTIL_CONVERSION_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
bool strToBool(const string &s);
|
||||
int strToInt(const string &s);
|
||||
float strToFloat(const string &s);
|
||||
|
||||
bool strToBool(const string &s, bool *b);
|
||||
bool strToInt(const string &s, int *i);
|
||||
bool strToFloat(const string &s, float *f);
|
||||
|
||||
string boolToStr(bool b);
|
||||
string intToStr(int i);
|
||||
string intToHex(int i);
|
||||
string floatToStr(float f);
|
||||
string doubleToStr(double f);
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,86 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_FACTORY_
|
||||
#define _SHARED_UTIL_FACTORY_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
using std::map;
|
||||
using std::string;
|
||||
using std::pair;
|
||||
using std::runtime_error;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
// =====================================================
|
||||
// class SingleFactoryBase
|
||||
// =====================================================
|
||||
|
||||
class SingleFactoryBase{
|
||||
public:
|
||||
virtual ~SingleFactoryBase(){}
|
||||
virtual void *newInstance()= 0;
|
||||
};
|
||||
|
||||
// =====================================================
|
||||
// class SingleFactory
|
||||
// =====================================================
|
||||
|
||||
template<typename T>
|
||||
class SingleFactory: public SingleFactoryBase{
|
||||
public:
|
||||
virtual void *newInstance() {return new T();}
|
||||
};
|
||||
|
||||
// =====================================================
|
||||
// class MultiFactory
|
||||
// =====================================================
|
||||
|
||||
template<typename T>
|
||||
class MultiFactory{
|
||||
private:
|
||||
typedef map<string, SingleFactoryBase*> Factories;
|
||||
typedef pair<string, SingleFactoryBase*> FactoryPair;
|
||||
|
||||
private:
|
||||
Factories factories;
|
||||
|
||||
public:
|
||||
virtual ~MultiFactory(){
|
||||
for(Factories::iterator it= factories.begin(); it!=factories.end(); ++it){
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
void registerClass(string classId){
|
||||
factories.insert(FactoryPair(classId, new SingleFactory<R>()));
|
||||
}
|
||||
|
||||
T *newInstance(string classId){
|
||||
Factories::iterator it= factories.find(classId);
|
||||
if(it == factories.end()){
|
||||
throw runtime_error("Unknown class identifier: " + classId);
|
||||
}
|
||||
return static_cast<T*>(it->second->newInstance());
|
||||
}
|
||||
|
||||
bool isClassId(string classId){
|
||||
return factories.find(classId)!=factories.end();
|
||||
}
|
||||
};
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,121 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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 _LEAKDUMPER_H_
|
||||
#define _LEAKDUMPER_H_
|
||||
|
||||
//#define SL_LEAK_DUMP
|
||||
//SL_LEAK_DUMP controls if leak dumping is enabled or not
|
||||
|
||||
#ifdef SL_LEAK_DUMP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
|
||||
//including this header in any file of a project will cause all
|
||||
//leaks to be dumped into leak_dump.txt, but only allocations that
|
||||
//ocurred in a file where this header is included will have
|
||||
//file and line number
|
||||
|
||||
struct AllocInfo{
|
||||
int line;
|
||||
const char *file;
|
||||
size_t bytes;
|
||||
void *ptr;
|
||||
bool free;
|
||||
bool array;
|
||||
|
||||
AllocInfo();
|
||||
AllocInfo(void* ptr, const char* file, int line, size_t bytes, bool array);
|
||||
};
|
||||
|
||||
// =====================================================
|
||||
// class AllocRegistry
|
||||
// =====================================================
|
||||
|
||||
class AllocRegistry{
|
||||
private:
|
||||
static const unsigned maxAllocs= 40000;
|
||||
|
||||
private:
|
||||
AllocRegistry();
|
||||
|
||||
private:
|
||||
AllocInfo allocs[maxAllocs]; //array to store allocation info
|
||||
int allocCount; //allocations
|
||||
size_t allocBytes; //bytes allocated
|
||||
int nonMonitoredCount;
|
||||
size_t nonMonitoredBytes;
|
||||
|
||||
public:
|
||||
~AllocRegistry();
|
||||
|
||||
static AllocRegistry &getInstance();
|
||||
|
||||
void allocate(AllocInfo info);
|
||||
void deallocate(void* ptr, bool array);
|
||||
void reset();
|
||||
void dump(const char *path);
|
||||
};
|
||||
|
||||
//if an allocation ocurrs in a file where "leaks_dumper.h" is not included
|
||||
//this operator new is called and file and line will be unknown
|
||||
inline void * operator new (size_t bytes){
|
||||
void *ptr= malloc(bytes);
|
||||
AllocRegistry::getInstance().allocate(AllocInfo(ptr, "unknown", 0, bytes, false));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void operator delete(void *ptr){
|
||||
AllocRegistry::getInstance().deallocate(ptr, false);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
inline void * operator new[](size_t bytes){
|
||||
void *ptr= malloc(bytes);
|
||||
AllocRegistry::getInstance().allocate(AllocInfo(ptr, "unknown", 0, bytes, true));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void operator delete [](void *ptr){
|
||||
AllocRegistry::getInstance().deallocate(ptr, true);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
//if an allocation ocurrs in a file where "leaks_dumper.h" is included
|
||||
//this operator new is called and file and line will be known
|
||||
inline void * operator new (size_t bytes, char* file, int line){
|
||||
void *ptr= malloc(bytes);
|
||||
AllocRegistry::getInstance().allocate(AllocInfo(ptr, file, line, bytes, false));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void operator delete(void *ptr, char* file, int line){
|
||||
AllocRegistry::getInstance().deallocate(ptr, false);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
inline void * operator new[](size_t bytes, char* file, int line){
|
||||
void *ptr= malloc(bytes);
|
||||
AllocRegistry::getInstance().allocate(AllocInfo(ptr, file, line, bytes, true));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
inline void operator delete [](void *ptr, char* file, int line){
|
||||
AllocRegistry::getInstance().deallocate(ptr, true);
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
#define new new(__FILE__, __LINE__)
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@@ -1,99 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_PROFILER_H_
|
||||
#define _SHARED_UTIL_PROFILER_H_
|
||||
|
||||
//#define SL_PROFILE
|
||||
//SL_PROFILE controls if profile is enabled or not
|
||||
|
||||
#include "platform_util.h"
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
using std::list;
|
||||
using std::string;
|
||||
|
||||
using Shared::Platform::Chrono;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
#ifdef SL_PROFILE
|
||||
|
||||
// =====================================================
|
||||
// class Section
|
||||
// =====================================================
|
||||
|
||||
class Section{
|
||||
public:
|
||||
typedef list<Section*> SectionContainer;
|
||||
private:
|
||||
string name;
|
||||
Chrono chrono;
|
||||
int64 milisElapsed;
|
||||
Section *parent;
|
||||
SectionContainer children;
|
||||
|
||||
public:
|
||||
Section(const string &name);
|
||||
|
||||
Section *getParent() {return parent;}
|
||||
const string &getName() const {return name;}
|
||||
|
||||
void setParent(Section *parent) {this->parent= parent;}
|
||||
|
||||
void start() {chrono.start();}
|
||||
void stop() {milisElapsed+=chrono.getMillis();}
|
||||
|
||||
void addChild(Section *child) {children.push_back(child);}
|
||||
Section *getChild(const string &name);
|
||||
|
||||
void print(FILE *outSream, int tabLevel=0);
|
||||
};
|
||||
|
||||
// =====================================================
|
||||
// class Profiler
|
||||
// =====================================================
|
||||
|
||||
class Profiler{
|
||||
private:
|
||||
Section *rootSection;
|
||||
Section *currSection;
|
||||
private:
|
||||
Profiler();
|
||||
public:
|
||||
~Profiler();
|
||||
static Profiler &getInstance();
|
||||
void sectionBegin(const string &name);
|
||||
void sectionEnd(const string &name);
|
||||
};
|
||||
|
||||
#endif //SL_PROFILE
|
||||
|
||||
// =====================================================
|
||||
// class funtions
|
||||
// =====================================================
|
||||
|
||||
inline void profileBegin(const string §ionName){
|
||||
#ifdef SL_PROFILE
|
||||
Profiler::getInstance().sectionBegin(sectionName);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void profileEnd(const string §ionName){
|
||||
#ifdef SL_PROFILE
|
||||
Profiler::getInstance().sectionEnd(sectionName);
|
||||
#endif
|
||||
}
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,74 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_PROPERTIES_H_
|
||||
#define _SHARED_UTIL_PROPERTIES_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
using std::map;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::pair;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
// =====================================================
|
||||
// class Properties
|
||||
//
|
||||
/// ini-like file loader
|
||||
// =====================================================
|
||||
|
||||
class Properties{
|
||||
private:
|
||||
static const int maxLine= 1024;
|
||||
|
||||
public:
|
||||
typedef pair<string, string> PropertyPair;
|
||||
typedef map<string, string> PropertyMap;
|
||||
typedef vector<PropertyPair> PropertyVector;
|
||||
|
||||
private:
|
||||
PropertyVector propertyVector;
|
||||
PropertyMap propertyMap;
|
||||
string path;
|
||||
|
||||
public:
|
||||
void clear();
|
||||
void load(const string &path);
|
||||
void save(const string &path);
|
||||
|
||||
int getPropertyCount() {return propertyVector.size();}
|
||||
string getKey(int i) {return propertyVector[i].first;}
|
||||
string getString(int i) {return propertyVector[i].second;}
|
||||
|
||||
bool getBool(const string &key) const;
|
||||
int getInt(const string &key) const;
|
||||
int getInt(const string &key, int min, int max) const;
|
||||
float getFloat(const string &key) const;
|
||||
float getFloat(const string &key, float min, float max) const;
|
||||
const string &getString(const string &key) const;
|
||||
|
||||
void setInt(const string &key, int value);
|
||||
void setBool(const string &key, bool value);
|
||||
void setFloat(const string &key, float value);
|
||||
void setString(const string &key, const string &value);
|
||||
|
||||
string toString();
|
||||
|
||||
|
||||
};
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,41 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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_RANDOM_H_
|
||||
#define _SHARED_UTIL_RANDOM_H_
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
// =====================================================
|
||||
// class Random
|
||||
// =====================================================
|
||||
|
||||
class Random{
|
||||
private:
|
||||
static const int m;
|
||||
static const int a;
|
||||
static const int b;
|
||||
|
||||
private:
|
||||
int lastNumber;
|
||||
|
||||
public:
|
||||
Random();
|
||||
void init(int seed);
|
||||
|
||||
int rand();
|
||||
int randRange(int min, int max);
|
||||
float randRange(float min, float max);
|
||||
};
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
@@ -1,58 +0,0 @@
|
||||
// ==============================================================
|
||||
// This file is part of Glest Shared Library (www.glest.org)
|
||||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// 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>
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}}//end namespace
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user