Debian Codegraph @

DataModel/LockableItem.cpp

raw symbols ◀ ▶ SCIP
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 #include "DataModel/LockableItem.h"
22 #include "DataModel/Object.h"
23 #include "Utils/Utils.h"
24
25 using std::map;
26 using std::stringstream;
27 using std::string;
28
29 namespace DataModel
30 {
31 string LockableItem::Serialize() const
32 {
33 return "lockstate=" + std::to_string(lockState)
34 + ";locobaseid=" + std::to_string(locoBaseIdentifier.GetObjectID())
35 + ";locobasetype=" + std::to_string(locoBaseIdentifier.GetObjectType());
36 }
37
•38 void LockableItem::Deserialize(const map<string, string> arguments)
39 {
40 const LocoID locoID = Utils::Utils::GetIntegerMapEntry(arguments, "locoID", LocoNone); // FIXME: Remove later: 2024-02-08
41 locoBaseIdentifier.SetObjectID(Utils::Utils::GetIntegerMapEntry(arguments, "locobaseid", locoID));
42 locoBaseIdentifier.SetObjectType(static_cast<ObjectType>(Utils::Utils::GetIntegerMapEntry(arguments, "locobasetype", ObjectTypeLoco)));
43 lockState = static_cast<LockState>(Utils::Utils::GetIntegerMapEntry(arguments, "lockState", LockStateFree)); // FIXME: Remove later: 2024-02-08
44 lockState = static_cast<LockState>(Utils::Utils::GetIntegerMapEntry(arguments, "lockstate", lockState));
45 }
46
47 bool LockableItem::Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
48 {
49 std::lock_guard<std::mutex> Guard(lockMutex);
50 if (this->locoBaseIdentifier == locoBaseIdentifier)
51 {
52 if (lockState == LockStateFree)
53 {
54 lockState = LockStateReserved;
55 }
56 return true;
57 }
58
59 if (this->locoBaseIdentifier.IsSet() || (lockState != LockStateFree))
60 {
61 Object *object = dynamic_cast<Object*>(this);
62 if (!object)
63 {
64 return false;
65 }
66
67 logger->Debug(Languages::TextIsNotFree, object->GetName());
68 return false;
69 }
70
71 lockState = LockStateReserved;
72 this->locoBaseIdentifier = locoBaseIdentifier;
73 return true;
74 }
75
76 bool LockableItem::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
77 {
78 std::lock_guard<std::mutex> Guard(lockMutex);
79
80 if (this->locoBaseIdentifier != locoBaseIdentifier)
81 {
82 Object *object = dynamic_cast<Object*>(this);
83 if (object == nullptr)
84 {
85 return false;
86 }
87
88 logger->Debug(Languages::TextIsNotFree, object->GetName());
89 return false;
90 }
91
92 if (lockState != LockStateReserved && lockState != LockStateHardLocked)
93 {
94 Object *object = dynamic_cast<Object*>(this);
95 if (object == nullptr)
96 {
97 return false;
98 }
99
100 logger->Debug(Languages::TextIsNotFree, object->GetName());
101 return false;
102 }
103
104 lockState = LockStateHardLocked;
105 return true;
106 }
107
108 bool LockableItem::Release(__attribute__((unused)) Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier)
109 {
110 std::lock_guard<std::mutex> Guard(lockMutex);
111 if (this->locoBaseIdentifier != locoBaseIdentifier && locoBaseIdentifier.IsSet())
112 {
113 return false;
114 }
115 this->locoBaseIdentifier.Clear();
116 lockState = LockStateFree;
117 return true;
118 }
119
120 } // namespace DataModel
121