61 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#pragma once
 | 
						||
#include "type.h"
 | 
						||
namespace refl {
 | 
						||
	class UClass;
 | 
						||
	class FieldPtr;
 | 
						||
	//生命周期短,适用于传参,不建议保存数据
 | 
						||
	//只能指向指针,引用=>指针,指针=>指针,T => T*,T的类型丢失
 | 
						||
	struct Any {
 | 
						||
	public:
 | 
						||
		const void* ptr;
 | 
						||
		const UClass* cls;
 | 
						||
		constexpr Any(const void* ptr, const UClass* cls) : ptr(ptr), cls(cls) {
 | 
						||
			//assert(cls->flag & CLASS_POINTER_FLAG);
 | 
						||
		}
 | 
						||
		constexpr Any() : ptr(nullptr), cls(nullptr) {}
 | 
						||
		//右值=>右值压入栈,caller入栈地址
 | 
						||
		//左值=>caller变量地址
 | 
						||
		template<typename T>
 | 
						||
		constexpr Any(T&& v) : ptr(&v), cls(&TypeInfo<args_type_t<T>>::StaticClass) {
 | 
						||
			if constexpr (std::is_same_v<args_type_t<T>, Any>) {
 | 
						||
				ptr = v.ptr;
 | 
						||
				cls = v.cls;
 | 
						||
			}
 | 
						||
		}
 | 
						||
		template<typename T>
 | 
						||
		constexpr Any(T* v) : ptr(v), cls(&TypeInfo<args_type_t<T>>::StaticClass) {
 | 
						||
			if constexpr (std::is_same_v<args_type_t<T>, Any>) {
 | 
						||
				ptr = v->ptr;
 | 
						||
				cls = v->cls;
 | 
						||
			}
 | 
						||
		}
 | 
						||
		template<typename T>//参数 T* => T*
 | 
						||
		constexpr inline T CastTo() const {
 | 
						||
			if constexpr (std::is_pointer_v<T>) {
 | 
						||
				return (T)ptr;
 | 
						||
			}
 | 
						||
			else if constexpr (std::is_reference_v<T>) {
 | 
						||
				using RT = std::remove_reference_t<T>;
 | 
						||
				return *(RT*)ptr;
 | 
						||
			}
 | 
						||
			else {
 | 
						||
				return *(T*)ptr;
 | 
						||
			}
 | 
						||
		}
 | 
						||
		bool Check(const UClass* toClass)const;
 | 
						||
		constexpr bool IsValid() const{
 | 
						||
			return cls != nullptr && ptr != nullptr;
 | 
						||
		}
 | 
						||
		constexpr int Size()const;
 | 
						||
		constexpr const UClass* Parent()const;
 | 
						||
		constexpr Any Change(const void* ptr)const {
 | 
						||
			return {ptr, cls};
 | 
						||
		}
 | 
						||
		Any Member(const FieldPtr& field)const;
 | 
						||
		Any Member(int i)const;
 | 
						||
		int ArraySize()const;
 | 
						||
		bool IsArray()const;
 | 
						||
		bool IsObject()const;
 | 
						||
		bool Construct(const zstd::sarray<Any>& ArgsList)const;
 | 
						||
	};
 | 
						||
} |