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

[struct_pack][feat] add support for user_defined type id #453

Merged
merged 4 commits into from
Sep 14, 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
4 changes: 2 additions & 2 deletions include/ylt/struct_pack/md5_constexpr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ struct string_literal {

constexpr bool empty() const { return !Size; }

constexpr char &operator[](std::size_t sz) { return ar[sz]; }
constexpr CharType &operator[](std::size_t sz) { return ar[sz]; }

constexpr const char &operator[](std::size_t sz) const { return ar[sz]; }

constexpr const char *data() const { return &ar[0]; }
constexpr const CharType *data() const { return &ar[0]; }

private:
CharType ar[Size + 1];
Expand Down
27 changes: 27 additions & 0 deletions include/ylt/struct_pack/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
#endif
}

template <typename T>
[[noreturn]] constexpr T declval() {
unreachable();
}

template <typename U>
constexpr auto get_types();

Expand Down Expand Up @@ -84,6 +89,28 @@ constexpr std::size_t alignment_v = 0;

#if __cpp_concepts >= 201907L

template <typename T>
concept has_user_defined_id = requires {
typename std::integral_constant<std::size_t, T::struct_pack_id>;
};

#else

template <typename T, typename = void>
struct has_user_defined_id_impl : std::false_type {};

template <typename T>
struct has_user_defined_id_impl<
T, std::void_t<std::integral_constant<std::size_t, T::struct_pack_id>>>
: std::true_type {};

template <typename T>
constexpr bool has_user_defined_id = has_user_defined_id_impl<T>::value;

#endif

#if __cpp_concepts >= 201907L

