property_list_helper.h 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**************************************************************************/
  2. /* property_list_helper.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PROPERTY_LIST_HELPER_H
  31. #define PROPERTY_LIST_HELPER_H
  32. #include "core/object/method_bind.h"
  33. #include "core/object/object.h"
  34. class PropertyListHelper {
  35. struct Property {
  36. PropertyInfo info;
  37. Variant default_value;
  38. MethodBind *setter = nullptr;
  39. MethodBind *getter = nullptr;
  40. };
  41. static Vector<PropertyListHelper *> base_helpers;
  42. String prefix;
  43. MethodBind *array_length_getter = nullptr;
  44. HashMap<String, Property> property_list;
  45. Object *object = nullptr;
  46. const Property *_get_property(const String &p_property, int *r_index) const;
  47. void _call_setter(const MethodBind *p_setter, int p_index, const Variant &p_value) const;
  48. Variant _call_getter(const Property *p_property, int p_index) const;
  49. int _call_array_length_getter() const;
  50. public:
  51. static void clear_base_helpers();
  52. static void register_base_helper(PropertyListHelper *p_helper);
  53. void set_prefix(const String &p_prefix);
  54. template <typename G>
  55. void set_array_length_getter(G p_array_length_getter) {
  56. array_length_getter = create_method_bind(p_array_length_getter);
  57. }
  58. // Register property without setter/getter. Only use when you don't need PropertyListHelper for _set/_get logic.
  59. void register_property(const PropertyInfo &p_info, const Variant &p_default);
  60. template <typename S, typename G>
  61. void register_property(const PropertyInfo &p_info, const Variant &p_default, S p_setter, G p_getter) {
  62. Property property;
  63. property.info = p_info;
  64. property.default_value = p_default;
  65. property.setter = create_method_bind(p_setter);
  66. property.getter = create_method_bind(p_getter);
  67. property_list[p_info.name] = property;
  68. }
  69. bool is_initialized() const;
  70. void setup_for_instance(const PropertyListHelper &p_base, Object *p_object);
  71. bool is_property_valid(const String &p_property, int *r_index = nullptr) const;
  72. void get_property_list(List<PropertyInfo> *p_list) const;
  73. bool property_get_value(const String &p_property, Variant &r_ret) const;
  74. bool property_set_value(const String &p_property, const Variant &p_value) const;
  75. bool property_can_revert(const String &p_property) const;
  76. bool property_get_revert(const String &p_property, Variant &r_value) const;
  77. void clear();
  78. };
  79. #endif // PROPERTY_LIST_HELPER_H