Debian Codegraph @

DataModel/Cluster.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches 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 #include <map>
22 #include <string>
23
24 #include "DataModel/Cluster.h"
25 #include "DataModel/LockableItem.h"
26 #include "DataModel/Relation.h"
27 #include "DataModel/Signal.h"
28 #include "DataModel/Track.h"
29 #include "Manager.h"
30 #include "Utils/Utils.h"
31
32 using std::map;
33 using std::string;
34 using std::to_string;
35
36 namespace DataModel
37 {
38 std::string Cluster::Serialize() const
39 {
40 string str;
41 str += "objectType=Cluster;";
42 str += Object::Serialize();
43 str += ";orientation=";
44 str += to_string(orientation);
45 return str;
46 }
47
48 bool Cluster::Deserialize(const string& serialized)
49 {
50 map<string,string> arguments;
51 ParseArguments(serialized, arguments);
52 Object::Deserialize(arguments);
53 if (!arguments.count("objectType") || arguments.at("objectType").compare("Cluster") != 0)
54 {
55 return false;
56 }
57 orientation = static_cast<Orientation>(Utils::Utils::GetBoolMapEntry(arguments, "orientation", OrientationRight));
58 return true;
59 }
60
61 bool Cluster::CanSetLocoBaseOrientationUnlocked(const Orientation orientation, const ObjectIdentifier& locoBaseIdentifier)
62 {
63 if (this->orientation == orientation)
64 {
65 return true;
66 }
67 for (auto relation : tracks)
68 {
69 Track* track = dynamic_cast<Track*>(relation->GetObject2());
70 if (!track)
71 {
72 return false;
73 }
74
75 // invert track orientation if needed (XOR) and check with desired orientation value
76 const Relation::Data invert = relation->GetData();
77 if ((track->GetLocoBaseOrientation() ^ invert) == orientation)
78 {
79 continue;
80 }
81
82 const DataModel::LockableItem::LockState lockState = track->GetLockState();
83 if (lockState == DataModel::LockableItem::LockStateFree)
84 {
85 continue;
86 }
87
88 const ObjectIdentifier& locoBaseOfTrack = track->GetLocoBase();
89 if ((lockState == DataModel::LockableItem::LockStateReserved) && locoBaseOfTrack.IsSet() && (locoBaseOfTrack == locoBaseIdentifier))
90 {
91 continue;
92 }
93
94 return false;
95 }
96 return true;
97 }
98
99 bool Cluster::SetLocoBaseOrientation(const Orientation orientation,
100 const ObjectIdentifier& locoBaseIdentifier)
101 {
102 std::lock_guard<std::mutex> Guard(orientationMutex);
103 if (!CanSetLocoBaseOrientationUnlocked(orientation, locoBaseIdentifier))
104 {
105 return false;
106 }
107 this->orientation = orientation;
108 for (auto relation : tracks)
109 {
110 Track* track = dynamic_cast<Track*>(relation->GetObject2());
111 if (!track)
112 {
113 continue;
114 }
115 // invert orientation if needed (XOR) and set track orientation
116 const Relation::Data invert = relation->GetData();
117 track->SetLocoBaseOrientationForce(static_cast<Orientation>(orientation ^ invert));
118 }
119 return true;
120 }
121
122 void Cluster::DeleteTracks()
123 {
124 while (tracks.size() > 0)
125 {
126 Relation* trackRelation = tracks.back();
127 Track* track = dynamic_cast<Track*>(trackRelation->GetObject2());
128 if (track)
129 {
130 track->DeleteCluster();
131 }
132 tracks.pop_back();
133 delete trackRelation;
134 }
135 }
136
137 void Cluster::DeleteTrack(Track* trackToDelete)
138 {
139 for (unsigned int index = 0; index < tracks.size(); ++index)
140 {
141 if (tracks[index]->GetObject2() != trackToDelete)
142 {
143 continue;
144 }
145 delete tracks[index];
146 tracks.erase(tracks.begin() + index);
147 trackToDelete->DeleteCluster();
148 return;
149 }
150 }
151
152 void Cluster::AssignTracks(const std::vector<DataModel::Relation*>& newTracks)
153 {
154 DeleteTracks();
155 tracks = newTracks;
156 for (auto trackRelation : tracks)
157 {
158 Track* track = dynamic_cast<Track*>(trackRelation->GetObject2());
159 if (track)
160 {
161 track->SetCluster(this, static_cast<bool>(trackRelation->GetData()));
162 }
163 }
164 }
165 } // namespace DataModel