@@ -72,9 +72,12 @@ namespace Mirror {
72
72
const TypeInfo* removePointer;
73
73
};
74
74
75
+ class Any ;
76
+ class Class ;
77
+
75
78
MIRROR_API bool PointerConvertible (const TypeInfoCompact& inSrcType, const TypeInfoCompact& inDstType);
76
- MIRROR_API bool PolymorphismConvertible (const TypeInfoCompact& inSrcType, const TypeInfoCompact& inDstType);
77
- MIRROR_API bool Convertible (const TypeInfoCompact& inSrcType, const TypeInfoCompact& inDstType);
79
+ MIRROR_API bool PolymorphismConvertible (const TypeInfoCompact& inSrcType, const TypeInfoCompact& inDstType, const Class* inSrcDynamicClass );
80
+ MIRROR_API bool Convertible (const TypeInfoCompact& inSrcType, const TypeInfoCompact& inDstType, const Class* inSrcDynamicClass );
78
81
79
82
enum class AnyPolicy : uint8_t {
80
83
memoryHolder,
@@ -83,8 +86,6 @@ namespace Mirror {
83
86
max
84
87
};
85
88
86
- class Any ;
87
-
88
89
using TemplateViewId = uint32_t ;
89
90
using TemplateViewRttiPtr = const void *;
90
91
@@ -105,6 +106,7 @@ namespace Mirror {
105
106
using JsonDeserializeFunc = void (void *, const rapidjson::Value&);
106
107
using ToStringFunc = std::string(const void *);
107
108
using GetTemplateViewRttiFunc = std::pair<TemplateViewId, TemplateViewRttiPtr>();
109
+ using GetDynamicClassFunc = const Class*(const void *);
108
110
109
111
template <typename T> static void Detor (void * inThis) noexcept ;
110
112
template <typename T> static void CopyConstruct (void * inThis, const void * inOther);
@@ -128,6 +130,7 @@ namespace Mirror {
128
130
template <typename T> static void JsonDeserialize (void * inThis, const rapidjson::Value& inJsonValue);
129
131
template <typename T> static std::string ToString (const void * inThis);
130
132
template <typename T> static std::pair<TemplateViewId, TemplateViewRttiPtr> GetTemplateViewRtti ();
133
+ template <typename T> static const Class* GetDynamicClass (const void * inThis);
131
134
132
135
DetorFunc* detor;
133
136
CopyConstructFunc* copyConstruct;
@@ -151,6 +154,7 @@ namespace Mirror {
151
154
JsonDeserializeFunc* jsonDeserialize;
152
155
ToStringFunc* toString;
153
156
GetTemplateViewRttiFunc* getTemplateViewRtti;
157
+ GetDynamicClassFunc* getDynamicClass;
154
158
};
155
159
156
160
template <typename T, size_t N = 1 >
@@ -176,7 +180,8 @@ namespace Mirror {
176
180
&AnyRtti::JsonSerialize<T>,
177
181
&AnyRtti::JsonDeserialize<T>,
178
182
&AnyRtti::ToString<T>,
179
- &AnyRtti::GetTemplateViewRtti<T>
183
+ &AnyRtti::GetTemplateViewRtti<T>,
184
+ &AnyRtti::GetDynamicClass<T>
180
185
};
181
186
182
187
template <typename T>
@@ -245,8 +250,6 @@ namespace Mirror {
245
250
template <typename T> T As () const ;
246
251
template <Common::CppNotRef T> T* TryAs ();
247
252
template <Common::CppNotRef T> T* TryAs () const ;
248
- template <typename B, typename T> T PolyAs ();
249
- template <typename B, typename T> T PolyAs () const ;
250
253
251
254
template <ValidTemplateView V> bool CanAsTemplateView () const ;
252
255
TemplateViewRttiPtr GetTemplateViewRtti () const ;
@@ -280,6 +283,7 @@ namespace Mirror {
280
283
const TypeInfo* RemovePointerType () const ;
281
284
Mirror::TypeId TypeId ();
282
285
Mirror::TypeId TypeId () const ;
286
+ const Class* GetDynamicClass () const ;
283
287
void Reset ();
284
288
bool Empty () const ;
285
289
size_t Serialize (Common::BinarySerializeStream& inStream) const ;
@@ -366,6 +370,7 @@ namespace Mirror {
366
370
Argument& operator =(Any&& inAny);
367
371
368
372
template <typename T> T As () const ;
373
+ template <Common::CppNotRef T> T* TryAs () const ;
369
374
370
375
template <ValidTemplateView V> bool CanAsTemplateView () const ;
371
376
TemplateViewRttiPtr GetTemplateViewRtti () const ;
@@ -378,6 +383,7 @@ namespace Mirror {
378
383
const TypeInfo* RemoveRefType () const ;
379
384
const TypeInfo* AddPointerType () const ;
380
385
const TypeInfo* RemovePointerType () const ;
386
+ const Class* GetDynamicClass () const ;
381
387
382
388
private:
383
389
template <typename F> decltype (auto ) Delegate(F&& inFunc) const ;
@@ -822,12 +828,12 @@ namespace Mirror {
822
828
const MemberFunction& GetMemberFunction (const Id& inId) const ;
823
829
Any GetDefaultObject () const ;
824
830
bool IsTransient () const ;
825
-
826
831
Any ConstructDyn (const ArgumentList& arguments) const ;
827
832
Any NewDyn (const ArgumentList& arguments) const ;
828
833
Any InplaceNewDyn (void * ptr, const ArgumentList& arguments) const ;
829
834
void DestructDyn (const Argument& argument) const ;
830
835
void DeleteDyn (const Argument& argument) const ;
836
+ Any Cast (const Argument& objPtrOrRef) const ;
831
837
832
838
private:
833
839
static std::unordered_map<TypeId, Id> typeToIdMap;
@@ -837,14 +843,17 @@ namespace Mirror {
837
843
838
844
using BaseClassGetter = std::function<const Class*()>;
839
845
using InplaceGetter = std::function<Any(void *)>;
846
+ using DefaultObjectCreator = std::function<Any()>;
847
+ using Caster = std::function<Any(const Mirror::Argument&)>;
840
848
841
849
struct ConstructParams {
842
850
Id id;
843
851
const TypeInfo* typeInfo;
844
852
size_t memorySize;
845
853
BaseClassGetter baseClassGetter;
846
854
InplaceGetter inplaceGetter;
847
- std::function<Any()> defaultObjectCreator;
855
+ Caster caster;
856
+ DefaultObjectCreator defaultObjectCreator;
848
857
std::optional<Destructor::ConstructParams> destructorParams;
849
858
std::optional<Constructor::ConstructParams> defaultConstructorParams;
850
859
std::optional<Constructor::ConstructParams> moveConstructorParams;
@@ -853,7 +862,7 @@ namespace Mirror {
853
862
854
863
explicit Class (ConstructParams&& params);
855
864
856
- void CreateDefaultObject (const std::function<Any()> & inCreator);
865
+ void CreateDefaultObject (const DefaultObjectCreator & inCreator);
857
866
Destructor& EmplaceDestructor (Destructor::ConstructParams&& inParams);
858
867
Constructor& EmplaceConstructor (const Id& inId, Constructor::ConstructParams&& inParams);
859
868
Variable& EmplaceStaticVariable (const Id& inId, Variable::ConstructParams&& inParams);
@@ -865,6 +874,7 @@ namespace Mirror {
865
874
size_t memorySize;
866
875
BaseClassGetter baseClassGetter;
867
876
InplaceGetter inplaceGetter;
877
+ Caster caster;
868
878
Any defaultObject;
869
879
std::optional<Destructor> destructor;
870
880
std::unordered_map<Id, Constructor, IdHashProvider> constructors;
@@ -2882,8 +2892,25 @@ namespace Mirror {
2882
2892
{
2883
2893
return {
2884
2894
TemplateViewRttiGetter<T>::Id (),
2885
- TemplateViewRttiGetter<T>::Get ()
2886
- };
2895
+ TemplateViewRttiGetter<T>::Get ()};
2896
+ }
2897
+
2898
+ template <typename T>
2899
+ const Class* AnyRtti::GetDynamicClass (const void * inThis)
2900
+ {
2901
+ if constexpr (std::is_pointer_v<T>) {
2902
+ if constexpr (MetaClass<std::decay_t <std::remove_pointer_t <T>>>) {
2903
+ return &(*static_cast <const T*>(inThis))->GetClass ();
2904
+ } else {
2905
+ return nullptr ;
2906
+ }
2907
+ } else {
2908
+ if constexpr (MetaClass<T>) {
2909
+ return &static_cast <const T*>(inThis)->GetClass ();
2910
+ } else {
2911
+ return nullptr ;
2912
+ }
2913
+ }
2887
2914
}
2888
2915
2889
2916
template <typename T>
@@ -3030,7 +3057,8 @@ namespace Mirror {
3030
3057
Assert (!IsArray ());
3031
3058
return Mirror::Convertible (
3032
3059
{ Type (), RemoveRefType (), RemovePointerType () },
3033
- { GetTypeInfo<T>(), GetTypeInfo<std::remove_reference_t <T>>(), GetTypeInfo<std::remove_pointer_t <T>>() });
3060
+ { GetTypeInfo<T>(), GetTypeInfo<std::remove_reference_t <T>>(), GetTypeInfo<std::remove_pointer_t <T>>() },
3061
+ rtti->getDynamicClass (Data ()));
3034
3062
}
3035
3063
3036
3064
template <typename T>
@@ -3039,7 +3067,8 @@ namespace Mirror {
3039
3067
Assert (!IsArray ());
3040
3068
return Mirror::Convertible (
3041
3069
{ Type (), RemoveRefType (), RemovePointerType () },
3042
- { GetTypeInfo<T>(), GetTypeInfo<std::remove_reference_t <T>>(), GetTypeInfo<std::remove_pointer_t <T>>() });
3070
+ { GetTypeInfo<T>(), GetTypeInfo<std::remove_reference_t <T>>(), GetTypeInfo<std::remove_pointer_t <T>>() },
3071
+ rtti->getDynamicClass (Data ()));
3043
3072
}
3044
3073
3045
3074
template <typename T>
@@ -3062,7 +3091,8 @@ namespace Mirror {
3062
3091
Assert (!IsArray ());
3063
3092
const bool convertible = Mirror::Convertible (
3064
3093
{ AddPointerType (), AddPointerType (), RemoveRefType () },
3065
- { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() });
3094
+ { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() },
3095
+ rtti->getDynamicClass (Data ()));
3066
3096
return convertible ? static_cast <std::remove_cvref_t <T>*>(Data ()) : nullptr ;
3067
3097
}
3068
3098
@@ -3072,24 +3102,11 @@ namespace Mirror {
3072
3102
Assert (!IsArray ());
3073
3103
const bool convertible = Mirror::Convertible (
3074
3104
{ AddPointerType (), AddPointerType (), RemoveRefType () },
3075
- { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() });
3105
+ { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() },
3106
+ rtti->getDynamicClass (Data ()));
3076
3107
return convertible ? static_cast <std::remove_cvref_t <T>*>(Data ()) : nullptr ;
3077
3108
}
3078
3109
3079
- template <typename B, typename T>
3080
- T Any::PolyAs ()
3081
- {
3082
- Assert (!IsArray ());
3083
- return dynamic_cast <T>(As<B>());
3084
- }
3085
-
3086
- template <typename B, typename T>
3087
- T Any::PolyAs () const
3088
- {
3089
- Assert (!IsArray ());
3090
- return dynamic_cast <T>(As<B>());
3091
- }
3092
-
3093
3110
template <ValidTemplateView V>
3094
3111
bool Any::CanAsTemplateView () const
3095
3112
{
@@ -3162,11 +3179,19 @@ namespace Mirror {
3162
3179
template <typename T>
3163
3180
T Argument::As () const // NOLINT
3164
3181
{
3165
- return Delegate ([](auto && value) -> decltype (auto ) {
3182
+ return Delegate ([& ](auto && value) -> decltype (auto ) {
3166
3183
return value.template As <T>();
3167
3184
});
3168
3185
}
3169
3186
3187
+ template <Common::CppNotRef T>
3188
+ T* Argument::TryAs () const
3189
+ {
3190
+ return Delegate ([&](auto && value) -> decltype (auto ) {
3191
+ return value.template TryAs <T>();
3192
+ });
3193
+ }
3194
+
3170
3195
template <ValidTemplateView V>
3171
3196
bool Argument::CanAsTemplateView () const
3172
3197
{
0 commit comments