-
Notifications
You must be signed in to change notification settings - Fork 127
/
extend.h
188 lines (160 loc) · 5.54 KB
/
extend.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __X_PACK_EXTEND_H
#define __X_PACK_EXTEND_H
#include <cstddef>
#include <map>
#include <set>
#include "config.h"
#include "util.h"
namespace xpack {
// user flag
#define X_PACK_FLAG_0 0
#define X_PACK_FLAG_OE (1<<0) // omitempty, in encode
#define X_PACK_FLAG_M (1<<1) // mandatory, in decode
#define X_PACK_FLAG_EN (1<<2) // empty as null, in json encode
#define X_PACK_FLAG_SL (1<<3) // encode as one line, currently only supports json vector
#define X_PACK_FLAG_ATTR (1<<15) // for xml encode, encode in attribute
// for xml encode/decode: <label attr1="a" attr2="b">content</label>
// CONTENT flag for decode and encode content
// A structure can only have one CONTENT flag. If multiple ones are defined, the behavior is undefined.
#define X_PACK_FLAG_XML_CONTENT (1<<16)
// control flag
#define X_PACK_CTRL_FLAG_INHERIT (1<<0)
// Alias name. [def ][type:name[,flag,key@value,flag]] def not support flag
struct Alias {
const char *raw; // raw name
const char *alias; // alias define
struct Type {
std::string name;
std::set<std::string> flags;
std::map<std::string, std::string> kv_flags;
};
Alias(const char *_raw, const char *_alias):raw(_raw), alias(_alias) {
std::vector<std::string> tps;
Util::split(tps, alias, ' ');
for (size_t i=0; i<tps.size(); ++i) {
std::vector<std::string> typeFlag;
Util::split(typeFlag, tps[i], ':', 1);
if (typeFlag.size() == 1) { // no ':', default name
def = tps[i];
} else { // type:name[,flags]
Type tp;
std::vector<std::string> nameFlags;
Util::split(nameFlags, typeFlag[1], ',');
tp.name = nameFlags[0];
if (nameFlags.size() > 1) {
for (size_t j=1; j<nameFlags.size(); ++j) {
std::vector<std::string> kvFlag;
Util::split(kvFlag, nameFlags[j], '@', 1);
if (kvFlag.size() == 1) {
tp.flags.insert(nameFlags[j]);
} else {
tp.kv_flags[kvFlag[0]] = kvFlag[1];
}
}
}
types[typeFlag[0]] = tp;
}
}
}
const char *Name(const char *type) const {
std::map<std::string, Type>::const_iterator iter = types.find(type);
if (iter == types.end()) {
if (def.empty()) {
return raw;
} else {
return def.c_str();
}
} else {
return iter->second.name.c_str();
}
}
bool Flag(const std::string&type, const std::string& flag, std::string *value=NULL) const {
std::map<std::string, Type>::const_iterator it1 = types.find(type);
if (it1 != types.end()) {
if (it1->second.flags.find(flag) != it1->second.flags.end()) {
return true;
}
std::map<std::string, std::string>::const_iterator it2 = it1->second.kv_flags.find(flag);
if (it2 != it1->second.kv_flags.end()) {
if (NULL != value) {
*value = it2->second;
}
return true;
}
}
return false;
}
private:
std::string def; // default name
std::map<std::string, Type> types;
};
struct Extend {
int flag;
int ctrl_flag;
const Alias *alias;
Extend(int _flag, const Alias *_alias):flag(_flag), ctrl_flag(0), alias(_alias) {
}
Extend(const Extend *ext) {
if (NULL != ext) {
flag = ext->flag;
ctrl_flag = ext->ctrl_flag;
alias = ext->alias;
} else {
flag = 0;
ctrl_flag = 0;
alias = NULL;
}
}
static int Flag(const Extend *ext) {
if (NULL == ext) {
return 0;
} else {
return ext->flag;
}
}
static int CtrlFlag(const Extend *ext) {
if (NULL == ext) {
return 0;
} else {
return ext->ctrl_flag;
}
}
static bool AliasFlag(const Extend *ext, const std::string&type, const std::string& flag, std::string *value=NULL) {
if (NULL==ext || NULL==ext->alias) {
return false;
}
return ext->alias->Flag(type, flag, value);
}
static bool OmitEmpty(const Extend *ext) {
return NULL!=ext && (ext->flag&X_PACK_FLAG_OE);
}
static bool EmptyNull(const Extend *ext) {
return NULL!=ext && (ext->flag&X_PACK_FLAG_EN);
}
static bool Mandatory(const Extend *ext) {
return NULL!=ext && (ext->flag&X_PACK_FLAG_M);
}
static bool Attribute(const Extend *ext) {
return NULL!=ext && (ext->flag&X_PACK_FLAG_ATTR);
}
static bool XmlContent(const Extend *ext) {
return NULL!=ext && (ext->flag&X_PACK_FLAG_XML_CONTENT);
}
};
}
#endif