template <typename T>
concept writer_t = requires(T t) {
t.write((const char *)nullptr, std::size_t{});
Expand Down
62 changes: 32 additions & 30 deletions include/ylt/struct_pack/struct_pack_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,6 @@ template <typename... T>
constexpr inline bool is_trivial_tuple<tuplet::tuple<T...>> = true;
#endif

template <typename T>
[[noreturn]] constexpr T declval() {
unreachable();
}

template <typename U>
constexpr auto get_types() {
using T = remove_cvref_t<U>;
Expand Down Expand Up @@ -347,6 +342,8 @@ enum class type_id {
monostate_t = 250,
// circle_flag
circle_flag = 251,
// end helper with user defined type ID
type_end_flag_with_id = 252,
trivial_class_t = 253,
// struct type
non_trivial_class_t = 254,
Expand Down Expand Up @@ -915,11 +912,18 @@ constexpr void calculate_trival_obj_size<P, T, I>::operator()(

} // namespace align

// This help function is just to improve unit test coverage. :)
// The original lambada in `get_type_literal` is a compile-time expression.
// Currently, the unit test coverage tools like
// [Coverage](https://clang.llvm.org/docs/SourceBasedCodeCoverage.html)
// can not detect code that is run at compile time.
template <typename Arg>
constexpr decltype(auto) get_type_end_flag() {
if constexpr (has_user_defined_id<Arg>) {
return string_literal<char, 1>{
{static_cast<char>(type_id::type_end_flag_with_id)}} +
get_size_literal<Arg::struct_pack_id>();
}
else {
return string_literal<char, 1>{{static_cast<char>(type_id::type_end_flag)}};
}
}

template <typename Args, typename... ParentArgs, std::size_t... I>
constexpr decltype(auto) get_type_literal(std::index_sequence<I...>);

Expand All @@ -941,26 +945,24 @@ constexpr decltype(auto) get_type_literal() {
}
else {
constexpr auto id = get_type_id<Arg>();
constexpr auto ret = string_literal<char, 1>{{static_cast<char>(id)}};
constexpr auto begin = string_literal<char, 1>{{static_cast<char>(id)}};
if constexpr (id == type_id::non_trivial_class_t ||
id == type_id::trivial_class_t) {
using Args = decltype(get_types<Arg>());
constexpr auto end = get_type_end_flag<Arg>();
constexpr auto body = get_type_literal<Args, Arg, ParentArgs...>(
std::make_index_sequence<std::tuple_size_v<Args>>());
if constexpr (is_trivial_serializable<Arg, true>::value) {
static_assert(
align::pack_alignment_v<Arg> <= align::alignment_v<Arg>,
"If you add #pragma_pack to a struct, please specify the "
"struct_pack::pack_alignment_v<T>.");
constexpr auto end = string_literal<char, 1>{
{static_cast<char>(type_id::type_end_flag)}};
return ret + body + get_size_literal<align::pack_alignment_v<Arg>>() +
return begin + body +
get_size_literal<align::pack_alignment_v<Arg>>() +
get_size_literal<align::alignment_v<Arg>>() + end;
}
else {
constexpr auto end = string_literal<char, 1>{
{static_cast<char>(type_id::type_end_flag)}};
return ret + body + end;
return begin + body + end;
}
}
else if constexpr (id == type_id::variant_t) {
Expand All @@ -971,12 +973,12 @@ constexpr decltype(auto) get_type_literal() {
std::make_index_sequence<std::variant_size_v<Arg>>());
constexpr auto end = string_literal<char, 1>{
{static_cast<char>(type_id::type_end_flag)}};
return ret + body + end;
return begin + body + end;
}
else if constexpr (id == type_id::array_t) {
constexpr auto sz = get_array_size<Arg>();
static_assert(sz > 0, "The array's size must greater than zero!");
return ret +
return begin +
get_type_literal<
remove_cvref_t<decltype(std::declval<Arg>()[0])>, Arg,
ParentArgs...>() +
Expand All @@ -985,38 +987,39 @@ constexpr decltype(auto) get_type_literal() {
else if constexpr (id == type_id::bitset_t) {
constexpr auto sz = get_array_size<Arg>();
static_assert(sz > 0, "The array's size must greater than zero!");
return ret + get_size_literal<sz>();
return begin + get_size_literal<sz>();
}
else if constexpr (unique_ptr<Arg>) {
return ret +
return begin +
get_type_literal<remove_cvref_t<typename Arg::element_type>, Arg,
ParentArgs...>();
}
else if constexpr (id == type_id::container_t ||
id == type_id::optional_t || id == type_id::string_t) {
return ret + get_type_literal<remove_cvref_t<typename Arg::value_type>,
Arg, ParentArgs...>();
return begin +
get_type_literal<remove_cvref_t<typename Arg::value_type>, Arg,
ParentArgs...>();
}
else if constexpr (id == type_id::set_container_t) {
return ret + get_type_literal<remove_cvref_t<typename Arg::key_type>,
Arg, ParentArgs...>();
return begin + get_type_literal<remove_cvref_t<typename Arg::key_type>,
Arg, ParentArgs...>();
}
else if constexpr (id == type_id::map_container_t) {
return ret +
return begin +
get_type_literal<remove_cvref_t<typename Arg::key_type>, Arg,
ParentArgs...>() +
get_type_literal<remove_cvref_t<typename Arg::mapped_type>, Arg,
ParentArgs...>();
}
else if constexpr (id == type_id::expected_t) {
return ret +
return begin +
get_type_literal<remove_cvref_t<typename Arg::value_type>, Arg,
ParentArgs...>() +
get_type_literal<remove_cvref_t<typename Arg::error_type>, Arg,
ParentArgs...>();
}
else if constexpr (id != type_id::compatible_t) {
return ret;
return begin;
}
else {
return string_literal<char, 0>{};
Expand Down Expand Up @@ -1054,10 +1057,9 @@ constexpr decltype(auto) get_types_literal() {
}
else {
constexpr auto root_id = get_type_id<remove_cvref_t<T>>();
constexpr auto end =
string_literal<char, 1>{{static_cast<char>(type_id::type_end_flag)}};
if constexpr (root_id == type_id::non_trivial_class_t ||
root_id == type_id::trivial_class_t) {
constexpr auto end = get_type_end_flag<remove_cvref_t<T>>();
constexpr auto begin =
string_literal<char, 1>{{static_cast<char>(root_id)}};
constexpr auto body = get_types_literal_impl<T, Args...>();
Expand Down
82 changes: 82 additions & 0 deletions src/struct_pack/tests/test_compile_time_calculate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <iostream>
#include <memory>
#include <type_traits>
#include <ylt/struct_pack.hpp>

#include "doctest.h"
Expand Down Expand Up @@ -347,3 +349,83 @@ TEST_CASE("type calculate") {
serialize(std::tuple<std::tuple<int, int>>{})));
}
}

struct foo_trivial {
int a;
double b;
};

struct foo_trivial_with_ID {
int a;
double b;
constexpr static std::size_t struct_pack_id = 1;
};
struct foo_trivial_with_ID2 {
int a;
double b;
constexpr static int struct_pack_id = 2;
};
struct bar_trivial_with_ID2 {
uint32_t a;
double b;
constexpr static int struct_pack_id = 2;
};

struct foo {
std::vector<int> a;
std::list<foo> b;
};
struct bar;
struct bar {
std::vector<int> a;
std::vector<bar> b;
};

struct foo_with_ID {
std::vector<int> a;
std::list<foo_with_ID> b;
constexpr static std::size_t struct_pack_id = 0;
};

struct bar_with_ID {
std::vector<int> a;
std::map<int, bar_with_ID> b;
constexpr static std::size_t struct_pack_id = 0;
};

struct foo_with_ID1 {
std::vector<int> a;
std::list<foo_with_ID1> b;
constexpr static std::size_t struct_pack_id = 1;
};

struct bar_with_ID1 {
std::vector<int> a;
std::vector<bar_with_ID1> b;
constexpr static std::size_t struct_pack_id = 1;
};

TEST_CASE("test user defined ID") {
{
static_assert(has_user_defined_id<foo_trivial_with_ID>);
static_assert(struct_pack::get_type_literal<foo_trivial>() !=
struct_pack::get_type_literal<foo_trivial_with_ID>());
static_assert(struct_pack::get_type_literal<foo_trivial_with_ID2>() !=
struct_pack::get_type_literal<foo_trivial_with_ID>());
static_assert(struct_pack::get_type_literal<foo_trivial_with_ID2>() !=
struct_pack::get_type_literal<bar_trivial_with_ID2>());
}
{
static_assert(has_user_defined_id<foo_with_ID>);
static_assert(struct_pack::get_type_literal<foo>() !=
struct_pack::get_type_literal<foo_with_ID>());
static_assert(struct_pack::get_type_literal<foo_with_ID>() !=
struct_pack::get_type_literal<foo_with_ID1>());
auto a = struct_pack::get_type_literal<foo_with_ID1>();
auto b = struct_pack::get_type_literal<bar_with_ID1>();
static_assert(struct_pack::get_type_literal<foo_with_ID1>() ==
struct_pack::get_type_literal<bar_with_ID1>());
static_assert(struct_pack::get_type_literal<foo_with_ID>() !=
struct_pack::get_type_literal<bar_with_ID>());
}
}
Loading