property.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* property.h - Node property macros
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef CORE_NODE_PROPERTY_H_27CFE1E6_DA00_521C_9DCD_356AED53BCF0
  18. #define CORE_NODE_PROPERTY_H_27CFE1E6_DA00_521C_9DCD_356AED53BCF0
  19. /*
  20. * NOTE: static_pointer_cast might be unsafe (silently ignoring when node is
  21. * being used incorrectly), so using dynamic_pointer_cast (which would lead
  22. * to instant segfault instead) for now. However, it might inflict too much
  23. * performance hit since getter can be used very frequently.
  24. * Because of this, we might revert to static or even reinterpret cast in
  25. * the future...
  26. */
  27. #define NODE_PROPERTY(name, type) \
  28. public: \
  29. inline shared_ptr<BaseValue<type>> get_##name() const { \
  30. return this->template get_property_as<type>(#name); \
  31. } \
  32. inline void set_##name(shared_ptr<BaseValue<type>> value) { \
  33. this->set_property(#name, dynamic_pointer_cast<AbstractValue>(value)); \
  34. } \
  35. private: \
  36. string name { #name }
  37. #define NODE_STATIC_PROPERTY(name, type) \
  38. public: \
  39. inline shared_ptr<Value<type>> get_##name() const { \
  40. return dynamic_pointer_cast<Value<type>>(this->get_property(#name)); \
  41. } \
  42. inline void set_##name(shared_ptr<Value<type>> value) { \
  43. this->set_property(#name, dynamic_pointer_cast<AbstractValue>(value)); \
  44. } \
  45. private: \
  46. string name { #name }
  47. #define NODE_LIST_PROPERTY(name, type) \
  48. public: \
  49. inline shared_ptr<AbstractListLinked> list_##name() const { \
  50. return dynamic_pointer_cast<AbstractListLinked>(this->get_property(#name)); \
  51. } \
  52. NODE_PROPERTY(name, vector<type>)
  53. #endif