1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
| /********************************************************
* Libs_base.hpp
*
* Created on: Sep 18, 2014
* Author: dev
********************************************************/
#pragma once
#include <iostream>
#include <typeinfo>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
#include "IObject.hpp"
#include "Sysutils.hpp"
#include "IToString.hpp"
#include "Log.hpp"
namespace cy {
// abstract class for error
struct Abstract {
virtual void foo() = 0;
virtual ~Abstract() {
}
};
template<typename T> struct error {
static void set() {
new T();
}
};
/*
* constructeurs
*/
class baseConstructor {
public:
virtual IObject *create() = 0;
virtual ~baseConstructor() {
}
};
template<typename Type>
class constructor: public baseConstructor {
public:
virtual IObject *create() {
return dynamic_cast<IObject*>(new Type());
}
};
typedef map<string, baseConstructor *> mapSBC;
/*
* Class static de references
*
*/
template<class Interface>
class Referencies: TIToString<Referencies<Interface>> {
private:
Referencies() {
}
class Initializer {
public:
Initializer() {
Log::i("Referencies", "Initializer : constructor");
//cout << "[Referencies] static constructor" << endl;
////added = NULL;
}
};
//friend class Initializer;
static Initializer init;
static void addedFunc(string added) {
}
public:
static void initialize() {
//cout << "[Referencies] initialize<" << type_name<Interface>() << ">"
// << endl;
ptrlist = new mapSBC();
// a mettre obligatoirement sinon ca ne decharge pas la librairie (une des deux lignes lignes ci dessous)
//added = addedFunc;
added = NULL;
}
static void clear() {
//cout << "[Referencies] CLEAR";
if (ptrlist->size() == 0) {
//cout << " EMPTY" << endl;
return;
}
//cout << endl;
for (mapSBC::iterator it = ptrlist->begin(); it != ptrlist->end();
++it) {
if (it->second != NULL) {
// cout << " - delete " << it->first << " = " << it->second
// << endl;
delete it->second;
}
}
ptrlist->clear();
}
static void add(string className, baseConstructor *c) {
// cout << "[Referencies] ADD < " << type_name<Interface>().c_str()
// << "> = " << className.c_str() << " constructor = " << c
// << endl;
Log::i("Referencies", "Add<%s> = %s", type_name<Interface>().c_str(),
className.c_str());
//Log::i("Referencies", "Add : added = %d", added);
//list.insert(std::pair<string,baseConstructor *>(className, (baseConstructor *)NULL));
// if(ptrlist == NULL)
// ptrlist = new mapSBC();
(*ptrlist)[className] = c;
if (added != NULL) {
//Log::i("Referencies", "Add : raise added");
added(className);
}
}
static void remove(string className) {
// cout << "[Referencies] REMOVE <" << type_name<Interface>().c_str()
// << "> = " << className.c_str() << " YYY" << endl;
Log::i("Referencies", "Remove<%s> = %s", type_name<Interface>().c_str(),
className.c_str());
mapSBC::iterator itx = ptrlist->find(className);
if (itx == ptrlist->end())
return;
if (itx->second != NULL)
delete itx->second;
ptrlist->erase(itx);
}
static void setNotification(notiFyAddedEventHandler a) {
Log::i("Referencies", "setNotification : for %s",
type_name<Interface>());
added = a;
Log::i("Referencies", "setNotification : added = %d", added);
}
static vector<string> getClass() {
vector<string> vec;
for (mapSBC::iterator it = ptrlist->begin(); it != ptrlist->end(); ++it)
vec.push_back((*it).first);
return vec;
}
static bool exist(string className) {
mapSBC::iterator itx = ptrlist->find(className);
return (itx != ptrlist->end());
}
static Interface* newInstance(string className) {
mapSBC::iterator itx = ptrlist->find(className);
if (itx == ptrlist->end())
return NULL;
try {
Interface* i = dynamic_cast<Interface*>((*ptrlist)[className]->create());
return i;
} catch (...) {
return NULL;
}
}
static void show() {
cout << "[Referencies] show<" << type_name<Interface>() << ">";
if (ptrlist->size() == 0) {
cout << " EMPTY " << endl;
return;
}
cout << endl;
try {
for (mapSBC::iterator it = ptrlist->begin(); it != ptrlist->end();
++it) {
cout << " - " << (*it).first << endl;
}
} catch (...) {
}
}
virtual string toString() {
string ret = "";
for (mapSBC::iterator it = ptrlist->begin(); it != ptrlist->end();
++it) {
ret += (*it).first + " ";
}
return ret;
}
static notiFyAddedEventHandler added;
//static int test;
private:
//static map<string, baseConstructor *> list;
static map<string, baseConstructor *> *ptrlist;
};
/* membre static de Referencies */
//template<class Interface>
//map<string, baseConstructor *> Referencies<Interface>::list;
template<class Interface>
mapSBC *Referencies<Interface>::ptrlist;
template<class Interface>
notiFyAddedEventHandler Referencies<Interface>::added = NULL;
//template<class Interface>
//Referencies<Interface>::Initializer Referencies<Interface>::init;
/*
*
*
*/
template<class Interface, class Type, bool B = __is_base_of(Interface, Type)>
class AddReferency {
public:
AddReferency() {
// cout << "[AddReferency] ADD <" << type_name<Interface>() << "> = "
// << type_name<Type>()
// << "******************************************* " << endl;
Log::i("AddReferency", "Add<%s> = %s", type_name<Interface>().c_str(),
type_name<Type>().c_str());
Referencies<Interface>::add(type_name<Type>(), new constructor<Type>());
// Referencies<Interface>::add(type_name<Type>(), NULL);
}
~AddReferency() {
// cout << "[AddReferency] REMOVE <" << type_name<Interface>() << "> = "
// << type_name<Type>()
// << "***************************************" << endl;
Log::i("AddReferency", "Remove<%s> = %s",
type_name<Interface>().c_str(), type_name<Type>().c_str());
Referencies<Interface>::remove(type_name<Type>());
}
};
template<class Interface, class Type>
class AddReferency<Interface, Type, 0> {
public:
AddReferency() {
//cout << " ADD probleme" << endl;
Log::i("AddReferency", "Add Error<%s> = %s",
type_name<Interface>().c_str(), type_name<Type>().c_str());
error<Abstract>::set();
}
~AddReferency() {
}
};
} // end namespace |
Partager