Skip to content

Commit

Permalink
chore: update meojson to v4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 14, 2025
1 parent 75a0559 commit 5a28b99
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 11 deletions.
82 changes: 82 additions & 0 deletions 3rdparty/include/meojson/common/array.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <initializer_list>
Expand Down Expand Up @@ -72,6 +74,17 @@ class basic_array
{
}

template <typename... elem_ts>
basic_array(const std::tuple<elem_ts...>& tup)
{
foreach_tuple(tup, std::make_index_sequence<std::tuple_size_v<std::tuple<elem_ts...>>>());
}
template <typename first_t, typename second_t>
basic_array(std::pair<first_t, second_t> pair)
: _array_data({ std::move(pair.first), std::move(pair.second) })
{
}

~basic_array() noexcept = default;

bool empty() const noexcept { return _array_data.empty(); }
Expand Down Expand Up @@ -102,6 +115,10 @@ class basic_array
size_t Size,
template <typename, size_t> typename fixed_array_t = std::array>
fixed_array_t<value_t, Size> as_fixed_array() const;
template <typename... elem_ts>
std::tuple<elem_ts...> as_tuple() const;
template <typename first_t, typename second_t>
std::pair<first_t, second_t> as_pair() const;

// Usage: get(key_1, key_2, ..., default_value);
template <typename... key_then_default_value_t>
Expand Down Expand Up @@ -178,6 +195,18 @@ class basic_array
return as_fixed_array<value_t, Size, fixed_array_t>();
}

template <typename... elem_ts>
explicit operator std::tuple<elem_ts...>() const
{
return as_tuple<elem_ts...>();
}

template <typename elem1_t, typename elem2_t>
explicit operator std::pair<elem1_t, elem2_t>() const
{
return as_pair<elem1_t, elem2_t>();
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_from_json_in_member<jsonization_t, string_t>::value, bool> =
Expand Down Expand Up @@ -215,6 +244,17 @@ class basic_array
template <typename value_t>
auto get_helper(const value_t& default_value, size_t pos) const;

template <typename tuple_t>
tuple_t as_tuple_templ() const;
template <size_t index, typename tuple_t>
void set_tuple(tuple_t& tup) const;

template <typename Tuple, std::size_t... Is>
void foreach_tuple(const Tuple& t, std::index_sequence<Is...>)
{
(_array_data.emplace_back(std::get<Is>(t)), ...);
}

string_t format(size_t indent, size_t indent_times) const;

private:
Expand Down Expand Up @@ -377,6 +417,48 @@ inline fixed_array_t<value_t, Size> basic_array<string_t>::as_fixed_array() cons
return result;
}

template <typename string_t>
template <size_t index, typename tuple_t>
inline void basic_array<string_t>::set_tuple(tuple_t& tup) const
{
using elem_t = std::tuple_element_t<index, tuple_t>;

if constexpr (index > 0) {
set_tuple<index - 1>(tup);
}

std::get<index>(tup) = static_cast<elem_t>(at(index));
}

template <typename string_t>
template <typename tuple_t>
inline tuple_t basic_array<string_t>::as_tuple_templ() const
{
constexpr size_t tuple_size = std::tuple_size_v<tuple_t>;

if (size() != tuple_size) {
throw exception("Wrong array size");
}

tuple_t result;
set_tuple<tuple_size - 1>(result);
return result;
}

template <typename string_t>
template <typename... elem_ts>
inline std::tuple<elem_ts...> basic_array<string_t>::as_tuple() const
{
return as_tuple_templ<std::tuple<elem_ts...>>();
}

template <typename string_t>
template <typename first_t, typename second_t>
inline std::pair<first_t, second_t> basic_array<string_t>::as_pair() const
{
return as_tuple_templ<std::pair<first_t, second_t>>();
}

