1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#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& copyFrom(Myself const& b) {
dictionary_ = b.dictionary_;
return *this;
}
};
Self<Myself> const self;
public:
ObjDictionary(): self(true) {
}
ObjDictionary(ObjDictionary const& d): self(false) {
self.copyFrom(b.self);
}
ObjDictionary(std::map<Key, Value> const& d): self(true) {
self()->dictionary_ = 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 end() const {
return self->dictionary_.end();
}
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);
}
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 = self->dictionary_.begin(); it != self->dictionary_.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*)b));
}
char const* ctype() const {
return typeid(*this).name();
}
std::string type() const {
return std::string(ctype());
}
};
}
#endif // oo_ObjDictionary_H__
|