zengine-old/engine/3rdparty/zlib/include/refl/detail/any.h

67 lines
1.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "type.h"
namespace refl {
class UClass;
class Container;
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 operator bool() 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;
Container ToContainer()const;
int ArraySize()const;
bool IsArray()const;
bool IsObject()const;
bool IsContainer()const;
bool IsSequence()const;
bool IsMap()const;
bool Construct(const zstd::sarray<Any>& ArgsList)const;
void Destruct()const;
};
}