From 149a3639473f448c072fa276cd532a697a18c986 Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Sun, 11 Aug 2024 14:37:09 +0300 Subject: [PATCH] moved from include/libjsonincpp to include/jsonincpp, added const version of everything, bs now uses getenv --- building/main.cpp | 13 +++-- .../container_parsing.cpp | 0 .../{libjsonincpp => jsonincpp}/generator.cpp | 0 .../inner_storage.cpp | 0 .../inner_storage.h | 0 .../{libjsonincpp => jsonincpp}/integer.cpp | 4 +- .../{libjsonincpp => jsonincpp}/integer.h | 4 +- .../{libjsonincpp => jsonincpp}/jsonobj.cpp | 6 ++- .../{libjsonincpp => jsonincpp}/jsonobj.h | 43 +++++++++++++--- .../{libjsonincpp => jsonincpp}/parser.cpp | 0 .../{libjsonincpp => jsonincpp}/parser.h | 0 .../parser_context.cpp | 0 .../quality_of_life.cpp | 39 ++++++++++++--- .../quality_of_life_2.cpp | 50 ++++++++++++++++--- .../string_representation.h | 0 .../{libjsonincpp => jsonincpp}/utf8.cpp | 0 .../{libjsonincpp => jsonincpp}/utf8.h | 0 src/tests/test0.cpp | 4 +- 18 files changed, 131 insertions(+), 32 deletions(-) rename src/library/{libjsonincpp => jsonincpp}/container_parsing.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/generator.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/inner_storage.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/inner_storage.h (100%) rename src/library/{libjsonincpp => jsonincpp}/integer.cpp (93%) rename src/library/{libjsonincpp => jsonincpp}/integer.h (88%) rename src/library/{libjsonincpp => jsonincpp}/jsonobj.cpp (95%) rename src/library/{libjsonincpp => jsonincpp}/jsonobj.h (72%) rename src/library/{libjsonincpp => jsonincpp}/parser.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/parser.h (100%) rename src/library/{libjsonincpp => jsonincpp}/parser_context.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/quality_of_life.cpp (72%) rename src/library/{libjsonincpp => jsonincpp}/quality_of_life_2.cpp (59%) rename src/library/{libjsonincpp => jsonincpp}/string_representation.h (100%) rename src/library/{libjsonincpp => jsonincpp}/utf8.cpp (100%) rename src/library/{libjsonincpp => jsonincpp}/utf8.h (100%) diff --git a/building/main.cpp b/building/main.cpp index c45780a..968bc25 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -38,7 +38,7 @@ struct TestWebsiteBuildScript { const NormalCBuildSystemCommandMeaning& cmd) : build_type(_build_type), make_tests(make_tests) { - ASSERT(build_type == "release" || build_type == "debug", "Unknown build type"); + ASSERT(build_type == "release" || build_type == "debug", "Unknown build type " + _build_type); std::vector ext_targets; @@ -58,7 +58,7 @@ struct TestWebsiteBuildScript { "container_parsing.cpp", }; for (std::string& u: T.units) - u = "library/libjsonincpp/" + u; + u = "library/jsonincpp/" + u; T.include_pr = "library"; T.include_ir = ""; T.exported_headers = { @@ -68,7 +68,7 @@ struct TestWebsiteBuildScript { "integer.h", }; for (std::string& u: T.exported_headers) - u = "libjsonincpp/" + u; + u = "jsonincpp/" + u; T.installation_dir = ""; T.pc_output_path = "libjsonincpp.pc"; my_targets.push_back(T); @@ -94,7 +94,12 @@ int main(int argc, char** argv) { } NormalCBuildSystemCommandMeaning cmd; regular_bs_cli_cmd_interpret(args, cmd); - TestWebsiteBuildScript bs("debug", false, cmd); + const char* BS_SCRIPT_TYPE = getenv("BS_SCRIPT_TYPE"); + const char* BS_SCRIPT_TESTS = getenv("BS_SCRIPT_TESTS"); + TestWebsiteBuildScript bs( + BS_SCRIPT_TYPE ? BS_SCRIPT_TYPE : "release", + BS_SCRIPT_TESTS ? true : false, + cmd); if (cmd.need_to_build) complete_tasks_of_build_units(bs.runlevel_1); umask(~0755); diff --git a/src/library/libjsonincpp/container_parsing.cpp b/src/library/jsonincpp/container_parsing.cpp similarity index 100% rename from src/library/libjsonincpp/container_parsing.cpp rename to src/library/jsonincpp/container_parsing.cpp diff --git a/src/library/libjsonincpp/generator.cpp b/src/library/jsonincpp/generator.cpp similarity index 100% rename from src/library/libjsonincpp/generator.cpp rename to src/library/jsonincpp/generator.cpp diff --git a/src/library/libjsonincpp/inner_storage.cpp b/src/library/jsonincpp/inner_storage.cpp similarity index 100% rename from src/library/libjsonincpp/inner_storage.cpp rename to src/library/jsonincpp/inner_storage.cpp diff --git a/src/library/libjsonincpp/inner_storage.h b/src/library/jsonincpp/inner_storage.h similarity index 100% rename from src/library/libjsonincpp/inner_storage.h rename to src/library/jsonincpp/inner_storage.h diff --git a/src/library/libjsonincpp/integer.cpp b/src/library/jsonincpp/integer.cpp similarity index 93% rename from src/library/libjsonincpp/integer.cpp rename to src/library/jsonincpp/integer.cpp index 2065f38..31512d3 100644 --- a/src/library/libjsonincpp/integer.cpp +++ b/src/library/jsonincpp/integer.cpp @@ -47,13 +47,13 @@ namespace json { free(uncomprehendable_horror); } - bool Integer::operator==(const Integer &other) { + bool Integer::operator==(const Integer &other) const { if (uncomprehendable_horror || other.uncomprehendable_horror) return to_string() == other.to_string(); return value == other.value; } - bool Integer::operator!=(const Integer &other) { + bool Integer::operator!=(const Integer &other) const { return !(*this == other); } } diff --git a/src/library/libjsonincpp/integer.h b/src/library/jsonincpp/integer.h similarity index 88% rename from src/library/libjsonincpp/integer.h rename to src/library/jsonincpp/integer.h index 20a9683..8cd7385 100644 --- a/src/library/libjsonincpp/integer.h +++ b/src/library/jsonincpp/integer.h @@ -25,8 +25,8 @@ namespace json { ~Integer(); - bool operator==(const Integer& other); - bool operator!=(const Integer& other); + bool operator==(const Integer& other) const; + bool operator!=(const Integer& other) const; }; } diff --git a/src/library/libjsonincpp/jsonobj.cpp b/src/library/jsonincpp/jsonobj.cpp similarity index 95% rename from src/library/libjsonincpp/jsonobj.cpp rename to src/library/jsonincpp/jsonobj.cpp index e21ec63..ced9981 100644 --- a/src/library/libjsonincpp/jsonobj.cpp +++ b/src/library/jsonincpp/jsonobj.cpp @@ -85,6 +85,10 @@ namespace json { } JSON_reference JSON::r() noexcept { - return {this, {}}; + return {*this, {}}; + } + + JSON_reference_const JSON::r() const noexcept { + return {*this, false}; } } diff --git a/src/library/libjsonincpp/jsonobj.h b/src/library/jsonincpp/jsonobj.h similarity index 72% rename from src/library/libjsonincpp/jsonobj.h rename to src/library/jsonincpp/jsonobj.h index 6f2c07d..673910b 100644 --- a/src/library/libjsonincpp/jsonobj.h +++ b/src/library/jsonincpp/jsonobj.h @@ -27,6 +27,7 @@ namespace json { }; struct JSON_reference; + struct JSON_reference_const; struct JSON { void* value = NULL; @@ -48,6 +49,8 @@ namespace json { JSON_reference r() noexcept; + JSON_reference_const r() const noexcept; + json_t getType() const; bool isNull() const; @@ -72,17 +75,28 @@ namespace json { bool toBool() const; - Integer& asInteger() const; + const Integer& asInteger() const; - std::string& asString() const; + const std::string& asString() const; - std::vector& asArray() const; + const std::vector& asArray() const; - std::map& asDictionary() const; + const std::map& asDictionary() const; + + Integer& asInteger(); + + std::string& asString(); + + std::vector& asArray(); + + std::map& asDictionary(); JSON_reference operator[](size_t index); JSON_reference operator[](const std::string& key); + JSON_reference_const operator[](size_t index) const; + JSON_reference_const operator[](const std::string& key) const; + JSON& operator=(int64_t V); JSON& operator=(const Integer& V); JSON& operator=(const char* V); @@ -102,18 +116,33 @@ namespace json { /* These references get invalidated as soon as referenced object or any of its parents get changed */ struct JSON_reference { - JSON* last_real = NULL; + JSON& last_real; std::vector imaginary_chain; - bool isDefined() const; + bool isDefined(); - JSON& operator*() const; + JSON& operator*(); + JSON& g(); void operator=(const JSON& obj); JSON_reference operator[](size_t index); JSON_reference operator[](const std::string& key); }; + + /* text */ + struct JSON_reference_const { + const JSON& last_real; + bool bad = false; + + bool isDefined(); + + const JSON& operator*(); + const JSON& g(); + + JSON_reference_const operator[](size_t index); + JSON_reference_const operator[](const std::string& key); + }; } #endif diff --git a/src/library/libjsonincpp/parser.cpp b/src/library/jsonincpp/parser.cpp similarity index 100% rename from src/library/libjsonincpp/parser.cpp rename to src/library/jsonincpp/parser.cpp diff --git a/src/library/libjsonincpp/parser.h b/src/library/jsonincpp/parser.h similarity index 100% rename from src/library/libjsonincpp/parser.h rename to src/library/jsonincpp/parser.h diff --git a/src/library/libjsonincpp/parser_context.cpp b/src/library/jsonincpp/parser_context.cpp similarity index 100% rename from src/library/libjsonincpp/parser_context.cpp rename to src/library/jsonincpp/parser_context.cpp diff --git a/src/library/libjsonincpp/quality_of_life.cpp b/src/library/jsonincpp/quality_of_life.cpp similarity index 72% rename from src/library/libjsonincpp/quality_of_life.cpp rename to src/library/jsonincpp/quality_of_life.cpp index 54d636a..60c7747 100644 --- a/src/library/libjsonincpp/quality_of_life.cpp +++ b/src/library/jsonincpp/quality_of_life.cpp @@ -58,30 +58,49 @@ namespace json { } } - Integer& JSON::asInteger() const { + const Integer& JSON::asInteger() const { if (isInteger()) - return *static_cast(value); + return *((const Integer*)value); throw misuse("json obj is not integer"); } - std::string& JSON::asString() const { + const std::string& JSON::asString() const { if (isString()) - return *static_cast(value); + return *(const std::string*)value; throw misuse("json obj is not string"); } - std::vector& JSON::asArray() const { + const std::vector& JSON::asArray() const { if (isArray()) return static_cast(value)->data; throw misuse("json obj is not array"); } - std::map& JSON::asDictionary() const { + const std::map& JSON::asDictionary() const { if (isDictionary()) return static_cast(value)->data; throw misuse("json obj is not dictionary"); } + Integer & JSON::asInteger() { + return const_cast(const_cast(this)->asInteger()); + } + + std::string &JSON::asString() { + return const_cast(const_cast(this)->asString()); + } + + std::vector &JSON::asArray() { + return const_cast&>(const_cast(this)->asArray()); + } + + std::map &JSON::asDictionary() { + return const_cast&>(const_cast(this)->asDictionary()); + } + + + + JSON_reference JSON::operator[](size_t index) { return r()[index]; } @@ -90,6 +109,14 @@ namespace json { return r()[key]; } + JSON_reference_const JSON::operator[](size_t index) const { + return r()[index]; + } + + JSON_reference_const JSON::operator[](const std::string &key) const { + return r()[key]; + } + JSON& JSON::operator=(int64_t V) { nullify(*this); value = new Integer(V); diff --git a/src/library/libjsonincpp/quality_of_life_2.cpp b/src/library/jsonincpp/quality_of_life_2.cpp similarity index 59% rename from src/library/libjsonincpp/quality_of_life_2.cpp rename to src/library/jsonincpp/quality_of_life_2.cpp index a1fae8a..3593fce 100644 --- a/src/library/libjsonincpp/quality_of_life_2.cpp +++ b/src/library/jsonincpp/quality_of_life_2.cpp @@ -1,18 +1,22 @@ #include "jsonobj.h" namespace json { - bool JSON_reference::isDefined() const { + bool JSON_reference::isDefined(){ return imaginary_chain.empty(); } - JSON& JSON_reference::operator*() const { + JSON& JSON_reference::operator*(){ + return g(); + } + + JSON & JSON_reference::g() { if (!isDefined()) throw misuse("dereferencing json reference with non-empty imaginary part"); - return *last_real; + return last_real; } void JSON_reference::operator=(const JSON &obj) { - JSON* cur_last_real = last_real; + JSON* cur_last_real = &last_real; for (const auto& ck: imaginary_chain) { if (ck.type == undefined_array_element) { if (cur_last_real->type == null_symbol) @@ -38,8 +42,8 @@ namespace json { elongated.push_back({undefined_array_element, index, ""}); return {last_real, elongated}; } - if (last_real->isArray() && last_real->asArray().size() > index) { - return {&last_real->asArray()[index], {}}; + if (last_real.isArray() && last_real.asArray().size() > index) { + return {last_real.asArray()[index], {}}; } return {last_real, {ImaginaryKeyChainEValue{undefined_array_element, index, ""}}}; } @@ -50,9 +54,39 @@ namespace json { elongated.push_back({undefined_dictionary_element, 0, key}); return {last_real, elongated}; } - if (last_real->isDictionary() && last_real->asDictionary().count(key) > 0) { - return {&last_real->asDictionary()[key], {}}; + if (last_real.isDictionary() && last_real.asDictionary().count(key) > 0) { + return {last_real.asDictionary()[key], {}}; } return {last_real, {ImaginaryKeyChainEValue{undefined_dictionary_element, 0, key}}}; } + + bool JSON_reference_const::isDefined() { + return !bad; + } + + const JSON & JSON_reference_const::operator*() { + return g(); + } + + const JSON & JSON_reference_const::g() { + if (bad) + throw misuse("dereferencing const json reference with non-empty imaginary part"); + return last_real; + } + + JSON_reference_const JSON_reference_const::operator[](size_t index) { + if (bad) + return {last_real, true}; + if (last_real.isArray() && last_real.asArray().size() > index) + return {last_real.asArray()[index], false}; + return {last_real, true}; + } + + JSON_reference_const JSON_reference_const::operator[](const std::string &key) { + if (bad) + return {last_real, true}; + if (last_real.isDictionary() && last_real.asDictionary().count(key) > 0) + return {last_real.asDictionary().at(key), false}; + return {last_real, true}; + } } diff --git a/src/library/libjsonincpp/string_representation.h b/src/library/jsonincpp/string_representation.h similarity index 100% rename from src/library/libjsonincpp/string_representation.h rename to src/library/jsonincpp/string_representation.h diff --git a/src/library/libjsonincpp/utf8.cpp b/src/library/jsonincpp/utf8.cpp similarity index 100% rename from src/library/libjsonincpp/utf8.cpp rename to src/library/jsonincpp/utf8.cpp diff --git a/src/library/libjsonincpp/utf8.h b/src/library/jsonincpp/utf8.h similarity index 100% rename from src/library/libjsonincpp/utf8.h rename to src/library/jsonincpp/utf8.h diff --git a/src/tests/test0.cpp b/src/tests/test0.cpp index 09ba6e9..acf07aa 100644 --- a/src/tests/test0.cpp +++ b/src/tests/test0.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include using namespace json;