IProperty.h 923 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <memory>
  3. #include <string>
  4. namespace mdd
  5. {
  6. class IPropertyValue
  7. {
  8. public:
  9. virtual ~IPropertyValue(){}
  10. virtual std::string GetPropertyType() = 0;
  11. };
  12. template<typename TYPE>
  13. class PropertyValue : public IPropertyValue
  14. {
  15. public:
  16. std::string GetPropertyType()
  17. {
  18. return typeid(TYPE).name();
  19. }
  20. std::shared_ptr<TYPE> value = std::make_shared<TYPE>();
  21. };
  22. class IProperty
  23. {
  24. protected:
  25. virtual IPropertyValue* GetPropertyValue() = 0;
  26. public:
  27. virtual ~IProperty(){}
  28. virtual std::shared_ptr<IProperty> GetSubProp(const std::string& name) = 0;
  29. template<typename EXPECTED_TYPE>
  30. std::shared_ptr<EXPECTED_TYPE> Value()
  31. {
  32. if (GetPropertyValue()->GetPropertyType() != typeid(EXPECTED_TYPE).name())
  33. {
  34. return std::make_shared<EXPECTED_TYPE>();
  35. }
  36. else
  37. {
  38. return dynamic_cast<PropertyValue<EXPECTED_TYPE>>(GetPropertyValue())->value;
  39. }
  40. }
  41. };
  42. }