diff options
Diffstat (limited to 'core/rid.h')
-rw-r--r-- | core/rid.h | 206 |
1 files changed, 110 insertions, 96 deletions
diff --git a/core/rid.h b/core/rid.h index 2de6956096..8dc535c9c1 100644 --- a/core/rid.h +++ b/core/rid.h @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,183 +33,197 @@ #include "safe_refcount.h" #include "typedefs.h" #include "os/memory.h" -#include "hash_map.h" +#include "set.h" #include "list.h" /** @author Juan Linietsky <reduzio@gmail.com> */ + class RID_OwnerBase; -typedef uint32_t ID; +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; - ID _id; - RID_OwnerBase *owner; + + mutable RID_Data *_data; + public: - _FORCE_INLINE_ ID get_id() const { return _id; } - bool operator==(const RID& p_rid) const { + _FORCE_INLINE_ RID_Data *get_data() const { return _data; } + + _FORCE_INLINE_ bool operator==(const RID& p_rid) const { - return _id==p_rid._id; + return _data==p_rid._data; } _FORCE_INLINE_ bool operator<(const RID& p_rid) const { - return _id < p_rid._id; + return _data < p_rid._data; } _FORCE_INLINE_ bool operator<=(const RID& p_rid) const { - return _id <= p_rid._id; + return _data <= p_rid._data; } _FORCE_INLINE_ bool operator>(const RID& p_rid) const { - return _id > p_rid._id; + return _data > p_rid._data; } - bool operator!=(const RID& p_rid) const { + _FORCE_INLINE_ bool operator!=(const RID& p_rid) const { - return _id!=p_rid._id; + return _data!=p_rid._data; } - _FORCE_INLINE_ bool is_valid() const { return _id>0; } + _FORCE_INLINE_ bool is_valid() const { return _data!=NULL; } - operator const void*() const { - return is_valid() ? this : 0; - }; + _FORCE_INLINE_ uint32_t get_id() const { return _data?_data->get_id():0; } _FORCE_INLINE_ RID() { - _id = 0; - owner=0; + _data=NULL; } + }; 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); } - ID new_ID(); + + 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 bool owns(const RID& p_rid) const=0; - virtual void get_owned_list(List<RID> *p_owned) const=0; - static void init_rid(); + virtual void get_owned_list(List<RID> *p_owned)=0; + static void init_rid(); virtual ~RID_OwnerBase() {} }; -template<class T,bool thread_safe=false> +template<class T> class RID_Owner : public RID_OwnerBase { public: - - typedef void (*ReleaseNotifyFunc)(void*user,T *p_data); -private: - - Mutex *mutex; - mutable HashMap<ID,T*> id_map; - +#ifdef DEBUG_ENABLED + mutable Set<RID_Data*> id_map; +#endif public: - RID make_rid(T * p_data) { + _FORCE_INLINE_ RID make_rid(T * p_data) { - if (thread_safe) { - mutex->lock(); - } - ID id = new_ID(); - id_map[id]=p_data; RID rid; - set_id(rid,id); - set_ownage(rid); + _set_data(rid,p_data); - if (thread_safe) { - mutex->unlock(); - } +#ifdef DEBUG_ENABLED + id_map.insert(p_data) ; +#endif return rid; } _FORCE_INLINE_ T * get(const RID& p_rid) { - if (thread_safe) { - mutex->lock(); - } - - T**elem = id_map.getptr(p_rid.get_id()); +#ifdef DEBUG_ENABLED - if (thread_safe) { - mutex->unlock(); - } - - ERR_FAIL_COND_V(!elem,NULL); - - return *elem; + 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()); } - virtual bool owns(const RID& p_rid) const { + _FORCE_INLINE_ T * getornull(const RID& p_rid) { - if (thread_safe) { - mutex->lock(); - } - - T**elem = id_map.getptr(p_rid.get_id()); +#ifdef DEBUG_ENABLED - if (thread_safe) { - mutex->lock(); + 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()); - return elem!=NULL; } - virtual void free(RID p_rid) { - 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 { - - if (thread_safe) { - mutex->lock(); - } + _FORCE_INLINE_ T * getptr(const RID& p_rid) { - const ID*id=NULL; - while((id=id_map.next(id))) { + return static_cast<T*>(p_rid.get_data()); - RID rid; - set_id(rid,*id); - set_ownage(rid); - p_owned->push_back(rid); + } - } - if (thread_safe) { - mutex->lock(); - } + _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 } - RID_Owner() { - if (thread_safe) { - - mutex = Mutex::create(); - } + 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) { + - ~RID_Owner() { - if (thread_safe) { +#ifdef DEBUG_ENABLED - memdelete(mutex); + 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 + } + }; |