2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* rid.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
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 "safe_refcount.h"
|
|
|
|
#include "typedefs.h"
|
|
|
|
#include "os/memory.h"
|
2016-10-03 21:33:42 +02:00
|
|
|
#include "set.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
class RID_OwnerBase;
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
class RID {
|
|
|
|
friend class RID_OwnerBase;
|
2016-10-03 21:33:42 +02:00
|
|
|
|
|
|
|
mutable RID_Data *_data;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ RID_Data *get_data() const { return _data; }
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const RID& p_rid) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return _data==p_rid._data;
|
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
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return _data < p_rid._data;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ bool operator<=(const RID& p_rid) const {
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return _data <= p_rid._data;
|
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
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return _data > p_rid._data;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ bool operator!=(const RID& p_rid) const {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
return _data!=p_rid._data;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ bool is_valid() const { return _data!=NULL; }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ uint32_t get_id() const { return _data?_data->get_id():0; }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ RID() {
|
2016-10-03 21:33:42 +02:00
|
|
|
_data=NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class RID_OwnerBase {
|
|
|
|
protected:
|
2016-10-03 21:33:42 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-01-10 11:08:40 +01:00
|
|
|
_FORCE_INLINE_ bool _is_owner(const RID& p_rid) const {
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-01-10 11:08:40 +01:00
|
|
|
return this==p_rid._data->_owner;
|
2016-10-03 21:33:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ void _remove_owner(RID& p_rid) {
|
|
|
|
|
2017-01-10 11:08:40 +01:00
|
|
|
p_rid._data->_owner=NULL;
|
2016-10-03 21:33:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
#
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
virtual void get_owned_list(List<RID> *p_owned)=0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
static void init_rid();
|
2014-02-10 02:10:30 +01:00
|
|
|
virtual ~RID_OwnerBase() {}
|
|
|
|
};
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
template<class T>
|
2014-02-10 02:10:30 +01:00
|
|
|
class RID_Owner : public RID_OwnerBase {
|
|
|
|
public:
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
mutable Set<RID_Data*> id_map;
|
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ RID make_rid(T * p_data) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RID rid;
|
2016-10-03 21:33:42 +02:00
|
|
|
_set_data(rid,p_data);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
id_map.insert(p_data) ;
|
|
|
|
#endif
|
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
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ T * get(const RID& p_rid) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
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());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ T * getornull(const RID& p_rid) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
if (p_rid.get_data()) {
|
|
|
|
ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()),NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
#endif
|
|
|
|
return static_cast<T*>(p_rid.get_data());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
_FORCE_INLINE_ T * getptr(const RID& p_rid) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
return static_cast<T*>(p_rid.get_data());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-19 16:14:41 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
_FORCE_INLINE_ bool owns(const RID& p_rid) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
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
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void free(RID p_rid) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
id_map.erase(p_rid.get_data());
|
|
|
|
#else
|
|
|
|
_remove_owner(p_rid);
|
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
void get_owned_list(List<RID> *p_owned) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-10-03 21:33:42 +02:00
|
|
|
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);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
#endif
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|