| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Config/Layer.h" |
| 5 | |
| 6 | namespace Config |
| 7 | { |
| 8 | ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer) |
| 9 | { |
| 10 | } |
| 11 | |
| 12 | ConfigLayerLoader::~ConfigLayerLoader() = default; |
| 13 | |
| 14 | LayerType ConfigLayerLoader::GetLayer() const |
| 15 | { |
| 16 | return m_layer; |
| 17 | } |
| 18 | |
| 19 | Layer::Layer(LayerType type) : m_layer(type) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | Layer::Layer(std::unique_ptr<ConfigLayerLoader> loader) |
| 24 | : m_layer(loader->GetLayer()), m_loader(std::move(loader)) |
| 25 | { |
| 26 | Load(); |
| 27 | } |
| 28 | |
| 29 | Layer::~Layer() |
| 30 | { |
| 31 | Save(); |
| 32 | } |
| 33 | |
| 34 | bool Layer::Exists(const Location& location) const |
| 35 | { |
| 36 | const auto iter = m_map.find(location); |
| 37 | return iter != m_map.end() && iter->second.has_value(); |
| 38 | } |
| 39 | |
| 40 | bool Layer::DeleteKey(const Location& location) |
| 41 | { |
| 42 | m_is_dirty = true; |
| 43 | bool had_value = false; |
| 44 | const auto iter = m_map.find(location); |
| 45 | if (iter != m_map.end() && iter->second.has_value()) |
| 46 | { |
| 47 | iter->second.reset(); |
| 48 | had_value = true; |
| 49 | } |
| 50 | |
| 51 | return had_value; |
| 52 | } |
| 53 | |
| 54 | void Layer::DeleteAllKeys() |
| 55 | { |
| 56 | m_is_dirty = true; |
| 57 | for (auto& pair : m_map) |
| 58 | { |
| 59 | pair.second.reset(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | Section Layer::GetSection(System system, const std::string& section) |
| 64 | { |
| 65 | return Section{m_map.lower_bound(Location{system, section, ""}), |
| 66 | m_map.lower_bound(Location{system, section + '\001', ""})}; |
| 67 | } |
| 68 | |
| 69 | ConstSection Layer::GetSection(System system, const std::string& section) const |
| 70 | { |
| 71 | return ConstSection{m_map.lower_bound(Location{system, section, ""}), |
| 72 | m_map.lower_bound(Location{system, section + '\001', ""})}; |
| 73 | } |
| 74 | |
| 75 | void Layer::Load() |
| 76 | { |
| 77 | if (m_loader) |
| 78 | m_loader->Load(this); |
| 79 | m_is_dirty = false; |
| 80 | } |
| 81 | |
| 82 | void Layer::Save() |
| 83 | { |
| 84 | if (!m_loader || !m_is_dirty) |
| 85 | return; |
| 86 | |
| 87 | m_loader->Save(this); |
| 88 | m_is_dirty = false; |
| 89 | } |
| 90 | |
| 91 | LayerType Layer::GetLayer() const |
| 92 | { |
| 93 | return m_layer; |
| 94 | } |
| 95 | |
| 96 | const LayerMap& Layer::GetLayerMap() const |
| 97 | { |
| 98 | return m_map; |
| 99 | } |
| 100 | } // namespace Config |