diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-19 07:25:48 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-19 07:25:48 +0800 |
commit | fe926756145c5e5cf5f315af0acdbfd85ba27543 (patch) | |
tree | 4d75f94b87fd6d60262f2377d92f5896faf1be7d /meowpp/oo | |
parent | b2b55d8c642524274d8115d5b1863e1a40715887 (diff) | |
download | meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.gz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.bz2 meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.lz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.xz meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.tar.zst meow-fe926756145c5e5cf5f315af0acdbfd85ba27543.zip |
x
Diffstat (limited to 'meowpp/oo')
-rw-r--r-- | meowpp/oo/ObjArray.h | 26 | ||||
-rw-r--r-- | meowpp/oo/ObjBase.h | 95 | ||||
-rw-r--r-- | meowpp/oo/ObjSelector.h | 4 | ||||
-rw-r--r-- | meowpp/oo/ObjTypes.h | 19 |
4 files changed, 98 insertions, 46 deletions
diff --git a/meowpp/oo/ObjArray.h b/meowpp/oo/ObjArray.h index 4417524..b58b89c 100644 --- a/meowpp/oo/ObjArray.h +++ b/meowpp/oo/ObjArray.h @@ -37,19 +37,19 @@ private: 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() { } @@ -79,7 +79,7 @@ public: self()->array_.resize(res); return size(); } - + void clear() { self()->array_.clear(); } @@ -96,7 +96,7 @@ public: self()->array_.push_back(e); return entry(size() - 1); } - + bool popBack() { if (empty()) return false; self()->array_.pop_back(); @@ -106,11 +106,11 @@ public: 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]; } @@ -128,7 +128,7 @@ public: } return true; } - + bool read(FILE* f, bool bin, unsigned int fg) { size_t sz; if (bin) { @@ -143,19 +143,19 @@ public: } 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()); } diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h index 974fdf1..0ae9427 100644 --- a/meowpp/oo/ObjBase.h +++ b/meowpp/oo/ObjBase.h @@ -2,11 +2,10 @@ #define oo_ObjBase_H__ #include <cstdio> - #include <typeinfo> #include <string> -namespace meow{ +namespace meow { /*! * @brief 一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能 @@ -18,26 +17,80 @@ 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()); } + + /*! + * @brief 將物件寫入檔案, 預設implement為直接回傳 \c false + * + * @param [in] f 檔案 + * @param [in] bin 是否為binary模式 + * @param [in] fg 使用者自訂的argument + * @return 成功或失敗 + */ + virtual bool write(FILE* f, bool bin, unsigned int fg) const { + return false; + } + + /*! + * @brief 將物件從檔案讀出, 預設implement為直接回傳 \c false + * + * @param [in] f 檔案 + * @param [in] bin 是否為binary模式 + * @param [in] fg 使用者自訂的argument + * @return 成功或失敗 + */ + 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= + * + * @param [in] b 資料來源 + * @return \c this + */ + virtual ObjBase* copyFrom(ObjBase const* b) { + (*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; + } + + /*! + * @brief 用C-style string回傳base的type name + */ + static char const* ctypeBase() { + return typeid(ObjBase).name(); + } + + /*! + * @brief 用std::string回傳base的type name + */ + static std::string typeBase() { + static std::string s(ctypeBase()); + return s; + } }; -} +} // meow #endif // oo_ObjBase_H__ diff --git a/meowpp/oo/ObjSelector.h b/meowpp/oo/ObjSelector.h index 7664a11..58d86a0 100644 --- a/meowpp/oo/ObjSelector.h +++ b/meowpp/oo/ObjSelector.h @@ -11,7 +11,7 @@ #include <cstdlib> #include <cstdio> -namespace meow{ +namespace meow { /*! * @brief 利用register的概念, 達到runtime用string選擇要new的class @@ -186,7 +186,7 @@ public: * @brief 從檔案中讀取一個物件(該物件必須要有註冊過) */ static ObjBase* read(FILE* f, bool binary) { - char name[2048]; + static char name[2048]; size_t len; unsigned int fg; if (binary) { diff --git a/meowpp/oo/ObjTypes.h b/meowpp/oo/ObjTypes.h index 4092d63..48a4a07 100644 --- a/meowpp/oo/ObjTypes.h +++ b/meowpp/oo/ObjTypes.h @@ -21,6 +21,8 @@ private: Type data_; Myself() { } + Myself(Type const& t): data_(t) { + } ~Myself() { } Myself copyFrom(Myself const& b) { @@ -30,17 +32,15 @@ private: Self<data_> const self; public: //! @brief constructor - ObjType(): self(true) { + ObjType(): self() { } - //! @brief constructor, 並且copy資料 - ObjType(ObjType const& a): self(false) { - self().copyFrom(a.self); + //! @brief constructor, 並且給值 + ObyType(Type const& t): self(Myself(t)) { } - //! @brief constructor, 並且給值 - ObyType(Type const& t): self(true) { - self().data_ = t; + //! @brief constructor, 並且copy資料 + ObjType(ObjType const& a): self(a.self, COPY_FROM) { } ~ObjType() { @@ -93,8 +93,7 @@ public: } char const* ctype() const { - static char const* ptr = typeid(*this).name(); - return ptr; + return typeid(*this).name(); } std::string type() const { @@ -197,6 +196,6 @@ typedef ObjType<size_t , ReaderWriter_size_t> ObjSizeT; typedef ObjType<double , ReaderWriter_double> ObjDouble; typedef ObjType<std::string, ReaderWriter_string> ObjString; -} +} // meow #endif // oo_ObjType_H__ |