1 /*************************************************************************/
2 /* reference.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
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 #include "reference.h"
31 #include "script_language.h"
32
init_ref()33 bool Reference::init_ref() {
34
35 if (refcount.ref()) {
36
37 // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
38 // at the same time, which is never likely to happen (would be crazy to do)
39 // so don't do it.
40
41 if (refcount_init.get() > 0) {
42 refcount_init.unref();
43 refcount.unref(); // first referencing is already 1, so compensate for the ref above
44 }
45
46 return true;
47 } else {
48
49 return false;
50 }
51 }
52
_bind_methods()53 void Reference::_bind_methods() {
54
55 ObjectTypeDB::bind_method(_MD("init_ref"), &Reference::init_ref);
56 ObjectTypeDB::bind_method(_MD("reference"), &Reference::reference);
57 ObjectTypeDB::bind_method(_MD("unreference"), &Reference::unreference);
58 }
59
reference_get_count() const60 int Reference::reference_get_count() const {
61 return refcount.get();
62 }
63
reference()64 void Reference::reference() {
65
66 refcount.ref();
67 if (get_script_instance()) {
68 get_script_instance()->refcount_incremented();
69 }
70 }
unreference()71 bool Reference::unreference() {
72
73 bool die = refcount.unref();
74
75 if (get_script_instance()) {
76 die = die && get_script_instance()->refcount_decremented();
77 }
78
79 return die;
80 }
81
Reference()82 Reference::Reference() {
83
84 refcount.init();
85 refcount_init.init();
86 }
87
~Reference()88 Reference::~Reference() {
89 }
90
get_ref() const91 Variant WeakRef::get_ref() const {
92
93 if (ref == 0)
94 return Variant();
95
96 Object *obj = ObjectDB::get_instance(ref);
97 if (!obj)
98 return Variant();
99 Reference *r = obj->cast_to<Reference>();
100 if (r) {
101
102 return REF(r);
103 }
104
105 return obj;
106 }
107
set_obj(Object * p_object)108 void WeakRef::set_obj(Object *p_object) {
109 ref = p_object ? p_object->get_instance_ID() : 0;
110 }
111
set_ref(const REF & p_ref)112 void WeakRef::set_ref(const REF &p_ref) {
113
114 ref = p_ref.is_valid() ? p_ref->get_instance_ID() : 0;
115 }
116
WeakRef()117 WeakRef::WeakRef() {
118 ref = 0;
119 }
120
_bind_methods()121 void WeakRef::_bind_methods() {
122
123 ObjectTypeDB::bind_method(_MD("get_ref:Object"), &WeakRef::get_ref);
124 }
125 #if 0
126
127 Reference * RefBase::get_reference_from_ref(const RefBase &p_base) {
128
129 return p_base.get_reference();
130 }
131 void RefBase::ref_inc(Reference *p_reference) {
132
133 p_reference->refcount.ref();
134 }
135 bool RefBase::ref_dec(Reference *p_reference) {
136
137 bool ref = p_reference->refcount.unref();
138 return ref;
139 }
140
141 Reference *RefBase::first_ref(Reference *p_reference) {
142
143 if (p_reference->refcount.ref()) {
144
145 // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME
146 // at the same time, which is never likely to happen (would be crazy to do)
147 // so don't do it.
148
149 if (p_reference->refcount_init.get()>0) {
150 p_reference->refcount_init.unref();
151 p_reference->refcount.unref(); // first referencing is already 1, so compensate for the ref above
152 }
153
154 return p_reference;
155 } else {
156
157 return 0;
158 }
159
160 }
161 char * RefBase::get_refptr_data(const RefPtr &p_refptr) const {
162
163 return p_refptr.data;
164 }
165 #endif
166