Skip to content

Commit

Permalink
Rename from constexpr_json to json2cpp, cache size()
Browse files Browse the repository at this point in the history
  • Loading branch information
lefticus committed Feb 25, 2022
1 parent 8de13fa commit b34d892
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SOFTWARE.
#include <stdexcept>
#include <string_view>

namespace constexpr_json {
namespace json2cpp {
template<typename First, typename Second> struct pair
{
First first;
Expand Down Expand Up @@ -289,13 +289,19 @@ template<typename CharType> struct basic_json

[[nodiscard]] constexpr std::size_t size() const noexcept
{
if (is_null()) { return 0; }
if (is_object()) { return data.get_if_object()->size(); }
if (is_array()) { return data.get_if_array()->size(); }
return size_;
}

[[nodiscard]] static constexpr std::size_t size(const basic_json &obj) noexcept
{
if (obj.is_null()) { return 0; }
if (obj.is_object()) { return obj.data.get_if_object()->size(); }
if (obj.is_array()) { return obj.data.get_if_array()->size(); }

return 1;
}


[[nodiscard]] constexpr const basic_json &operator[](const std::size_t idx) const
{
if (const auto &children = array_data(); idx < children.size()) {
Expand Down Expand Up @@ -413,7 +419,9 @@ template<typename CharType> struct basic_json
return is_null() || is_string() || is_boolean() || is_number() || is_binary();
}


data_t data;
std::size_t size_{size(*this)};
};

using json = basic_json<char>;
Expand Down
42 changes: 21 additions & 21 deletions include/json2cpp/json2cpp_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SOFTWARE.

#pragma once

#include <json2cpp/constexpr_json.hpp>
#include <json2cpp/json2cpp.hpp>
#include <string>

#include <utility>
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace adapters {
* Note that this constructor will throw an exception if the value is not
* an array.
*/
explicit json2cppJsonArray(const constexpr_json::json &value) : m_value(value)
explicit json2cppJsonArray(const json2cpp::json &value) : m_value(value)
{
if (!value.is_array()) { throwRuntimeError("Value is not an array."); }
}
Expand Down Expand Up @@ -126,14 +126,14 @@ namespace adapters {
*
* Note that the value returned by this function is a singleton.
*/
static const constexpr_json::json &emptyArray()
static const json2cpp::json &emptyArray()
{
static const constexpr_json::json array = constexpr_json::json::array();
static const json2cpp::json array = json2cpp::json::array();
return array;
}

/// Reference to the contained value
const constexpr_json::json &m_value;
const json2cpp::json &m_value;
};

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ namespace adapters {
* Note that this constructor will throw an exception if the value is not
* an object.
*/
explicit json2cppJsonObject(const constexpr_json::json &value) : m_value(value)
explicit json2cppJsonObject(const json2cpp::json &value) : m_value(value)
{
if (!value.is_object()) { throwRuntimeError("Value is not an object."); }
}
Expand Down Expand Up @@ -207,14 +207,14 @@ namespace adapters {
*
* Note that the value returned by this function is a singleton.
*/
static const constexpr_json::json &emptyObject()
static const json2cpp::json &emptyObject()
{
static const constexpr_json::json object = constexpr_json::json::object();
static const json2cpp::json object = json2cpp::json::object();
return object;
}

/// Reference to the contained object
const constexpr_json::json &m_value;
const json2cpp::json &m_value;
};


Expand All @@ -235,15 +235,15 @@ namespace adapters {
*
* @param source the json2cppJson value to be copied
*/
explicit json2cppJsonFrozenValue(constexpr_json::json source) : m_value(std::move(source)) {}
explicit json2cppJsonFrozenValue(json2cpp::json source) : m_value(std::move(source)) {}

FrozenValue *clone() const override { return new json2cppJsonFrozenValue(m_value); }

bool equalTo(const Adapter &other, bool strict) const override;

private:
/// Stored json2cppJson value
constexpr_json::json m_value;
json2cpp::json m_value;
};


Expand All @@ -268,7 +268,7 @@ namespace adapters {
json2cppJsonValue() : m_value(emptyObject()) {}

/// Construct a wrapper for a specific json2cppJson value
explicit json2cppJsonValue(const constexpr_json::json &value) : m_value(value) {}
explicit json2cppJsonValue(const json2cpp::json &value) : m_value(value) {}

/**
* @brief Create a new json2cppJsonFrozenValue instance that contains the
Expand Down Expand Up @@ -413,14 +413,14 @@ namespace adapters {

private:
/// Return a reference to an empty object singleton
static const constexpr_json::json &emptyObject()
static const json2cpp::json &emptyObject()
{
static const constexpr_json::json object = constexpr_json::json::object();
static const json2cpp::json object = json2cpp::json::object();
return object;
}

/// Reference to the contained json2cppJson value.
const constexpr_json::json &m_value;
const json2cpp::json &m_value;
};

/**
Expand All @@ -444,7 +444,7 @@ namespace adapters {
json2cppJsonAdapter() : BasicAdapter() {}

/// Construct a json2cppJsonAdapter containing a specific json2cpp Json object
explicit json2cppJsonAdapter(const constexpr_json::json &value) : BasicAdapter(json2cppJsonValue{ value }) {}
explicit json2cppJsonAdapter(const json2cpp::json &value) : BasicAdapter(json2cppJsonValue{ value }) {}
};

/**
Expand All @@ -471,7 +471,7 @@ namespace adapters {
*
* @param itr json2cppJson iterator to store
*/
explicit json2cppJsonArrayValueIterator(const constexpr_json::json::const_iterator &itr) : m_itr(itr) {}
explicit json2cppJsonArrayValueIterator(const json2cpp::json::const_iterator &itr) : m_itr(itr) {}

/// Returns a json2cppJsonAdapter that contains the value of the current
/// element.
Expand Down Expand Up @@ -522,7 +522,7 @@ namespace adapters {
void advance(std::ptrdiff_t n) { m_itr += n; }

private:
constexpr_json::json::const_iterator m_itr;
json2cpp::json::const_iterator m_itr;
};


Expand Down Expand Up @@ -550,7 +550,7 @@ namespace adapters {
*
* @param itr json2cppJson iterator to store
*/
explicit json2cppJsonObjectMemberIterator(const constexpr_json::json::const_iterator &itr) : m_itr(itr) {}
explicit json2cppJsonObjectMemberIterator(const json2cpp::json::const_iterator &itr) : m_itr(itr) {}

/**
* @brief Returns a json2cppJsonObjectMember that contains the key and value
Expand Down Expand Up @@ -601,13 +601,13 @@ namespace adapters {

private:
/// Iternal copy of the original json2cppJson iterator
constexpr_json::json::const_iterator m_itr;
json2cpp::json::const_iterator m_itr;
};

/// Specialisation of the AdapterTraits template struct for json2cppJsonAdapter.
template<> struct AdapterTraits<valijson::adapters::json2cppJsonAdapter>
{
typedef constexpr_json::json DocumentType;
typedef json2cpp::json DocumentType;

static std::string adapterName() { return "json2cppJsonAdapter"; }
};
Expand Down
10 changes: 5 additions & 5 deletions src/json2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
results.hpp.push_back(fmt::format("#ifndef {}_COMPILED_JSON", document_name));
results.hpp.push_back(fmt::format("#define {}_COMPILED_JSON", document_name));

results.hpp.emplace_back("#include <json2cpp/constexpr_json.hpp>");
results.hpp.emplace_back("#include <json2cpp/json2cpp.hpp>");

results.hpp.push_back(fmt::format("namespace compiled_json::{} {{", document_name));
results.hpp.push_back(fmt::format(" const constexpr_json::json &get_{}();", document_name));
results.hpp.push_back(fmt::format(" const json2cpp::json &get_{}();", document_name));
results.hpp.emplace_back("}");

results.hpp.emplace_back("#endif");
Expand All @@ -110,16 +110,16 @@ compile_results compile(const std::string_view document_name, const nlohmann::js
results.impl.push_back(fmt::format("#ifndef {}_COMPILED_JSON_IMPL", document_name));
results.impl.push_back(fmt::format("#define {}_COMPILED_JSON_IMPL", document_name));

results.impl.emplace_back("#include <json2cpp/constexpr_json.hpp>");
results.impl.emplace_back("#include <json2cpp/json2cpp.hpp>");

results.impl.push_back(fmt::format("namespace compiled_json::{} {{\nusing json = constexpr_json::basic_json<char>;\nusing data_t=constexpr_json::data_variant<char>;\nusing string_view=std::basic_string_view<char>;\nusing array_t=constexpr_json::basic_array_t<char>;\nusing object_t=constexpr_json::basic_object_t<char>;\nusing value_pair_t=constexpr_json::basic_value_pair_t<char>;\n", document_name));
results.impl.push_back(fmt::format("namespace compiled_json::{} {{\nusing json = json2cpp::basic_json<char>;\nusing data_t=json2cpp::data_variant<char>;\nusing string_view=std::basic_string_view<char>;\nusing array_t=json2cpp::basic_array_t<char>;\nusing object_t=json2cpp::basic_object_t<char>;\nusing value_pair_t=json2cpp::basic_value_pair_t<char>;\n", document_name));


const auto last_obj_name = compile(json, obj_count, results.impl);

results.impl.push_back(fmt::format("static constexpr auto document = json{{{{{}}}}};", last_obj_name));

results.impl.push_back(fmt::format("const constexpr_json::json &get_{}() {{ return document; }}", document_name));
results.impl.push_back(fmt::format("const json2cpp::json &get_{}() {{ return document; }}", document_name));
results.impl.emplace_back("}");
results.impl.emplace_back("#endif");

Expand Down

0 comments on commit b34d892

Please sign in to comment.