31 #ifndef SWORD25_OBJECTREGISTRY_H 32 #define SWORD25_OBJECTREGISTRY_H 34 #include "common/func.h" 35 #include "common/hashmap.h" 36 #include "common/textconsole.h" 37 #include "sword25/kernel/common.h" 47 uint registerObject(T *objectPtr) {
50 error(
"Cannot register a null pointer.");
55 uint handle = findHandleByPtr(objectPtr);
57 warning(
"Tried to register a object that was already registered.");
62 _handle2PtrMap[_nextHandle] = objectPtr;
63 _ptr2HandleMap[objectPtr] = _nextHandle;
69 uint registerObject(T *objectPtr, uint handle) {
71 if (objectPtr == 0 || handle == 0) {
72 error(
"Cannot register a null pointer or a null handle.");
77 uint handleTest = findHandleByPtr(objectPtr);
78 if (handleTest != 0) {
79 error(
"Tried to register a object that was already registered.");
83 else if (findPtrByHandle(handle) != 0) {
84 error(
"Tried to register a handle that is already taken.");
89 _handle2PtrMap[handle] = objectPtr;
90 _ptr2HandleMap[objectPtr] = handle;
94 if (handle >= _nextHandle)
95 _nextHandle = handle + 1;
101 void deregisterObject(T *objectPtr) {
102 uint handle = findHandleByPtr(objectPtr);
106 _handle2PtrMap.
erase(findHandleByPtr(objectPtr));
107 _ptr2HandleMap.
erase(objectPtr);
109 warning(
"Tried to remove a object that was not registered.");
113 T *resolveHandle(uint handle) {
115 T *objectPtr = findPtrByHandle(handle);
121 uint resolvePtr(T *objectPtr) {
123 uint handle = findHandleByPtr(objectPtr);
131 bool operator()(
const T *x,
const T *y)
const {
136 uint operator()(
const T *x)
const {
144 HANDLE2PTR_MAP _handle2PtrMap;
145 PTR2HANDLE_MAP _ptr2HandleMap;
148 T *findPtrByHandle(uint handle) {
150 typename HANDLE2PTR_MAP::const_iterator it = _handle2PtrMap.find(handle);
153 return (it != _handle2PtrMap.end()) ? it->_value : 0;
156 uint findHandleByPtr(T *objectPtr) {
158 typename PTR2HANDLE_MAP::const_iterator it = _ptr2HandleMap.find(objectPtr);
161 return (it != _ptr2HandleMap.end()) ? it->_value : 0;
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void erase(iterator entry)
Definition: hashmap.h:710
Definition: objectregistry.h:130
Definition: objectregistry.h:135
Definition: objectregistry.h:42