aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/oo
diff options
context:
space:
mode:
authorcathook <b01902109@csie.ntu.edu.tw>2014-06-01 13:56:57 +0800
committercathook <b01902109@csie.ntu.edu.tw>2014-06-01 13:56:57 +0800
commitd5052f1c296dddf51b3e83d59bf3e3c1952cb2d0 (patch)
tree16f7920c5079e0aefcf9509d2dbab59c464d42bd /meowpp/oo
parentbd58f63900410ec4764031f2e6de2d75e91434b3 (diff)
downloadmeow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.gz
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.bz2
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.lz
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.xz
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.zst
meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.zip
big chnage
Diffstat (limited to 'meowpp/oo')
-rw-r--r--meowpp/oo/!readme.asciidoc32
-rw-r--r--meowpp/oo/ObjArray.h166
-rw-r--r--meowpp/oo/ObjBase.h58
-rw-r--r--meowpp/oo/ObjDictionary.h158
-rw-r--r--meowpp/oo/ObjProperties.h55
-rw-r--r--meowpp/oo/ObjSelector.h245
-rw-r--r--meowpp/oo/ObjTypes.h202
7 files changed, 842 insertions, 74 deletions
diff --git a/meowpp/oo/!readme.asciidoc b/meowpp/oo/!readme.asciidoc
new file mode 100644
index 0000000..2e91ca5
--- /dev/null
+++ b/meowpp/oo/!readme.asciidoc
@@ -0,0 +1,32 @@
+
+物件相關
+
+===== ObjBase.h
+
+.Classes
+* `meow::ObjBase`
+
+===== ObjTypes.h
+
+.Classes
+* `meow::ObjType`
+* `meow::ObjInt`
+* `meow::ObjSizeT`
+* `meow::ObjDouble`
+* `meow::ObjString`
+
+===== ObjArray.h
+
+.Classes
+* `meow::ObjArray`
+
+===== ObjDictionary.h
+
+.Classes
+* `meow::ObjDictionary`
+
+===== ObjSelector.h
+
+.Classes
+* `meow::ObjSelector<SID>`
+
diff --git a/meowpp/oo/ObjArray.h b/meowpp/oo/ObjArray.h
new file mode 100644
index 0000000..4417524
--- /dev/null
+++ b/meowpp/oo/ObjArray.h
@@ -0,0 +1,166 @@
+#ifndef oo_ObjArray_H__
+#define oo_ObjArray_H__
+
+#include "ObjBase.h"
+
+#include "../Self.h"
+
+#include <vector>
+#include <string>
+#include <typeinfo>
+
+#include <cstdio>
+#include <cstdlib>
+
+namespace meow {
+
+/*!
+ * @brief 純粹把 \c std::vector 包起來, 變成繼承自 ObjBase
+ *
+ * @author cathook
+ */
+template<class T>
+class ObjArray: public ObjBase {
+private:
+ struct Myself {
+ std::vector<T> array_;
+ Myself() {
+ }
+ ~Myself() {
+ }
+ Myself& copyFrom(Myself const& b) {
+ array_ = b.array_;
+ return *this;
+ }
+ };
+ Self<Myself> const self;
+public:
+ ObjArray(): self(true) {
+ }
+
+ ObjArray(ObjArray const& a): self(false) {
+ self().copyFrom(a.self);
+ }
+
+ ObjArray(std::vector<T> const& a): self(true) {
+ self()->array_ = a;
+ }
+
+ ObjArray(size_t sz, T const& e): self(true) {
+ self()->array_.resize(sz, e);
+ }
+
+ ~ObjArray() {
+ }
+
+ ObjArray& copyFrom(ObjArray const& a) {
+ self().copyFrom(a.self);
+ return *this;
+ }
+
+ ObjArray& referenceFrom(ObjArray const& a) {
+ self().referenceFrom(a.self);
+ return *this;
+ }
+
+ size_t size() const {
+ return self->array_.size();
+ }
+ bool empty() const {
+ return self->array_.empty();
+ }
+
+ size_t size(size_t res, T const& i) {
+ self()->array_.resize(res, i);
+ return size();
+ }
+
+ size_t size(size_t res) {
+ self()->array_.resize(res);
+ return size();
+ }
+
+ void clear() {
+ self()->array_.clear();
+ }
+
+ T const& entry(size_t i) const {
+ return self->array_[i];
+ }
+ T const& entry(size_t i, T const& e) {
+ self()->array_[i] = e;
+ return entry(i);
+ }
+
+ T const& putBack(T const& e) {
+ self()->array_.push_back(e);
+ return entry(size() - 1);
+ }
+
+ bool popBack() {
+ if (empty()) return false;
+ self()->array_.pop_back();
+ return true;
+ }
+
+ ObjArray& operator=(ObjArray const& a) {
+ return copyFrom(a);
+ }
+
+ T const& operator[](size_t i) const {
+ return self->array_[i];
+ }
+
+ T& operator[](size_t i) {
+ return self()->array_[i];
+ }
+
+ bool write(FILE* f, bool bin, unsigned int fg) const {
+ size_t sz = size();
+ if (bin) {
+ if (fwrite(&sz, sizeof(size_t), 1, f) < 1) return false;
+ }
+ else {
+ if (fprintf(f, "%lu\n", sz) < 1) return false;
+ }
+ for (size_t i = 0; i < sz; i++) {
+ if (self->array_[i].write(f, bin, fg) == false) return false;
+ }
+ return true;
+ }
+
+ bool read(FILE* f, bool bin, unsigned int fg) {
+ size_t sz;
+ if (bin) {
+ if (fread(&sz, sizeof(size_t), 1, f) < 1) return false;
+ }
+ else {
+ if (fscanf(f, "%lu\n", &sz) < 0) return false;
+ }
+ size(sz);
+ for (size_t i = 0; i < sz; i++) {
+ if (self()->array_[i].read(f, bin, fg) == false) return false;
+ }
+ return true;
+ }
+
+ ObjBase* create() const {
+ return new ObjArray();
+ }
+
+ ObjBase* copyFrom(ObjBase const* b) {
+ return &(copyFrom(*b));
+ }
+
+ char const* ctype() const {
+ return typeid(*this).name();
+ }
+
+ std::string type() const {
+ return std::string(ctype());
+ }
+};
+
+}
+
+#endif // oo_ObjArray_H__
diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h
index 3aa99d7..974fdf1 100644
--- a/meowpp/oo/ObjBase.h
+++ b/meowpp/oo/ObjBase.h
@@ -7,33 +7,37 @@
#include <string>
namespace meow{
- class ObjBase{
- protected:
- ObjBase(){ }
- public:
- virtual ~ObjBase(){ }
- //
- 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;
- }
- virtual std::string type() const{
- return std::string(ctype());
- }
- //
- static char const* ctypeBase(){
- static char const* ptr = typeid(ObjBase).name();
- return ptr;
- }
- static std::string typeBase(){
- return std::string(ctypeBase());
- }
- };
+
+/*!
+ * @brief 一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
+ *
+ * @author cathook
+ */
+class ObjBase {
+protected:
+ ObjBase(){ }
+public:
+ virtual ~ObjBase(){ }
+ //
+ virtual bool write(FILE* f,bool bin,unsigned int fg) const { return false; }
+ virtual bool read(FILE* f,bool bin,unsigned int fg) { return false; }
+ //
+ virtual ObjBase* create() const { return NULL; }
+ virtual ObjBase* copyFrom(ObjBase const* b) { (*this) = (*b); return this; }
+ //
+ virtual char const* ctype() const{
+ static char const* ptr = typeid(*this).name();
+ return ptr;
+ }
+ virtual std::string type() const{ return std::string(ctype()); }
+ //
+ static char const* ctypeBase(){
+ static char const* ptr = typeid(ObjBase).name();
+ return ptr;
+ }
+ static std::string typeBase(){ return std::string(ctypeBase()); }
+};
+
}
#endif // oo_ObjBase_H__
diff --git a/meowpp/oo/ObjDictionary.h b/meowpp/oo/ObjDictionary.h
new file mode 100644
index 0000000..39e103e
--- /dev/null
+++ b/meowpp/oo/ObjDictionary.h
@@ -0,0 +1,158 @@
+#ifndef oo_ObjDictionary_H__
+#define oo_ObjDictionary_H__
+
+#include "ObjBase.h"
+
+#include "../Self.h"
+
+#include <string>
+#include <typeinfo>
+#include <map>
+
+#include <cstdio>
+#include <cstdlib>
+
+namespace meow {
+
+/*!
+ * @brief 純粹把 \c std::map 包起來, 變成繼承自 ObjBase
+ *
+ * @author cathook
+ */
+template<class Key, class Value>
+class ObjDictionary: public ObjBase {
+private:
+ struct Myself {
+ std::map<Key, Value> dictionary_;
+ Myself() {
+ }
+ ~Myself() {
+ }
+ Myself& copyFrom(Myself const& b) {
+ dictionary_ = b.dictionary_;
+ return *this;
+ }
+ };
+ Self<Myself> const self;
+public:
+ ObjDictionary(): self(true) {
+ }
+
+ ObjDictionary(ObjDictionary const& d): self(false) {
+ self.copyFrom(b.self);
+ }
+
+ ObjDictionary(std::map<Key, Value> const& d): self(true) {
+ self()->dictionary_ = d;
+ }
+
+ ~ObjDictionary() {
+ }
+
+ ObjDictionary& copyFrom(ObjDictionary const& d) {
+ self().copyFrom(d.self);
+ return *this;
+ }
+
+ ObjDictionary& referenceFrom(ObjDictionary const& d) {
+ self().referenceFrom(d.self);
+ return *this;
+ }
+
+ size_t size() const {
+ return self->dictionary_.size();
+ }
+ bool empty() const {
+ return self->dictionary_.empty();
+ }
+
+ void clear() {
+ self()->dictionary_.clear();
+ }
+
+ std::map<Key, Value>::const_iterator end() const {
+ return self->dictionary_.end();
+ }
+
+ std::map<Key, Value>::iterator end() {
+ return self()->dictionary_.end();
+ }
+
+ std::map<Key, Value>::const_iterator find(Key const& k) const {
+ return self->dictionary_.find(k);
+ }
+
+ std::map<Key, Value>::iterator find(Key const& k) {
+ return self()->dictionary_.find(k);
+ }
+
+ bool exist(Key const& k) const {
+ return (find() != end());
+ }
+
+ void insert(Key const& k, Value const& v) {
+ self->dictionary_.insert(std::pair<Key, Value>(k, v));
+ }
+
+ ObjDictionary& operator=(ObjDictionary const& a) {
+ return copyFrom(a);
+ }
+
+ Value& operator[](Key const& k) {
+ return self()->dictionary_[k];
+ }
+
+ bool write(FILE* f, bool bin, unsigned int fg) const {
+ size_t sz = size();
+ if (bin) {
+ if (fwrite(&sz, sizeof(size_t), 1, f) < 1) return false;
+ }
+ else {
+ if (fprintf(f, "%lu\n", sz) < 1) return false;
+ }
+ for (std::map<Key, Value>::const_iterator
+ it = self->dictionary_.begin(); it != self->dictionary_.end(); ++it) {
+ if (it->first .write(f, bin, fg) == false) return false;
+ if (it->second.write(f, bin, fg) == false) return false;
+ }
+ return true;
+ }
+
+ bool read(FILE* f, bool bin, unsigned int fg) {
+ size_t sz;
+ if (bin) {
+ if (fread(&sz, sizeof(size_t), 1, f) < 1) return false;
+ }
+ else {
+ if (fscanf(f, "%lu\n", &sz) < 0) return false;
+ }
+ for (size_t i = 0; i < sz; i++) {
+ Key k;
+ Value v;
+ if (k.read(f, bin, fg) == false) return false;
+ if (v.read(f, bin, fg) == false) return false;
+ insert(k, v);
+ }
+ return true;
+ }
+
+ ObjBase* create() const {
+ return new ObjDictionary();
+ }
+
+ ObjBase* copyFrom(ObjBase const* b) {
+ return &(copyFrom(*(ObjDictionary*)b));
+ }
+
+ char const* ctype() const {
+ return typeid(*this).name();
+ }
+
+ std::string type() const {
+ return std::string(ctype());
+ }
+};
+
+}
+
+#endif // oo_ObjDictionary_H__
diff --git a/meowpp/oo/ObjProperties.h b/meowpp/oo/ObjProperties.h
new file mode 100644
index 0000000..1d1ac64
--- /dev/null
+++ b/meowpp/oo/ObjProperties.h
@@ -0,0 +1,55 @@
+#ifndef oo_ObjProperties_H__
+#define oo_ObjProperties_H__
+
+#include "ObjBase.h"
+
+#include <cstdlib>
+
+namespace meow {
+
+template<size_t SID>
+
+//! 目前擺爛中
+class ObjProperties: public ObjBase {
+private:
+public:
+ ObjProperties();
+
+ ObjProperties(ObjProperties const& p);
+
+ virtual ~ObjProperties();
+
+ size_t propertySize() const;
+
+ bool propertyEmpty() const;
+
+ void propertyClear();
+
+ ObjBase const* property(std::string name) const;
+
+ ObjBase* property(std::string name);
+
+ bool propertyAdd(std::string name, ObjBase* obj, bool autoRemove);
+
+ bool propertyDel(std::string name);
+
+ ObjProperties& properties() const;
+
+ ObjProperties& properties(ObjProperties const& p);
+
+ bool write(FILE* f, bool bin, unsigned int fg) const;
+
+ bool read(FILE* f, bool bin, unsigned int fg);
+
+ ObjBase* create() const;
+
+ ObjBase* copyFrom(ObjBase const* b);
+
+ char const* ctype() const;
+
+ std::string type() const;
+};
+
+}
+
+#endif // oo_ObjProperties_H__
diff --git a/meowpp/oo/ObjSelector.h b/meowpp/oo/ObjSelector.h
index 58debcb..7664a11 100644
--- a/meowpp/oo/ObjSelector.h
+++ b/meowpp/oo/ObjSelector.h
@@ -3,60 +3,211 @@
#include "ObjBase.h"
+#include <utility>
+#include <vector>
+#include <string>
+#include <map>
+
#include <cstdlib>
+#include <cstdio>
namespace meow{
- template<size_t id>
- class ObjSelector{
- private:
- typedef std::map <std::string, ObjBase*> Funcs;
- typedef std::vector<std::string > Types;
- //
- static Funcs& funcs(){
- static Funcs f;
- return f;
- }
- //
- std::string name;
- ObjBase* ptr;
- public:
- ObjSelector( ObjBase* o){ add(name = o->type(), ptr = o); }
- ObjSelector(std::string n, ObjBase* o){ add(name = n , ptr = o); }
- ~ObjSelector(){ del(name); }
- //
- static void add(std::string n, ObjBase* b){
- del(n);
- funcs()[n] = b;
- }
- static void del(std::string s){
- if(funcs().find(s) != funcs().end()){
- delete funcs()[s];
- funcs().erase(s);
- }
- }
- static ObjBase const* access(std::string s){
- if(funcs().find(s) == funcs().end()) return NULL;
- return funcs()[s];
+
+/*!
+ * @brief 利用register的概念, 達到runtime用string選擇要new的class
+ *
+ * @author cathook
+ */
+template<size_t id> //!< 讓程式可以有不只一個 \c ObjSelector
+class ObjSelector {
+private:
+ struct Info {
+ ObjSelector* parent_;
+ ObjBase const* pointer_;
+ bool autoDelete_;
+ //
+ Info(ObjSelector* parent,
+ ObjBase const* ptr,
+ bool autoDelete) {
+ parent_ = parent;
+ pointer_ = ptr;
+ autoDelete_ = autoDelete;
+ }
+ ~Info() {
+ if (autoDelete_) {
+ delete pointer_;
}
- static ObjBase* get(std::string s){
- if(funcs().find(s) == funcs().end() || funcs()[s] == NULL)
- return NULL;
- return funcs()[s]->create();
+ if (parent_ != NULL) {
+ parent_->me_.second = NULL;
}
- 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();
+ }
+ };
+ friend struct Info;
+
+ typedef typename std::map<std::string, Info*> Funcs;
+ typedef typename std::map<std::string, Info*>::iterator FuncsIterator;
+
+ static Funcs& funcs() {
+ static Funcs f;
+ return f;
+ }
+ static Info* add(std::string name,
+ ObjSelector* parent,
+ ObjBase* ptr,
+ bool autoDelete) {
+ Info* info = new Info(parent, ptr, autoDelete);
+ del(name);
+ funcs()[name] = info;
+ return info;
+ }
+
+ std::pair<std::string, Info*> me_;
+public:
+ /*!
+ * @brief 新增(註冊) 一個Class (必須要繼承自 \c ObjBase) 並且給定其Name
+ */
+ static void add(std::string name, ObjBase* obj, bool autoDelete) {
+ add(name, NULL, obj, autoDelete);
+ }
+
+ /*!
+ * @brief 新增(註冊) 一個Class (必須要繼承自 \c ObjBase) 並且默認type為name
+ */
+ static void add(ObjBase* obj, bool autoDelete) {
+ add(obj->type(), NULL, obj, autoDelete);
+ }
+
+ /*!
+ * @brief 依照name刪除之前註冊過得Class
+ */
+ static void del(std::string name) {
+ if (funcs().find(name) != funcs().end()) {
+ delete funcs()[name];
+ funcs().erase(name);
+ }
+ }
+
+ /*!
+ * @brief 取得之前註冊過得Class
+ */
+ static ObjBase const* get(std::string name) {
+ if (funcs().find(name) == funcs().end()) return NULL;
+ return funcs()[name]->pointer_;
+ }
+
+ /*!
+ * @brief 回傳一個之前註冊過得Class new出來的實體
+ */
+ static ObjBase* create(std::string name) {
+ ObjBase const* ptr = get(name);
+ if(ptr == NULL) return NULL;
+ return ptr->create();
+ }
+
+ /*!
+ * @brief 利用type檢查是否有註冊過同種類的Class
+ */
+ static bool exist(ObjBase* obj) {
+ for (FuncsIterator it = funcs().begin(); it != funcs().end(); it++) {
+ if (it->second->pointer_ == obj ||
+ (it->second->pointer_ != NULL &&
+ it->second->pointer_->type() == obj->type())) {
+ return true;
}
- static Types lst(){
- Types ret;
- for(Funcs::iterator it = funcs().begin(); it != funcs().end(); it++)
- ret.push_back(it->first);
- return ret;
+ }
+ return false;
+ }
+
+ /*!
+ * @brief 利用type尋找name
+ */
+ static std::string name(ObjBase* obj) {
+ for (FuncsIterator it = funcs().begin(); it != funcs().end(); it++) {
+ if (it->second->pointer_ == obj ||
+ (it->second->pointer_ != NULL &&
+ it->second->pointer_->type() == obj->type())) {
+ return it->first;
}
- };
+ }
+ return std::string();
+ }
+
+ /*!
+ * @brief 回傳所有註冊過的name
+ */
+ static std::vector<std::string> names() {
+ std::vector<std::string> ret;
+ for (FuncsIterator it = funcs().begin(); it != funcs().end(); it++)
+ ret.push_back(it->first);
+ return ret;
+ }
+
+ /*!
+ * @brief 宣告一個ObjSelector實體, 並且註冊一個 ObjBase
+ */
+ ObjSelector(std::string name, ObjBase* obj, bool autoDelete) {
+ me_.first = name;
+ me_.second = add(me_.first, this, obj, autoDelete);
+ }
+
+ /*!
+ * @brief 宣告一個ObjSelector實體, 並且註冊一個 ObjBase
+ */
+ ObjSelector(ObjBase* obj, bool autoDelete) {
+ me_.first = obj->type();
+ me_.second = add(me_.first, this, obj, autoDelete);
+ }
+
+ //! 解構子
+ ~ObjSelector() {
+ if (me_.second != NULL) {
+ del(me_.first);
+ }
+ }
+
+ /*!
+ * @brief 將一個物件寫到檔案裡(該物件必須要有註冊過)
+ */
+ static bool write(FILE* f, bool binary, ObjBase* obj, unsigned int fg) {
+ if (!exist(obj)) return false;
+ char const* nme = name(obj).c_str();
+ size_t len = strlen(nme);
+ if (binary) {
+ if (fwrite(&len, sizeof(size_t ), 1, f) < 1) return false;
+ if (fwrite(nme , sizeof(char ), len, f) < len) return false;
+ if (fwrite(&fg , sizeof(unsigned int), 1, f) < 1) return false;
+ } else {
+ if (fprintf(f, "%s %u\n", nme, fg) < 2) return false;
+ }
+ return obj->write(f, binary, fg);
+ }
+
+ /*!
+ * @brief 從檔案中讀取一個物件(該物件必須要有註冊過)
+ */
+ static ObjBase* read(FILE* f, bool binary) {
+ char name[2048];
+ size_t len;
+ unsigned int fg;
+ if (binary) {
+ 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 {
+ if (fscanf(f, "%s %u", name, &fg) < 2) return NULL;
+ }
+ ObjBase* ret = create(std::string(name));
+ if (ret != NULL && ret->read(f, binary, fg) == false) {
+ delete ret;
+ ret = NULL;
+ }
+ return ret;
+ }
+};
+
+static const size_t kGlobalSeletorID = 0;
+
}
#endif // oo_ObjSelector_H__
diff --git a/meowpp/oo/ObjTypes.h b/meowpp/oo/ObjTypes.h
new file mode 100644
index 0000000..4092d63
--- /dev/null
+++ b/meowpp/oo/ObjTypes.h
@@ -0,0 +1,202 @@
+#ifndef oo_ObjType_H__
+#define oo_ObjType_H__
+
+#include "../Self.h"
+#include "ObjBase.h"
+
+#include <cstdlib>
+#include <cstdio>
+
+namespace meow {
+
+/*!
+ * @brief 純粹把給定的 \c Type 包起來, 變成繼承自 ObjBase
+ *
+ * @author cathook
+ */
+template<class Type, class ReaderWriter>
+class ObjType: public ObjBase {
+private:
+ struct Myself {
+ Type data_;
+ Myself() {
+ }
+ ~Myself() {
+ }
+ Myself copyFrom(Myself const& b) {
+ data_ = b.data_;
+ }
+ };
+ Self<data_> const self;
+public:
+ //! @brief constructor
+ ObjType(): self(true) {
+ }
+
+ //! @brief constructor, 並且copy資料
+ ObjType(ObjType const& a): self(false) {
+ self().copyFrom(a.self);
+ }
+
+ //! @brief constructor, 並且給值
+ ObyType(Type const& t): self(true) {
+ self().data_ = t;
+ }
+
+ ~ObjType() {
+ }
+
+ ObjType& copyFrom(ObjType const& a) {
+ self().copyFrom(a.self);
+ return *this;
+ }
+
+ ObjType& referenceFrom(ObjType const& a) {
+ self().referenceFrom(a.self);
+ return *this;
+ }
+
+ Type const& access() const {
+ return self->data_;
+ }
+
+ Type& modify() {
+ return self()->data_;
+ }
+
+ ObjType& operator=(ObjType const& a) {
+ return copyFrom(a);
+ }
+
+ Type const& operator()() const {
+ return access();
+ }
+
+ Type& operator()() {
+ return modify();
+ }
+
+ bool write(FILE* f, bool bin, unsigned int fg) const {
+ return ReaderWriter::write(f, bin, fg, self->data_);
+ }
+
+ bool read(FILE* f, bool bin, unsigned int fg) {
+ return ReaderWriter::read(f, bin, fg, &(self()->data_));
+ }
+
+ ObjBase* create() const {
+ return new ObjType();
+ }
+
+ ObjBase* copyFrom(ObjBase const* b) {
+ return &(copyFrom(&(ObjType const*)b));
+ }
+
+ char const* ctype() const {
+ static char const* ptr = typeid(*this).name();
+ return ptr;
+ }
+
+ std::string type() const {
+ return std::string(ctype());
+ }
+};
+
+class ReaderWriter_int {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, int const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%d\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, int* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%d", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_size_t {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, size_t const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%lu\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, size_t* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%lu", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_double {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, double const& k) {
+ if (bin) {
+ return (fwrite(&k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fprintf(f, "%.15f\n", k) == 1);
+ }
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, double* k) {
+ if (bin) {
+ return (fread(k, sizeof(k), 1, f) == 1);
+ }
+ else {
+ return (fscanf(f, "%lf", k) == 1);
+ }
+ }
+};
+
+class ReaderWriter_string {
+public:
+ static bool write(FILE* f, bool bin, unsigned int fg, std::string const& k) {
+ size_t len = k.size();
+ char const* buf = k.c_str();
+ if (bin) {
+ if (fwrite(&len, sizeof(len) , 1, f) < 1) return false;
+ if (fwrite( buf, sizeof(char), len, f) < len) return false;
+ }
+ else {
+ if (fprintf(f, "%s\n", buf) < 1) return false;
+ }
+ return true;
+ }
+ static bool read(FILE* f, bool bin, unsigned int fg, std::string* k) {
+ size_t len;
+ char buf[2048];
+ if (bin) {
+ if (fread(&len, sizeof(len) , 1, f) < 1) return false;
+ if (fread( buf, sizeof(char), len, f) < len) return false;
+ buf[len] = '\0';
+ }
+ else {
+ if (fscanf(f, "%s", buf) < 1) return false;
+ }
+ (*k) = buf;
+ return true;
+ }
+};
+
+typedef ObjType<int , ReaderWriter_int > ObjInt;
+typedef ObjType<size_t , ReaderWriter_size_t> ObjSizeT;
+typedef ObjType<double , ReaderWriter_double> ObjDouble;
+typedef ObjType<std::string, ReaderWriter_string> ObjString;
+
+}
+
+#endif // oo_ObjType_H__