| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
| 6 | #include "Utility/BsAny.h" |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | /** @addtogroup RTTI |
| 11 | * @{ |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Interface implemented by classes that provide run time type information. |
| 16 | * |
| 17 | * @note |
| 18 | * Any class implementing this interface must implement the getRTTI() method, as well as a static getRTTIStatic() |
| 19 | * method, returning the same value as getRTTI(). Object returned by those methods is used for retrieving actual RTTI |
| 20 | * data about the class. |
| 21 | */ |
| 22 | class BS_UTILITY_EXPORT IReflectable |
| 23 | { |
| 24 | public: |
| 25 | virtual ~IReflectable() = default; |
| 26 | |
| 27 | /** |
| 28 | * Returns an interface you can use to access class' Run Time Type Information. |
| 29 | * |
| 30 | * @note |
| 31 | * You must derive your own version of RTTITypeBase, in which you may encapsulate all reflection specific operations. |
| 32 | */ |
| 33 | virtual RTTITypeBase* getRTTI() const = 0; |
| 34 | |
| 35 | /** Returns all available RTTI types. */ |
| 36 | static UnorderedMap<UINT32, RTTITypeBase*>& getAllRTTITypes() |
| 37 | { |
| 38 | static UnorderedMap<UINT32, RTTITypeBase*> mAllRTTITypes; |
| 39 | return mAllRTTITypes; |
| 40 | } |
| 41 | |
| 42 | /** Returns true if current RTTI class is derived from @p base (Or if it is the same type as base). */ |
| 43 | bool isDerivedFrom(RTTITypeBase* base); |
| 44 | |
| 45 | /** Returns an unique type identifier of the class. */ |
| 46 | UINT32 getTypeId() const; |
| 47 | |
| 48 | /** |
| 49 | * Returns the type name of the class. |
| 50 | * |
| 51 | * @note Name is not necessarily unique. |
| 52 | */ |
| 53 | const String& getTypeName() const; |
| 54 | |
| 55 | /** Creates an empty instance of a class from a type identifier. */ |
| 56 | static SPtr<IReflectable> createInstanceFromTypeId(UINT32 rttiTypeId); |
| 57 | |
| 58 | /** @name Internal |
| 59 | * @{ |
| 60 | */ |
| 61 | |
| 62 | /** Called by each type implementing RTTITypeBase, on program load. */ |
| 63 | static void _registerRTTIType(RTTITypeBase* rttiType); |
| 64 | |
| 65 | /** Returns class' RTTI type from type id. */ |
| 66 | static RTTITypeBase* _getRTTIfromTypeId(UINT32 rttiTypeId); |
| 67 | |
| 68 | /** Checks if the provided type id is unique. */ |
| 69 | static bool _isTypeIdDuplicate(UINT32 typeId); |
| 70 | |
| 71 | /** |
| 72 | * Iterates over all RTTI types and reports any circular references (for example one type having a field referencing |
| 73 | * another type, and that type having a field referencing the first type). Circular references are problematic |
| 74 | * because when serializing the system cannot determine in which order they should be resolved. In that case user |
| 75 | * should use RTTI_Flag_WeakRef to mark one of the references as weak. This flags tells the system that the reference |
| 76 | * may be resolved in an undefined order, but also no longer guarantees that object assigned to that field during |
| 77 | * deserialization will be fully deserialized itself, as that might be delayed to a later time. |
| 78 | */ |
| 79 | static void _checkForCircularReferences(); |
| 80 | |
| 81 | /** Returns an interface you can use to access class' Run Time Type Information. */ |
| 82 | static RTTITypeBase* getRTTIStatic(); |
| 83 | |
| 84 | /** @} */ |
| 85 | }; |
| 86 | |
| 87 | /** @} */ |
| 88 | } |
| 89 | |