@@ -72,9 +72,12 @@ namespace Mirror {
7272 const TypeInfo* removePointer;
7373 };
7474
75+ class Any ;
76+ class Class ;
77+
7578 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 );
7881
7982 enum class AnyPolicy : uint8_t {
8083 memoryHolder,
@@ -83,8 +86,6 @@ namespace Mirror {
8386 max
8487 };
8588
86- class Any ;
87-
8889 using TemplateViewId = uint32_t ;
8990 using TemplateViewRttiPtr = const void *;
9091
@@ -105,6 +106,7 @@ namespace Mirror {
105106 using JsonDeserializeFunc = void (void *, const rapidjson::Value&);
106107 using ToStringFunc = std::string(const void *);
107108 using GetTemplateViewRttiFunc = std::pair<TemplateViewId, TemplateViewRttiPtr>();
109+ using GetDynamicClassFunc = const Class*(const void *);
108110
109111 template <typename T> static void Detor (void * inThis) noexcept ;
110112 template <typename T> static void CopyConstruct (void * inThis, const void * inOther);
@@ -128,6 +130,7 @@ namespace Mirror {
128130 template <typename T> static void JsonDeserialize (void * inThis, const rapidjson::Value& inJsonValue);
129131 template <typename T> static std::string ToString (const void * inThis);
130132 template <typename T> static std::pair<TemplateViewId, TemplateViewRttiPtr> GetTemplateViewRtti ();
133+ template <typename T> static const Class* GetDynamicClass (const void * inThis);
131134
132135 DetorFunc* detor;
133136 CopyConstructFunc* copyConstruct;
@@ -151,6 +154,7 @@ namespace Mirror {
151154 JsonDeserializeFunc* jsonDeserialize;
152155 ToStringFunc* toString;
153156 GetTemplateViewRttiFunc* getTemplateViewRtti;
157+ GetDynamicClassFunc* getDynamicClass;
154158 };
155159
156160 template <typename T, size_t N = 1 >
@@ -176,7 +180,8 @@ namespace Mirror {
176180 &AnyRtti::JsonSerialize<T>,
177181 &AnyRtti::JsonDeserialize<T>,
178182 &AnyRtti::ToString<T>,
179- &AnyRtti::GetTemplateViewRtti<T>
183+ &AnyRtti::GetTemplateViewRtti<T>,
184+ &AnyRtti::GetDynamicClass<T>
180185 };
181186
182187 template <typename T>
@@ -245,8 +250,6 @@ namespace Mirror {
245250 template <typename T> T As () const ;
246251 template <Common::CppNotRef T> T* TryAs ();
247252 template <Common::CppNotRef T> T* TryAs () const ;
248- template <typename B, typename T> T PolyAs ();
249- template <typename B, typename T> T PolyAs () const ;
250253
251254 template <ValidTemplateView V> bool CanAsTemplateView () const ;
252255 TemplateViewRttiPtr GetTemplateViewRtti () const ;
@@ -280,6 +283,7 @@ namespace Mirror {
280283 const TypeInfo* RemovePointerType () const ;
281284 Mirror::TypeId TypeId ();
282285 Mirror::TypeId TypeId () const ;
286+ const Class* GetDynamicClass () const ;
283287 void Reset ();
284288 bool Empty () const ;
285289 size_t Serialize (Common::BinarySerializeStream& inStream) const ;
@@ -366,6 +370,7 @@ namespace Mirror {
366370 Argument& operator =(Any&& inAny);
367371
368372 template <typename T> T As () const ;
373+ template <Common::CppNotRef T> T* TryAs () const ;
369374
370375 template <ValidTemplateView V> bool CanAsTemplateView () const ;
371376 TemplateViewRttiPtr GetTemplateViewRtti () const ;
@@ -378,6 +383,7 @@ namespace Mirror {
378383 const TypeInfo* RemoveRefType () const ;
379384 const TypeInfo* AddPointerType () const ;
380385 const TypeInfo* RemovePointerType () const ;
386+ const Class* GetDynamicClass () const ;
381387
382388 private:
383389 template <typename F> decltype (auto ) Delegate(F&& inFunc) const ;
@@ -822,12 +828,12 @@ namespace Mirror {
822828 const MemberFunction& GetMemberFunction (const Id& inId) const ;
823829 Any GetDefaultObject () const ;
824830 bool IsTransient () const ;
825-
826831 Any ConstructDyn (const ArgumentList& arguments) const ;
827832 Any NewDyn (const ArgumentList& arguments) const ;
828833 Any InplaceNewDyn (void * ptr, const ArgumentList& arguments) const ;
829834 void DestructDyn (const Argument& argument) const ;
830835 void DeleteDyn (const Argument& argument) const ;
836+ Any Cast (const Argument& objPtrOrRef) const ;
831837
832838 private:
833839 static std::unordered_map<TypeId, Id> typeToIdMap;
@@ -837,14 +843,17 @@ namespace Mirror {
837843
838844 using BaseClassGetter = std::function<const Class*()>;
839845 using InplaceGetter = std::function<Any(void *)>;
846+ using DefaultObjectCreator = std::function<Any()>;
847+ using Caster = std::function<Any(const Mirror::Argument&)>;
840848
841849 struct ConstructParams {
842850 Id id;
843851 const TypeInfo* typeInfo;
844852 size_t memorySize;
845853 BaseClassGetter baseClassGetter;
846854 InplaceGetter inplaceGetter;
847- std::function<Any()> defaultObjectCreator;
855+ Caster caster;
856+ DefaultObjectCreator defaultObjectCreator;
848857 std::optional<Destructor::ConstructParams> destructorParams;
849858 std::optional<Constructor::ConstructParams> defaultConstructorParams;
850859 std::optional<Constructor::ConstructParams> moveConstructorParams;
@@ -853,7 +862,7 @@ namespace Mirror {
853862
854863 explicit Class (ConstructParams&& params);
855864
856- void CreateDefaultObject (const std::function<Any()> & inCreator);
865+ void CreateDefaultObject (const DefaultObjectCreator & inCreator);
857866 Destructor& EmplaceDestructor (Destructor::ConstructParams&& inParams);
858867 Constructor& EmplaceConstructor (const Id& inId, Constructor::ConstructParams&& inParams);
859868 Variable& EmplaceStaticVariable (const Id& inId, Variable::ConstructParams&& inParams);
@@ -865,6 +874,7 @@ namespace Mirror {
865874 size_t memorySize;
866875 BaseClassGetter baseClassGetter;
867876 InplaceGetter inplaceGetter;
877+ Caster caster;
868878 Any defaultObject;
869879 std::optional<Destructor> destructor;
870880 std::unordered_map<Id, Constructor, IdHashProvider> constructors;
@@ -2882,8 +2892,25 @@ namespace Mirror {
28822892 {
28832893 return {
28842894 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+ }
28872914 }
28882915
28892916 template <typename T>
@@ -3030,7 +3057,8 @@ namespace Mirror {
30303057 Assert (!IsArray ());
30313058 return Mirror::Convertible (
30323059 { 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 ()));
30343062 }
30353063
30363064 template <typename T>
@@ -3039,7 +3067,8 @@ namespace Mirror {
30393067 Assert (!IsArray ());
30403068 return Mirror::Convertible (
30413069 { 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 ()));
30433072 }
30443073
30453074 template <typename T>
@@ -3062,7 +3091,8 @@ namespace Mirror {
30623091 Assert (!IsArray ());
30633092 const bool convertible = Mirror::Convertible (
30643093 { AddPointerType (), AddPointerType (), RemoveRefType () },
3065- { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() });
3094+ { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() },
3095+ rtti->getDynamicClass (Data ()));
30663096 return convertible ? static_cast <std::remove_cvref_t <T>*>(Data ()) : nullptr ;
30673097 }
30683098
@@ -3072,24 +3102,11 @@ namespace Mirror {
30723102 Assert (!IsArray ());
30733103 const bool convertible = Mirror::Convertible (
30743104 { AddPointerType (), AddPointerType (), RemoveRefType () },
3075- { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() });
3105+ { GetTypeInfo<T*>(), GetTypeInfo<T*>(), GetTypeInfo<T>() },
3106+ rtti->getDynamicClass (Data ()));
30763107 return convertible ? static_cast <std::remove_cvref_t <T>*>(Data ()) : nullptr ;
30773108 }
30783109
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-
30933110 template <ValidTemplateView V>
30943111 bool Any::CanAsTemplateView () const
30953112 {
@@ -3162,11 +3179,19 @@ namespace Mirror {
31623179 template <typename T>
31633180 T Argument::As () const // NOLINT
31643181 {
3165- return Delegate ([](auto && value) -> decltype (auto ) {
3182+ return Delegate ([& ](auto && value) -> decltype (auto ) {
31663183 return value.template As <T>();
31673184 });
31683185 }
31693186
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+
31703195 template <ValidTemplateView V>
31713196 bool Argument::CanAsTemplateView () const
31723197 {
0 commit comments