| 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 "DataModel/AccessoryBase.h" |
| 27 |
#include "DataModel/LayoutItem.h" |
| 28 |
#include "DataModel/LockableItem.h" |
| 29 |
#include "DataModel/Track.h" |
| 30 |
#include "DataTypes.h" |
| 31 |
#include "Languages.h" |
| 32 |
|
| 33 |
class Manager; |
| 34 |
|
| 35 |
namespace DataModel |
| 36 |
{ |
| 37 |
class Signal : public AccessoryBase, public LayoutItem, public LockableItem |
| 38 |
{ |
| 39 |
public: |
| 40 |
class StateOption |
| 41 |
{ |
| 42 |
public: |
| 43 |
StateOption() = delete; |
| 44 |
StateOption& operator=(const StateOption&) = delete; |
| 45 |
|
| 46 |
inline StateOption(const Languages::TextSelector text, const AddressOffset addressOffset) |
| 47 |
: text(text), |
| 48 |
addressOffset(addressOffset) |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
inline StateOption(const StateOption&) = default; |
| 53 |
|
| 54 |
inline operator Languages::TextSelector() const |
| 55 |
{ |
| 56 |
return text; |
| 57 |
} |
| 58 |
|
| 59 |
const Languages::TextSelector text; |
| 60 |
const AddressOffset addressOffset; |
| 61 |
}; |
| 62 |
|
| 63 |
Signal() = delete; |
| 64 |
Signal(const Signal&) = delete; |
| 65 |
Signal& operator=(const Signal&) = delete; |
| 66 |
|
| 67 |
inline Signal(__attribute__((unused)) const Manager* const manager, const SignalID signalID) |
| 68 |
: AccessoryBase(), |
| 69 |
LayoutItem(signalID), |
| 70 |
LockableItem(), |
| 71 |
track(nullptr) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
inline Signal(const Manager* const manager, const std::string& serialized) |
| 76 |
: Signal(manager, SignalNone) |
| 77 |
{ |
| 78 |
Deserialize(serialized); |
| 79 |
} |
| 80 |
|
| 81 |
virtual void SetAccessoryType(AccessoryType type) override; |
| 82 |
|
| 83 |
virtual ObjectType GetObjectType() const override; |
| 84 |
|
| 85 |
virtual std::string GetLayoutType() const override; |
| 86 |
|
| 87 |
std::string Serialize() const override; |
| 88 |
|
| 89 |
using HardwareHandle::Deserialize; |
| 90 |
virtual bool Deserialize(const std::string& serialized) override; |
| 91 |
|
| 92 |
inline Track* GetTrack() const |
| 93 |
{ |
| 94 |
return track; |
| 95 |
} |
| 96 |
|
| 97 |
inline void SetTrack(Track* const track = nullptr) |
| 98 |
{ |
| 99 |
this->track = track; |
| 100 |
} |
| 101 |
|
| 102 |
bool UsesAddress(Address address) const; |
| 103 |
|
| 104 |
std::map<DataModel::AccessoryState,StateOption> GetStateOptions() const; |
| 105 |
|
| 106 |
inline Address GetMappedAddress() const |
| 107 |
{ |
| 108 |
AddressOffset offset = GetStateAddressOffset(); |
| 109 |
return offset < 0 ? 0 : GetAddress() + ((offset) >> 1); |
| 110 |
} |
| 111 |
|
| 112 |
inline AccessoryState GetMappedAccessoryState() const |
| 113 |
{ |
| 114 |
return static_cast<AccessoryState>(GetStateAddressOffset() & 0x01); |
| 115 |
} |
| 116 |
|
| 117 |
inline void SetStateAddressOffset(const AccessoryState state, const AddressOffset addressOffset) |
| 118 |
{ |
| 119 |
stateAddressMap[state] = addressOffset; |
| 120 |
} |
| 121 |
|
| 122 |
inline void SetStateAddressOffsets(const std::map<AccessoryState,AddressOffset>& newOffsets) |
| 123 |
{ |
| 124 |
stateAddressMap = newOffsets; |
| 125 |
} |
| 126 |
|
| 127 |
inline DataModel::AddressOffset GetStateAddressOffset() const |
| 128 |
{ |
| 129 |
return GetStateAddressOffset(GetAccessoryState()); |
| 130 |
} |
| 131 |
|
| 132 |
inline AddressOffset GetStateAddressOffset(const AccessoryState state) const |
| 133 |
{ |
| 134 |
return stateAddressMap.count(state) != 1 ? -1 : stateAddressMap.at(state); |
| 135 |
} |
| 136 |
|
| 137 |
AccessoryState CalculateMappedSignalState(const Address address, const AccessoryState state) const; |
| 138 |
|
| 139 |
Signal& operator=(const Hardware::AccessoryCacheEntry& accessory); |
| 140 |
|
| 141 |
private: |
| 142 |
void ResetStateAddressMap(); |
| 143 |
|
| 144 |
std::map<AccessoryState,AddressOffset> stateAddressMap; |
| 145 |
|
| 146 |
Track* track; |
| 147 |
}; |
| 148 |
} // namespace DataModel |
| 149 |
|