Debian Codegraph @

Hardware/AccessoryCache.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
27 #include "DataTypes.h"
28 #include "DataModel/ObjectIdentifier.h"
29
30 class Manager;
31
32 namespace Hardware
33 {
34 class AccessoryCacheEntry
35 {
36 AccessoryCacheEntry() = delete;
37
38 public:
39 inline AccessoryCacheEntry(const ControlID controlId)
40 : controlId(controlId),
41 protocol(ProtocolNone),
42 address(AddressNone),
43 matchKey("")
44 {
45 }
46
47 inline ControlID GetControlID() const
48 {
49 return controlId;
50 }
51
52 inline DataModel::ObjectIdentifier GetObjectIdentifier() const
53 {
54 return objectIdentifier;
55 }
56
57 inline void SetObjectIdentifier(const DataModel::ObjectIdentifier objectIdentifier)
58 {
59 this->objectIdentifier = objectIdentifier;
60 }
61
62 inline void ClearObjectIdentifier()
63 {
64 this->objectIdentifier.Clear();
65 }
66
67 inline const std::string& GetName() const
68 {
69 return name;
70 }
71
72 inline void SetName(const std::string& name)
73 {
74 this->name = name;
75 }
76
77 inline Protocol GetProtocol() const
78 {
79 return protocol;
80 }
81
82 inline void SetProtocol(const Protocol protocol)
83 {
84 this->protocol = protocol;
85 }
86
87 inline Address GetAddress() const
88 {
89 return address;
90 }
91
92 inline void SetAddress(const Address address)
93 {
94 this->address = address;
95 }
96
97 inline const std::string& GetMatchKey() const
98 {
99 return matchKey;
100 }
101
102 inline void SetMatchKey(const std::string& matchKey)
103 {
104 this->matchKey = matchKey;
105 }
106
107 inline void SetMatchKey(const unsigned int matchKey)
108 {
109 this->matchKey = std::to_string(matchKey);
110 }
111
112 private:
113 const ControlID controlId;
114 DataModel::ObjectIdentifier objectIdentifier;
115 std::string name;
116 Protocol protocol;
117 Address address;
118 std::string matchKey;
119 };
120
121 class AccessoryCache
122 {
123 AccessoryCache() = delete;
124 AccessoryCache(const AccessoryCache& rhs) = delete;
125 AccessoryCache& operator= (const AccessoryCache& rhs) = delete;
126
127 public:
128 inline AccessoryCache(const ControlID controlId,
129 Manager* const manager)
130 : controlId(controlId),
131 manager(manager)
132 {
133 }
134
135 inline ControlID GetControlId() const
136 {
137 return controlId;
138 }
139
140 inline void Save(AccessoryCacheEntry& entry)
141 {
142 const std::string& oldMatchKey = entry.GetMatchKey();
143 Save(entry, oldMatchKey);
144 }
145
146 void Save(AccessoryCacheEntry& entry, const std::string& oldMatchKey);
147
148 DataModel::ObjectIdentifier Delete(const std::string& matchKey);
149
150 inline const AccessoryCacheEntry Get(const std::string& matchKey) const
151 {
152 return entries.count(matchKey) == 0 ? AccessoryCacheEntry(controlId) : entries.at(matchKey);
153 }
154
155 inline const std::map<std::string,AccessoryCacheEntry>& GetAll() const
156 {
157 return entries;
158 }
159
160 void SetObjectIdentifier(const DataModel::ObjectIdentifier objectIdentifier, const std::string& matchKey);
161
162 private:
163 bool UpdateData1(AccessoryCacheEntry& entry, const std::string& matchKey);
164 bool UpdateData2(AccessoryCacheEntry& entry, const std::string& matchKey);
165
166 const ControlID controlId;
167 Manager* const manager;
168 std::map<std::string,AccessoryCacheEntry> entries;
169 };
170 } // namespace Hardware
171