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 "BsCorePrerequisites.h" |
6 | #include "Reflection/BsRTTIType.h" |
7 | #include "Resources/BsResourceHandle.h" |
8 | #include "Resources/BsResources.h" |
9 | |
10 | namespace bs |
11 | { |
12 | /** @cond RTTI */ |
13 | /** @addtogroup RTTI-Impl-Core |
14 | * @{ |
15 | */ |
16 | |
17 | class BS_CORE_EXPORT ResourceHandleRTTI : public RTTIType<TResourceHandleBase<false>, IReflectable, ResourceHandleRTTI> |
18 | { |
19 | private: |
20 | UUID& getUUID(TResourceHandleBase<false>* obj) { return obj->mData != nullptr ? obj->mData->mUUID : UUID::EMPTY; } |
21 | void setUUID(TResourceHandleBase<false>* obj, UUID& uuid) { obj->mData->mUUID = uuid; } |
22 | |
23 | public: |
24 | ResourceHandleRTTI() |
25 | { |
26 | addPlainField("mUUID" , 0, &ResourceHandleRTTI::getUUID, &ResourceHandleRTTI::setUUID); |
27 | } |
28 | |
29 | void onDeserializationEnded(IReflectable* obj, SerializationContext* context) override |
30 | { |
31 | TResourceHandleBase<false>* resourceHandle = static_cast<TResourceHandleBase<false>*>(obj); |
32 | |
33 | if(resourceHandle->mData && !resourceHandle->mData->mUUID.empty()) |
34 | { |
35 | HResource loadedResource = gResources()._getResourceHandle(resourceHandle->mData->mUUID); |
36 | |
37 | resourceHandle->releaseRef(); |
38 | resourceHandle->mData = loadedResource.mData; |
39 | resourceHandle->addRef(); |
40 | } |
41 | } |
42 | |
43 | const String& getRTTIName() override |
44 | { |
45 | static String name = "ResourceHandleBase" ; |
46 | return name; |
47 | } |
48 | |
49 | UINT32 getRTTIId() override |
50 | { |
51 | return TID_ResourceHandle; |
52 | } |
53 | |
54 | SPtr<IReflectable> newRTTIObject() override |
55 | { |
56 | SPtr<TResourceHandleBase<false>> obj = bs_shared_ptr<TResourceHandleBase<false>> |
57 | (new (bs_alloc<TResourceHandleBase<false>>()) TResourceHandleBase<false>()); |
58 | obj->mData = bs_shared_ptr_new<ResourceHandleData>(); |
59 | obj->mData->mRefCount.fetch_add(1, std::memory_order_relaxed); |
60 | |
61 | return obj; |
62 | } |
63 | }; |
64 | |
65 | class BS_CORE_EXPORT WeakResourceHandleRTTI : public RTTIType<TResourceHandleBase<true>, IReflectable, WeakResourceHandleRTTI> |
66 | { |
67 | private: |
68 | UUID& getUUID(TResourceHandleBase<true>* obj) { return obj->mData != nullptr ? obj->mData->mUUID : UUID::EMPTY; } |
69 | void setUUID(TResourceHandleBase<true>* obj, UUID& uuid) { obj->mData->mUUID = uuid; } |
70 | |
71 | public: |
72 | WeakResourceHandleRTTI() |
73 | { |
74 | addPlainField("mUUID" , 0, &WeakResourceHandleRTTI::getUUID, &WeakResourceHandleRTTI::setUUID); |
75 | } |
76 | |
77 | void onDeserializationEnded(IReflectable* obj, SerializationContext* context) override |
78 | { |
79 | TResourceHandleBase<true>* resourceHandle = static_cast<TResourceHandleBase<true>*>(obj); |
80 | |
81 | if (resourceHandle->mData && !resourceHandle->mData->mUUID.empty()) |
82 | { |
83 | HResource loadedResource = gResources()._getResourceHandle(resourceHandle->mData->mUUID); |
84 | resourceHandle->mData = loadedResource.mData; |
85 | } |
86 | } |
87 | |
88 | const String& getRTTIName() override |
89 | { |
90 | static String name = "WeakResourceHandleBase" ; |
91 | return name; |
92 | } |
93 | |
94 | UINT32 getRTTIId() override |
95 | { |
96 | return TID_WeakResourceHandle; |
97 | } |
98 | |
99 | SPtr<IReflectable> newRTTIObject() override |
100 | { |
101 | SPtr<TResourceHandleBase<true>> obj = bs_shared_ptr<TResourceHandleBase<true>> |
102 | (new (bs_alloc<TResourceHandleBase<true>>()) TResourceHandleBase<true>()); |
103 | obj->mData = bs_shared_ptr_new<ResourceHandleData>(); |
104 | |
105 | return obj; |
106 | } |
107 | }; |
108 | |
109 | /** @} */ |
110 | /** @endcond */ |
111 | } |