| 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 <map> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "DataTypes.h" |
| 27 |
|
| 28 |
namespace DataModel |
| 29 |
{ |
| 30 |
class ObjectIdentifier |
| 31 |
{ |
| 32 |
public: |
| 33 |
inline ObjectIdentifier() |
| 34 |
: objectType(ObjectTypeNone), |
| 35 |
objectID(ObjectNone) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
inline ObjectIdentifier(const std::string& text) |
| 40 |
{ |
| 41 |
*this = text; |
| 42 |
} |
| 43 |
|
| 44 |
inline ObjectIdentifier(const ObjectType objectType, const ObjectID objectID) |
| 45 |
: objectType(objectType), |
| 46 |
objectID(objectID) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
inline ObjectIdentifier(const std::map<std::string,std::string>& arguments) |
| 51 |
{ |
| 52 |
Deserialize(arguments); |
| 53 |
} |
| 54 |
|
| 55 |
inline ObjectIdentifier(const ObjectIdentifier& other) = default; |
| 56 |
|
| 57 |
inline std::string Serialize() const |
| 58 |
{ |
| 59 |
return GetObjectTypeAsString() + "=" + GetObjectIdAsString(); |
| 60 |
} |
| 61 |
|
| 62 |
bool Deserialize(const std::map<std::string,std::string>& arguments); |
| 63 |
|
| 64 |
inline ObjectIdentifier& operator=(const ObjectIdentifier& other) = default; |
| 65 |
|
| 66 |
inline void SetObjectType(const ObjectType objectType) |
| 67 |
{ |
| 68 |
this->objectType = objectType; |
| 69 |
} |
| 70 |
|
| 71 |
inline void SetObjectID(const ObjectID objectID) |
| 72 |
{ |
| 73 |
this->objectID = objectID; |
| 74 |
} |
| 75 |
|
| 76 |
ObjectIdentifier& operator=(const std::string& text); |
| 77 |
|
| 78 |
inline bool operator==(const ObjectIdentifier& other) const |
| 79 |
{ |
| 80 |
return this->objectType == other.objectType && this->objectID == other.objectID; |
| 81 |
} |
| 82 |
|
| 83 |
inline bool operator!=(const ObjectIdentifier& other) const |
| 84 |
{ |
| 85 |
return !(*this == other); |
| 86 |
} |
| 87 |
|
| 88 |
inline operator std::string() const |
| 89 |
{ |
| 90 |
return GetObjectTypeAsString() + GetObjectIdAsString(); |
| 91 |
} |
| 92 |
|
| 93 |
inline void Clear() |
| 94 |
{ |
| 95 |
objectType = ObjectTypeNone; |
| 96 |
objectID = ObjectNone; |
| 97 |
} |
| 98 |
|
| 99 |
inline bool IsSet() const |
| 100 |
{ |
| 101 |
return (objectType != ObjectTypeNone) && (objectID != ObjectNone); |
| 102 |
} |
| 103 |
|
| 104 |
inline ObjectType GetObjectType() const |
| 105 |
{ |
| 106 |
return objectType; |
| 107 |
} |
| 108 |
|
| 109 |
inline ObjectID GetObjectID() const |
| 110 |
{ |
| 111 |
return objectID; |
| 112 |
} |
| 113 |
|
| 114 |
inline std::string GetObjectTypeAsString() const |
| 115 |
{ |
| 116 |
return ObjectTypeToString(objectType); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
inline std::string GetObjectIdAsString() const |
| 121 |
{ |
| 122 |
return std::to_string(objectID); |
| 123 |
} |
| 124 |
|
| 125 |
static std::string ObjectTypeToString(const ObjectType objectType); |
| 126 |
|
| 127 |
private: |
| 128 |
ObjectType objectType; |
| 129 |
ObjectID objectID; |
| 130 |
}; |
| 131 |
} // namespace DataModel |
| 132 |
|