Skip to content

Commit

Permalink
[struct_pack][fix] fix windows ci, fix compatible in windows (#771)
Browse files Browse the repository at this point in the history
* fix msvc

* fix OOM in msvc

* fix

* fix ce in vs2019
  • Loading branch information
poor-circle authored Sep 12, 2024
1 parent 8d0ad68 commit b4eb280
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 101 deletions.
4 changes: 0 additions & 4 deletions cmake/subdir.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ endif()
foreach(child ${children})
get_filename_component(subdir_name ${child} NAME)
string(TOUPPER ${subdir_name} subdir_name)
if((${subdir_name} STREQUAL "STRUCT_PACK" OR ${subdir_name} STREQUAL "STRUCT_PB") AND (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC"))
message(STATUS "skip ${subdir_name}")
continue()
endif()

if (BUILD_${subdir_name})
message(STATUS "BUILD_${subdir_name}: ${BUILD_${subdir_name}}")
Expand Down
20 changes: 9 additions & 11 deletions include/ylt/reflection/member_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#endif

#include "user_reflect_macro.hpp"

namespace struct_pack {
template <typename T, uint64_t version>
struct compatible;
}
namespace ylt::reflection {
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
Expand All @@ -25,9 +28,7 @@ concept expected = requires(Type e) {
e.has_value();
e.error();
requires std::is_same_v<void, typename remove_cvref_t<Type>::value_type> ||
requires(Type e) {
e.value();
};
requires(Type e) { e.value(); };
};
#else
template <typename T, typename = void>
Expand Down Expand Up @@ -71,9 +72,8 @@ constexpr bool optional = !expected<T> && optional_impl<T>::value;
namespace internal {
#if __cpp_concepts >= 201907L
template <typename Type>
concept tuple_size = requires(Type tuple) {
std::tuple_size<remove_cvref_t<Type>>::value;
};
concept tuple_size =
requires(Type tuple) { std::tuple_size<remove_cvref_t<Type>>::value; };
#else
template <typename T, typename = void>
struct tuple_size_impl : std::false_type {};
Expand All @@ -87,14 +87,12 @@ template <typename T>
constexpr bool tuple_size = tuple_size_impl<T>::value;
#endif

template <typename T, uint64_t version = 0>
struct compatible;

template <typename Type>
constexpr inline bool is_compatible_v = false;

template <typename Type, uint64_t version>
constexpr inline bool is_compatible_v<compatible<Type, version>> = true;
constexpr inline bool is_compatible_v<struct_pack::compatible<Type, version>> =
true;

struct UniversalVectorType {
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/reflection/member_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// modified based on:
// https://github.com/getml/reflect-cpp/blob/main/include/rfl/internal/bind_fake_object_to_tuple.hpp
// thanks for alxn4's greate idea!
// thanks for alxn4's great idea!
namespace ylt::reflection {
namespace internal {

Expand Down
22 changes: 22 additions & 0 deletions include/ylt/struct_pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ template <
typename = std::enable_if_t<struct_pack::detail::deserialize_view<View>>>
#endif
[[nodiscard]] auto deserialize(const View &v) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to(ret.value(), v);
if SP_UNLIKELY (errc) {
Expand All @@ -408,6 +410,8 @@ template <

template <typename... Args>
[[nodiscard]] auto deserialize(const char *data, size_t size) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
if (auto errc = deserialize_to(ret.value(), data, size); errc) {
ret = unexpected<struct_pack::err_code>{errc};
Expand All @@ -421,6 +425,8 @@ template <typename... Args, typename Reader,
typename = std::enable_if_t<struct_pack::reader_t<Reader>>>
#endif
[[nodiscard]] auto deserialize(Reader &v) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to(ret.value(), v);
if SP_UNLIKELY (errc) {
Expand All @@ -435,6 +441,8 @@ template <typename... Args, struct_pack::detail::deserialize_view View>
template <typename... Args, typename View>
#endif
[[nodiscard]] auto deserialize(const View &v, size_t &consume_len) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to(ret.value(), v, consume_len);
if SP_UNLIKELY (errc) {
Expand All @@ -446,6 +454,8 @@ template <typename... Args, typename View>
template <typename... Args>
[[nodiscard]] auto deserialize(const char *data, size_t size,
size_t &consume_len) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to(ret.value(), data, size, consume_len);
if SP_UNLIKELY (errc) {
Expand All @@ -463,6 +473,8 @@ template <
typename = std::enable_if_t<struct_pack::detail::deserialize_view<View>>>
#endif
[[nodiscard]] auto deserialize(const View &v) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to<conf>(ret.value(), v);
if SP_UNLIKELY (errc) {
Expand All @@ -487,6 +499,8 @@ template <uint64_t conf, typename... Args, typename Reader,
typename = std::enable_if_t<struct_pack::reader_t<Reader>>>
#endif
[[nodiscard]] auto deserialize(Reader &v) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to<conf>(ret.value(), v);
if SP_UNLIKELY (errc) {
Expand All @@ -502,6 +516,8 @@ template <uint64_t conf, typename... Args,
template <uint64_t conf, typename... Args, typename View>
#endif
[[nodiscard]] auto deserialize(const View &v, size_t &consume_len) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to<conf>(ret.value(), v, consume_len);
if SP_UNLIKELY (errc) {
Expand All @@ -513,6 +529,8 @@ template <uint64_t conf, typename... Args, typename View>
template <uint64_t conf, typename... Args>
[[nodiscard]] auto deserialize(const char *data, size_t size,
size_t &consume_len) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to<conf>(ret.value(), data, size, consume_len);
if SP_UNLIKELY (errc) {
Expand All @@ -527,6 +545,8 @@ template <typename... Args, struct_pack::detail::deserialize_view View>
template <typename... Args, typename View>
#endif
[[nodiscard]] auto deserialize_with_offset(const View &v, size_t &offset) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to_with_offset(ret.value(), v, offset);
if SP_UNLIKELY (errc) {
Expand All @@ -538,6 +558,8 @@ template <typename... Args, typename View>
template <typename... Args>
[[nodiscard]] auto deserialize_with_offset(const char *data, size_t size,
size_t &offset) {
static_assert(sizeof...(Args) > 0,
"the correct code is struct_pack::deserialize<Type...>();");
expected<detail::get_args_type<Args...>, struct_pack::err_code> ret;
auto errc = deserialize_to_with_offset(ret.value(), data, size, offset);
if SP_UNLIKELY (errc) {
Expand Down
16 changes: 12 additions & 4 deletions src/struct_pack/tests/test_many_members.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "doctest.h"
#include "ylt/struct_pack.hpp"
#include "ylt/struct_pack/type_calculate.hpp"
// struct_pack support 255 member max
struct many_members {
int a1;
std::string b1;
Expand Down Expand Up @@ -128,6 +129,8 @@ struct many_members {
std::string b61;
int a62;
std::string b62;
#ifndef _MSC_VER
// too many variable may cause msvc OOM, disable least variables
int a63;
std::string b63;
int a64;
Expand Down Expand Up @@ -259,6 +262,7 @@ struct many_members {
int a127;
std::string b127;
int a128;
#endif
};
struct many_members2 : public many_members {};
STRUCT_PACK_REFL(many_members2, a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6,
Expand All @@ -270,9 +274,13 @@ STRUCT_PACK_REFL(many_members2, a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6,
a38, b38, a39, b39, a40, b40, a41, b41, a42, b42, a43, b43,
a44, b44, a45, b45, a46, b46, a47, b47, a48, b48, a49, b49,
a50, b50, a51, b51, a52, b52, a53, b53, a54, b54, a55, b55,
a56, b56, a57, b57, a58, b58, a59, b59, a60, b60, a61, b61,
a62, b62);
a56, b56, a57, b57, a58, b58, a59, b59, a60, b60);
TEST_CASE("test many members") {
CHECK(struct_pack::get_type_literal<many_members>().size() == 384);
CHECK(struct_pack::get_type_literal<many_members2>().size() == 188);
int size = 384;

#ifdef _MSC_VER
size = 188;
#endif
CHECK(struct_pack::get_type_literal<many_members>().size() == size);
CHECK(struct_pack::get_type_literal<many_members2>().size() == 182);
}
14 changes: 0 additions & 14 deletions test.cpp

This file was deleted.

67 changes: 0 additions & 67 deletions test_install.sh

This file was deleted.

0 comments on commit b4eb280

Please sign in to comment.