From 33d419e4d54d969798af80f05e05f0c447a99594 Mon Sep 17 00:00:00 2001 From: cathook Date: Fri, 2 May 2014 04:10:56 +0800 Subject: big change about dir structure --- meowpp/oo/ObjBase.h | 8 +++++++- meowpp/oo/ObjPort.h | 18 ++++++++++++++++++ meowpp/oo/ObjPort.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ meowpp/oo/ObjSelector.h | 11 +++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 meowpp/oo/ObjPort.h create mode 100644 meowpp/oo/ObjPort.hpp (limited to 'meowpp/oo') 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 + #include #include @@ -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 + +namespace meow{ + template + 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 + +namespace meow{ + template + 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(name); + if(ret->read(__f, __binary, fg) == false){ + delete ret; + ret = NULL; + } + return ret; + } + + + template + inline ObjPort::bool write(FILE* __f, bool __binary, + ObjBase* __obj, unsigned int __fg){ + std::string name = Selector::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++) -- cgit v1.2.3