1 #ifndef oo_ObjDictionary_H__
2 #define oo_ObjDictionary_H__
22 template<
class Key,
class Value>
26 std::map<Key, Value> dictionary_;
31 Myself(Myself
const& b): dictionary_(b.dictionary_) {
44 self.copyFrom(b.self);
64 return self->dictionary_.size();
68 return self->dictionary_.empty();
72 self()->dictionary_.clear();
75 std::map<Key, Value>::const_iterator
first()
const {
76 return self()->dictionary_.begin();
79 std::map<Key, Value>::iterator
first() {
80 return self()->dictionary_.begin();
83 std::map<Key, Value>::const_iterator
end()
const {
84 return self()->dictionary_.end();
87 std::map<Key, Value>::iterator
end() {
88 return self()->dictionary_.end();
91 std::map<Key, Value>::const_iterator
find(Key
const& k)
const {
92 return self()->dictionary_.find(k);
95 std::map<Key, Value>::iterator
find(Key
const& k) {
96 return self()->dictionary_.find(k);
99 bool exist(Key
const& k)
const {
103 void insert(Key
const& k, Value
const& v) {
104 self->dictionary_.insert(std::pair<Key, Value>(k, v));
112 return self()->dictionary_[k];
115 bool write(FILE* f,
bool bin,
unsigned int fg)
const {
118 if (fwrite(&sz,
sizeof(
size_t), 1, f) < 1)
return false;
121 if (fprintf(f,
"%lu\n", sz) < 1)
return false;
123 for (std::map<Key, Value>::const_iterator it = begin(); it !=
end(); ++it) {
124 if (it->first .write(f, bin, fg) ==
false)
return false;
125 if (it->second.write(f, bin, fg) ==
false)
return false;
130 bool read(FILE* f,
bool bin,
unsigned int fg) {
133 if (fread(&sz,
sizeof(
size_t), 1, f) < 1)
return false;
136 if (fscanf(f,
"%lu\n", &sz) < 0)
return false;
138 for (
size_t i = 0; i < sz; i++) {
141 if (k.read(f, bin, fg) ==
false)
return false;
142 if (v.read(f, bin, fg) ==
false)
return false;
157 return typeid(*this).name();
161 return std::string(
ctype());
167 #endif // oo_ObjDictionary_H__
ObjDictionary(ObjDictionary const &d)
std::map< Key, Value >::iterator first()
ObjDictionary & copyFrom(ObjDictionary const &d)
ObjDictionary & operator=(ObjDictionary const &a)
std::map< Key, Value >::const_iterator first() const
std::string type() const
用std::string回傳這個class的type name
bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
ObjDictionary & referenceFrom(ObjDictionary const &d)
ObjBase * copyFrom(ObjBase const *b)
複製, 預設使用operator=
std::map< Key, Value >::iterator find(Key const &k)
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
ObjBase * create() const
回傳一個new出來的物件, 預設implement為直接回傳 NULL
Value operator[](Key const &k)
char const * ctype() const
用C-style string回傳這個class的type name
ObjDictionary(std::map< Key, Value > const &d)
void insert(Key const &k, Value const &v)
std::map< Key, Value >::const_iterator find(Key const &k) const
std::map< Key, Value >::const_iterator end() const
std::map< Key, Value >::iterator end()
純粹把 std::map 包起來, 變成繼承自 ObjBase
bool exist(Key const &k) const
bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false