Property.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include <map>
  3. #include "IProperty.h"
  4. namespace mdd
  5. {
  6. template<typename TYPE>
  7. class Property : public IProperty
  8. {
  9. PropertyValue<TYPE> val;
  10. std::map<std::string, std::shared_ptr<IProperty>> _subprops;
  11. protected:
  12. virtual IPropertyValue* GetPropertyValue()
  13. {
  14. return &val;
  15. }
  16. public:
  17. typedef std::shared_ptr<Property<TYPE>> Ptr;
  18. virtual std::shared_ptr<IProperty> GetSubProp(const std::string& name)
  19. {
  20. auto sub = _subprops.find(name);
  21. if (sub == _subprops.end())
  22. {
  23. return std::make_shared<Property<bool>>();
  24. }
  25. else
  26. {
  27. return sub->second;
  28. }
  29. }
  30. template<typename T>
  31. std::shared_ptr<Property<T>> addSubProperty(const std::string& name)
  32. {
  33. auto newprop = std::make_shared<Property<T>>();
  34. _subprops[name] = newprop;
  35. return newprop;
  36. }
  37. template<typename T>
  38. std::shared_ptr<Property<T>> addSubProperty(const std::string& name, const T& default_val)
  39. {
  40. auto newprop = std::make_shared<Property<T>>();
  41. newprop->Value() = default_val;
  42. _subprops[name] = newprop;
  43. return newprop;
  44. }
  45. TYPE& Value()
  46. {
  47. return *val.value;
  48. }
  49. };
  50. }