Commit f0c2e0ef1e2b10b19fea60d5e6580910a92092e1
1 parent
9044a240
JSON: use std::shared_ptr internally
Showing
2 changed files
with
40 additions
and
46 deletions
include/qpdf/JSON.hh
| ... | ... | @@ -42,6 +42,7 @@ |
| 42 | 42 | #include <vector> |
| 43 | 43 | #include <list> |
| 44 | 44 | #include <functional> |
| 45 | +#include <memory> | |
| 45 | 46 | |
| 46 | 47 | class JSON |
| 47 | 48 | { |
| ... | ... | @@ -156,13 +157,13 @@ class JSON |
| 156 | 157 | { |
| 157 | 158 | virtual ~JSON_dictionary(); |
| 158 | 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 | 162 | struct JSON_array: public JSON_value |
| 162 | 163 | { |
| 163 | 164 | virtual ~JSON_array(); |
| 164 | 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 | 168 | struct JSON_string: public JSON_value |
| 168 | 169 | { |
| ... | ... | @@ -194,7 +195,7 @@ class JSON |
| 194 | 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 | 200 | static bool |
| 200 | 201 | checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v, |
| ... | ... | @@ -211,10 +212,10 @@ class JSON |
| 211 | 212 | ~Members(); |
| 212 | 213 | |
| 213 | 214 | private: |
| 214 | - Members(PointerHolder<JSON_value>); | |
| 215 | + Members(std::shared_ptr<JSON_value>); | |
| 215 | 216 | Members(Members const&); |
| 216 | 217 | |
| 217 | - PointerHolder<JSON_value> value; | |
| 218 | + std::shared_ptr<JSON_value> value; | |
| 218 | 219 | }; |
| 219 | 220 | |
| 220 | 221 | PointerHolder<Members> m; | ... | ... |
libqpdf/JSON.cc
| ... | ... | @@ -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 | 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 | 17 | m(new Members(value)) |
| 18 | 18 | { |
| 19 | 19 | } |
| ... | ... | @@ -30,9 +30,7 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const |
| 30 | 30 | { |
| 31 | 31 | std::string result = "{"; |
| 32 | 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 | 35 | if (first) |
| 38 | 36 | { |
| ... | ... | @@ -44,8 +42,8 @@ std::string JSON::JSON_dictionary::unparse(size_t depth) const |
| 44 | 42 | } |
| 45 | 43 | result.append(1, '\n'); |
| 46 | 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 | 48 | if (! first) |
| 51 | 49 | { |
| ... | ... | @@ -64,9 +62,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const |
| 64 | 62 | { |
| 65 | 63 | std::string result = "["; |
| 66 | 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 | 67 | if (first) |
| 72 | 68 | { |
| ... | ... | @@ -78,7 +74,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const |
| 78 | 74 | } |
| 79 | 75 | result.append(1, '\n'); |
| 80 | 76 | result.append(2 * (1 + depth), ' '); |
| 81 | - result += (*iter)->unparse(1 + depth); | |
| 77 | + result += element->unparse(1 + depth); | |
| 82 | 78 | } |
| 83 | 79 | if (! first) |
| 84 | 80 | { |
| ... | ... | @@ -212,7 +208,7 @@ JSON::encode_string(std::string const& str) |
| 212 | 208 | JSON |
| 213 | 209 | JSON::makeDictionary() |
| 214 | 210 | { |
| 215 | - return JSON(new JSON_dictionary()); | |
| 211 | + return JSON(std::make_shared<JSON_dictionary>()); | |
| 216 | 212 | } |
| 217 | 213 | |
| 218 | 214 | JSON |
| ... | ... | @@ -231,7 +227,7 @@ JSON::addDictionaryMember(std::string const& key, JSON const& val) |
| 231 | 227 | } |
| 232 | 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 | 232 | return obj->members[encode_string(key)]; |
| 237 | 233 | } |
| ... | ... | @@ -239,7 +235,7 @@ JSON::addDictionaryMember(std::string const& key, JSON const& val) |
| 239 | 235 | JSON |
| 240 | 236 | JSON::makeArray() |
| 241 | 237 | { |
| 242 | - return JSON(new JSON_array()); | |
| 238 | + return JSON(std::make_shared<JSON_array>()); | |
| 243 | 239 | } |
| 244 | 240 | |
| 245 | 241 | JSON |
| ... | ... | @@ -257,7 +253,7 @@ JSON::addArrayElement(JSON const& val) |
| 257 | 253 | } |
| 258 | 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 | 258 | return arr->elements.back(); |
| 263 | 259 | } |
| ... | ... | @@ -265,37 +261,37 @@ JSON::addArrayElement(JSON const& val) |
| 265 | 261 | JSON |
| 266 | 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 | 267 | JSON |
| 272 | 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 | 273 | JSON |
| 278 | 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 | 279 | JSON |
| 284 | 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 | 285 | JSON |
| 290 | 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 | 291 | JSON |
| 296 | 292 | JSON::makeNull() |
| 297 | 293 | { |
| 298 | - return JSON(new JSON_null()); | |
| 294 | + return JSON(std::make_shared<JSON_null>()); | |
| 299 | 295 | } |
| 300 | 296 | |
| 301 | 297 | bool |
| ... | ... | @@ -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 | 490 | if (sch_dict->members.count(key) == 0) |
| 497 | 491 | { |
| 498 | 492 | QTC::TC("libtests", "JSON key extra in object"); |
| ... | ... | @@ -518,14 +512,13 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v, |
| 518 | 512 | return false; |
| 519 | 513 | } |
| 520 | 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 | 517 | checkSchemaInternal( |
| 526 | - (*iter).get(), | |
| 518 | + element.get(), | |
| 527 | 519 | sch_arr->elements.at(0).get(), |
| 528 | 520 | flags, errors, prefix + "." + QUtil::int_to_string(i)); |
| 521 | + ++i; | |
| 529 | 522 | } |
| 530 | 523 | } |
| 531 | 524 | else if (! sch_str) |
| ... | ... | @@ -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 | 557 | private: |
| 565 | 558 | void getToken(); |
| ... | ... | @@ -599,7 +592,7 @@ namespace { |
| 599 | 592 | char const* tok_end; |
| 600 | 593 | char const* p; |
| 601 | 594 | parser_state_e parser_state; |
| 602 | - std::vector<PointerHolder<JSON>> stack; | |
| 595 | + std::vector<std::shared_ptr<JSON>> stack; | |
| 603 | 596 | std::vector<parser_state_e> ps_stack; |
| 604 | 597 | std::string dict_key; |
| 605 | 598 | }; |
| ... | ... | @@ -987,7 +980,7 @@ JSONParser::handleToken() |
| 987 | 980 | // looking at an item or a delimiter. It will always be exactly |
| 988 | 981 | // one of those two or an error condition. |
| 989 | 982 | |
| 990 | - PointerHolder<JSON> item; | |
| 983 | + std::shared_ptr<JSON> item; | |
| 991 | 984 | char delimiter = '\0'; |
| 992 | 985 | switch (lex_state) |
| 993 | 986 | { |
| ... | ... | @@ -995,11 +988,11 @@ JSONParser::handleToken() |
| 995 | 988 | switch (*tok_start) |
| 996 | 989 | { |
| 997 | 990 | case '{': |
| 998 | - item = new JSON(JSON::makeDictionary()); | |
| 991 | + item = std::make_shared<JSON>(JSON::makeDictionary()); | |
| 999 | 992 | break; |
| 1000 | 993 | |
| 1001 | 994 | case '[': |
| 1002 | - item = new JSON(JSON::makeArray()); | |
| 995 | + item = std::make_shared<JSON>(JSON::makeArray()); | |
| 1003 | 996 | break; |
| 1004 | 997 | |
| 1005 | 998 | default: |
| ... | ... | @@ -1032,21 +1025,21 @@ JSONParser::handleToken() |
| 1032 | 1025 | "JSON: offset " + QUtil::int_to_string(p - cstr) + |
| 1033 | 1026 | ": number with no digits"); |
| 1034 | 1027 | } |
| 1035 | - item = new JSON(JSON::makeNumber(value)); | |
| 1028 | + item = std::make_shared<JSON>(JSON::makeNumber(value)); | |
| 1036 | 1029 | break; |
| 1037 | 1030 | |
| 1038 | 1031 | case ls_alpha: |
| 1039 | 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 | 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 | 1040 | else if (value == "null") |
| 1048 | 1041 | { |
| 1049 | - item = new JSON(JSON::makeNull()); | |
| 1042 | + item = std::make_shared<JSON>(JSON::makeNull()); | |
| 1050 | 1043 | } |
| 1051 | 1044 | else |
| 1052 | 1045 | { |
| ... | ... | @@ -1058,7 +1051,7 @@ JSONParser::handleToken() |
| 1058 | 1051 | break; |
| 1059 | 1052 | |
| 1060 | 1053 | case ls_string: |
| 1061 | - item = new JSON(JSON::makeString(s_value)); | |
| 1054 | + item = std::make_shared<JSON>(JSON::makeString(s_value)); | |
| 1062 | 1055 | break; |
| 1063 | 1056 | |
| 1064 | 1057 | case ls_backslash: |
| ... | ... | @@ -1215,7 +1208,7 @@ JSONParser::handleToken() |
| 1215 | 1208 | } |
| 1216 | 1209 | else if (item.get()) |
| 1217 | 1210 | { |
| 1218 | - PointerHolder<JSON> tos; | |
| 1211 | + std::shared_ptr<JSON> tos; | |
| 1219 | 1212 | if (! stack.empty()) |
| 1220 | 1213 | { |
| 1221 | 1214 | tos = stack.back(); |
| ... | ... | @@ -1284,7 +1277,7 @@ JSONParser::handleToken() |
| 1284 | 1277 | lex_state = ls_top; |
| 1285 | 1278 | } |
| 1286 | 1279 | |
| 1287 | -PointerHolder<JSON> | |
| 1280 | +std::shared_ptr<JSON> | |
| 1288 | 1281 | JSONParser::parse(std::string const& s) |
| 1289 | 1282 | { |
| 1290 | 1283 | cstr = s.c_str(); | ... | ... |