1 #ifndef oo_ObjSelector_H__
2 #define oo_ObjSelector_H__
34 autoDelete_ = autoDelete;
41 if (parent_ != NULL) {
42 parent_->me_.second = NULL;
48 typedef typename std::map<std::string, Info*> Infos;
49 typedef typename std::map<std::string, Info*>::iterator InfosIterator;
51 static Infos& funcs() {
59 Info* info =
new Info(parent, ptr, autoDelete);
65 std::pair<std::string, Info*> me_;
71 add(name, NULL, obj, autoDelete);
78 add(obj->
type(), NULL, obj, autoDelete);
85 if (funcs().find(name) != funcs().end()) {
95 if (funcs().find(
name) == funcs().end())
return NULL;
96 return funcs()[
name]->pointer_;
104 if(ptr == NULL)
return NULL;
112 for (InfosIterator it = funcs().begin(); it != funcs().end(); it++) {
113 if (it->second->pointer_ == obj ||
114 (it->second->pointer_ != NULL &&
115 it->second->pointer_->type() == obj->
type())) {
126 for (InfosIterator it = funcs().begin(); it != funcs().end(); it++) {
127 if (it->second->pointer_ == obj ||
128 (it->second->pointer_ != NULL &&
129 it->second->pointer_->type() == obj->
type())) {
133 return std::string();
139 static std::vector<std::string>
names() {
140 std::vector<std::string> ret;
141 for (InfosIterator it = funcs().begin(); it != funcs().end(); it++)
142 ret.push_back(it->first);
151 me_.second = add(me_.first,
this, obj, autoDelete);
158 me_.first = obj->
type();
159 me_.second = add(me_.first,
this, obj, autoDelete);
164 if (me_.second != NULL) {
172 static bool write(FILE* f,
bool binary,
ObjBase* obj,
unsigned int fg) {
173 if (!
exist(obj))
return false;
174 char const* nme =
name(obj).c_str();
175 size_t len = strlen(nme);
177 if (fwrite(&len,
sizeof(
size_t ), 1, f) < 1)
return false;
178 if (fwrite(nme ,
sizeof(
char ), len, f) < len)
return false;
179 if (fwrite(&fg ,
sizeof(
unsigned int), 1, f) < 1)
return false;
181 if (fprintf(f,
"%s %u\n", nme, fg) < 2)
return false;
183 return obj->
write(f, binary, fg);
190 static char name[2048];
194 if (fread(&len,
sizeof(
size_t ), 1, f) < 1)
return NULL;
195 if (fread(name,
sizeof(
char ), len, f) < len)
return NULL;
196 if (fread(&fg ,
sizeof(
unsigned int), 1, f) < 1)
return NULL;
199 if (fscanf(f,
"%s %u", name, &fg) < 2)
return NULL;
202 if (ret != NULL && ret->
read(f, binary, fg) ==
false) {
214 #endif // oo_ObjSelector_H__
static void add(ObjBase *obj, bool autoDelete)
新增(註冊) 一個Class (必須要繼承自 ObjBase) 並且默認type為name
static ObjBase * read(FILE *f, bool binary)
從檔案中讀取一個物件(該物件必須要有註冊過)
ObjSelector(ObjBase *obj, bool autoDelete)
宣告一個ObjSelector實體, 並且註冊一個 ObjBase
static bool exist(ObjBase *obj)
利用type檢查是否有註冊過同種類的Class
利用register的概念, 達到runtime用string選擇要new的class
virtual ObjBase * create() const
回傳一個new出來的物件, 預設implement為直接回傳 NULL
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
ObjSelector(std::string name, ObjBase *obj, bool autoDelete)
宣告一個ObjSelector實體, 並且註冊一個 ObjBase
static ObjBase * create(std::string name)
回傳一個之前註冊過得Class new出來的實體
static std::string name(ObjBase *obj)
利用type尋找name
static void add(std::string name, ObjBase *obj, bool autoDelete)
新增(註冊) 一個Class (必須要繼承自 ObjBase) 並且給定其Name
virtual bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false
static bool write(FILE *f, bool binary, ObjBase *obj, unsigned int fg)
將一個物件寫到檔案裡(該物件必須要有註冊過)
static std::vector< std::string > names()
回傳所有註冊過的name
static const size_t kGlobalSeletorID
static void del(std::string name)
依照name刪除之前註冊過得Class
virtual bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
virtual std::string type() const
用std::string回傳這個class的type name