ngscopeclient 0.1-dev+51fbda87c
IDTable.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2024 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
48class IDTable : public Bijection<uintptr_t, void*>
49{
50public:
51 IDTable()
52 : m_nextID(1)
53 {
54 emplace(0, nullptr);
55 }
56
64 uintptr_t emplace(void* p)
65 {
66 if(HasID(p))
67 return m_reverseMap[p];
68
69 uint32_t id = m_nextID ++;
70 Bijection::emplace(id, p);
71 return id;
72 }
73
80 void emplace(uintptr_t id, void* p)
81 {
82 ReserveID(id);
83 Bijection::emplace(id, p);
84 }
85
91 bool HasID(void* p)
92 { return (m_reverseMap.find(p) != m_reverseMap.end()); }
93
99 bool HasID(uintptr_t id)
100 { return (m_forwardMap.find(id) != m_forwardMap.end()); }
101
105 void ReserveID(uintptr_t id)
106 { m_nextID = std::max(m_nextID, id+1); }
107
111 void clear()
112 {
113 m_forwardMap.clear();
114 m_reverseMap.clear();
115 m_nextID = 1;
116 }
117
118protected:
119
121 uintptr_t m_nextID;
122};
123
124#endif
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:49
void ReserveID(uintptr_t id)
Marks an ID as unavailable for use, without assigning an pointer to it.
Definition: IDTable.h:105
uintptr_t emplace(void *p)
Store a new object in the table.
Definition: IDTable.h:64
void clear()
Deletes all entries from the table.
Definition: IDTable.h:111
bool HasID(void *p)
Checks if we have an object at a specific pointer.
Definition: IDTable.h:91
bool HasID(uintptr_t id)
Checks if we have an object with a specific ID.
Definition: IDTable.h:99
uintptr_t m_nextID
Index of the next ID to be assigned.
Definition: IDTable.h:121
void emplace(uintptr_t id, void *p)
Store a new object in the table using a specific ID.
Definition: IDTable.h:80