summaryrefslogtreecommitdiff
path: root/core/rid.h
blob: 7e1240918194fee01d7fc664c31d0805d4e9055b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*************************************************************************/
/*  rid.h                                                                */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef RID_H
#define RID_H

#include "core/list.h"
#include "core/os/memory.h"
#include "core/safe_refcount.h"
#include "core/set.h"
#include "core/typedefs.h"

class RID_OwnerBase;

class RID_Data {

	friend class RID_OwnerBase;

#ifndef DEBUG_ENABLED
	RID_OwnerBase *_owner;
#endif
	uint32_t _id;

public:
	_FORCE_INLINE_ uint32_t get_id() const { return _id; }

	virtual ~RID_Data();
};

class RID {
	friend class RID_OwnerBase;

	mutable RID_Data *_data;

public:
	_FORCE_INLINE_ RID_Data *get_data() const { return _data; }

	_FORCE_INLINE_ bool operator==(const RID &p_rid) const {

		return _data == p_rid._data;
	}
	_FORCE_INLINE_ bool operator<(const RID &p_rid) const {

		return _data < p_rid._data;
	}
	_FORCE_INLINE_ bool operator<=(const RID &p_rid) const {

		return _data <= p_rid._data;
	}
	_FORCE_INLINE_ bool operator>(const RID &p_rid) const {

		return _data > p_rid._data;
	}
	_FORCE_INLINE_ bool operator!=(const RID &p_rid) const {

		return _data != p_rid._data;
	}
	_FORCE_INLINE_ bool is_valid() const { return _data != NULL; }

	_FORCE_INLINE_ uint32_t get_id() const { return _data ? _data->get_id() : 0; }

	_FORCE_INLINE_ RID() {
		_data = NULL;
	}
};

class RID_OwnerBase {
protected:
	static SafeRefCount refcount;
	_FORCE_INLINE_ void _set_data(RID &p_rid, RID_Data *p_data) {
		p_rid._data = p_data;
		refcount.ref();
		p_data->_id = refcount.get();
#ifndef DEBUG_ENABLED
		p_data->_owner = this;
#endif
	}

#ifndef DEBUG_ENABLED

	_FORCE_INLINE_ bool _is_owner(const RID &p_rid) const {

		return this == p_rid._data->_owner;
	}

	_FORCE_INLINE_ void _remove_owner(RID &p_rid) {

		p_rid._data->_owner = NULL;
	}
#endif

public:
	virtual void get_owned_list(List<RID> *p_owned) = 0;

	static void init_rid();
	virtual ~RID_OwnerBase() {}
};

template <class T>
class RID_Owner : public RID_OwnerBase {
public:
#ifdef DEBUG_ENABLED
	mutable Set<RID_Data *> id_map;
#endif
public:
	_FORCE_INLINE_ RID make_rid(T *p_data) {

		RID rid;
		_set_data(rid, p_data);

#ifdef DEBUG_ENABLED
		id_map.insert(p_data);
#endif

		return rid;
	}

	_FORCE_INLINE_ T *get(const RID &p_rid) {

#ifdef DEBUG_ENABLED

		ERR_FAIL_COND_V(!p_rid.is_valid(), NULL);
		ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
#endif
		return static_cast<T *>(p_rid.get_data());
	}

	_FORCE_INLINE_ T *getornull(const RID &p_rid) {

#ifdef DEBUG_ENABLED

		if (p_rid.get_data()) {
			ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
		}
#endif
		return static_cast<T *>(p_rid.get_data());
	}

	_FORCE_INLINE_ T *getptr(const RID &p_rid) {

		return static_cast<T *>(p_rid.get_data());
	}

	_FORCE_INLINE_ bool owns(const RID &p_rid) const {

		if (p_rid.get_data() == NULL)
			return false;
#ifdef DEBUG_ENABLED
		return id_map.has(p_rid.get_data());
#else
		return _is_owner(p_rid);
#endif
	}

	void free(RID p_rid) {

#ifdef DEBUG_ENABLED
		id_map.erase(p_rid.get_data());
#else
		_remove_owner(p_rid);
#endif
	}

	void get_owned_list(List<RID> *p_owned) {

#ifdef DEBUG_ENABLED

		for (typename Set<RID_Data *>::Element *E = id_map.front(); E; E = E->next()) {
			RID r;
			_set_data(r, static_cast<T *>(E->get()));
			p_owned->push_back(r);
		}
#endif
	}
};

#endif