wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
SceneItemRegistry.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <algorithm>
7
8#include <QtGlobal>
9
10#include "App/Core/ItemWithId.h"
11
13{
14 return m_elementRegistry.value(id, nullptr);
15}
16
17bool SceneItemRegistry::contains(const int id) const
18{
19 return m_elementRegistry.contains(id);
20}
21
23{
24 return m_lastId;
25}
26
27void SceneItemRegistry::setLastId(const int newLastId)
28{
29 m_lastId = (std::max)(m_lastId, newLastId);
30}
31
33{
34 return ++m_lastId;
35}
36
37void SceneItemRegistry::updateItemId(ItemWithId *item, const int newId)
38{
39 // Called before addItem() to pre-assign a specific ID (undo/redo restore path).
40 // The item is not yet in the registry; addItem() will preserve this positive ID.
41 item->setId(newId);
42 setLastId(newId);
43}
44
46{
47 if (auto *item = m_elementRegistry.value(id, nullptr)) {
48 item->setRegistry(nullptr);
49 }
50 m_elementRegistry.remove(id);
51}
52
54{
55 if (!item) {
56 return;
57 }
58 // HC invariant: an id must map to exactly one live item — never to a stale pointer
59 // left behind by a missed unregisterItem (the WIREDPANDA-HC family upstream condition)
60 Q_ASSERT(!m_elementRegistry.contains(item->id())
61 || m_elementRegistry.value(item->id()) == item);
62 m_elementRegistry[item->id()] = item;
63 item->setRegistry(this);
64}
65
67{
68 if (!item) {
69 return;
70 }
71 m_elementRegistry.remove(item->id());
72 item->setRegistry(nullptr);
73 // HC postcondition: after unregister, the registry must not still resolve this id
74 Q_ASSERT(!m_elementRegistry.contains(item->id()));
75}
76
78{
79 if (!item) {
80 return;
81 }
82 // Identity-checked: a reused id may already have been claimed by a new item by the
83 // time this fires (e.g. undo/redo restoring a different item under the same id).
84 if (m_elementRegistry.value(item->id()) == item) {
85 m_elementRegistry.remove(item->id());
86 }
87}
88
90{
91 for (auto *item : std::as_const(m_elementRegistry)) {
92 item->setRegistry(nullptr);
93 }
94}
Defines ItemWithId, the base class for all uniquely identified circuit items.
SceneItemRegistry: maps stable integer IDs to the scene's identifiable items.
Base class providing a unique integer identifier for circuit items.
Definition ItemWithId.h:31
void setId(const int id)
Sets the identifier to id.
Definition ItemWithId.h:46
int id() const
Returns the unique integer identifier of this item, or -1 if unassigned.
Definition ItemWithId.h:40
void registerItem(ItemWithId *item)
Registers item under its current id.
int nextId()
Returns a fresh, previously unused id.
bool contains(int id) const
Returns true if an item is registered under id.
void unregisterItem(ItemWithId *item)
Removes item's mapping from the registry.
void updateItemId(ItemWithId *item, int newId)
Pre-assigns newId to item before it is added (undo/redo restore path).
int lastId() const
Returns the highest id assigned so far.
void forget(ItemWithId *item)
void setLastId(int newLastId)
Raises the last-assigned id to newLastId (never lowers it).
ItemWithId * itemById(int id) const
Returns the item registered under id, or nullptr.