Plasma Engine  2.0
Loading...
Searching...
No Matches
GuidHandleMap.h
1#pragma once
2
3#include <EditorEngineProcessFramework/EditorEngineProcessFrameworkDLL.h>
4
5template <typename HandleType>
7{
8public:
9 void Clear()
10 {
11 m_GuidToHandle.Clear();
12 m_HandleToGuid.Clear();
13 }
14
15 void RegisterObject(plUuid guid, HandleType handle)
16 {
17 auto it = m_GuidToHandle.Find(guid);
18 if (it.IsValid())
19 {
20 // During undo/redo we may register the same object again. In that case, just use the new version.
21 UnregisterObject(guid);
22 }
23 m_GuidToHandle[guid] = handle;
24 m_HandleToGuid[handle] = guid;
25
26 PL_ASSERT_DEV(m_GuidToHandle.GetCount() == m_HandleToGuid.GetCount(), "1:1 relationship is broken. Check operator< for handle type.");
27 }
28
29 void UnregisterObject(plUuid guid)
30 {
31 const HandleType handle = m_GuidToHandle[guid];
32 m_GuidToHandle.Remove(guid);
33 m_HandleToGuid.Remove(handle);
34
35 PL_ASSERT_DEV(m_GuidToHandle.GetCount() == m_HandleToGuid.GetCount(), "1:1 relationship is broken. Check operator< for handle type.");
36 }
37
38 void UnregisterObject(HandleType handle)
39 {
40 const plUuid guid = m_HandleToGuid[handle];
41 m_GuidToHandle.Remove(guid);
42 m_HandleToGuid.Remove(handle);
43
44 PL_ASSERT_DEV(m_GuidToHandle.GetCount() == m_HandleToGuid.GetCount(), "1:1 relationship is broken. Check operator< for handle type.");
45 }
46
47 HandleType GetHandle(plUuid guid) const
48 {
49 HandleType res = HandleType();
50 m_GuidToHandle.TryGetValue(guid, res);
51 return res;
52 }
53
54 plUuid GetGuid(HandleType handle) const { return m_HandleToGuid.GetValueOrDefault(handle, plUuid()); }
55
56 const plMap<HandleType, plUuid>& GetHandleToGuidMap() const { return m_HandleToGuid; }
57
58private:
60 plMap<HandleType, plUuid> m_HandleToGuid;
61};
Definition GuidHandleMap.h:7
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashTable_inl.h:343
bool TryGetValue(const CompatibleKeyType &key, ValueType &out_value) const
Returns whether an entry with the given key was found and if found writes out the corresponding value...
ConstIterator Find(const CompatibleKeyType &key) const
Searches for key, returns a plHashTableBaseConstIterator to it or an invalid iterator,...
Definition HashTable_inl.h:528
void Clear()
Clears the table.
Definition HashTable_inl.h:355
bool Remove(const CompatibleKeyType &key, ValueType *out_pOldValue=nullptr)
Removes the entry with the given key. Returns whether an entry was removed and optionally writes out ...
Definition HashTable.h:333
void Clear()
Destroys all elements in the map and resets its size to zero.
Definition Map_inl.h:175
bool Remove(const CompatibleKeyType &key)
Erases the key/value pair with the given key, if it exists. O(log n) operation.
Definition Map_inl.h:545
plUInt32 GetCount() const
Returns the number of elements currently stored in the map. O(1) operation.
Definition Map_inl.h:200
const ValueType & GetValueOrDefault(const CompatibleKeyType &key, const ValueType &defaultValue) const
Either returns the value of the entry with the given key, if found, or the provided default value.
Definition Map.h:408
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11