| 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/ObjectIdentifier.h" |
| 22 |
#include "Utils/Integer.h" |
| 23 |
#include "Utils/Utils.h" |
| 24 |
|
| 25 |
namespace DataModel |
| 26 |
{ |
| 27 |
void ObjectIdentifier::Deserialize(const std::map<std::string, std::string>& arguments) |
| 28 |
{ |
| 29 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "track", ObjectNone)); |
| 30 |
if (objectID != ObjectNone) |
| 31 |
{ |
| 32 |
objectType = ObjectTypeTrack; |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "signal", ObjectNone)); |
| 37 |
if (objectID != ObjectNone) |
| 38 |
{ |
| 39 |
objectType = ObjectTypeSignal; |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "switch", ObjectNone)); |
| 44 |
if (objectID != ObjectNone) |
| 45 |
{ |
| 46 |
objectType = ObjectTypeSwitch; |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "accessory", ObjectNone)); |
| 51 |
if (objectID != ObjectNone) |
| 52 |
{ |
| 53 |
objectType = ObjectTypeAccessory; |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "feedback", ObjectNone)); |
| 58 |
if (objectID != ObjectNone) |
| 59 |
{ |
| 60 |
objectType = ObjectTypeFeedback; |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "route", ObjectNone)); |
| 65 |
if (objectID != ObjectNone) |
| 66 |
{ |
| 67 |
objectType = ObjectTypeRoute; |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "text", ObjectNone)); |
| 72 |
if (objectID != ObjectNone) |
| 73 |
{ |
| 74 |
objectType = ObjectTypeText; |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "pause", ObjectNone)); |
| 79 |
if (objectID != ObjectNone) |
| 80 |
{ |
| 81 |
objectType = ObjectTypePause; |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "multipleunit", ObjectNone)); |
| 86 |
if (objectID != ObjectNone) |
| 87 |
{ |
| 88 |
objectType = ObjectTypeMultipleUnit; |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
objectID = static_cast<ObjectID>(Utils::Utils::GetIntegerMapEntry(arguments, "booster", ObjectNone)); |
| 93 |
if (objectID != ObjectNone) |
| 94 |
{ |
| 95 |
objectType = ObjectTypeBooster; |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
objectType = ObjectTypeNone; |
| 100 |
} |
| 101 |
|
| 102 |
ObjectIdentifier& ObjectIdentifier::operator=(const std::string& text) |
| 103 |
{ |
| 104 |
if (text.substr(0, 5).compare("track") == 0) |
| 105 |
{ |
| 106 |
objectType = ObjectTypeTrack; |
| 107 |
objectID = Utils::Integer::StringToInteger(text.substr(5), ObjectNone); |
| 108 |
return *this; |
| 109 |
} |
| 110 |
if (text.substr(0, 6).compare("signal") == 0) |
| 111 |
{ |
| 112 |
objectType = ObjectTypeSignal; |
| 113 |
objectID = Utils::Integer::StringToInteger(text.substr(6), ObjectNone); |
| 114 |
return *this; |
| 115 |
} |
| 116 |
if (text.substr(0, 5).compare("route") == 0) |
| 117 |
{ |
| 118 |
objectType = ObjectTypeRoute; |
| 119 |
objectID = Utils::Integer::StringToInteger(text.substr(5), ObjectNone); |
| 120 |
return *this; |
| 121 |
} |
| 122 |
if (text.substr(0, 4).compare("loco") == 0) |
| 123 |
{ |
| 124 |
objectType = ObjectTypeLoco; |
| 125 |
objectID = Utils::Integer::StringToInteger(text.substr(4), ObjectNone); |
| 126 |
return *this; |
| 127 |
} |
| 128 |
if (text.substr(0, 8).compare("feedback") == 0) |
| 129 |
{ |
| 130 |
objectType = ObjectTypeFeedback; |
| 131 |
objectID = Utils::Integer::StringToInteger(text.substr(8), ObjectNone); |
| 132 |
return *this; |
| 133 |
} |
| 134 |
if (text.substr(0, 9).compare("accessory") == 0) |
| 135 |
{ |
| 136 |
objectType = ObjectTypeAccessory; |
| 137 |
objectID = Utils::Integer::StringToInteger(text.substr(9), ObjectNone); |
| 138 |
return *this; |
| 139 |
} |
| 140 |
if (text.substr(0, 6).compare("switch") == 0) |
| 141 |
{ |
| 142 |
objectType = ObjectTypeSwitch; |
| 143 |
objectID = Utils::Integer::StringToInteger(text.substr(6), ObjectNone); |
| 144 |
return *this; |
| 145 |
} |
| 146 |
if (text.substr(0, 5).compare("layer") == 0) |
| 147 |
{ |
| 148 |
objectType = ObjectTypeLayer; |
| 149 |
objectID = Utils::Integer::StringToInteger(text.substr(5), ObjectNone); |
| 150 |
return *this; |
| 151 |
} |
| 152 |
if (text.substr(0, 7).compare("cluster") == 0) |
| 153 |
{ |
| 154 |
objectType = ObjectTypeCluster; |
| 155 |
objectID = Utils::Integer::StringToInteger(text.substr(7), ObjectNone); |
| 156 |
return *this; |
| 157 |
} |
| 158 |
if (text.substr(0, 9).compare("timetable") == 0) |
| 159 |
{ |
| 160 |
objectType = ObjectTypeTimeTable; |
| 161 |
objectID = Utils::Integer::StringToInteger(text.substr(9), ObjectNone); |
| 162 |
return *this; |
| 163 |
} |
| 164 |
if (text.substr(0, 4).compare("text") == 0) |
| 165 |
{ |
| 166 |
objectType = ObjectTypeText; |
| 167 |
objectID = Utils::Integer::StringToInteger(text.substr(4), ObjectNone); |
| 168 |
return *this; |
| 169 |
} |
| 170 |
if (text.substr(0, 5).compare("pause") == 0) |
| 171 |
{ |
| 172 |
objectType = ObjectTypePause; |
| 173 |
objectID = Utils::Integer::StringToInteger(text.substr(5), ObjectNone); |
| 174 |
return *this; |
| 175 |
} |
| 176 |
if (text.substr(0, 12).compare("multipleunit") == 0) |
| 177 |
{ |
| 178 |
objectType = ObjectTypeMultipleUnit; |
| 179 |
objectID = Utils::Integer::StringToInteger(text.substr(12), ObjectNone); |
| 180 |
return *this; |
| 181 |
} |
| 182 |
if (text.substr(0, 7).compare("booster") == 0) |
| 183 |
{ |
| 184 |
objectType = ObjectTypeBooster; |
| 185 |
objectID = Utils::Integer::StringToInteger(text.substr(7), ObjectNone); |
| 186 |
return *this; |
| 187 |
} |
| 188 |
objectType = ObjectTypeTrack; |
| 189 |
objectID = Utils::Integer::StringToInteger(text, ObjectNone); |
| 190 |
return *this; |
| 191 |
} |
| 192 |
|
| 193 |
std::string ObjectIdentifier::ObjectTypeToString(const ObjectType objectType) |
| 194 |
{ |
| 195 |
switch (objectType) |
| 196 |
{ |
| 197 |
case ObjectTypeNone: |
| 198 |
return "none"; |
| 199 |
|
| 200 |
case ObjectTypeLoco: |
| 201 |
return "loco"; |
| 202 |
|
| 203 |
case ObjectTypeTrack: |
| 204 |
return "track"; |
| 205 |
|
| 206 |
case ObjectTypeFeedback: |
| 207 |
return "feedback"; |
| 208 |
|
| 209 |
case ObjectTypeAccessory: |
| 210 |
return "accessory"; |
| 211 |
|
| 212 |
case ObjectTypeSwitch: |
| 213 |
return "switch"; |
| 214 |
|
| 215 |
case ObjectTypeRoute: |
| 216 |
return "route"; |
| 217 |
|
| 218 |
case ObjectTypeLayer: |
| 219 |
return "layer"; |
| 220 |
|
| 221 |
case ObjectTypeSignal: |
| 222 |
return "signal"; |
| 223 |
|
| 224 |
case ObjectTypeCluster: |
| 225 |
return "cluster"; |
| 226 |
|
| 227 |
case ObjectTypeTimeTable: |
| 228 |
return "timetable"; |
| 229 |
|
| 230 |
case ObjectTypeText: |
| 231 |
return "text"; |
| 232 |
|
| 233 |
case ObjectTypePause: |
| 234 |
return "pause"; |
| 235 |
|
| 236 |
case ObjectTypeMultipleUnit: |
| 237 |
return "multipleunit"; |
| 238 |
|
| 239 |
case ObjectTypeBooster: |
| 240 |
return "booster"; |
| 241 |
} |
| 242 |
return "object"; |
| 243 |
} |
| 244 |
} // namespace DataModel |
| 245 |
|