Debian Codegraph @

DataModel/Cluster.cpp

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