Layer.cpp 1.9 KB

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