ngscopeclient v0.1-rc1
IDTable.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef IDTable_h
37#define IDTable_h
38
39#include "SerializableObject.h"
40
50class IDTable : public Bijection<uintptr_t, SerializableObject*>
51{
52public:
53 IDTable()
54 : m_nextID(1)
55 {
56 emplace(0, nullptr);
57 }
58
67 {
68 if(HasID(p))
69 return m_reverseMap[p];
70
71 uint32_t id = m_nextID ++;
72 Bijection::emplace(id, p);
73 return id;
74 }
75
82 void emplace(uintptr_t id, SerializableObject* p)
83 {
84 ReserveID(id);
85 Bijection::emplace(id, p);
86 }
87
94 { return (m_reverseMap.find(p) != m_reverseMap.end()); }
95
101 bool HasID(uintptr_t id)
102 { return (m_forwardMap.find(id) != m_forwardMap.end()); }
103
107 void ReserveID(uintptr_t id)
108 { m_nextID = std::max(m_nextID, id+1); }
109
113 template<class T>
114 T Lookup(uintptr_t id)
115 { return dynamic_cast<T>(m_forwardMap[id]); }
116
118 [[deprecated]]
120 { return m_forwardMap[key]; }
121
124 { return m_reverseMap[key]; }
125
129 virtual void clear()
130 {
131 m_forwardMap.clear();
132 m_reverseMap.clear();
133 m_nextID = 1;
134 }
135
136protected:
137
139 uintptr_t m_nextID;
140};
141
142#endif
Declaration of SerializableObject.
A strict one-to-one mapping from objects of type T1 to type T2 (which must be different types).
Definition: Bijection.h:48
forwardType m_forwardMap
Map of object-to-object in the forward direction.
Definition: Bijection.h:160
void emplace(T1 a, T2 b)
Adds a new entry to the bijection.
Definition: Bijection.h:73
reverseType m_reverseMap
Map of object-to-object in the reverse direction.
Definition: Bijection.h:163
Bidirectional table mapping integer IDs in scopesession files to object pointers.
Definition: IDTable.h:51
void ReserveID(uintptr_t id)
Marks an ID as unavailable for use, without assigning an pointer to it.
Definition: IDTable.h:107
uintptr_t emplace(SerializableObject *p)
Store a new object in the table.
Definition: IDTable.h:66
virtual void clear()
Deletes all entries from the table.
Definition: IDTable.h:129
SerializableObject * operator[](uintptr_t key)
Legacy object lookup.
Definition: IDTable.h:119
bool HasID(uintptr_t id)
Checks if we have an object with a specific ID.
Definition: IDTable.h:101
uintptr_t m_nextID
Index of the next ID to be assigned.
Definition: IDTable.h:139
T Lookup(uintptr_t id)
Type-safe object lookup.
Definition: IDTable.h:114
void emplace(uintptr_t id, SerializableObject *p)
Store a new object in the table using a specific ID.
Definition: IDTable.h:82
bool HasID(SerializableObject *p)
Checks if we have an object at a specific pointer.
Definition: IDTable.h:93
uintptr_t operator[](SerializableObject *key)
Forward lookup.
Definition: IDTable.h:123
Definition: SerializableObject.h:40