ngscopeclient v0.2
Loading...
Searching...
No Matches
IDTable.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2026 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#include <type_traits>
41
49{
50public:
51
53 typedef std::map<uintptr_t, SerializableObject*, std::less<uintptr_t> > forwardType;
54
56 typedef std::map<SerializableObject*, uintptr_t, std::less<SerializableObject*> > reverseType;
57
59 typename forwardType::const_iterator begin()
60 { return m_forwardMap.begin(); }
61
63 typename forwardType::const_iterator end()
64 { return m_forwardMap.end(); }
65
66public:
67 IDTable()
68 : m_nextID(1)
69 {
70 emplace(0, nullptr);
71 }
72
81 {
82 if(HasID(p))
83 return m_reverseMap[p];
84
85 uint32_t id = m_nextID ++;
86 m_forwardMap[id] = p;
87 m_reverseMap[p] = id;
88 return id;
89 }
90
98 {
99 ReserveID(id);
100 m_forwardMap[id] = p;
101 m_reverseMap[p] = id;
102 }
103
110 { return (m_reverseMap.find(p) != m_reverseMap.end()); }
111
118 { return (m_forwardMap.find(id) != m_forwardMap.end()); }
119
124 { m_nextID = std::max(m_nextID, id+1); }
125
129 template<class T>
131 {
132 static_assert(std::is_base_of_v<SerializableObject, T> == true);
133 return dynamic_cast<T*>(m_forwardMap[id]);
134 }
135
139
143 void clear()
144 {
145 m_forwardMap.clear();
146 m_reverseMap.clear();
147 m_nextID = 1;
148 }
149
156 {
157 auto value = m_forwardMap[key];
158 m_forwardMap.erase(key);
159 m_reverseMap.erase(value);
160 }
161
168 {
169 auto value = m_reverseMap[key];
170 m_reverseMap.erase(key);
171 m_forwardMap.erase(value);
172 }
173
184
186 size_t size() const
187 { return m_forwardMap.size(); }
188
189protected:
190
193
196
199};
200
201#endif
Declaration of SerializableObject.
Definition AcceleratorBuffer.h:204
Bidirectional table mapping integer IDs in scopesession files to object pointers.
Definition IDTable.h:49
void ReserveID(uintptr_t id)
Marks an ID as unavailable for use, without assigning an pointer to it.
Definition IDTable.h:123
void erase(SerializableObject *key)
Erase an entry given a reverse key.
Definition IDTable.h:167
size_t size() const
Return the number of entries in the bijection.
Definition IDTable.h:186
reverseType m_reverseMap
Map of object-to-object in the reverse direction.
Definition IDTable.h:198
std::map< SerializableObject *, uintptr_t, std::less< SerializableObject * > > reverseType
Type of the reverse map.
Definition IDTable.h:56
void replace(SerializableObject *oldval, SerializableObject *newval)
Replaces one value with another, keeping the keys identical.
Definition IDTable.h:177
T * Lookup(uintptr_t id)
Type-safe object lookup.
Definition IDTable.h:130
void clear()
Deletes all entries from the table.
Definition IDTable.h:143
uintptr_t emplace(SerializableObject *p)
Store a new object in the table.
Definition IDTable.h:80
bool HasID(uintptr_t id)
Checks if we have an object with a specific ID.
Definition IDTable.h:117
uintptr_t m_nextID
Index of the next ID to be assigned.
Definition IDTable.h:192
std::map< uintptr_t, SerializableObject *, std::less< uintptr_t > > forwardType
Type of the forward map.
Definition IDTable.h:53
forwardType m_forwardMap
Map of object-to-object in the forward direction.
Definition IDTable.h:195
void emplace(uintptr_t id, SerializableObject *p)
Store a new object in the table using a specific ID.
Definition IDTable.h:97
bool HasID(SerializableObject *p)
Checks if we have an object at a specific pointer.
Definition IDTable.h:109
uintptr_t operator[](SerializableObject *key)
Forward lookup.
Definition IDTable.h:137
void erase(uintptr_t key)
Erase an entry given a forward key.
Definition IDTable.h:155
forwardType::const_iterator begin()
Get an iterator to the start of the forward map.
Definition IDTable.h:59
forwardType::const_iterator end()
Get an iterator to the end of the forward map.
Definition IDTable.h:63
Definition SerializableObject.h:40