| 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 "Hardware/LocoCache.h" |
| 22 | #include "Manager.h" |
| 23 | |
| 24 | namespace Hardware |
| 25 | { |
| 26 | void LocoCacheEntry::UpdateSlaves(Manager* manager) |
| 27 | { |
| 28 | for (auto & slave : slaves) |
| 29 | { |
| 30 | DataModel::Loco* loco = manager->GetLocoByMatchKey(controlId, slave.GetMatchKey()); |
| 31 | if (!loco) |
| 32 | { |
| 33 | continue; |
| 34 | } |
| 35 | slave.SetLocoID(loco->GetID()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void LocoCache::Save(LocoCacheEntry& entry, const std::string& oldMatchKey) |
| 40 | { |
| 41 | const std::string& matchKey = entry.GetMatchKey(); |
| 42 | DataModel::Loco* loco = nullptr; |
| 43 | |
| 44 | const bool matchKeyChanged = matchKey.compare(oldMatchKey) != 0; |
| 45 | if (matchKeyChanged) |
| 46 | { |
| 47 | const LocoID locoId = Delete(oldMatchKey); |
| 48 | loco = manager->GetLoco(locoId); |
| 49 | } |
| 50 | |
| 51 | if (loco == nullptr) |
| 52 | { |
| 53 | loco = manager->GetLocoByMatchKey(GetControlId(), oldMatchKey); |
| 54 | } |
| 55 | |
| 56 | if (loco == nullptr && matchKeyChanged) |
| 57 | { |
| 58 | loco = manager->GetLocoByMatchKey(GetControlId(), matchKey); |
| 59 | } |
| 60 | |
| 61 | if (loco != nullptr) |
| 62 | { |
| 63 | entry.SetLocoID(loco->GetID()); |
| 64 | *loco = entry; |
| 65 | } |
| 66 | entries.emplace(matchKey, entry); |
| 67 | } |
| 68 | |
| 69 | LocoID LocoCache::Delete(const std::string& matchKey) |
| 70 | { |
| 71 | LocoID locoId = Get(matchKey).GetLocoID(); |
| 72 | manager->LocoRemoveMatchKey(locoId); |
| 73 | entries.erase(matchKey); |
| 74 | return locoId; |
| 75 | } |
| 76 | |
| 77 | void LocoCache::SetLocoId(const LocoID locoId, const std::string& matchKey) |
| 78 | { |
| 79 | if (locoId != LocoNone) |
| 80 | { |
| 81 | for (auto& locoCacheEntry : entries) |
| 82 | { |
| 83 | LocoCacheEntry& entry = locoCacheEntry.second; |
| 84 | if (entry.GetLocoID() == locoId) |
| 85 | { |
| 86 | entry.SetLocoID(LocoNone); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | auto entry = entries.find(matchKey); |
| 92 | if (entry == entries.end()) |
| 93 | { |
| 94 | return; |
| 95 | } |
| 96 | entry->second.SetLocoID(locoId); |
| 97 | } |
| 98 | } // namespace Hardware |
| 99 |