-
Notifications
You must be signed in to change notification settings - Fork 0
/
uccontract.cpp
302 lines (265 loc) · 10.4 KB
/
uccontract.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010,2012.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to jason.denton@gmail.com
* http://www.greatpanic.com/code.html
*/
#include "univcont.h"
#include <cstring>
#include <string>
#include "uccontract.h"
namespace JAD {
ContractMap UCContract::__typelibrary;
unsigned UCContract::compare_map(const UniversalContainer& uc) const
{
if (data_type != uc_Map || uc.get_type() != uc_Map) return 0;
unsigned result = 0;
unsigned req_count = 0;
UniversalMap::iterator it = map_begin(uc);
UniversalMap::iterator eit = map_end(uc);
ContractMap* required = constraints.map_constraints.require_map;
ContractMap* optional = constraints.map_constraints.optional_map;
for (; it != eit; it++) {
if (it->first[0] == '#') continue; //skip # keys
if (required && required->count(it->first) > 0) {
req_count++;
result |= required->operator[](it->first)->compare(it->second);
}
else if (optional && optional->count(it->first) > 0) {
result |= optional->operator[](it->first)->compare(it->second);
}
else {
result |= ucc_EXTRA_MAP_ELEMENT;
}
}
if (req_count != required->size()) result |= ucc_MISSING_REQUIRED_MAP_ELEMENT;
return result;
}
unsigned UCContract::compare_array(const UniversalContainer& uc) const
{
if (data_type != uc_Array || uc.get_type() != uc_Array) return 0;
unsigned result = 0;
UniversalContainer tmp = (long int) uc.size();
UniversalArray::iterator it;
UniversalArray::iterator eit = vector_end(uc);
if (constraints.array_constraints.size)
result |= constraints.array_constraints.size->compare(tmp);
if (constraints.array_constraints.forall) {
it = vector_begin(uc);
for (;it < eit; it++)
result |= constraints.array_constraints.forall->compare(*it);
}
if (constraints.array_constraints.exists) {
ContractVector::iterator exists_list_it = constraints.array_constraints.exists->begin();
ContractVector::iterator exists_list_eit = constraints.array_constraints.exists->end();
for (; exists_list_it < exists_list_eit; exists_list_it++) {
it = vector_begin(uc);
bool found = false;
for (;it < eit && !found; it++)
found = (*exists_list_it)->compare(*it) == 0;
if (!found)
result |= ucc_MISSING_REQUIRED_ARRAY_ELEMENT;
}
}
return result;
}
unsigned UCContract::compare(const UniversalContainer& uc) const
{
int tmp_int;
double tmp_dbl;
if (data_type == uc_Unknown) {
if (!constraints.user_type) throw ucexception(uce_BadContract);
return __typelibrary[*(constraints.user_type)]->compare(uc);
}
if (data_type != uc.get_type()) return ucc_IMPROPER_TYPE;
switch(data_type) {
case uc_Integer :
case uc_Character :
tmp_int = uc;
if ((constraints.int_pair.has_lower && tmp_int < constraints.int_pair.low) ||
(constraints.int_pair.has_upper && tmp_int > constraints.int_pair.high))
return ucc_CONSTRAINT_VIOLATION;
break;
case uc_Real :
tmp_dbl = uc;
if ((constraints.real_pair.has_lower && tmp_dbl < constraints.real_pair.low) ||
(constraints.real_pair.has_upper && tmp_dbl > constraints.real_pair.high))
return ucc_CONSTRAINT_VIOLATION;
break;
case uc_String :
if (constraints.regex && regexec(constraints.regex,uc.c_str(),0,NULL,0))
return ucc_STRING_DOES_NOT_MATCH;
break;
case uc_Map :
return compare_map(uc);
break;
case uc_Array :
return compare_array(uc);
break;
}
return 0;
}
UCContract::UCContract(UniversalContainer& uc)
{
if (uc.get_type() != uc_Map) throw ucdataexception(uce_BadContract,uc);
if (!uc.exists("type")) throw ucdataexception(uce_BadContract,uc);
memset(this,0,sizeof(UCContract));
std::string* type = uc["type"];
if (!type->compare("string")) {
data_type = uc_String;
if (uc.exists("regex")) {
constraints.regex = new regex_t;
regcomp(constraints.regex,uc["regex"].c_str(),REG_EXTENDED | REG_NOSUB);
}
} //end string
else if (!type->compare("integer")) {
data_type = uc_Integer;
if (uc.exists("lower_bound")) {
constraints.int_pair.low = uc["lower_bound"];
constraints.int_pair.has_lower = true;
}
if (uc.exists("upper_bound")) {
constraints.int_pair.high = uc["upper_bound"];
constraints.int_pair.has_upper = true;
}
} //end int
else if (!type->compare("real")) {
data_type = uc_Real;
if (uc.exists("lower_bound")) {
constraints.real_pair.low = uc["lower_bound"];
constraints.real_pair.has_lower = true;
}
if (uc.exists("upper_bound")) {
constraints.real_pair.high = uc["upper_bound"];
constraints.real_pair.has_upper = true;
}
} //end real
else if (!type->compare("map")) {
data_type = uc_Map;
if (uc.exists("required_members")) {
constraints.map_constraints.require_map = new ContractMap;
UniversalMap::iterator b = map_begin(uc["required_members"]);
UniversalMap::iterator e = map_end(uc["required_members"]);
for (;b != e; b++)
(*(constraints.map_constraints.require_map))[b->first] = new UCContract(b->second);
}
if (uc.exists("optional_members")) {
constraints.map_constraints.optional_map = new ContractMap;
UniversalMap::iterator b = map_begin(uc["optional_members"]);
UniversalMap::iterator e = map_end(uc["optional_members"]);
for (;b != e; b++)
(*(constraints.map_constraints.optional_map))[b->first] = new UCContract(b->second);
}
} //end map
else if (!type->compare("array")) {
data_type = uc_Array;
if (uc.exists("forall")) {
constraints.array_constraints.forall = new UCContract(uc["forall"]);
}
if (uc.exists("size")) {
if (!constraints.array_constraints.size)
constraints.array_constraints.size = new UCContract(uc["size"]);
}
if (uc.exists("exists")) {
constraints.array_constraints.exists = new ContractVector;
UniversalArray::iterator b = vector_begin(uc["exists"]);
UniversalArray::iterator e = vector_end(uc["exists"]);
for(;b != e; b++)
constraints.array_constraints.exists->push_back(new UCContract(*b));
}
}// end array
else if (!type->compare("character")) {
data_type = uc_Character;
if (uc.exists("lower_bound")) {
constraints.int_pair.low = uc["lower_bound"];
constraints.int_pair.has_lower = true;
}
if (uc.exists("upper_bound")) {
constraints.int_pair.low = uc["upper_bound"];
constraints.int_pair.has_upper = true;
}
} //end character
else if (!type->compare("boolean")) {
data_type = uc_Boolean;
} //end boolean
else if (__typelibrary.count(*type) > 0) {
data_type = uc_Unknown;
constraints.user_type = new string(*type);
}
else {
throw ucexception(uce_BadContract);
}
}
UCContract::~UCContract(void)
{
switch(data_type) {
case(uc_String):
if (constraints.regex) regfree(constraints.regex);
break;
case(uc_Map):
if (constraints.map_constraints.require_map) delete constraints.map_constraints.require_map;
if (constraints.map_constraints.optional_map) delete constraints.map_constraints.optional_map;
break;
case(uc_Array):
if (constraints.array_constraints.size) delete constraints.array_constraints.size;
if (constraints.array_constraints.forall) delete constraints.array_constraints.forall;
if (constraints.array_constraints.exists) delete constraints.array_constraints.exists;
}
}
static const char* ucc_IMPROPER_TYPE_str = "Element has wrong type.";
static const char* ucc_CONSTRAINT_VIOLATION_str = "A constraint was violated.";
static const char* ucc_EXTRA_MAP_ELEMENT_str = "An map element not specified in the contract is present.";
static const char* ucc_MISSING_REQUIRED_MAP_ELEMENT_str = "A required map element is missing.";
static const char* ucc_MISSING_REQUIRED_ARRAY_ELEMENT_str = "An array element that must exist is not present.";
static const char* ucc_STRING_DOES_NOT_MATCH_str = "A string does not match its given regex expression.";
std::vector<const char*> UCContract::error_messages(unsigned result)
{
std::vector<const char*> mesg;
if (result & ucc_IMPROPER_TYPE) mesg.push_back(ucc_IMPROPER_TYPE_str);
if (result & ucc_CONSTRAINT_VIOLATION) mesg.push_back(ucc_CONSTRAINT_VIOLATION_str);
if (result & ucc_EXTRA_MAP_ELEMENT) mesg.push_back(ucc_EXTRA_MAP_ELEMENT_str);
if (result & ucc_MISSING_REQUIRED_MAP_ELEMENT)
mesg.push_back(ucc_MISSING_REQUIRED_MAP_ELEMENT_str);
if (result & ucc_MISSING_REQUIRED_ARRAY_ELEMENT)
mesg.push_back(ucc_MISSING_REQUIRED_ARRAY_ELEMENT_str);
if (result & ucc_STRING_DOES_NOT_MATCH)
mesg.push_back(ucc_STRING_DOES_NOT_MATCH_str);
return mesg;
}
void UCContract::compare_and_throw(UniversalContainer& uc, unsigned mask) const
{
unsigned result = compare(uc) & mask;
if (result) {
std::vector<const char*> msgs = error_messages(result);
UniversalContainer uce = ucexception(uce_ContractViolation);
for (size_t i = 0; i < msgs.size(); i++)
uce["violations"][i] = msgs[i];
uce["compare_result"] = int(result);
throw uce;
}
}
void UCContract::add_to_library(std::string const& name, UniversalContainer& uc)
{
if (__typelibrary.count(name) > 0) {
delete __typelibrary[name];
}
__typelibrary[name]= new UCContract(uc);
}
unsigned UCContract::compare(std::string const& type, const UniversalContainer& uc)
{
if (__typelibrary.count(type) < 1) return ucc_IMPROPER_TYPE;
return __typelibrary[type]->compare(uc);
}
void UCContract::compare_and_throw(std::string const& type, UniversalContainer& uc, unsigned mask)
{
if (__typelibrary.count(type) < 1) {
UniversalContainer uce = ucexception(uce_ContractViolation);
uce["violations"][0] = ucc_IMPROPER_TYPE_str;
uce["compare_result"] = int(ucc_IMPROPER_TYPE);
throw uce;
}
__typelibrary[type]->compare_and_throw(uc,mask);
}
} //end namespace