Debian Codegraph @

Hardware/LocoCache.h

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 #pragma once
22
23 #include <cstring>
24 #include <string>
25 #include <map>
26 #include <vector>
27
28 #include "DataTypes.h"
29 #include "DataModel/LocoFunctions.h"
30
31 class Manager;
32
33 namespace Hardware
34 {
35 class LocoCacheEntrySlave
36 {
37 public:
38 LocoCacheEntrySlave() = delete;
39
40 inline LocoCacheEntrySlave(const Protocol protocol,
41 const Address address,
42 const std::string matchKey)
43 : protocol(protocol),
44 address(address),
45 matchKey(matchKey),
46 locoID(LocoNone)
47 {
48 }
49
50 inline Protocol GetProtocol() const
51 {
52 return protocol;
53 }
54
55 inline Address GetAddress() const
56 {
57 return address;
58 }
59
60 inline const std::string& GetMatchKey() const
61 {
62 return matchKey;
63 }
64
65 inline void SetLocoID(LocoID locoID)
66 {
67 this->locoID = locoID;
68 }
69
70 inline LocoID GetLocoID() const
71 {
72 return locoID;
73 }
74
75 private:
76 Protocol protocol;
77 Address address;
78 std::string matchKey;
79 LocoID locoID;
80 };
81
82 class LocoCacheEntry
83 {
84 public:
85 LocoCacheEntry() = delete;
86
87 inline LocoCacheEntry(const ControlID controlId)
88 : controlId(controlId),
89 locoId(LocoNone),
90 protocol(ProtocolNone),
91 address(AddressNone),
92 type(LocoTypeLoco),
93 matchKey("")
94 {
95 }
96
97 inline ControlID GetControlID() const
98 {
99 return controlId;
100 }
101
102 inline LocoID GetLocoID() const
103 {
104 return locoId;
105 }
106
107 inline void SetLocoID(const LocoID locoId)
108 {
109 this->locoId = locoId;
110 }
111
112 inline const std::string& GetName() const
113 {
114 return name;
115 }
116
117 inline void SetName(const std::string& name)
118 {
119 this->name = name;
120 }
121
122 inline Protocol GetProtocol() const
123 {
124 return protocol;
125 }
126
127 inline void SetProtocol(const Protocol protocol)
128 {
129 this->protocol = protocol;
130 }
131
132 inline Address GetAddress() const
133 {
134 return address;
135 }
136
137 inline void SetAddress(const Address address)
138 {
139 this->address = address;
140 }
141
142 inline LocoType GetType() const
143 {
144 return type;
145 }
146
147 inline void SetType(LocoType type)
148 {
149 this->type = type;
150 }
151
152 inline void SetFunction(const DataModel::LocoFunctionNr nr,
153 const DataModel::LocoFunctionType type,
154 const DataModel::LocoFunctionIcon icon,
155 const DataModel::LocoFunctionTimer timer)
156 {
157 functions.SetFunction(nr, type, icon, timer);
158 }
159
160 inline void ClearFunction(const DataModel::LocoFunctionNr nr)
161 {
162 functions.ClearFunction(nr);
163 }
164
165 inline std::vector<DataModel::LocoFunctionEntry> GetFunctionStates() const
166 {
167 return functions.GetFunctionStates();
168 }
169
170 inline const std::string& GetMatchKey() const
171 {
172 return matchKey;
173 }
174
175 inline void SetMatchKey(const std::string& matchKey)
176 {
177 this->matchKey = matchKey;
178 }
179
180 inline void SetMatchKey(const unsigned int matchKey)
181 {
182 this->matchKey = std::to_string(matchKey);
183 }
184
185 inline void AddSlave(const Protocol protocol, const Address address, const std::string& matchKey)
186 {
187 slaves.push_back(LocoCacheEntrySlave(protocol, address, matchKey));
188 }
189
190 void UpdateSlaves(Manager* manager);
191
192 inline std::vector<LocoID> GetSlaveIDs() const
193 {
194 std::vector<LocoID> out;
195 for (auto const & slave : slaves)
196 {
197 const LocoID slaveID = slave.GetLocoID();
198 if (slaveID == LocoNone)
199 {
200 continue;
201 }
202 out.push_back(slaveID);
203 }
204 return out;
205 }
206
207 private:
208 const ControlID controlId;
209 LocoID locoId;
210 std::string name;
211 Protocol protocol;
212 Address address;
213 LocoType type;
214 std::string matchKey;
215 DataModel::LocoFunctions functions;
216 std::vector<LocoCacheEntrySlave> slaves;
217 };
218
219 class LocoCache
220 {
221 public:
222 inline LocoCache(const ControlID controlId,
223 Manager* const manager)
224 : controlId(controlId),
225 manager(manager)
226 {
227 }
228
229 LocoCache() = delete;
230
231 LocoCache(const LocoCache& rhs) = delete;
232
233 LocoCache& operator= (const LocoCache& rhs) = delete;
234
235 inline ControlID GetControlId() const
236 {
237 return controlId;
238 }
239
240 inline void Save(LocoCacheEntry& entry)
241 {
242 const std::string& oldMatchKey = entry.GetMatchKey();
243 Save(entry, oldMatchKey);
244 }
245
246 void Save(LocoCacheEntry& entry, const std::string& oldMatchKey);
247
248 LocoID Delete(const std::string& matchKey);
249
250 inline const LocoCacheEntry Get(const std::string& matchKey) const
251 {
252 return entries.count(matchKey) == 0 ? LocoCacheEntry(controlId) : entries.at(matchKey);
253 }
254
255 inline const std::map<std::string,LocoCacheEntry>& GetAll() const
256 {
257 return entries;
258 }
259
260 void SetLocoId(const LocoID locoId, const std::string& matckKey);
261
262 inline void UpdateSlaves()
263 {
264 for (auto & entry : entries)
265 {
266 entry.second.UpdateSlaves(manager);
267 }
268 }
269
270 private:
271 const ControlID controlId;
272 Manager* const manager;
273 std::map<std::string,LocoCacheEntry> entries;
274 };
275 } // namespace Hardware
276