zengine-old/engine/3rdparty/ylt/test/struct_pack.cpp
2024-03-28 22:10:55 +08:00

58 lines
2.1 KiB
C++

#include <ylt/struct_pack.hpp>
#include <iostream>
using namespace std;
using namespace struct_pack;
using UniversalVectorType = detail::UniversalVectorType;
using UniversalType = detail::UniversalType;
using UniversalOptionalType = detail::UniversalOptionalType;
using UniversalIntegralType = detail::UniversalIntegralType;
using UniversalNullptrType = detail::UniversalNullptrType;
using UniversalCompatibleType = detail::UniversalCompatibleType;
struct person {
int64_t id;
std::string name;
int age;
double salary;
};
template <typename T, typename construct_param_t, typename = void, typename... Args>
struct is_constructable_impl : std::false_type {};
template <typename T, typename construct_param_t, typename... Args>
struct is_constructable_impl < T, construct_param_t,
std::void_t<
decltype(T{ {Args{}}..., {construct_param_t{}} }) > , Args... >
: std::true_type {};
template <typename T, typename construct_param_t, typename... Args>
bool is_constructable = is_constructable_impl<T, construct_param_t, void, Args...>::value;
template <typename T, typename... Args>
std::size_t members_count_impl() {
if (is_constructable<T, UniversalVectorType, Args...>) {
cout << "is_constructable UniversalVectorType \n";
//return members_count_impl<T, Args..., UniversalVectorType>();
}
else if (is_constructable<T, UniversalType, Args...>) {
cout << "is_constructable UniversalType \n";
//return members_count_impl<T, UniversalType, Args...>();
}
else {
return sizeof...(Args);
}
return sizeof...(Args);
}
int main() {
person person1{ .id = 1, .name = "hello struct pack", .age = 20, .salary = 1024.42 };
using type = remove_cvref_t<decltype(person1)>;
using type2 = remove_cvref_t<type>;
auto Count = members_count<type>;
int isc = members_count_impl<type2>();
int c2 = detail::members_count<type>();
// 1行代码序列化
vector<char> buffer = serialize(person1);
// 只反序列化person的第2个字段
auto name = get_field<person, 1>(buffer.data(), buffer.size());
cout << "hello " << name.value() << "Count == " << Count << c2;
}