From a589a56102aab024c050acb8d848933d40cbf419 Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Thu, 3 May 2018 15:49:10 +0100 Subject: [PATCH 1/6] Supply `value_type` typedef in `json_arrayt` for STL algorithm usage. Some of the STL algorithms require the `value_type` typedef on containers. Adding this typedef allows these algorithms to be used with `json_arrayt`. The following example will not compile without this commit. ``` std::vector example_strings = {"green", "eggs", "ham"}; json_arrayt json_array; std::transform( example_strings.cbegin(), example_strings.cend(), std::back_inserter(json_array), [](std::string string) { return json_stringt(string); }); ``` --- src/util/json.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/util/json.h b/src/util/json.h index 018b92606f6..150e453474a 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -169,6 +169,8 @@ class json_arrayt:public jsont array.push_back(jsont()); return array.back(); } + + typedef jsont value_type; // NOLINT(readability/identifiers) }; class json_stringt:public jsont From 28117d202e8b3b72a42dfb16b5c0c233f4c18717 Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Thu, 3 May 2018 15:50:50 +0100 Subject: [PATCH 2/6] Expose `emplace_back` method of underlying `std::vector` in `json_arrayt`. Using this method facilitates construction of the items in the json_array, using move rather than copy construction. --- src/util/json.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/util/json.h b/src/util/json.h index 150e453474a..cea19c33ff8 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -170,6 +170,12 @@ class json_arrayt:public jsont return array.back(); } + template + void emplace_back(argumentst &&... arguments) + { + array.emplace_back(std::forward(arguments)...); + } + typedef jsont value_type; // NOLINT(readability/identifiers) }; From ce674b57d40a64666e04b441b06ab3151e8f7bb8 Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Thu, 3 May 2018 15:58:38 +0100 Subject: [PATCH 3/6] Update constructor of `jsont` based on the copy and move idiom. This improves the case where `jsont` is constructed based on the result of a function. In this case the copy will be elided and `value` will be initialised based on the move constructor. --- src/util/json.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/json.h b/src/util/json.h index cea19c33ff8..f8126b1fea6 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -117,7 +117,7 @@ class jsont { } - jsont(kindt _kind, const std::string &_value):kind(_kind), value(_value) + jsont(kindt _kind, std::string _value):kind(_kind), value(std::move(_value)) { } From 2907ba9275ba8c0fc96b92fee935afc08407328d Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Thu, 3 May 2018 16:50:09 +0100 Subject: [PATCH 4/6] Allow constructon of `json_stringt` from `irep_idt`. Allow constructon of `jsont`/`json_stringt` from `std::string`/`irep_idt`/string literals. This allows for a reduction in the amount of boilerplate code. For example instead of writing ``` result["file"] = json_stringt(id2string(location.get_file())); ``` we could write ``` result["file"] = json_stringt(location.get_file()); ``` The constructor from string literal is required when constructors from both `std::string` and `irep_idt` are provided. Otherwise the compiler does not know whether to implicitly convert string literals into a `std:string` or a `irep_idt`. --- src/util/json.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/util/json.h b/src/util/json.h index f8126b1fea6..05e4278605d 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -15,6 +15,8 @@ Author: Daniel Kroening, kroening@kroening.com #include #include +#include "irep.h" + class json_objectt; class json_arrayt; @@ -182,8 +184,20 @@ class json_arrayt:public jsont class json_stringt:public jsont { public: - explicit json_stringt(const std::string &_value): - jsont(kindt::J_STRING, _value) + explicit json_stringt(std::string _value): + jsont(kindt::J_STRING, std::move(_value)) + { + } + +#ifndef USE_STD_STRING + explicit json_stringt(const irep_idt &_value) + : jsont(kindt::J_STRING, id2string(_value)) + { + } +#endif + + /// Constructon from string literal. + explicit json_stringt(const char *_value) : jsont(kindt::J_STRING, _value) { } }; From 402bc56eb719fadf89becbd429a11c5fdff6c25c Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Wed, 9 May 2018 10:31:11 +0100 Subject: [PATCH 5/6] Clang format updates. --- src/util/json.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/json.h b/src/util/json.h index 05e4278605d..cde34f0d9a3 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -119,7 +119,7 @@ class jsont { } - jsont(kindt _kind, std::string _value):kind(_kind), value(std::move(_value)) + jsont(kindt _kind, std::string _value) : kind(_kind), value(std::move(_value)) { } @@ -184,8 +184,8 @@ class json_arrayt:public jsont class json_stringt:public jsont { public: - explicit json_stringt(std::string _value): - jsont(kindt::J_STRING, std::move(_value)) + explicit json_stringt(std::string _value) + : jsont(kindt::J_STRING, std::move(_value)) { } From f3670e347e59d2cd03c6be118e4b8a1d7348ef81 Mon Sep 17 00:00:00 2001 From: Thomas Spriggs Date: Thu, 10 May 2018 14:38:43 +0100 Subject: [PATCH 6/6] Expose `begin` and `end` methods of underlying `std::vector` in `json_arrayt`. This facilitates access to the elements in a `json_arrayt`. --- src/util/json.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/util/json.h b/src/util/json.h index cde34f0d9a3..35aea966e08 100644 --- a/src/util/json.h +++ b/src/util/json.h @@ -178,6 +178,36 @@ class json_arrayt:public jsont array.emplace_back(std::forward(arguments)...); } + std::vector::iterator begin() + { + return array.begin(); + } + + std::vector::const_iterator begin() const + { + return array.begin(); + } + + std::vector::const_iterator cbegin() const + { + return array.cbegin(); + } + + std::vector::iterator end() + { + return array.end(); + } + + std::vector::const_iterator end() const + { + return array.end(); + } + + std::vector::const_iterator cend() const + { + return array.cend(); + } + typedef jsont value_type; // NOLINT(readability/identifiers) };