Templates -- Meow  1.1.2
不能,也不應該先編譯成obj-file的templates
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  Myself() {
28  }
29  ~Myself() {
30  }
31  Myself& copyFrom(Myself const& b) {
32  dictionary_ = b.dictionary_;
33  return *this;
34  }
35  };
36  Self<Myself> const self;
37 public:
38  ObjDictionary(): self(true) {
39  }
40 
41  ObjDictionary(ObjDictionary const& d): self(false) {
42  self.copyFrom(b.self);
43  }
44 
45  ObjDictionary(std::map<Key, Value> const& d): self(true) {
46  self()->dictionary_ = d;
47  }
48 
50  }
51 
53  self().copyFrom(d.self);
54  return *this;
55  }
56 
58  self().referenceFrom(d.self);
59  return *this;
60  }
61 
62  size_t size() const {
63  return self->dictionary_.size();
64  }
65  bool empty() const {
66  return self->dictionary_.empty();
67  }
68 
69  void clear() {
70  self()->dictionary_.clear();
71  }
72 
73  std::map<Key, Value>::const_iterator end() const {
74  return self->dictionary_.end();
75  }
76 
77  std::map<Key, Value>::iterator end() {
78  return self()->dictionary_.end();
79  }
80 
81  std::map<Key, Value>::const_iterator find(Key const& k) const {
82  return self->dictionary_.find(k);
83  }
84 
85  std::map<Key, Value>::iterator find(Key const& k) {
86  return self()->dictionary_.find(k);
87  }
88 
89  bool exist(Key const& k) const {
90  return (find() != end());
91  }
92 
93  void insert(Key const& k, Value const& v) {
94  self->dictionary_.insert(std::pair<Key, Value>(k, v));
95  }
96 
98  return copyFrom(a);
99  }
100 
101  Value& operator[](Key const& k) {
102  return self()->dictionary_[k];
103  }
104 
105  bool write(FILE* f, bool bin, unsigned int fg) const {
106  size_t sz = size();
107  if (bin) {
108  if (fwrite(&sz, sizeof(size_t), 1, f) < 1) return false;
109  }
110  else {
111  if (fprintf(f, "%lu\n", sz) < 1) return false;
112  }
113  for (std::map<Key, Value>::const_iterator
114  it = self->dictionary_.begin(); it != self->dictionary_.end(); ++it) {
115  if (it->first .write(f, bin, fg) == false) return false;
116  if (it->second.write(f, bin, fg) == false) return false;
117  }
118  return true;
119  }
120 
121  bool read(FILE* f, bool bin, unsigned int fg) {
122  size_t sz;
123  if (bin) {
124  if (fread(&sz, sizeof(size_t), 1, f) < 1) return false;
125  }
126  else {
127  if (fscanf(f, "%lu\n", &sz) < 0) return false;
128  }
129  for (size_t i = 0; i < sz; i++) {
130  Key k;
131  Value v;
132  if (k.read(f, bin, fg) == false) return false;
133  if (v.read(f, bin, fg) == false) return false;
134  insert(k, v);
135  }
136  return true;
137  }
138 
139  ObjBase* create() const {
140  return new ObjDictionary();
141  }
142 
143  ObjBase* copyFrom(ObjBase const* b) {
144  return &(copyFrom(*(ObjDictionary*)b));
145  }
146 
147  char const* ctype() const {
148  return typeid(*this).name();
149  }
150 
151  std::string type() const {
152  return std::string(ctype());
153  }
154 };
155 
156 }
157 
158 #endif // oo_ObjDictionary_H__