Commit f0c2e0ef1e2b10b19fea60d5e6580910a92092e1

Authored by Jay Berkenbilt
1 parent 9044a240

JSON: use std::shared_ptr internally

include/qpdf/JSON.hh
@@ -42,6 +42,7 @@ @@ -42,6 +42,7 @@
42 #include <vector> 42 #include <vector>
43 #include <list> 43 #include <list>
44 #include <functional> 44 #include <functional>
  45 +#include <memory>
45 46
46 class JSON 47 class JSON
47 { 48 {
@@ -156,13 +157,13 @@ class JSON @@ -156,13 +157,13 @@ class JSON
156 { 157 {
157 virtual ~JSON_dictionary(); 158 virtual ~JSON_dictionary();
158 virtual std::string unparse(size_t depth) const; 159 virtual std::string unparse(size_t depth) const;
159 - std::map<std::string, PointerHolder<JSON_value> > members; 160 + std::map<std::string, std::shared_ptr<JSON_value>> members;
160 }; 161 };
161 struct JSON_array: public JSON_value 162 struct JSON_array: public JSON_value
162 { 163 {
163 virtual ~JSON_array(); 164 virtual ~JSON_array();
164 virtual std::string unparse(size_t depth) const; 165 virtual std::string unparse(size_t depth) const;
165 - std::vector<PointerHolder<JSON_value> > elements; 166 + std::vector<std::shared_ptr<JSON_value>> elements;
166 }; 167 };
167 struct JSON_string: public JSON_value 168 struct JSON_string: public JSON_value
168 { 169 {
@@ -194,7 +195,7 @@ class JSON @@ -194,7 +195,7 @@ class JSON
194 virtual std::string unparse(size_t depth) const; 195 virtual std::string unparse(size_t depth) const;
195 }; 196 };
196 197
197 - JSON(PointerHolder<JSON_value>); 198 + JSON(std::shared_ptr<JSON_value>);
198 199
199 static bool 200 static bool
200 checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v, 201 checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
@@ -211,10 +212,10 @@ class JSON @@ -211,10 +212,10 @@ class JSON
211 ~Members(); 212 ~Members();
212 213
213 private: 214 private:
214 - Members(PointerHolder<JSON_value>); 215 + Members(std::shared_ptr<JSON_value>);
215 Members(Members const&); 216 Members(Members const&);
216 217
217 - PointerHolder<JSON_value> value; 218 + std::shared_ptr<JSON_value> value;
218 }; 219 };
219 220
220 PointerHolder<Members> m; 221 PointerHolder<Members> m;
libqpdf/JSON.cc
@@ -8,12 +8,12 @@ JSON::Members::~Members() @@ -8,12 +8,12 @@ JSON::Members::~Members()
8 { 8 {
9 } 9 }
10 10
11 -JSON::Members::Members(PointerHolder<JSON_value> value) : 11 +JSON::Members::Members(std::shared_ptr<JSON_value> value) :
12 value(value) 12 value(value)
13 { 13 {
14 } 14 }
15 15
16 -JSON::JSON(PointerHolder<JSON_value> value) : 16 +JSON::JSON(std::shared_ptr<JSON_value> value) :
17 m(new Members(value)) 17 m(new Members(value))
18 { 18 {
19 } 19 }
@@ -30,9 +30,7 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const @@ -30,9 +30,7 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const
30 { 30 {
31 std::string result = "{"; 31 std::string result = "{";
32 bool first = true; 32 bool first = true;
33 - for (std::map<std::string, PointerHolder<JSON_value> >::const_iterator  
34 - iter = members.begin();  
35 - iter != members.end(); ++iter) 33 + for (auto const& iter: members)
36 { 34 {
37 if (first) 35 if (first)
38 { 36 {
@@ -44,8 +42,8 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const @@ -44,8 +42,8 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const
44 } 42 }
45 result.append(1, '\n'); 43 result.append(1, '\n');
46 result.append(2 * (1 + depth), ' '); 44 result.append(2 * (1 + depth), ' ');
47 - result += ("\"" + (*iter).first + "\": " +  
48 - (*iter).second->unparse(1 + depth)); 45 + result += ("\"" + iter.first + "\": " +
  46 + iter.second->unparse(1 + depth));
49 } 47 }
50 if (! first) 48 if (! first)
51 { 49 {
@@ -64,9 +62,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const @@ -64,9 +62,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const
64 { 62 {
65 std::string result = "["; 63 std::string result = "[";
66 bool first = true; 64 bool first = true;
67 - for (std::vector<PointerHolder<JSON_value> >::const_iterator iter =  
68 - elements.begin();  
69 - iter != elements.end(); ++iter) 65 + for (auto const& element: elements)
70 { 66 {
71 if (first) 67 if (first)
72 { 68 {
@@ -78,7 +74,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const @@ -78,7 +74,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const
78 } 74 }
79 result.append(1, '\n'); 75 result.append(1, '\n');
80 result.append(2 * (1 + depth), ' '); 76 result.append(2 * (1 + depth), ' ');
81 - result += (*iter)->unparse(1 + depth); 77 + result += element->unparse(1 + depth);
82 } 78 }
83 if (! first) 79 if (! first)
84 { 80 {
@@ -212,7 +208,7 @@ JSON::encode_string(std::string const&amp; str) @@ -212,7 +208,7 @@ JSON::encode_string(std::string const&amp; str)
212 JSON 208 JSON
213 JSON::makeDictionary() 209 JSON::makeDictionary()
214 { 210 {
215 - return JSON(new JSON_dictionary()); 211 + return JSON(std::make_shared<JSON_dictionary>());
216 } 212 }
217 213
218 JSON 214 JSON
@@ -231,7 +227,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val) @@ -231,7 +227,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val)
231 } 227 }
232 else 228 else
233 { 229 {
234 - obj->members[encode_string(key)] = new JSON_null(); 230 + obj->members[encode_string(key)] = std::make_shared<JSON_null>();
235 } 231 }
236 return obj->members[encode_string(key)]; 232 return obj->members[encode_string(key)];
237 } 233 }
@@ -239,7 +235,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val) @@ -239,7 +235,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val)
239 JSON 235 JSON
240 JSON::makeArray() 236 JSON::makeArray()
241 { 237 {
242 - return JSON(new JSON_array()); 238 + return JSON(std::make_shared<JSON_array>());
243 } 239 }
244 240
245 JSON 241 JSON
@@ -257,7 +253,7 @@ JSON::addArrayElement(JSON const&amp; val) @@ -257,7 +253,7 @@ JSON::addArrayElement(JSON const&amp; val)
257 } 253 }
258 else 254 else
259 { 255 {
260 - arr->elements.push_back(new JSON_null()); 256 + arr->elements.push_back(std::make_shared<JSON_null>());
261 } 257 }
262 return arr->elements.back(); 258 return arr->elements.back();
263 } 259 }
@@ -265,37 +261,37 @@ JSON::addArrayElement(JSON const&amp; val) @@ -265,37 +261,37 @@ JSON::addArrayElement(JSON const&amp; val)
265 JSON 261 JSON
266 JSON::makeString(std::string const& utf8) 262 JSON::makeString(std::string const& utf8)
267 { 263 {
268 - return JSON(new JSON_string(utf8)); 264 + return JSON(std::make_shared<JSON_string>(utf8));
269 } 265 }
270 266
271 JSON 267 JSON
272 JSON::makeInt(long long int value) 268 JSON::makeInt(long long int value)
273 { 269 {
274 - return JSON(new JSON_number(value)); 270 + return JSON(std::make_shared<JSON_number>(value));
275 } 271 }
276 272
277 JSON 273 JSON
278 JSON::makeReal(double value) 274 JSON::makeReal(double value)
279 { 275 {
280 - return JSON(new JSON_number(value)); 276 + return JSON(std::make_shared<JSON_number>(value));
281 } 277 }
282 278
283 JSON 279 JSON
284 JSON::makeNumber(std::string const& encoded) 280 JSON::makeNumber(std::string const& encoded)
285 { 281 {
286 - return JSON(new JSON_number(encoded)); 282 + return JSON(std::make_shared<JSON_number>(encoded));
287 } 283 }
288 284
289 JSON 285 JSON
290 JSON::makeBool(bool value) 286 JSON::makeBool(bool value)
291 { 287 {
292 - return JSON(new JSON_bool(value)); 288 + return JSON(std::make_shared<JSON_bool>(value));
293 } 289 }
294 290
295 JSON 291 JSON
296 JSON::makeNull() 292 JSON::makeNull()
297 { 293 {
298 - return JSON(new JSON_null()); 294 + return JSON(std::make_shared<JSON_null>());
299 } 295 }
300 296
301 bool 297 bool
@@ -488,11 +484,9 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v, @@ -488,11 +484,9 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
488 } 484 }
489 } 485 }
490 } 486 }
491 - for (std::map<std::string, PointerHolder<JSON_value>>::iterator iter =  
492 - this_dict->members.begin();  
493 - iter != this_dict->members.end(); ++iter) 487 + for (auto const& iter: this_dict->members)
494 { 488 {
495 - std::string const& key = (*iter).first; 489 + std::string const& key = iter.first;
496 if (sch_dict->members.count(key) == 0) 490 if (sch_dict->members.count(key) == 0)
497 { 491 {
498 QTC::TC("libtests", "JSON key extra in object"); 492 QTC::TC("libtests", "JSON key extra in object");
@@ -518,14 +512,13 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v, @@ -518,14 +512,13 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
518 return false; 512 return false;
519 } 513 }
520 int i = 0; 514 int i = 0;
521 - for (std::vector<PointerHolder<JSON_value> >::iterator iter =  
522 - this_arr->elements.begin();  
523 - iter != this_arr->elements.end(); ++iter, ++i) 515 + for (auto const& element: this_arr->elements)
524 { 516 {
525 checkSchemaInternal( 517 checkSchemaInternal(
526 - (*iter).get(), 518 + element.get(),
527 sch_arr->elements.at(0).get(), 519 sch_arr->elements.at(0).get(),
528 flags, errors, prefix + "." + QUtil::int_to_string(i)); 520 flags, errors, prefix + "." + QUtil::int_to_string(i));
  521 + ++i;
529 } 522 }
530 } 523 }
531 else if (! sch_str) 524 else if (! sch_str)
@@ -559,7 +552,7 @@ namespace { @@ -559,7 +552,7 @@ namespace {
559 { 552 {
560 } 553 }
561 554
562 - PointerHolder<JSON> parse(std::string const& s); 555 + std::shared_ptr<JSON> parse(std::string const& s);
563 556
564 private: 557 private:
565 void getToken(); 558 void getToken();
@@ -599,7 +592,7 @@ namespace { @@ -599,7 +592,7 @@ namespace {
599 char const* tok_end; 592 char const* tok_end;
600 char const* p; 593 char const* p;
601 parser_state_e parser_state; 594 parser_state_e parser_state;
602 - std::vector<PointerHolder<JSON>> stack; 595 + std::vector<std::shared_ptr<JSON>> stack;
603 std::vector<parser_state_e> ps_stack; 596 std::vector<parser_state_e> ps_stack;
604 std::string dict_key; 597 std::string dict_key;
605 }; 598 };
@@ -987,7 +980,7 @@ JSONParser::handleToken() @@ -987,7 +980,7 @@ JSONParser::handleToken()
987 // looking at an item or a delimiter. It will always be exactly 980 // looking at an item or a delimiter. It will always be exactly
988 // one of those two or an error condition. 981 // one of those two or an error condition.
989 982
990 - PointerHolder<JSON> item; 983 + std::shared_ptr<JSON> item;
991 char delimiter = '\0'; 984 char delimiter = '\0';
992 switch (lex_state) 985 switch (lex_state)
993 { 986 {
@@ -995,11 +988,11 @@ JSONParser::handleToken() @@ -995,11 +988,11 @@ JSONParser::handleToken()
995 switch (*tok_start) 988 switch (*tok_start)
996 { 989 {
997 case '{': 990 case '{':
998 - item = new JSON(JSON::makeDictionary()); 991 + item = std::make_shared<JSON>(JSON::makeDictionary());
999 break; 992 break;
1000 993
1001 case '[': 994 case '[':
1002 - item = new JSON(JSON::makeArray()); 995 + item = std::make_shared<JSON>(JSON::makeArray());
1003 break; 996 break;
1004 997
1005 default: 998 default:
@@ -1032,21 +1025,21 @@ JSONParser::handleToken() @@ -1032,21 +1025,21 @@ JSONParser::handleToken()
1032 "JSON: offset " + QUtil::int_to_string(p - cstr) + 1025 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1033 ": number with no digits"); 1026 ": number with no digits");
1034 } 1027 }
1035 - item = new JSON(JSON::makeNumber(value)); 1028 + item = std::make_shared<JSON>(JSON::makeNumber(value));
1036 break; 1029 break;
1037 1030
1038 case ls_alpha: 1031 case ls_alpha:
1039 if (value == "true") 1032 if (value == "true")
1040 { 1033 {
1041 - item = new JSON(JSON::makeBool(true)); 1034 + item = std::make_shared<JSON>(JSON::makeBool(true));
1042 } 1035 }
1043 else if (value == "false") 1036 else if (value == "false")
1044 { 1037 {
1045 - item = new JSON(JSON::makeBool(false)); 1038 + item = std::make_shared<JSON>(JSON::makeBool(false));
1046 } 1039 }
1047 else if (value == "null") 1040 else if (value == "null")
1048 { 1041 {
1049 - item = new JSON(JSON::makeNull()); 1042 + item = std::make_shared<JSON>(JSON::makeNull());
1050 } 1043 }
1051 else 1044 else
1052 { 1045 {
@@ -1058,7 +1051,7 @@ JSONParser::handleToken() @@ -1058,7 +1051,7 @@ JSONParser::handleToken()
1058 break; 1051 break;
1059 1052
1060 case ls_string: 1053 case ls_string:
1061 - item = new JSON(JSON::makeString(s_value)); 1054 + item = std::make_shared<JSON>(JSON::makeString(s_value));
1062 break; 1055 break;
1063 1056
1064 case ls_backslash: 1057 case ls_backslash:
@@ -1215,7 +1208,7 @@ JSONParser::handleToken() @@ -1215,7 +1208,7 @@ JSONParser::handleToken()
1215 } 1208 }
1216 else if (item.get()) 1209 else if (item.get())
1217 { 1210 {
1218 - PointerHolder<JSON> tos; 1211 + std::shared_ptr<JSON> tos;
1219 if (! stack.empty()) 1212 if (! stack.empty())
1220 { 1213 {
1221 tos = stack.back(); 1214 tos = stack.back();
@@ -1284,7 +1277,7 @@ JSONParser::handleToken() @@ -1284,7 +1277,7 @@ JSONParser::handleToken()
1284 lex_state = ls_top; 1277 lex_state = ls_top;
1285 } 1278 }
1286 1279
1287 -PointerHolder<JSON> 1280 +std::shared_ptr<JSON>
1288 JSONParser::parse(std::string const& s) 1281 JSONParser::parse(std::string const& s)
1289 { 1282 {
1290 cstr = s.c_str(); 1283 cstr = s.c_str();