Commit 7ae1e80fd6626ce07262656a7d822e68004754ae

Authored by m-holger
1 parent a4f3dddb

Change JSON::Members::value to std::unique_ptr

include/qpdf/JSON.hh
@@ -425,7 +425,7 @@ class JSON @@ -425,7 +425,7 @@ class JSON
425 std::function<void(Pipeline*)> fn; 425 std::function<void(Pipeline*)> fn;
426 }; 426 };
427 427
428 - JSON(std::shared_ptr<JSON_value>); 428 + JSON(std::unique_ptr<JSON_value>);
429 429
430 static bool checkSchemaInternal( 430 static bool checkSchemaInternal(
431 JSON_value* this_v, 431 JSON_value* this_v,
@@ -443,13 +443,13 @@ class JSON @@ -443,13 +443,13 @@ class JSON
443 ~Members() = default; 443 ~Members() = default;
444 444
445 private: 445 private:
446 - Members(std::shared_ptr<JSON_value>); 446 + Members(std::unique_ptr<JSON_value>);
447 Members(Members const&) = delete; 447 Members(Members const&) = delete;
448 448
449 - std::shared_ptr<JSON_value> value; 449 + std::unique_ptr<JSON_value> value;
450 // start and end are only populated for objects created by parse 450 // start and end are only populated for objects created by parse
451 - qpdf_offset_t start;  
452 - qpdf_offset_t end; 451 + qpdf_offset_t start{0};
  452 + qpdf_offset_t end{0};
453 }; 453 };
454 454
455 std::shared_ptr<Members> m; 455 std::shared_ptr<Members> m;
libqpdf/JSON.cc
@@ -9,15 +9,13 @@ @@ -9,15 +9,13 @@
9 #include <cstring> 9 #include <cstring>
10 #include <stdexcept> 10 #include <stdexcept>
11 11
12 -JSON::Members::Members(std::shared_ptr<JSON_value> value) :  
13 - value(value),  
14 - start(0),  
15 - end(0) 12 +JSON::Members::Members(std::unique_ptr<JSON_value> value) :
  13 + value(std::move(value))
16 { 14 {
17 } 15 }
18 16
19 -JSON::JSON(std::shared_ptr<JSON_value> value) :  
20 - m(new Members(value)) 17 +JSON::JSON(std::unique_ptr<JSON_value> value) :
  18 + m(new Members(std::move(value)))
21 { 19 {
22 } 20 }
23 21
@@ -278,7 +276,7 @@ JSON::encode_string(std::string const&amp; str) @@ -278,7 +276,7 @@ JSON::encode_string(std::string const&amp; str)
278 JSON 276 JSON
279 JSON::makeDictionary() 277 JSON::makeDictionary()
280 { 278 {
281 - return JSON(std::make_shared<JSON_dictionary>()); 279 + return JSON(std::make_unique<JSON_dictionary>());
282 } 280 }
283 281
284 JSON 282 JSON
@@ -286,7 +284,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val) @@ -286,7 +284,7 @@ JSON::addDictionaryMember(std::string const&amp; key, JSON const&amp; val)
286 { 284 {
287 if (auto* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get())) { 285 if (auto* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get())) {
288 return obj->members[encode_string(key)] = 286 return obj->members[encode_string(key)] =
289 - val.m->value ? val.m->value : std::make_shared<JSON_null>(); 287 + val.m->value ? val : makeNull();
290 } else { 288 } else {
291 throw std::runtime_error( 289 throw std::runtime_error(
292 "JSON::addDictionaryMember called on non-dictionary"); 290 "JSON::addDictionaryMember called on non-dictionary");
@@ -311,7 +309,7 @@ JSON::checkDictionaryKeySeen(std::string const&amp; key) @@ -311,7 +309,7 @@ JSON::checkDictionaryKeySeen(std::string const&amp; key)
311 JSON 309 JSON
312 JSON::makeArray() 310 JSON::makeArray()
313 { 311 {
314 - return JSON(std::make_shared<JSON_array>()); 312 + return JSON(std::make_unique<JSON_array>());
315 } 313 }
316 314
317 JSON 315 JSON
@@ -332,43 +330,43 @@ JSON::addArrayElement(JSON const&amp; val) @@ -332,43 +330,43 @@ JSON::addArrayElement(JSON const&amp; val)
332 JSON 330 JSON
333 JSON::makeString(std::string const& utf8) 331 JSON::makeString(std::string const& utf8)
334 { 332 {
335 - return JSON(std::make_shared<JSON_string>(utf8)); 333 + return JSON(std::make_unique<JSON_string>(utf8));
336 } 334 }
337 335
338 JSON 336 JSON
339 JSON::makeInt(long long int value) 337 JSON::makeInt(long long int value)
340 { 338 {
341 - return JSON(std::make_shared<JSON_number>(value)); 339 + return JSON(std::make_unique<JSON_number>(value));
342 } 340 }
343 341
344 JSON 342 JSON
345 JSON::makeReal(double value) 343 JSON::makeReal(double value)
346 { 344 {
347 - return JSON(std::make_shared<JSON_number>(value)); 345 + return JSON(std::make_unique<JSON_number>(value));
348 } 346 }
349 347
350 JSON 348 JSON
351 JSON::makeNumber(std::string const& encoded) 349 JSON::makeNumber(std::string const& encoded)
352 { 350 {
353 - return JSON(std::make_shared<JSON_number>(encoded)); 351 + return JSON(std::make_unique<JSON_number>(encoded));
354 } 352 }
355 353
356 JSON 354 JSON
357 JSON::makeBool(bool value) 355 JSON::makeBool(bool value)
358 { 356 {
359 - return JSON(std::make_shared<JSON_bool>(value)); 357 + return JSON(std::make_unique<JSON_bool>(value));
360 } 358 }
361 359
362 JSON 360 JSON
363 JSON::makeNull() 361 JSON::makeNull()
364 { 362 {
365 - return JSON(std::make_shared<JSON_null>()); 363 + return JSON(std::make_unique<JSON_null>());
366 } 364 }
367 365
368 JSON 366 JSON
369 JSON::makeBlob(std::function<void(Pipeline*)> fn) 367 JSON::makeBlob(std::function<void(Pipeline*)> fn)
370 { 368 {
371 - return JSON(std::make_shared<JSON_blob>(fn)); 369 + return JSON(std::make_unique<JSON_blob>(fn));
372 } 370 }
373 371
374 bool 372 bool