diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-05-02 04:10:56 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-05-02 04:10:56 +0800 |
commit | 33d419e4d54d969798af80f05e05f0c447a99594 (patch) | |
tree | c78355a2d334e34df865aca865dbb4864a85820c /meowpp/oo | |
parent | d2d7a49563a8f04bd07264a4a989d5656313d375 (diff) | |
download | meow-33d419e4d54d969798af80f05e05f0c447a99594.tar meow-33d419e4d54d969798af80f05e05f0c447a99594.tar.gz meow-33d419e4d54d969798af80f05e05f0c447a99594.tar.bz2 meow-33d419e4d54d969798af80f05e05f0c447a99594.tar.lz meow-33d419e4d54d969798af80f05e05f0c447a99594.tar.xz meow-33d419e4d54d969798af80f05e05f0c447a99594.tar.zst meow-33d419e4d54d969798af80f05e05f0c447a99594.zip |
big change about dir structure
Diffstat (limited to 'meowpp/oo')
-rw-r--r-- | meowpp/oo/ObjBase.h | 8 | ||||
-rw-r--r-- | meowpp/oo/ObjPort.h | 18 | ||||
-rw-r--r-- | meowpp/oo/ObjPort.hpp | 47 | ||||
-rw-r--r-- | meowpp/oo/ObjSelector.h | 11 |
4 files changed, 83 insertions, 1 deletions
diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h index 2c8cb70..3aa99d7 100644 --- a/meowpp/oo/ObjBase.h +++ b/meowpp/oo/ObjBase.h @@ -1,6 +1,8 @@ #ifndef oo_ObjBase_H__ #define oo_ObjBase_H__ +#include <cstdio> + #include <typeinfo> #include <string> @@ -11,7 +13,11 @@ namespace meow{ public: virtual ~ObjBase(){ } // - virtual ObjBase* create() const = 0; + virtual bool read (FILE* f, bool bin, unsigned int fg){ return false; } + virtual bool write(FILE* f, bool bin, unsigned int fg){ return false; } + // + virtual ObjBase* create() const{ return NULL; } + // virtual char const* ctype() const{ static char const* ptr = typeid(*this).name(); return ptr; diff --git a/meowpp/oo/ObjPort.h b/meowpp/oo/ObjPort.h new file mode 100644 index 0000000..b9baf0c --- /dev/null +++ b/meowpp/oo/ObjPort.h @@ -0,0 +1,18 @@ +#ifndef oo_ObjPort_H__ +#define oo_ObjPort_H__ + +#include "ObjBase.h" + +#include <cstdio> + +namespace meow{ + template<size_t sid> + class ObjPort{ + public: + static ObjBase* read (FILE* __f, bool __binary); + static bool write(FILE* __f, bool __binary, + ObjBase* __obj, unsigned int __fg); + }; +} + +#endif // oo_ObjPort_H__ diff --git a/meowpp/oo/ObjPort.hpp b/meowpp/oo/ObjPort.hpp new file mode 100644 index 0000000..d458adf --- /dev/null +++ b/meowpp/oo/ObjPort.hpp @@ -0,0 +1,47 @@ +#include "ObjPort.h" + +#include "ObjSelector.h" +#include "ObjBase.h" + +#include <cstdio> + +namespace meow{ + template<size_t sid> + inline ObjPort::ObjBase* read(FILE* __f, bool __binary){ + char name[1024]; + unsigned int fg; + if(__binary){ + size_t len; + if(fread(&len, sizeof(size_t), 1, __f) < 1) return NULL; + if(fread(name, sizeof(char), len, __f) < len) return NULL; + if(fread(&fg, sizeof(unsigned int), 1, __f) < 1) return NULL; + name[len] = '\0'; + }else{ + fscanf(__f, "%s %u", name, &fg); + } + ObjBase* ret = Selector<sid>(name); + if(ret->read(__f, __binary, fg) == false){ + delete ret; + ret = NULL; + } + return ret; + } + + + template<size_t sid> + inline ObjPort::bool write(FILE* __f, bool __binary, + ObjBase* __obj, unsigned int __fg){ + std::string name = Selector<sid>::find(__obj); + if(__binary){ + size_t len = name.size(); + if(fwrite(&len, sizeof(size_t), 1, __f) < 1) return false; + if(fwrite(name.c_str(), sizeof(char), len, __f) < len) return false; + if(fwrite(&__fg, sizeof(unsigned int), 1, __f) < len) return false; + }else{ + if(fprintf(__f, "%s %u\n", name.c_str(), __fg) < 2) return false; + } + return __obj->write(__f, __binary, __fg); + } +} + +#endif // oo_ObjPort_H__ diff --git a/meowpp/oo/ObjSelector.h b/meowpp/oo/ObjSelector.h index e70c496..58debcb 100644 --- a/meowpp/oo/ObjSelector.h +++ b/meowpp/oo/ObjSelector.h @@ -34,11 +34,22 @@ namespace meow{ funcs().erase(s); } } + static ObjBase const* access(std::string s){ + if(funcs().find(s) == funcs().end()) return NULL; + return funcs()[s]; + } static ObjBase* get(std::string s){ if(funcs().find(s) == funcs().end() || funcs()[s] == NULL) return NULL; return funcs()[s]->create(); } + static std::string find(ObjBase* o){ + for(Funcs::iterator it = funcs().begin(); it != funcs().end(); it++) + if(it->second != NULL && it->second->type() == o->type()){ + return it->first; + } + return std::string(); + } static Types lst(){ Types ret; for(Funcs::iterator it = funcs().begin(); it != funcs().end(); it++) |