template <typename string_t>
template <typename... key_then_default_value_t>
inline auto basic_array<string_t>::get(key_then_default_value_t&&... keys_then_default_value) const
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/include/meojson/common/exception.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <exception>
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/include/meojson/common/object.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <initializer_list>
Expand Down
6 changes: 4 additions & 2 deletions 3rdparty/include/meojson/common/serialization.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <type_traits>
Expand All @@ -13,8 +15,8 @@ template <typename in_t, typename serializer_t>
class is_serializable
{
template <typename U>
static auto
test(int) -> decltype(std::declval<serializer_t>()(std::declval<U>()), std::true_type());
static auto test(int)
-> decltype(std::declval<serializer_t>()(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/include/meojson/common/types.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include "array.hpp"
Expand Down
30 changes: 25 additions & 5 deletions 3rdparty/include/meojson/common/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -163,7 +168,7 @@ class has_from_json_in_templ_spec
};

template <typename string_t>
static constexpr string_t unescape_string(const string_t& str)
inline constexpr string_t unescape_string(const string_t& str)
{
using char_t = typename string_t::value_type;

Expand Down Expand Up @@ -211,26 +216,41 @@ static constexpr string_t unescape_string(const string_t& str)
}

template <typename string_t>
static constexpr string_t true_string()
inline constexpr string_t true_string()
{
return { 't', 'r', 'u', 'e' };
}

template <typename string_t>
static constexpr string_t false_string()
inline constexpr string_t false_string()
{
return { 'f', 'a', 'l', 's', 'e' };
}

template <typename string_t>
static constexpr string_t null_string()
inline constexpr string_t null_string()
{
return { 'n', 'u', 'l', 'l' };
}

template <typename string_t, typename any_t>
string_t to_basic_string(any_t&& arg)
inline string_t to_basic_string(any_t&& arg)
{
#ifdef MEOJSON_KEEP_FLOATING_PRECISION
using real_type = std::remove_reference_t<any_t>;
if constexpr (std::is_floating_point_v<real_type>) {
if constexpr (std::is_same_v<string_t, std::string>) {
std::ostringstream oss;
oss << std::setprecision(std::numeric_limits<real_type>::max_digits10) << arg;
return oss.str();
}
else if constexpr (std::is_same_v<string_t, std::wstring>) {
std::wostringstream oss;
oss << std::setprecision(std::numeric_limits<real_type>::max_digits10) << arg;
return oss.str();
}
}
#endif
if constexpr (std::is_same_v<string_t, std::string>) {
return std::to_string(std::forward<any_t>(arg));
}
Expand Down
40 changes: 39 additions & 1 deletion 3rdparty/include/meojson/common/value.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <cstddef>
Expand Down Expand Up @@ -98,6 +100,12 @@ class basic_value
{
}

template <typename enum_t, std::enable_if_t<std::is_enum_v<enum_t>, bool> = true>
basic_value(enum_t e)
: basic_value(static_cast<std::underlying_type_t<enum_t>>(e))
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
Expand All @@ -114,6 +122,18 @@ class basic_value
{
}

template <typename... elem_ts>
basic_value(std::tuple<elem_ts...>&& tup)
: basic_value(basic_array<string_t>(std::forward<std::tuple<elem_ts...>>(tup)))
{
}

template <typename elem1_t, typename elem2_t>
basic_value(std::pair<elem1_t, elem2_t>&& pair)
: basic_value(basic_array<string_t>(std::pair<elem1_t, elem2_t>(pair)))
{
}

template <
typename value_t,
std::enable_if_t<!std::is_convertible_v<value_t, basic_value<string_t>>, bool> = true>
Expand Down Expand Up @@ -335,6 +355,24 @@ class basic_value
return dst;
}

template <typename enum_t, std::enable_if_t<std::is_enum_v<enum_t>, bool> = true>
explicit operator enum_t() const
{
return static_cast<enum_t>(static_cast<std::underlying_type_t<enum_t>>(*this));
}

template <typename... elem_ts>
explicit operator std::tuple<elem_ts...>() const
{
return as_array().template as_tuple<elem_ts...>();
}

template <typename elem1_t, typename elem2_t>
explicit operator std::pair<elem1_t, elem2_t>() const
{
return as_array().template as_pair<elem1_t, elem2_t>();
}

private:
friend class basic_array<string_t>;
friend class basic_object<string_t>;
Expand Down Expand Up @@ -505,7 +543,7 @@ inline bool basic_value<string_t>::is() const noexcept
else if constexpr (std::is_same_v<bool, value_t>) {
return is_boolean();
}
else if constexpr (std::is_arithmetic_v<value_t>) {
else if constexpr (std::is_arithmetic_v<value_t> || std::is_enum_v<value_t>) {
return is_number();
}
else if constexpr (std::is_constructible_v<string_t, value_t>) {
Expand Down
4 changes: 4 additions & 0 deletions 3rdparty/include/meojson/json.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

// IWYU pragma: begin_exports

#include "common/serialization.hpp"
#include "common/types.hpp"
#include "parser/parser.hpp"
#include "reflection/jsonization.hpp"

// IWYU pragma: end_exports
26 changes: 26 additions & 0 deletions 3rdparty/include/meojson/meojson.natvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>

<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="json::basic_value&lt;*&gt;">
<DisplayString>{{ type={_type} str={to_string()} }}</DisplayString>
<Expand>
<Item Name="[type]">_type</Item>
<Item Name="[str]">format(4)</Item>
<Item Name="[value]" Condition="_type&lt;5">_raw_data._Head</Item>
<Item Name="[array]" Condition="_type==5">*_raw_data._Tail._Head</Item>
<Item Name="[object]" Condition="_type==6">*_raw_data._Tail._Tail._Head</Item>
</Expand>
</Type>
<Type Name="json::basic_array&lt;*&gt;">
<DisplayString>{{ size={_array_data.size()} str={to_string()} }}</DisplayString>
<Expand>
<ExpandedItem>_array_data</ExpandedItem>
</Expand>
</Type>
<Type Name="json::basic_object&lt;*&gt;">
<DisplayString>{{ size={_object_data._Mypair._Myval2._Myval2._Mysize} str={to_string()} }}</DisplayString>
<Expand>
<ExpandedItem>_object_data</ExpandedItem>
</Expand>
</Type>
</AutoVisualizer>
2 changes: 2 additions & 0 deletions 3rdparty/include/meojson/parser/bitops.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
Expand Down
4 changes: 4 additions & 0 deletions 3rdparty/include/meojson/parser/packed_bytes.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <cstdint>
Expand Down Expand Up @@ -28,11 +30,13 @@ struct packed_bytes
};
}

#ifndef MEOJSON_DISABLE_PACKED_BYTES
#if defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP)
#include "packed_bytes_x86.hpp"
#elif defined(__ARM_NEON) || defined(_M_ARM) || defined(_M_ARM64)
#include "packed_bytes_arm.hpp"
#endif
#endif

namespace json::_packed_bytes
{
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/include/meojson/parser/packed_bytes_arm.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

// current NEON implementation doesn't outperform 64-bit scalar implementation
Expand Down
3 changes: 3 additions & 0 deletions 3rdparty/include/meojson/parser/packed_bytes_x86.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include "packed_bytes.hpp"

#include <emmintrin.h>
Expand Down
Loading

0 comments on commit 5a28b99

Please sign in to comment.