BsRTTIType.cpp 2.3 KB

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