00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _NUMBER_GEN_H
00034 #define _NUMBER_GEN_H
00035
00036 #include <map>
00037 #include "Log.h"
00038 #include "LogIdSet.h"
00039 #include "params.h"
00040 #include "mace-macros.h"
00041 #include "mstring.h"
00042
00069 class NumberGen {
00070 private:
00071 static const int DEFAULT_BASE = 0;
00072
00073
00074 unsigned val;
00075 typedef std::map<mace::string, NumberGen*> NumberGenMap;
00076 static NumberGenMap instances;
00077
00078 NumberGen(int base) {
00079 val = base;
00080 }
00081
00082 public:
00083 static const mace::string HANDLER_UID;
00084 static const mace::string PORT;
00085
00087 static void Reset() {
00088 for(NumberGenMap::iterator i = instances.begin(); i != instances.end(); i++) {
00089 delete i->second;
00090 }
00091 instances.clear();
00092 }
00093
00095 static void Reset(const mace::string& index) {
00096 NumberGenMap::iterator i = instances.find(index);
00097 if (i != instances.end()) {
00098 delete i->second;
00099 instances.erase(i);
00100 }
00101 }
00102
00104 static NumberGen* Instance(mace::string index) {
00105 ADD_FUNC_SELECTORS;
00106 if(params::containsKey("MACE_SIMULATE_NODES")) {
00107 index += params::get<std::string>("SIM_CURRENT_NODE");
00108 }
00109 if(instances.find(index) == instances.end()) {
00110 int base = DEFAULT_BASE;
00111 if(params::containsKey(index)) {
00112 base = params::get<int>(index);
00113 maceLog("Using params base for index %s %d", index.c_str(), base);
00114 }
00115 else {
00116 maceLog("Using default base for index %s %d", index.c_str(), DEFAULT_BASE);
00117 }
00118 instances[index] = new NumberGen(base);
00119 }
00120 return instances[index];
00121 }
00122
00124 unsigned GetVal() {
00125 return val++;
00126 }
00127 };
00128
00129 #endif // _NUMBER_GEN_H