Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update reflection #203

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")

add_definitions(-DTHROW_UNKNOWN_KEY)

option(ENABLE_SEQUENTIAL_PARSE "parse json sequential more efficient if the json fields sequences are the same with struct fields" OFF)
if (${ENABLE_SEQUENTIAL_PARSE})
ADD_DEFINITIONS(-DSEQUENTIAL_PARSE)
endif ()

option(HAS_RAPIDJSON "import rapidjson" OFF)
if (${HAS_RAPIDJSON})
add_definitions(-DHAS_RAPIDJSON)
Expand Down
1 change: 0 additions & 1 deletion benchmark/json_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define SEQUENTIAL_PARSE
#include "json_benchmark.h"

class ScopedTimer {
Expand Down
4 changes: 2 additions & 2 deletions frozen/bits/pmh.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct seed_or_index {

private:
static constexpr value_type MINUS_ONE =
std::numeric_limits<value_type>::max();
(std::numeric_limits<value_type>::max)();
static constexpr value_type HIGH_BIT = ~(MINUS_ONE >> 1);

value_type value_ = 0;
Expand Down Expand Up @@ -199,7 +199,7 @@ pmh_tables<M, Hash> constexpr make_pmh_tables(const carray<Item, N> &items,
carray<seed_or_index, M> G; // Default constructed to "index 0"

// H becomes the second hash table in the resulting pmh function
constexpr std::size_t UNUSED = std::numeric_limits<std::size_t>::max();
constexpr std::size_t UNUSED = (std::numeric_limits<std::size_t>::max)();
carray<std::size_t, M> H;
H.fill(UNUSED);

Expand Down
12 changes: 6 additions & 6 deletions frozen/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ class linear_congruential_engine {
state_ = modulo(tmp, std::integral_constant<UIntType, modulus>());
return state_;
}
constexpr void discard(unsigned long long n) {
while (n--)
operator()();
}
static constexpr result_type min() { return increment == 0u ? 1u : 0u; }
static constexpr result_type max() { return modulus - 1u; }
// constexpr void discard(unsigned long long n) {
// while (n--)
// operator()();
// }
// static constexpr result_type min() { return increment == 0u ? 1u : 0u; }
// static constexpr result_type max() { return modulus - 1u; }
friend constexpr bool operator==(linear_congruential_engine const &self,
linear_congruential_engine const &other) {
return self.state_ == other.state_;
Expand Down
36 changes: 29 additions & 7 deletions iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
#include <variant>
#include <vector>

#include "frozen/string.h"
#include "frozen/unordered_map.h"

#include "detail/itoa.hpp"
#include "detail/string_stream.hpp"
#include "detail/traits.hpp"
#include "frozen/string.h"
#include "frozen/unordered_map.h"

namespace iguana::detail {
/******************************************/
Expand Down Expand Up @@ -560,9 +559,13 @@ namespace iguana::detail {
} \
using size_type = \
std::integral_constant<size_t, GET_ARG_COUNT(__VA_ARGS__)>; \
constexpr static std::string_view name() { \
constexpr static std::string_view name() { return name_##STRUCT_NAME; } \
constexpr static std::string_view struct_name() { \
return std::string_view(#STRUCT_NAME, sizeof(#STRUCT_NAME) - 1); \
} \
constexpr static std::string_view fields() { \
return fields_##STRUCT_NAME; \
} \
constexpr static size_t value() { return size_type::value; } \
constexpr static std::array<frozen::string, size_type::value> arr() { \
return arr_##STRUCT_NAME; \
Expand All @@ -571,9 +574,12 @@ namespace iguana::detail {
return reflect_members{}; \
}

#define MAKE_META_DATA(STRUCT_NAME, N, ...) \
#define MAKE_META_DATA(STRUCT_NAME, TABLE_NAME, N, ...) \
constexpr inline std::array<frozen::string, N> arr_##STRUCT_NAME = { \
MARCO_EXPAND(MACRO_CONCAT(CON_STR, N)(__VA_ARGS__))}; \
constexpr inline std::string_view fields_##STRUCT_NAME = { \
MAKE_NAMES(__VA_ARGS__)}; \
constexpr inline std::string_view name_##STRUCT_NAME = TABLE_NAME; \
MAKE_META_DATA_IMPL(STRUCT_NAME, \
MAKE_ARG_LIST(N, &STRUCT_NAME::FIELD, __VA_ARGS__))

Expand Down Expand Up @@ -623,6 +629,10 @@ get_iguana_struct_map_impl(const std::array<frozen::string, sizeof...(Is)> &arr,
} // namespace iguana::detail

namespace iguana {
#define REFLECTION_WITH_NAME(STRUCT_NAME, TABLE_NAME, ...) \
MAKE_META_DATA(STRUCT_NAME, TABLE_NAME, GET_ARG_COUNT(__VA_ARGS__), \
__VA_ARGS__)

inline std::unordered_map<
std::string_view,
std::vector<std::pair<std::string_view, std::string_view>>>
Expand All @@ -639,7 +649,8 @@ template <typename T> inline constexpr auto get_iguana_struct_map() {
}

#define REFLECTION(STRUCT_NAME, ...) \
MAKE_META_DATA(STRUCT_NAME, GET_ARG_COUNT(__VA_ARGS__), __VA_ARGS__)
MAKE_META_DATA(STRUCT_NAME, #STRUCT_NAME, GET_ARG_COUNT(__VA_ARGS__), \
__VA_ARGS__)

#define REFLECTION_EMPTY(STRUCT_NAME) MAKE_META_DATA_EMPTY(STRUCT_NAME)

Expand Down Expand Up @@ -700,6 +711,12 @@ inline int add_custom_fields(std::string_view key,
return 0;
}

#ifdef _MSC_VER
#define IGUANA_UNIQUE_VARIABLE(str) MACRO_CONCAT(str, __COUNTER__)
#else
#define IGUANA_UNIQUE_VARIABLE(str) MACRO_CONCAT(str, __LINE__)
#endif

#define CUSTOM_FIELDS_IMPL(STRUCT_NAME, N, ...) \
inline auto IGUANA_UNIQUE_VARIABLE(STRUCT_NAME) = iguana::add_custom_fields( \
#STRUCT_NAME, {MARCO_EXPAND(MACRO_CONCAT(CON_STR, N)(__VA_ARGS__))});
Expand Down Expand Up @@ -798,6 +815,11 @@ template <typename T> constexpr const std::string_view get_name() {
return M::name();
}

template <typename T> constexpr const std::string_view get_fields() {
using M = Reflect_members<T>;
return M::fields();
}

template <typename T>
constexpr std::enable_if_t<is_reflection<T>::value, size_t> get_value() {
using M = decltype(iguana_reflect_members(std::declval<T>()));
Expand Down Expand Up @@ -891,4 +913,4 @@ constexpr std::enable_if_t<is_tuple<std::decay_t<T>>::value> for_each(T &&t,
std::make_index_sequence<SIZE>{});
}
} // namespace iguana
#endif // IGUANA_REFLECTION_HPP
#endif // IGUANA_REFLECTION_HPP
Loading