Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/property_list_helper.h
9821 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
HashMap<String, Property> property_list;
49
Object *object = nullptr;
50
51
const Property *_get_property(const String &p_property, int *r_index) const;
52
void _call_setter(const MethodBind *p_setter, int p_index, const Variant &p_value) const;
53
Variant _call_getter(const Property *p_property, int p_index) const;
54
int _call_array_length_getter() const;
55
56
public:
57
static void clear_base_helpers();
58
static void register_base_helper(PropertyListHelper *p_helper);
59
60
void set_prefix(const String &p_prefix);
61
template <typename G>
62
void set_array_length_getter(G p_array_length_getter) {
63
array_length_getter = create_method_bind(p_array_length_getter);
64
}
65
66
// Register property without setter/getter. Only use when you don't need PropertyListHelper for _set/_get logic.
67
void register_property(const PropertyInfo &p_info, const Variant &p_default);
68
69
template <typename S, typename G>
70
void register_property(const PropertyInfo &p_info, const Variant &p_default, S p_setter, G p_getter) {
71
Property property;
72
property.info = p_info;
73
property.default_value = p_default;
74
property.setter = create_method_bind(p_setter);
75
property.getter = create_method_bind(p_getter);
76
77
property_list[p_info.name] = property;
78
}
79
80
bool is_initialized() const;
81
void setup_for_instance(const PropertyListHelper &p_base, Object *p_object);
82
bool is_property_valid(const String &p_property, int *r_index = nullptr) const;
83
84
void get_property_list(List<PropertyInfo> *p_list) const;
85
bool property_get_value(const String &p_property, Variant &r_ret) const;
86
bool property_set_value(const String &p_property, const Variant &p_value) const;
87
bool property_can_revert(const String &p_property) const;
88
bool property_get_revert(const String &p_property, Variant &r_value) const;
89
90
void clear();
91
};
92
93