From a9955a1a51df2b268da4d28f9ad10dbaf9815634 Mon Sep 17 00:00:00 2001 From: cathook Date: Tue, 24 Jun 2014 04:01:53 +0800 Subject: ... --- meowpp/oo/ObjArray.h | 40 +++++++++++++++++++++------------------- meowpp/oo/ObjBase.h | 27 +++++++++++++++------------ meowpp/oo/ObjDictionary.h | 47 ++++++++++++++++++++++++++++------------------- meowpp/oo/ObjProperties.h | 24 ++++++++++++------------ meowpp/oo/ObjSelector.h | 43 ++++++++++++++++++++++--------------------- meowpp/oo/ObjTypes.h | 42 +++++++++++++++++++++--------------------- 6 files changed, 119 insertions(+), 104 deletions(-) (limited to 'meowpp/oo') diff --git a/meowpp/oo/ObjArray.h b/meowpp/oo/ObjArray.h index b58b89c..804f65f 100644 --- a/meowpp/oo/ObjArray.h +++ b/meowpp/oo/ObjArray.h @@ -24,30 +24,31 @@ class ObjArray: public ObjBase { private: struct Myself { std::vector array_; + Myself() { } - ~Myself() { + + Myself(Myself const& b): array_(b.array_) { } - Myself& copyFrom(Myself const& b) { - array_ = b.array_; - return *this; + + Myself(size_t sz, T const& e): array_(sz, e) { + } + + ~Myself() { } }; Self const self; public: - ObjArray(): self(true) { + ObjArray(): self() { } - ObjArray(ObjArray const& a): self(false) { - self().copyFrom(a.self); + ObjArray(ObjArray const& a): self(a.self, Self::COPY_FROM) { } - ObjArray(std::vector const& a): self(true) { - self()->array_ = a; + ObjArray(std::vector const& a): self(a) { } - ObjArray(size_t sz, T const& e): self(true) { - self()->array_.resize(sz, e); + ObjArray(size_t sz, T const& e): self(Myself(sz, e)) { } ~ObjArray() { @@ -84,15 +85,16 @@ public: self()->array_.clear(); } - T const& entry(size_t i) const { + T entry(size_t i) const { return self->array_[i]; } - T const& entry(size_t i, T const& e) { + + T entry(size_t i, T const& e) { self()->array_[i] = e; return entry(i); } - T const& putBack(T const& e) { + T putBack(T const& e) { self()->array_.push_back(e); return entry(size() - 1); } @@ -107,11 +109,11 @@ public: return copyFrom(a); } - T const& operator[](size_t i) const { + T operator[](size_t i) const { return self->array_[i]; } - T& operator[](size_t i) { + std::vector::reference operator[](size_t i) { return self()->array_[i]; } @@ -135,7 +137,7 @@ public: if (fread(&sz, sizeof(size_t), 1, f) < 1) return false; } else { - if (fscanf(f, "%lu\n", &sz) < 0) return false; + if (fscanf(f, "%lu\n", &sz) < 1) return false; } size(sz); for (size_t i = 0; i < sz; i++) { @@ -149,7 +151,7 @@ public: } ObjBase* copyFrom(ObjBase const* b) { - return &(copyFrom(*b)); + return &(copyFrom(*(ObjArray const*)b)); } char const* ctype() const { @@ -161,6 +163,6 @@ public: } }; -} +} // meow #endif // oo_ObjArray_H__ diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h index 0ae9427..253afb6 100644 --- a/meowpp/oo/ObjBase.h +++ b/meowpp/oo/ObjBase.h @@ -14,10 +14,14 @@ namespace meow { */ class ObjBase { protected: - ObjBase(){ } + + /*! + * @brief Constructor with doing nothing + */ + ObjBase() { } public: - virtual ~ObjBase(){ } - + virtual ~ObjBase() { } + /*! * @brief 將物件寫入檔案, 預設implement為直接回傳 \c false * @@ -29,7 +33,7 @@ public: virtual bool write(FILE* f, bool bin, unsigned int fg) const { return false; } - + /*! * @brief 將物件從檔案讀出, 預設implement為直接回傳 \c false * @@ -41,14 +45,14 @@ public: virtual bool read(FILE* f, bool bin, unsigned int fg) { return false; } - + /*! * @brief 回傳一個new出來的物件, 預設implement為直接回傳 \c NULL */ virtual ObjBase* create() const { return NULL; } - + /*! * @brief 複製, 預設使用operator= * @@ -59,29 +63,28 @@ public: (*this) = (*b); return this; } - + /*! * @brief 用C-style string回傳這個class的type name */ virtual char const* ctype() const { return typeid(*this).name(); } - + /*! * @brief 用std::string回傳這個class的type name */ virtual std::string type() const { - static std::string s(ctype()); - return s; + return std::string(ctype()); } - + /*! * @brief 用C-style string回傳base的type name */ static char const* ctypeBase() { return typeid(ObjBase).name(); } - + /*! * @brief 用std::string回傳base的type name */ diff --git a/meowpp/oo/ObjDictionary.h b/meowpp/oo/ObjDictionary.h index 39e103e..f43be58 100644 --- a/meowpp/oo/ObjDictionary.h +++ b/meowpp/oo/ObjDictionary.h @@ -24,26 +24,27 @@ class ObjDictionary: public ObjBase { private: struct Myself { std::map dictionary_; + Myself() { } - ~Myself() { + + Myself(Myself const& b): dictionary_(b.dictionary_) { } - Myself& copyFrom(Myself const& b) { - dictionary_ = b.dictionary_; - return *this; + + ~Myself() { } }; + Self const self; public: - ObjDictionary(): self(true) { + ObjDictionary(): self() { } - ObjDictionary(ObjDictionary const& d): self(false) { + ObjDictionary(ObjDictionary const& d): self(d.self, Self::COPY_FROM) { self.copyFrom(b.self); } - ObjDictionary(std::map const& d): self(true) { - self()->dictionary_ = d; + ObjDictionary(std::map const& d): self(Myself(d)) { } ~ObjDictionary() { @@ -62,6 +63,7 @@ public: size_t size() const { return self->dictionary_.size(); } + bool empty() const { return self->dictionary_.empty(); } @@ -70,8 +72,16 @@ public: self()->dictionary_.clear(); } + std::map::const_iterator first() const { + return self()->dictionary_.begin(); + } + + std::map::iterator first() { + return self()->dictionary_.begin(); + } + std::map::const_iterator end() const { - return self->dictionary_.end(); + return self()->dictionary_.end(); // OAO!!! } std::map::iterator end() { @@ -79,7 +89,7 @@ public: } std::map::const_iterator find(Key const& k) const { - return self->dictionary_.find(k); + return self()->dictionary_.find(k); // OAO!!! } std::map::iterator find(Key const& k) { @@ -97,8 +107,8 @@ public: ObjDictionary& operator=(ObjDictionary const& a) { return copyFrom(a); } - - Value& operator[](Key const& k) { + + Value operator[](Key const& k) { return self()->dictionary_[k]; } @@ -110,8 +120,7 @@ public: else { if (fprintf(f, "%lu\n", sz) < 1) return false; } - for (std::map::const_iterator - it = self->dictionary_.begin(); it != self->dictionary_.end(); ++it) { + for (std::map::const_iterator it = begin(); it != end(); ++it) { if (it->first .write(f, bin, fg) == false) return false; if (it->second.write(f, bin, fg) == false) return false; } @@ -135,19 +144,19 @@ public: } return true; } - + ObjBase* create() const { return new ObjDictionary(); } - + ObjBase* copyFrom(ObjBase const* b) { - return &(copyFrom(*(ObjDictionary*)b)); + return &(copyFrom(*(ObjDictionary const*)b)); } - + char const* ctype() const { return typeid(*this).name(); } - + std::string type() const { return std::string(ctype()); } diff --git a/meowpp/oo/ObjProperties.h b/meowpp/oo/ObjProperties.h index 1d1ac64..01e01d0 100644 --- a/meowpp/oo/ObjProperties.h +++ b/meowpp/oo/ObjProperties.h @@ -14,9 +14,9 @@ class ObjProperties: public ObjBase { private: public: ObjProperties(); - + ObjProperties(ObjProperties const& p); - + virtual ~ObjProperties(); size_t propertySize() const; @@ -26,27 +26,27 @@ public: 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; }; diff --git a/meowpp/oo/ObjSelector.h b/meowpp/oo/ObjSelector.h index 58d86a0..f08cfe4 100644 --- a/meowpp/oo/ObjSelector.h +++ b/meowpp/oo/ObjSelector.h @@ -25,7 +25,7 @@ private: ObjSelector* parent_; ObjBase const* pointer_; bool autoDelete_; - // + Info(ObjSelector* parent, ObjBase const* ptr, bool autoDelete) { @@ -33,6 +33,7 @@ private: pointer_ = ptr; autoDelete_ = autoDelete; } + ~Info() { if (autoDelete_) { delete pointer_; @@ -44,11 +45,11 @@ private: }; friend struct Info; - typedef typename std::map Funcs; - typedef typename std::map::iterator FuncsIterator; + typedef typename std::map Infos; + typedef typename std::map::iterator InfosIterator; - static Funcs& funcs() { - static Funcs f; + static Infos& funcs() { + static Infos f; return f; } static Info* add(std::string name, @@ -69,14 +70,14 @@ public: 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 */ @@ -86,7 +87,7 @@ public: funcs().erase(name); } } - + /*! * @brief 取得之前註冊過得Class */ @@ -94,7 +95,7 @@ public: if (funcs().find(name) == funcs().end()) return NULL; return funcs()[name]->pointer_; } - + /*! * @brief 回傳一個之前註冊過得Class new出來的實體 */ @@ -103,12 +104,12 @@ public: 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++) { + for (InfosIterator it = funcs().begin(); it != funcs().end(); it++) { if (it->second->pointer_ == obj || (it->second->pointer_ != NULL && it->second->pointer_->type() == obj->type())) { @@ -117,12 +118,12 @@ public: } return false; } - + /*! * @brief 利用type尋找name */ static std::string name(ObjBase* obj) { - for (FuncsIterator it = funcs().begin(); it != funcs().end(); it++) { + for (InfosIterator it = funcs().begin(); it != funcs().end(); it++) { if (it->second->pointer_ == obj || (it->second->pointer_ != NULL && it->second->pointer_->type() == obj->type())) { @@ -131,17 +132,17 @@ public: } return std::string(); } - + /*! * @brief 回傳所有註冊過的name */ static std::vector names() { std::vector ret; - for (FuncsIterator it = funcs().begin(); it != funcs().end(); it++) + for (InfosIterator it = funcs().begin(); it != funcs().end(); it++) ret.push_back(it->first); return ret; } - + /*! * @brief 宣告一個ObjSelector實體, 並且註冊一個 ObjBase */ @@ -149,7 +150,7 @@ public: me_.first = name; me_.second = add(me_.first, this, obj, autoDelete); } - + /*! * @brief 宣告一個ObjSelector實體, 並且註冊一個 ObjBase */ @@ -157,14 +158,14 @@ public: me_.first = obj->type(); me_.second = add(me_.first, this, obj, autoDelete); } - + //! 解構子 ~ObjSelector() { if (me_.second != NULL) { del(me_.first); } } - + /*! * @brief 將一個物件寫到檔案裡(該物件必須要有註冊過) */ @@ -181,7 +182,7 @@ public: } return obj->write(f, binary, fg); } - + /*! * @brief 從檔案中讀取一個物件(該物件必須要有註冊過) */ @@ -208,6 +209,6 @@ public: static const size_t kGlobalSeletorID = 0; -} +} // meow #endif // oo_ObjSelector_H__ diff --git a/meowpp/oo/ObjTypes.h b/meowpp/oo/ObjTypes.h index 48a4a07..ba2d358 100644 --- a/meowpp/oo/ObjTypes.h +++ b/meowpp/oo/ObjTypes.h @@ -19,30 +19,30 @@ class ObjType: public ObjBase { private: struct Myself { Type data_; + Myself() { } + Myself(Type const& t): data_(t) { } + ~Myself() { } - Myself copyFrom(Myself const& b) { - data_ = b.data_; - } }; - Self const self; + Self const self; public: //! @brief constructor ObjType(): self() { } - + //! @brief constructor, 並且給值 ObyType(Type const& t): self(Myself(t)) { } - + //! @brief constructor, 並且copy資料 - ObjType(ObjType const& a): self(a.self, COPY_FROM) { + ObjType(ObjType const& a): self(a.self, Self::COPY_FROM) { } - + ~ObjType() { } @@ -55,11 +55,11 @@ public: self().referenceFrom(a.self); return *this; } - - Type const& access() const { + + Type access() const { return self->data_; } - + Type& modify() { return self()->data_; } @@ -67,11 +67,11 @@ public: ObjType& operator=(ObjType const& a) { return copyFrom(a); } - - Type const& operator()() const { + + Type operator()() const { return access(); } - + Type& operator()() { return modify(); } @@ -79,23 +79,23 @@ public: 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)); + return &(copyFrom(*(ObjType const*)b)); } - + char const* ctype() const { return typeid(*this).name(); } - + std::string type() const { return std::string(ctype()); } @@ -177,7 +177,7 @@ public: } static bool read(FILE* f, bool bin, unsigned int fg, std::string* k) { size_t len; - char buf[2048]; + char buf[81920]; if (bin) { if (fread(&len, sizeof(len) , 1, f) < 1) return false; if (fread( buf, sizeof(char), len, f) < len) return false; -- cgit v1.2.3