godot/core/rid.h

208 lines
4.9 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* rid.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* 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 "hash_map.h"
#include "list.h"
#include "os/memory.h"
#include "safe_refcount.h"
#include "typedefs.h"
2014-02-10 02:10:30 +01:00
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class RID_OwnerBase;
typedef uint32_t ID;
2016-03-09 00:00:52 +01:00
class RID {
friend class RID_OwnerBase;
2014-02-10 02:10:30 +01:00
ID _id;
RID_OwnerBase *owner;
public:
2014-02-10 02:10:30 +01:00
_FORCE_INLINE_ ID get_id() const { return _id; }
bool operator==(const RID &p_rid) const {
2016-03-09 00:00:52 +01:00
return _id == p_rid._id;
2014-02-10 02:10:30 +01:00
}
_FORCE_INLINE_ bool operator<(const RID &p_rid) const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return _id < p_rid._id;
}
_FORCE_INLINE_ bool operator<=(const RID &p_rid) const {
2014-02-10 02:10:30 +01:00
return _id <= p_rid._id;
}
_FORCE_INLINE_ bool operator>(const RID &p_rid) const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return _id > p_rid._id;
}
bool operator!=(const RID &p_rid) const {
2016-03-09 00:00:52 +01:00
return _id != p_rid._id;
2014-02-10 02:10:30 +01:00
}
_FORCE_INLINE_ bool is_valid() const { return _id > 0; }
2014-02-10 02:10:30 +01:00
operator const void *() const {
2014-02-10 02:10:30 +01:00
return is_valid() ? this : 0;
};
_FORCE_INLINE_ RID() {
_id = 0;
owner = 0;
2014-02-10 02:10:30 +01:00
}
};
class RID_OwnerBase {
protected:
friend class RID;
void set_id(RID &p_rid, ID p_id) const { p_rid._id = p_id; }
void set_ownage(RID &p_rid) const { p_rid.owner = const_cast<RID_OwnerBase *>(this); }
2014-02-10 02:10:30 +01:00
ID new_ID();
2016-03-09 00:00:52 +01:00
public:
virtual bool owns(const RID &p_rid) const = 0;
virtual void get_owned_list(List<RID> *p_owned) const = 0;
2014-02-10 02:10:30 +01:00
static void init_rid();
virtual ~RID_OwnerBase() {}
};
template <class T, bool thread_safe = false>
2014-02-10 02:10:30 +01:00
class RID_Owner : public RID_OwnerBase {
public:
typedef void (*ReleaseNotifyFunc)(void *user, T *p_data);
2014-02-10 02:10:30 +01:00
2016-03-09 00:00:52 +01:00
private:
2014-02-10 02:10:30 +01:00
Mutex *mutex;
mutable HashMap<ID, T *> id_map;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
public:
RID make_rid(T *p_data) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
ID id = new_ID();
id_map[id] = p_data;
2014-02-10 02:10:30 +01:00
RID rid;
set_id(rid, id);
2014-02-10 02:10:30 +01:00
set_ownage(rid);
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->unlock();
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return rid;
}
2016-03-09 00:00:52 +01:00
_FORCE_INLINE_ T *get(const RID &p_rid) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
2016-03-09 00:00:52 +01:00
T **elem = id_map.getptr(p_rid.get_id());
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->unlock();
}
ERR_FAIL_COND_V(!elem, NULL);
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return *elem;
}
2016-03-09 00:00:52 +01:00
virtual bool owns(const RID &p_rid) const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
2016-03-09 00:00:52 +01:00
T **elem = id_map.getptr(p_rid.get_id());
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
2016-03-09 00:00:52 +01:00
return elem != NULL;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
virtual void free(RID p_rid) {
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
ERR_FAIL_COND(!owns(p_rid));
id_map.erase(p_rid.get_id());
}
virtual void get_owned_list(List<RID> *p_owned) const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
2016-03-09 00:00:52 +01:00
const ID *id = NULL;
while ((id = id_map.next(id))) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
RID rid;
set_id(rid, *id);
2014-02-10 02:10:30 +01:00
set_ownage(rid);
p_owned->push_back(rid);
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
mutex->lock();
}
}
RID_Owner() {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
mutex = Mutex::create();
}
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
~RID_Owner() {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (thread_safe) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memdelete(mutex);
}
}
};
#endif