Debian Codegraph @

Hardware/AccessoryCache.cpp

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include "DataModel/Accessory.h"
22 #include "DataModel/ObjectIdentifier.h"
23 #include "DataModel/Signal.h"
24 #include "DataModel/Switch.h"
25 #include "Hardware/AccessoryCache.h"
26 #include "Manager.h"
27
28 namespace Hardware
29 {
30 void AccessoryCache::Save(AccessoryCacheEntry& entry, const std::string& oldMatchKey)
31 {
32 const std::string& matchKey = entry.GetMatchKey();
33
34 bool ret = false;
35 const bool matchKeyChanged = matchKey.compare(oldMatchKey) != 0;
36 if (matchKeyChanged)
37 {
38 ret = UpdateData1(entry, oldMatchKey);
39 }
40
41 if (!ret)
42 {
43 ret = UpdateData2(entry, oldMatchKey);
44 }
45
46 if (!ret && matchKeyChanged)
47 {
48 ret = UpdateData2(entry, matchKey);
49 }
50 entries.emplace(matchKey, entry);
51 }
52
53 bool AccessoryCache::UpdateData1(AccessoryCacheEntry& entry, const std::string& matchKey)
54 {
55 const DataModel::ObjectIdentifier objectIdentifier = Delete(matchKey);
56 switch (objectIdentifier.GetObjectType())
57 {
58 case ObjectTypeAccessory:
59 {
60 DataModel::Accessory* accessory = manager->GetAccessory(objectIdentifier.GetObjectID());
61 if (accessory)
62 {
63 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeAccessory, accessory->GetID()));
64 *accessory = entry;
65 return true;
66 }
67 return false;
68 }
69
70 case ObjectTypeSignal:
71 {
72 DataModel::Signal* signal = manager->GetSignal(objectIdentifier.GetObjectID());
73 if (signal)
74 {
75 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeSignal, signal->GetID()));
76 *signal = entry;
77 return true;
78 }
79 return false;
80 }
81
82 case ObjectTypeSwitch:
83 {
84 DataModel::Switch* mySwitch = manager->GetSwitch(objectIdentifier.GetObjectID());
85 if (mySwitch)
86 {
87 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeSwitch, mySwitch->GetID()));
88 *mySwitch = entry;
89 return true;
90 }
91 return false;
92 }
93
94 default:
95 return false;
96 }
97 }
98
99 bool AccessoryCache::UpdateData2(AccessoryCacheEntry& entry, const std::string& matchKey)
100 {
101 DataModel::Accessory* accessory = manager->GetAccessoryByMatchKey(GetControlId(), matchKey);
102 if (accessory != nullptr)
103 {
104 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeAccessory, accessory->GetID()));
105 *accessory = entry;
106 return true;
107 }
108 DataModel::Signal* signal = manager->GetSignalByMatchKey(GetControlId(), matchKey);
109 if (signal != nullptr)
110 {
111 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeSignal, signal->GetID()));
112 *signal = entry;
113 return true;
114 }
115 DataModel::Switch* mySwitch = manager->GetSwitchByMatchKey(GetControlId(), matchKey);
116 if (mySwitch != nullptr)
117 {
118 entry.SetObjectIdentifier(DataModel::ObjectIdentifier(ObjectTypeSwitch, mySwitch->GetID()));
119 *mySwitch = entry;
120 return true;
121 }
122 return false;
123 }
124
125 DataModel::ObjectIdentifier AccessoryCache::Delete(const std::string& matchKey)
126 {
127 DataModel::ObjectIdentifier objectIdentifier = Get(matchKey).GetObjectIdentifier();
128 entries.erase(matchKey);
129 const ObjectType objectType = objectIdentifier.GetObjectType();
130 switch (objectType)
131 {
132 case ObjectTypeAccessory:
133 {
134 const AccessoryID accessoryId = objectIdentifier.GetObjectID();
135 manager->AccessoryRemoveMatchKey(accessoryId);
136 objectIdentifier.SetObjectID(accessoryId);
137 break;
138 }
139
140 case ObjectTypeSignal:
141 {
142 const SignalID signalId = objectIdentifier.GetObjectID();
143 manager->SignalRemoveMatchKey(signalId);
144 objectIdentifier.SetObjectID(signalId);
145 break;
146 }
147
148 case ObjectTypeSwitch:
149 {
150 const SwitchID switchId = objectIdentifier.GetObjectID();
151 manager->SwitchRemoveMatchKey(switchId);
152 objectIdentifier.SetObjectID(switchId);
153 break;
154 }
155
156 default:
157 return DataModel::ObjectIdentifier();
158 }
159 return objectIdentifier;
160 }
161
162 void AccessoryCache::SetObjectIdentifier(const DataModel::ObjectIdentifier objectIdentifier, const std::string& matchKey)
163 {
164 if (objectIdentifier.IsSet())
165 {
166 for (auto& accessoryCacheEntry : entries)
167 {
168 AccessoryCacheEntry& entry = accessoryCacheEntry.second;
169 if (entry.GetObjectIdentifier() == objectIdentifier)
170 {
171 entry.ClearObjectIdentifier();
172 }
173 }
174 }
175 auto entry = entries.find(matchKey);
176 if (entry == entries.end())
177 {
178 return;
179 }
180 entry->second.SetObjectIdentifier(objectIdentifier);
181 }
182 } // namespace Hardware
183