Debian Codegraph @

DataModel/Track.h

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 <mutex>
24 #include <string>
25 #include <vector>
26
27 #include "DataModel/Cluster.h"
28 #include "DataModel/Feedback.h"
29 #include "DataModel/LayoutItem.h"
30 #include "DataModel/LockableItem.h"
31 #include "DataModel/Relation.h"
32 #include "DataTypes.h"
33 #include "Logger/Logger.h"
34
35 class Manager;
36
37 namespace DataModel
38 {
39 class LocoBase;
40 class Route;
41
42 enum TrackType : unsigned char
43 {
44 TrackTypeStraight = 0,
45 TrackTypeTurn = 1,
46 TrackTypeEnd = 2,
47 TrackTypeBridge = 3,
48 TrackTypeTunnel = 4,
49 TrackTypeTunnelEnd = 5,
50 TrackTypeLink = 6,
51 TrackTypeCrossingLeft = 7,
52 TrackTypeCrossingRight = 8,
53 TrackTypeCrossingSymetric = 9
54 };
55
56 enum SelectRouteApproach : unsigned char
57 {
58 SelectRouteSystemDefault = 0,
59 SelectRouteDoNotCare = 1,
60 SelectRouteRandom = 2,
61 SelectRouteMinTrackLength = 3,
62 SelectRouteLongestUnused = 4
63 };
64
65 class Track : public LayoutItem, public LockableItem
66 {
67 public:
68 static const LayoutItem::LayoutItemSize MinLength = 1;
69 static const LayoutItem::LayoutItemSize MaxLength = 100;
70
71 inline Track(Manager* manager, const TrackID trackID)
72 : LayoutItem(trackID),
73 LockableItem(),
74 manager(manager),
75 trackType(TrackTypeStraight),
76 mainID(TrackNone),
77 mainTrack(nullptr),
78 cluster(nullptr),
79 clusterInverted(false),
80 selectRouteApproach(SelectRouteSystemDefault),
81 trackState(DataModel::Feedback::FeedbackStateFree),
82 trackStateDelayed(DataModel::Feedback::FeedbackStateFree),
83 locoBaseOrientation(OrientationRight),
84 blocked(false),
85 locoBaseDelayed(),
86 allowLocoTurn(true),
87 releaseWhenFree(false),
88 showName(true)
89 {
90 }
91
92 inline Track(Manager* manager, const std::string& serialized)
93 : Track(manager, TrackNone)
94 {
95 Deserialize(serialized);
96 }
97
98 inline ~Track()
99 {
100 DeleteFeedbacks();
101 DeleteSignals();
102 }
103
104 inline ObjectType GetObjectType() const override
105 {
106 return ObjectTypeTrack;
107 }
108
109 std::string Serialize() const override;
110
111 bool Deserialize(const std::string& serialized) override;
112
113 inline std::string GetLayoutType() const override
114 {
115 return Languages::GetText(Languages::TextTrack);
116 }
117
118 inline TrackType GetTrackType() const
119 {
120 return trackType;
121 }
122
123 inline void SetTrackType(const TrackType type)
124 {
125 this->trackType = type;
126 }
127
128 bool SetFeedbackState(const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState state);
129
130 inline DataModel::Feedback::FeedbackState GetStateDelayed() const
131 {
132 return trackStateDelayed;
133 }
134
135 bool AddRoute(Route* route);
136
137 bool DeleteRoute(Route* route);
138
139 inline SelectRouteApproach GetSelectRouteApproach() const
140 {
141 return selectRouteApproach;
142 }
143
144 inline void SetSelectRouteApproach(const SelectRouteApproach selectRouteApproach)
145 {
146 this->selectRouteApproach = selectRouteApproach;
147 }
148
149 inline const std::vector<const Route*> GetRoutes() const
150 {
151 std::vector<const Route*> out;
152 std::lock_guard<std::mutex> Guard(updateMutex);
153 for (auto route : routes)
154 {
155 out.push_back(route);
156 }
157 return out;
158 }
159
160 bool GetValidRoutes(Logger::Logger* logger,
161 const DataModel::LocoBase* loco,
162 const bool allowLocoTurn,
163 std::vector<Route*>& validRoutes) const;
164
165 inline Orientation GetLocoBaseOrientation() const
166 {
167 return locoBaseOrientation;
168 }
169
170 inline bool CanSetLocoBaseOrientation(const Orientation orientation)
171 {
172 return CanSetLocoBaseOrientation(orientation, GetLocoBase());
173 }
174
175 bool CanSetLocoBaseOrientation(const Orientation orientation, const ObjectIdentifier& locoBaseIdentifier);
176
177 bool SetLocoBaseOrientation(const Orientation orientation);
178
179 inline void SetLocoBaseOrientationForce(const Orientation orientation)
180 {
181 locoBaseOrientation = orientation;
182 }
183
184 inline bool GetBlocked() const
185 {
186 return blocked;
187 }
188
189 inline void SetBlocked(const bool blocked)
190 {
191 this->blocked = blocked;
192 }
193
194 inline const ObjectIdentifier& GetLocoBaseDelayed() const
195 {
196 return this->locoBaseDelayed;
197 }
198
199 inline bool GetReleaseWhenFree() const
200 {
201 return releaseWhenFree;
202 }
203
204 inline void SetReleaseWhenFree(const bool releaseWhenFree)
205 {
206 this->releaseWhenFree = releaseWhenFree;
207 }
208
209 inline bool GetShowName() const
210 {
211 return this->showName;
212 }
213
214 inline void SetShowName(const bool showName)
215 {
216 this->showName = showName;
217 }
218
219 inline const std::string& GetDisplayName() const
220 {
221 return this->displayName;
222 }
223
224 inline void SetDisplayName(const std::string& displayName)
225 {
226 this->displayName = displayName;
227 }
228
229 inline DataModel::Cluster* GetCluster() const
230 {
231 return cluster;
232 }
233
234 inline void SetCluster(DataModel::Cluster* const cluster, const bool inverted)
235 {
236 this->cluster = cluster;
237 this->clusterInverted = inverted;
238 }
239
240 inline void DeleteCluster()
241 {
242 this->cluster = nullptr;
243 this->clusterInverted = false;
244 }
245
246 inline bool GetAllowLocoTurn() const
247 {
248 return allowLocoTurn;
249 }
250
251 inline void SetAllowLocoTurn(bool allowLocoTurn)
252 {
253 this->allowLocoTurn = allowLocoTurn;
254 }
255
256 bool Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override;
257 bool Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override;
258 bool Release(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) override;
259
260 bool ReserveForce(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier);
261 bool ReleaseForce(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier);
262
263 void DeleteFeedbacks();
264 void AssignFeedbacks(const std::vector<DataModel::Relation*>& newFeedbacks);
265
266 inline const std::vector<DataModel::Relation*>& GetFeedbacks() const
267 {
268 return feedbacks;
269 }
270
271 void DeleteSignals();
272 void AssignSignals(const std::vector<DataModel::Relation*>& newSignals);
273
274 inline const std::vector<DataModel::Relation*>& GetSignals() const
275 {
276 return signals;
277 }
278
279 bool AddExtension(Track* track);
280
281 bool DeleteExtension(const Track* track);
282
283 inline std::vector<Track*> GetExtensions() const
284 {
285 return extensions;
286 }
287
288 inline Track* GetMain() const
289 {
290 return mainTrack;
291 }
292
293 inline TrackID GetOwnMainID() const
294 {
295 return mainID;
296 }
297
298 inline void SetMain(Track* main)
299 {
300 mainTrack = main;
301 mainID = (mainTrack ? mainTrack->GetID() : TrackNone);
302 }
303
304 void UpdateMain();
305
306 inline TrackID GetMainID() const
307 {
308 return (mainTrack ? mainTrack->GetID() : GetID());
309 }
310
311 inline const std::string& GetMainName() const
312 {
313 return (mainTrack ? mainTrack->GetName() : GetName());
314 }
315
316 inline const std::string& GetMainDisplayName() const
317 {
318 return (mainTrack ? mainTrack->GetDisplayName() : GetDisplayName());
319 }
320
321 inline DataModel::Feedback::FeedbackState GetMainStateDelayed() const
322 {
323 return (mainTrack ? mainTrack->GetStateDelayed() : GetStateDelayed());
324 }
325
326 inline bool GetMainBlocked() const
327 {
328 return (mainTrack ? mainTrack->GetBlocked() : GetBlocked());
329 }
330
331 inline Orientation GetMainLocoOrientation() const
332 {
333 return (mainTrack ? mainTrack->GetLocoBaseOrientation() : GetLocoBaseOrientation());
334 }
335
336 inline const ObjectIdentifier& GetMainLocoBaseDelayed() const
337 {
338 return (mainTrack ? mainTrack->GetLocoBaseDelayed() : GetLocoBaseDelayed());
339 }
340
341 private:
342 void PublishState() const;
343
344 void StopAllSignals(const ObjectIdentifier& locoBaseIdentifier);
345
346 bool FeedbackStateInternal(const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState state);
347 void OrderValidRoutes(std::vector<DataModel::Route*>& validRoutes) const;
348 SelectRouteApproach GetSelectRouteApproachCalculated() const;
349 bool ReleaseForceUnlocked(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier);
350
351 Manager* manager;
352 TrackType trackType;
353 TrackID mainID;
354 Track* mainTrack;
355 std::vector<Track*> extensions;
356
357 mutable std::mutex updateMutex;
358 std::vector<DataModel::Relation*> feedbacks;
359 std::vector<DataModel::Relation*> signals;
360 Cluster* cluster;
361 bool clusterInverted;
362 SelectRouteApproach selectRouteApproach;
363 DataModel::Feedback::FeedbackState trackState;
364 DataModel::Feedback::FeedbackState trackStateDelayed;
365 std::vector<Route*> routes;
366 Orientation locoBaseOrientation;
367 bool blocked;
368 ObjectIdentifier locoBaseDelayed;
369 bool allowLocoTurn;
370 bool releaseWhenFree;
371 bool showName;
372 std::string displayName;
373 };
374 } // namespace DataModel