diff options
Diffstat (limited to 'meowpp/oo')
-rw-r--r-- | meowpp/oo/!readme.asciidoc | 32 | ||||
-rw-r--r-- | meowpp/oo/ObjArray.h | 168 | ||||
-rw-r--r-- | meowpp/oo/ObjBase.h | 99 | ||||
-rw-r--r-- | meowpp/oo/ObjDictionary.h | 167 | ||||
-rw-r--r-- | meowpp/oo/ObjProperties.h | 55 | ||||
-rw-r--r-- | meowpp/oo/ObjSelector.h | 214 | ||||
-rw-r--r-- | meowpp/oo/ObjTypes.h | 201 |
7 files changed, 0 insertions, 936 deletions
diff --git a/meowpp/oo/!readme.asciidoc b/meowpp/oo/!readme.asciidoc deleted file mode 100644 index 2e91ca5..0000000 --- a/meowpp/oo/!readme.asciidoc +++ /dev/null @@ -1,32 +0,0 @@ - -物件相關 - -===== 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 deleted file mode 100644 index 804f65f..0000000 --- a/meowpp/oo/ObjArray.h +++ /dev/null @@ -1,168 +0,0 @@ -#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 const& b): array_(b.array_) { - } - - Myself(size_t sz, T const& e): array_(sz, e) { - } - - ~Myself() { - } - }; - Self<Myself> const self; -public: - ObjArray(): self() { - } - - ObjArray(ObjArray const& a): self(a.self, Self<Myself>::COPY_FROM) { - } - - ObjArray(std::vector<T> const& a): self(a) { - } - - ObjArray(size_t sz, T const& e): self(Myself(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 entry(size_t i) const { - return self->array_[i]; - } - - T entry(size_t i, T const& e) { - self()->array_[i] = e; - return entry(i); - } - - T 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 operator[](size_t i) const { - return self->array_[i]; - } - - std::vector<T>::reference 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) < 1) 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(*(ObjArray const*)b)); - } - - char const* ctype() const { - return typeid(*this).name(); - } - - std::string type() const { - return std::string(ctype()); - } -}; - -} // meow - -#endif // oo_ObjArray_H__ diff --git a/meowpp/oo/ObjBase.h b/meowpp/oo/ObjBase.h deleted file mode 100644 index 253afb6..0000000 --- a/meowpp/oo/ObjBase.h +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef oo_ObjBase_H__ -#define oo_ObjBase_H__ - -#include <cstdio> -#include <typeinfo> -#include <string> - -namespace meow { - -/*! - * @brief 一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能 - * - * @author cathook - */ -class ObjBase { -protected: - - /*! - * @brief Constructor with doing nothing - */ - ObjBase() { } -public: - virtual ~ObjBase() { } - - /*! - * @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 { - 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 - */ - static std::string typeBase() { - static std::string s(ctypeBase()); - return s; - } -}; - -} // meow - -#endif // oo_ObjBase_H__ diff --git a/meowpp/oo/ObjDictionary.h b/meowpp/oo/ObjDictionary.h deleted file mode 100644 index f43be58..0000000 --- a/meowpp/oo/ObjDictionary.h +++ /dev/null @@ -1,167 +0,0 @@ -#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 const& b): dictionary_(b.dictionary_) { - } - - ~Myself() { - } - }; - - Self<Myself> const self; -public: - ObjDictionary(): self() { - } - - ObjDictionary(ObjDictionary const& d): self(d.self, Self<Myself>::COPY_FROM) { - self.copyFrom(b.self); - } - - ObjDictionary(std::map<Key, Value> const& d): self(Myself(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 first() const { - return self()->dictionary_.begin(); - } - - std::map<Key, Value>::iterator first() { - return self()->dictionary_.begin(); - } - - std::map<Key, Value>::const_iterator end() const { - return self()->dictionary_.end(); // OAO!!! - } - - 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); // OAO!!! - } - - 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 = begin(); it != 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 const*)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 deleted file mode 100644 index 01e01d0..0000000 --- a/meowpp/oo/ObjProperties.h +++ /dev/null @@ -1,55 +0,0 @@ -#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 deleted file mode 100644 index f08cfe4..0000000 --- a/meowpp/oo/ObjSelector.h +++ /dev/null @@ -1,214 +0,0 @@ -#ifndef oo_ObjSelector_H__ -#define oo_ObjSelector_H__ - -#include "ObjBase.h" - -#include <utility> -#include <vector> -#include <string> -#include <map> - -#include <cstdlib> -#include <cstdio> - -namespace meow { - -/*! - * @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_; - } - if (parent_ != NULL) { - parent_->me_.second = NULL; - } - } - }; - friend struct Info; - - typedef typename std::map<std::string, Info*> Infos; - typedef typename std::map<std::string, Info*>::iterator InfosIterator; - - static Infos& funcs() { - static Infos 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 (InfosIterator it = funcs().begin(); it != funcs().end(); it++) { - if (it->second->pointer_ == obj || - (it->second->pointer_ != NULL && - it->second->pointer_->type() == obj->type())) { - return true; - } - } - return false; - } - - /*! - * @brief 利用type尋找name - */ - static std::string name(ObjBase* obj) { - for (InfosIterator 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 (InfosIterator 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) { - static 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; - -} // meow - -#endif // oo_ObjSelector_H__ diff --git a/meowpp/oo/ObjTypes.h b/meowpp/oo/ObjTypes.h deleted file mode 100644 index ba2d358..0000000 --- a/meowpp/oo/ObjTypes.h +++ /dev/null @@ -1,201 +0,0 @@ -#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(Type const& t): data_(t) { - } - - ~Myself() { - } - }; - Self<Type> 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, Self<Type>::COPY_FROM) { - } - - ~ObjType() { - } - - ObjType& copyFrom(ObjType const& a) { - self().copyFrom(a.self); - return *this; - } - - ObjType& referenceFrom(ObjType const& a) { - self().referenceFrom(a.self); - return *this; - } - - Type access() const { - return self->data_; - } - - Type& modify() { - return self()->data_; - } - - ObjType& operator=(ObjType const& a) { - return copyFrom(a); - } - - Type 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 { - return typeid(*this).name(); - } - - 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[81920]; - 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; - -} // meow - -#endif // oo_ObjType_H__ |