Skip to content

Commit

Permalink
perf: rename std_array to fixed_array
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Mar 15, 2024
1 parent 8aa6708 commit 7aebc26
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
14 changes: 11 additions & 3 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class basic_array
{
}

template <
typename fixed_array_t,
std::enable_if_t<_utils::is_fixed_array<fixed_array_t>, bool> = true>
basic_array(fixed_array_t arr)
: _array_data(std::make_move_iterator(arr.begin()), std::make_move_iterator(arr.end()))
{
}

template <
typename jsonization_t,
std::enable_if_t<_utils::has_to_json_in_member<jsonization_t>::value, bool> = true>
Expand Down Expand Up @@ -90,7 +98,7 @@ class basic_array
template <typename value_t, template <typename...> typename collection_t = std::vector>
collection_t<value_t> as_collection() const;
template <typename value_t, size_t Size>
std::array<value_t, Size> as_collection() const;
std::array<value_t, Size> as_fixed_array() const;

// Usage: get(key_1, key_2, ..., default_value);
template <typename... key_then_default_value_t>
Expand Down Expand Up @@ -160,7 +168,7 @@ class basic_array
template <typename value_t, size_t Size>
explicit operator std::array<value_t, Size>() const
{
return as_collection<value_t, Size>();
return as_fixed_array<value_t, Size>();
}

template <
Expand Down Expand Up @@ -349,7 +357,7 @@ inline collection_t<value_t> basic_array<string_t>::as_collection() const

