Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/property_list_helper.h
21731 views
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
31
#pragma once
32
33
#include "core/object/method_bind.h"
34
#include "core/object/object.h"
35
36
class PropertyListHelper {
37
struct Property {
38
PropertyInfo info;
39
Variant default_value;
40
MethodBind *setter = nullptr;
41
MethodBind *getter = nullptr;
42
};
43
44
static Vector<PropertyListHelper *> base_helpers;
45
46
String prefix;
47
MethodBind *array_length_getter = nullptr;
48
MethodBind *property_filter = nullptr;
49
HashMap<String, Property> property_list;
50
Object *object = nullptr;
51
52
bool allow_oob_assign = false;
53
54
const Property *_get_property(const String &p_property, int *r_index, bool p_allow_oob = false) const;
55
void _call_setter(const MethodBind *p_setter, int p_index, const Variant &p_value) const;
56
Variant _call_getter(const Property *p_property, int p_index) const;
57
int _call_array_length_getter() const;
58
59
public:
60
static void clear_base_helpers();
61
static void register_base_helper(PropertyListHelper *p_helper);
62
63
void set_prefix(const String &p_prefix);
64
template <typename G>
65
void set_array_length_getter(G p_array_length_getter) {
66
array_length_getter = create_method_bind(p_array_length_getter);
67
}
68
template <typename F>
69
void set_property_filter(F p_property_filter) {
70
property_filter = create_method_bind(p_property_filter);
71
}
72
73
// Register property without setter/getter. Only use when you don't need PropertyListHelper for _set/_get logic.
74
void register_property(const PropertyInfo &p_info, const Variant &p_default);
75
76
template <typename S, typename G>
77
void register_property(const PropertyInfo &p_info, const Variant &p_default, S p_setter, G p_getter) {
78
Property property;
79
property.info = p_info;
80
property.default_value = p_default;
81
property.setter = create_method_bind(p_setter);
82
property.getter = create_method_bind(p_getter);
83
84
property_list[p_info.name] = property;
85
}
86
87
bool is_initialized() const;
88
void setup_for_instance(const PropertyListHelper &p_base, Object *p_object);
89
bool is_property_valid(const String &p_property, int *r_index = nullptr) const;
90
91
void get_property_list(List<PropertyInfo> *p_list) const;
92
bool property_get_value(const String &p_property, Variant &r_ret) const;
93
bool property_set_value(const String &p_property, const Variant &p_value) const;
94
bool property_can_revert(const String &p_property) const;
95
bool property_get_revert(const String &p_property, Variant &r_value) const;
96
97
void enable_out_of_bounds_assign() { allow_oob_assign = true; }
98
99
void clear();
100
};
101
102