summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Config/Layer.h
blob: ade534ce02297b83cc2f717e84c2dec456a8f062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "Common/Config/Enums.h"
#include "Common/Config/Section.h"

namespace Config
{
using LayerMap = std::map<System, std::vector<Section>>;

class ConfigLayerLoader
{
public:
  explicit ConfigLayerLoader(LayerType layer);
  virtual ~ConfigLayerLoader();
  virtual void Load(Layer* config_layer) = 0;
  virtual void Save(Layer* config_layer) = 0;

  LayerType GetLayer() const;

private:
  const LayerType m_layer;
};

class Layer
{
public:
  explicit Layer(LayerType layer);
  explicit Layer(std::unique_ptr<ConfigLayerLoader> loader);
  virtual ~Layer();

  // Convenience functions
  bool Exists(System system, const std::string& section_name, const std::string& key);
  bool DeleteKey(System system, const std::string& section_name, const std::string& key);
  template <typename T>
  bool GetIfExists(System system, const std::string& section_name, const std::string& key, T* value)
  {
    if (Exists(system, section_name, key))
      return GetOrCreateSection(system, section_name)->Get(key, value);

    return false;
  }

  virtual Section* GetSection(System system, const std::string& section_name);
  virtual Section* GetOrCreateSection(System system, const std::string& section_name);

  // Explicit load and save of layers
  void Load();
  void Save();

  LayerType GetLayer() const;
  const LayerMap& GetLayerMap() const;
  // Stay away from this routine as much as possible
  ConfigLayerLoader* GetLoader() const;

protected:
  bool IsDirty() const;
  void ClearDirty();

  LayerMap m_sections;
  const LayerType m_layer;
  std::unique_ptr<ConfigLayerLoader> m_loader;
};

class RecursiveLayer final : public Layer
{
public:
  RecursiveLayer();
  Section* GetSection(System system, const std::string& section_name) override;
  Section* GetOrCreateSection(System system, const std::string& section_name) override;
};
}