Templates -- Meow  1.2.10
A C++ template contains kinds of interesting classes and functions
ObjDictionary.h
Go to the documentation of this file.
1 #ifndef oo_ObjDictionary_H__
2 #define oo_ObjDictionary_H__
3 
4 #include "ObjBase.h"
5 
6 #include "../Self.h"
7 
8 #include <string>
9 #include <typeinfo>
10 #include <map>
11 
12 #include <cstdio>
13 #include <cstdlib>
14 
15 namespace meow {
16 
22 template<class Key, class Value>
23 class ObjDictionary: public ObjBase {
24 private:
25  struct Myself {
26  std::map<Key, Value> dictionary_;
27 
28  Myself() {
29  }
30 
31  Myself(Myself const& b): dictionary_(b.dictionary_) {
32  }
33 
34  ~Myself() {
35  }
36  };
37 
38  Self<Myself> const self;
39 public:
40  ObjDictionary(): self() {
41  }
42 
43  ObjDictionary(ObjDictionary const& d): self(d.self, Self<Myself>::COPY_FROM) {
44  self.copyFrom(b.self);
45  }
46 
47  ObjDictionary(std::map<Key, Value> const& d): self(Myself(d)) {
48  }
49 
51  }
52 
54  self().copyFrom(d.self);
55  return *this;
56  }
57 
59  self().referenceFrom(d.self);
60  return *this;
61  }
62 
63  size_t size() const {
64  return self->dictionary_.size();
65  }
66 
67  bool empty() const {
68  return self->dictionary_.empty();
69  }
70 
71  void clear() {
72  self()->dictionary_.clear();
73  }
74 
75  std::map<Key, Value>::const_iterator first() const {
76  return self()->dictionary_.begin();
77  }
78 
79  std::map<Key, Value>::iterator first() {
80  return self()->dictionary_.begin();
81  }
82 
83  std::map<Key, Value>::const_iterator end() const {
84  return self()->dictionary_.end(); // OAO!!!
85  }
86 
87  std::map<Key, Value>::iterator end() {
88  return self()->dictionary_.end();
89  }
90 
91  std::map<Key, Value>::const_iterator find(Key const& k) const {
92  return self()->dictionary_.find(k); // OAO!!!
93  }
94 
95  std::map<Key, Value>::iterator find(Key const& k) {
96  return self()->dictionary_.find(k);
97  }
98 
99  bool exist(Key const& k) const {
100  return (find() != end());
101  }
102 
103  void insert(Key const& k, Value const& v) {
104  self->dictionary_.insert(std::pair<Key, Value>(k, v));
105  }
106 
108  return copyFrom(a);
109  }
110 
111  Value operator[](Key const& k) {
112  return self()->dictionary_[k];
113  }
114 
115  bool write(FILE* f, bool bin, unsigned int fg) const {
116  size_t sz = size();
117  if (bin) {
118  if (fwrite(&sz, sizeof(size_t), 1, f) < 1) return false;
119  }
120  else {
121  if (fprintf(f, "%lu\n", sz) < 1) return false;
122  }
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;
126  }
127  return true;
128  }
129 
130  bool read(FILE* f, bool bin, unsigned int fg) {
131  size_t sz;
132  if (bin) {
133  if (fread(&sz, sizeof(size_t), 1, f) < 1) return false;
134  }
135  else {
136  if (fscanf(f, "%lu\n", &sz) < 0) return false;
137  }
138  for (size_t i = 0; i < sz; i++) {
139  Key k;
140  Value v;
141  if (k.read(f, bin, fg) == false) return false;
142  if (v.read(f, bin, fg) == false) return false;
143  insert(k, v);
144  }
145  return true;
146  }
147 
148  ObjBase* create() const {
149  return new ObjDictionary();
150  }
151 
152  ObjBase* copyFrom(ObjBase const* b) {
153  return &(copyFrom(*(ObjDictionary const*)b));
154  }
155 
156  char const* ctype() const {
157  return typeid(*this).name();
158  }
159 
160  std::string type() const {
161  return std::string(ctype());
162  }
163 };
164 
165 }
166 
167 #endif // oo_ObjDictionary_H__
ObjDictionary(ObjDictionary const &d)
Definition: ObjDictionary.h:43
std::map< Key, Value >::iterator first()
Definition: ObjDictionary.h:79
ObjDictionary & copyFrom(ObjDictionary const &d)
Definition: ObjDictionary.h:53
size_t size() const
Definition: ObjDictionary.h:63
ObjDictionary & operator=(ObjDictionary const &a)
std::map< Key, Value >::const_iterator first() const
Definition: ObjDictionary.h:75
std::string type() const
用std::string回傳這個class的type name
bool empty() const
Definition: ObjDictionary.h:67
bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
ObjDictionary & referenceFrom(ObjDictionary const &d)
Definition: ObjDictionary.h:58
ObjBase * copyFrom(ObjBase const *b)
複製, 預設使用operator=
std::map< Key, Value >::iterator find(Key const &k)
Definition: ObjDictionary.h:95
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
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)
Definition: ObjDictionary.h:47
void insert(Key const &k, Value const &v)
std::map< Key, Value >::const_iterator find(Key const &k) const
Definition: ObjDictionary.h:91
std::map< Key, Value >::const_iterator end() const
Definition: ObjDictionary.h:83
std::map< Key, Value >::iterator end()
Definition: ObjDictionary.h:87
純粹把 std::map 包起來, 變成繼承自 ObjBase
Definition: ObjDictionary.h:23
bool exist(Key const &k) const
Definition: ObjDictionary.h:99
bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false