80 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			80 lines
		
	
	
		
			2.5 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;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								获取对象成员数量
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								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>
							 | 
						||
| 
								 | 
							
								constexpr bool is_constructable = is_constructable_impl<T, construct_param_t, void, Args...>::value;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								template <typename T, typename... Args>
							 | 
						||
| 
								 | 
							
								constexpr std::size_t members_count_impl() {
							 | 
						||
| 
								 | 
							
								    if constexpr (is_constructable<T, UniversalVectorType, Args...>) {
							 | 
						||
| 
								 | 
							
								        return members_count_impl<T, Args..., UniversalVectorType>();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    else if constexpr (is_constructable<T, UniversalType, Args...>) {
							 | 
						||
| 
								 | 
							
								        cout << "is_constructable UniversalType \n";
							 | 
						||
| 
								 | 
							
								        return members_count_impl<T, Args..., UniversalType>();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    return sizeof...(Args);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 获取成员类型
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								template <typename Type>
							 | 
						||
| 
								 | 
							
								concept type_string = requires(Type container) {
							 | 
						||
| 
								 | 
							
								    requires detail::is_char_t<typename std::remove_cvref_t<Type>::value_type>;
							 | 
						||
| 
								 | 
							
								container.length();
							 | 
						||
| 
								 | 
							
								container.data();
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								template <typename Type>
							 | 
						||
| 
								 | 
							
								concept type_struct = requires(Type container) {
							 | 
						||
| 
								 | 
							
								    requires std::is_class_v<Type>;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								template <typename Type>
							 | 
						||
| 
								 | 
							
								concept type_u32 = requires(Type container) {
							 | 
						||
| 
								 | 
							
								    requires sizeof(Type) == 4;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								获取成员信息
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								template <typename T>
							 | 
						||
| 
								 | 
							
								constexpr void visit_one_member_info(T& item) {
							 | 
						||
| 
								 | 
							
								    constexpr auto id = detail::get_type_id<T, 0>();
							 | 
						||
| 
								 | 
							
								    cout << "visit_one_member_info::" << (int)id << "=" << item << "\n";
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								template <typename... Args>
							 | 
						||
| 
								 | 
							
								void visit_member_info(Args&...args) {
							 | 
						||
| 
								 | 
							
								    cout << "visit_member_info::" << sizeof...(args) << "\n";
							 | 
						||
| 
								 | 
							
								    (visit_one_member_info(args), ...);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								template <typename T>
							 | 
						||
| 
								 | 
							
								void visit_struct_info(T& item) {
							 | 
						||
| 
								 | 
							
								    int count = members_count_impl<T>();
							 | 
						||
| 
								 | 
							
								    auto visitor = [](auto&&... items) {
							 | 
						||
| 
								 | 
							
								        visit_member_info<>(items...);
							 | 
						||
| 
								 | 
							
								    };
							 | 
						||
| 
								 | 
							
								    if (count == 4) {
							 | 
						||
| 
								 | 
							
								        auto&& [a1, a2, a3, a4] = item;
							 | 
						||
| 
								 | 
							
								        visitor(a1, a2, a3, a4);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |