BsRTTIType.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "BsRTTIType.h"
  2. #include "BsException.h"
  3. namespace BansheeEngine
  4. {
  5. RTTITypeBase::RTTITypeBase()
  6. { }
  7. RTTITypeBase::~RTTITypeBase()
  8. {
  9. for(auto iter = mFields.begin(); iter != mFields.end(); ++iter)
  10. bs_delete(*iter);
  11. mFields.clear();
  12. }
  13. RTTIField* RTTITypeBase::findField(const String& name)
  14. {
  15. auto foundElement = std::find_if(mFields.begin(), mFields.end(), [&name](RTTIField* x) { return x->mName == name; });
  16. if(foundElement == mFields.end())
  17. {
  18. BS_EXCEPT(InternalErrorException,
  19. "Cannot find a field with the specified name: " + name);
  20. }
  21. return *foundElement;
  22. }
  23. RTTIField* RTTITypeBase::findField(int uniqueFieldId)
  24. {
  25. auto foundElement = std::find_if(mFields.begin(), mFields.end(), [&uniqueFieldId](RTTIField* x) { return x->mUniqueId == uniqueFieldId; });
  26. if(foundElement == mFields.end())
  27. return nullptr;
  28. return *foundElement;
  29. }
  30. void RTTITypeBase::addNewField(RTTIField* field)
  31. {
  32. if(field == nullptr)
  33. {
  34. BS_EXCEPT(InvalidParametersException,
  35. "Field argument can't be null.");
  36. }
  37. int uniqueId = field->mUniqueId;
  38. auto foundElementById = std::find_if(mFields.begin(), mFields.end(), [uniqueId](RTTIField* x) { return x->mUniqueId == uniqueId; });
  39. if(foundElementById != mFields.end())
  40. {
  41. BS_EXCEPT(InternalErrorException,
  42. "Field with the same ID already exists.");
  43. }
  44. String& name = field->mName;
  45. auto foundElementByName = std::find_if(mFields.begin(), mFields.end(), [&name](RTTIField* x) { return x->mName == name; });
  46. if(foundElementByName != mFields.end())
  47. {
  48. BS_EXCEPT(InternalErrorException,
  49. "Field with the same name already exists.");
  50. }
  51. mFields.push_back(field);
  52. }
  53. void RTTITypeBase::throwCircularRefException(const String& myType, const String& otherType) const
  54. {
  55. BS_EXCEPT(InternalErrorException, "Found circular reference on RTTI type: " + myType + " to type: " + otherType + "."
  56. + " Either remove one of the references or mark it as a weak reference when defining the RTTI field.");
  57. }
  58. }