template <typename string_t>
template <typename value_t, size_t Size>
inline std::array<value_t, Size> basic_array<string_t>::as_collection() const
inline std::array<value_t, Size> basic_array<string_t>::as_fixed_array() const
{
if (size() != Size) {
throw exception("Wrong array size");
Expand Down
28 changes: 25 additions & 3 deletions include/common/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,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 Expand Up @@ -99,7 +99,8 @@ basic_value<string_t> serialize(in_t&& in, const serializer_t& serializer = {})
else if constexpr (std::is_constructible_v<basic_value<string_t>, in_t>) {
return basic_value<string_t>(std::forward<in_t>(in));
}
else if constexpr (_utils::is_collection<std::decay_t<in_t>>) {
else if constexpr (
_utils::is_collection<std::decay_t<in_t>> || _utils::is_fixed_array<std::decay_t<in_t>>) {
basic_array<string_t> arr;
for (auto&& elem : in) {
using elem_t = decltype(elem);
Expand Down Expand Up @@ -162,6 +163,27 @@ bool deserialize(
}
return true;
}
else if constexpr (_utils::is_fixed_array<std::decay_t<out_t>>) {
if (!in.is_array()) {
return false;
}
auto&& in_as_arr = in.as_array();
constexpr size_t out_size = _utils::fixed_array_size<out_t>;
if (in_as_arr.size() != out_size) {
return false;
}

for (size_t i = 0; i < out_size; ++i) {
auto&& j_elem = in_as_arr.at(i);
using elem_t = typename out_t::value_type;
elem_t elem {};
if (!deserialize<elem_t, deserializer_t, string_t>(j_elem, elem, deserializer)) {
return false;
}
out.at(i) = std::move(elem);
}
return true;
}
else if constexpr (_utils::is_map<std::decay_t<out_t>>) {
if (!in.is_object()) {
return false;
Expand Down
16 changes: 8 additions & 8 deletions include/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ constexpr bool is_map<T, std::void_t<typename T::key_type, typename T::mapped_ty
is_container<T>;

template <typename T, typename = void>
constexpr bool is_collection = false;
template <typename T>
constexpr bool is_collection<T> = is_container<T> && !is_map<T>;
constexpr bool is_fixed_array = false;
template <typename T, size_t Size>
constexpr bool is_fixed_array<std::array<T, Size>> = true;

template <typename T, typename = void>
constexpr bool is_std_array = false;
constexpr size_t fixed_array_size = 0;
template <typename T, size_t Size>
constexpr bool is_std_array<std::array<T, Size>> = true;
constexpr size_t fixed_array_size<std::array<T, Size>> = Size;

template <typename T, typename = void>
constexpr size_t std_array_size = 0;
template <typename T, size_t Size>
constexpr size_t std_array_size<std::array<T, Size>> = Size;
constexpr bool is_collection = false;
template <typename T>
constexpr bool is_collection<T> = is_container<T> && !is_map<T> && !is_fixed_array<T>;

template <typename T>
class has_to_json_in_member
Expand Down
36 changes: 22 additions & 14 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ class basic_value
{
}

template <
typename fixed_array_t,
std::enable_if_t<_utils::is_fixed_array<fixed_array_t>, bool> = true>
basic_value(fixed_array_t&& arr)
: basic_value(basic_array<string_t>(std::forward<fixed_array_t>(arr)))
{
}

template <
typename map_t,
std::enable_if_t<
Expand Down Expand Up @@ -182,7 +190,7 @@ class basic_value
template <typename value_t, template <typename...> typename collection_t = std::vector>
collection_t<value_t> as_collection() const;
template <typename value_t, size_t Size>
std::array<value_t, Size> as_collection() const;
std::array<value_t, Size> as_fixed_array() const;
template <typename value_t, template <typename...> typename map_t = std::map>
map_t<string_t, value_t> as_map() const;

Expand Down Expand Up @@ -281,7 +289,7 @@ class basic_value
template <typename value_t, size_t Size>
explicit operator std::array<value_t, Size>() const
{
return as_collection<value_t, Size>();
return as_fixed_array<value_t, Size>();
}

template <
Expand Down Expand Up @@ -499,13 +507,13 @@ inline bool basic_value<string_t>::is() const noexcept
else if constexpr (std::is_same_v<basic_array<string_t>, value_t>) {
return is_array();
}
else if constexpr (_utils::is_std_array<value_t>) {
return is_array() && all<typename value_t::value_type>()
&& as_array().size() == _utils::std_array_size<value_t>;
}
else if constexpr (_utils::is_collection<value_t>) {
return is_array() && all<typename value_t::value_type>();
}
else if constexpr (_utils::is_fixed_array<value_t>) {
return is_array() && all<typename value_t::value_type>()
&& as_array().size() == _utils::fixed_array_size<value_t>;
}
else if constexpr (std::is_same_v<basic_object<string_t>, value_t>) {
return is_object();
}
Expand Down Expand Up @@ -584,16 +592,16 @@ inline auto basic_value<string_t>::get_helper(
{
if constexpr (std::is_constructible_v<string_t, first_key_t>) {
return is_object() ? as_object().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else if constexpr (std::is_integral_v<std::decay_t<first_key_t>>) {
return is_array() ? as_array().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else {
Expand Down Expand Up @@ -936,9 +944,9 @@ inline collection_t<value_t> basic_value<string_t>::as_collection() const

template <typename string_t>
template <typename value_t, size_t Size>
inline std::array<value_t, Size> basic_value<string_t>::as_collection() const
inline std::array<value_t, Size> basic_value<string_t>::as_fixed_array() const
{
return as_array().template as_collection<value_t, Size>();
return as_array().template as_fixed_array<value_t, Size>();
}

template <typename string_t>
Expand Down
4 changes: 2 additions & 2 deletions test/serializing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ bool jsonizing()
}

std::array<int, 10> stdarr { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
json::value jarr = stdarr;
json::value jarr = json::array(stdarr);
std::array<int, 10> new_std_arr;
if (jarr.is<std::array<int, 10>>()) {
new_std_arr = jarr.as_collection<int, 10>();
new_std_arr = (std::array<int, 10>)jarr;
}
else {
std::cerr << "error std::array" << std::endl;
Expand Down

0 comments on commit 7aebc26

Please sign in to comment.