Debian Codegraph @

Hardware/FeedbackCache.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
29 class Manager;
30
31 namespace Hardware
32 {
33 class FeedbackCacheEntry
34 {
35 public:
36 FeedbackCacheEntry() = delete;
37
38 inline FeedbackCacheEntry(const ControlID controlId)
39 : controlId(controlId),
40 feedbackId(FeedbackNone),
41 pin(FeedbackPinNone),
42 matchKey("")
43 {
44 }
45
46 inline ControlID GetControlID() const
47 {
48 return controlId;
49 }
50
51 inline FeedbackID GetFeedbackID() const
52 {
53 return feedbackId;
54 }
55
56 inline void SetFeedbackID(const FeedbackID feedbackId)
57 {
58 this->feedbackId = feedbackId;
59 }
60
61 inline const std::string& GetName() const
62 {
63 return name;
64 }
65
66 inline void SetName(const std::string& name)
67 {
68 this->name = name;
69 }
70
71 inline FeedbackPin GetPin() const
72 {
73 return pin;
74 }
75
76 inline void SetPin(const FeedbackPin pin)
77 {
78 this->pin = pin;
79 }
80
81 inline const std::string& GetMatchKey() const
82 {
83 return matchKey;
84 }
85
86 inline void SetMatchKey(const std::string& matchKey)
87 {
88 this->matchKey = matchKey;
89 }
90
91 inline void SetMatchKey(const unsigned int matchKey)
92 {
93 this->matchKey = std::to_string(matchKey);
94 }
95
96 private:
97 const ControlID controlId;
98 FeedbackID feedbackId;
99 std::string name;
100 FeedbackPin pin;
101 std::string matchKey;
102 };
103
104 class FeedbackCache
105 {
106 public:
107 inline FeedbackCache(const ControlID controlId,
108 Manager* const manager)
109 : controlId(controlId),
110 manager(manager)
111 {
112 }
113
114 FeedbackCache() = delete;
115
116 FeedbackCache(const FeedbackCache& rhs) = delete;
117
118 FeedbackCache& operator= (const FeedbackCache& rhs) = delete;
119
120 inline ControlID GetControlId() const
121 {
122 return controlId;
123 }
124
125 inline void Save(FeedbackCacheEntry& entry)
126 {
127 const std::string& oldMatchKey = entry.GetMatchKey();
128 Save(entry, oldMatchKey);
129 }
130
131 void Save(FeedbackCacheEntry& entry, const std::string& oldMatchKey);
132
133 FeedbackID Delete(const std::string& matchKey);
134
135 inline const FeedbackCacheEntry Get(const std::string& matchKey) const
136 {
137 return entries.count(matchKey) == 0 ? FeedbackCacheEntry(controlId) : entries.at(matchKey);
138 }
139
140 inline const std::map<std::string,FeedbackCacheEntry>& GetAll() const
141 {
142 return entries;
143 }
144
145 void SetFeedbackId(const FeedbackID locoId, const std::string& matckKey);
146
147 private:
148 const ControlID controlId;
149 Manager* const manager;
150 std::map<std::string,FeedbackCacheEntry> entries;
151 };
152 } // namespace Hardware
153