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#include "Reflection/BsRTTIType.h"
4#include "Error/BsException.h"
5
6namespace bs
7{
8 RTTITypeBase::~RTTITypeBase()
9 {
10 for(const auto& item : mFields)
11 bs_delete(item);
12 }
13
14 RTTIField* RTTITypeBase::findField(const String& name)
15 {
16 auto foundElement = std::find_if(mFields.begin(), mFields.end(), [&name](RTTIField* x) { return x->mName == name; });
17
18 if(foundElement == mFields.end())
19 {
20 BS_EXCEPT(InternalErrorException,
21 "Cannot find a field with the specified name: " + name);
22 }
23
24 return *foundElement;
25 }
26
27 RTTIField* RTTITypeBase::findField(int uniqueFieldId)
28 {
29 auto foundElement = std::find_if(mFields.begin(), mFields.end(), [&uniqueFieldId](RTTIField* x) { return x->mUniqueId == uniqueFieldId; });
30
31 if(foundElement == mFields.end())
32 return nullptr;
33
34 return *foundElement;
35 }
36
37 void RTTITypeBase::addNewField(RTTIField* field)
38 {
39 if(field == nullptr)
40 {
41 BS_EXCEPT(InvalidParametersException,
42 "Field argument can't be null.");
43 }
44
45 int uniqueId = field->mUniqueId;
46 auto foundElementById = std::find_if(mFields.begin(), mFields.end(), [uniqueId](RTTIField* x) { return x->mUniqueId == uniqueId; });
47
48 if(foundElementById != mFields.end())
49 {
50 BS_EXCEPT(InternalErrorException,
51 "Field with the same ID already exists.");
52 }
53
54 String& name = field->mName;
55 auto foundElementByName = std::find_if(mFields.begin(), mFields.end(), [&name](RTTIField* x) { return x->mName == name; });
56
57 if(foundElementByName != mFields.end())
58 {
59 BS_EXCEPT(InternalErrorException,
60 "Field with the same name already exists.");
61 }
62
63 mFields.push_back(field);
64 }
65
66 class SerializationContextRTTI : public RTTIType<SerializationContext, IReflectable, SerializationContextRTTI>
67 {
68 const String& getRTTIName() override
69 {
70 static String name = "SerializationContext";
71 return name;
72 }
73
74 UINT32 getRTTIId() override
75 {
76 return TID_SerializationContext;
77 }
78
79 SPtr<IReflectable> newRTTIObject() override
80 {
81 BS_EXCEPT(InternalErrorException, "Cannot instantiate an abstract class.");
82 return nullptr;
83 }
84 };
85
86 RTTITypeBase* SerializationContext::getRTTIStatic()
87 {
88 return SerializationContextRTTI::instance();
89 }
90
91 RTTITypeBase* SerializationContext::getRTTI() const
92 {
93 return getRTTIStatic();
94 }
95
96 SPtr<IReflectable> rtti_create(UINT32 rttiId)
97 {
98 return IReflectable::createInstanceFromTypeId(rttiId);
99 }
100}
101