/
Layer.cpp
138 lines (113 loc) · 2.46 KB
/
Layer.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <cstring>
#include <map>
#include "Common/Config/Config.h"
#include "Common/Config/Layer.h"
namespace Config
{
namespace detail
{
std::string ValueToString(u16 value)
{
return StringFromFormat("0x%04x", value);
}
std::string ValueToString(u32 value)
{
return StringFromFormat("0x%08x", value);
}
std::string ValueToString(float value)
{
return StringFromFormat("%#.9g", value);
}
std::string ValueToString(double value)
{
return StringFromFormat("%#.17g", value);
}
std::string ValueToString(int value)
{
return std::to_string(value);
}
std::string ValueToString(bool value)
{
return StringFromBool(value);
}
std::string ValueToString(const std::string& value)
{
return value;
}
}
ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer)
{
}
ConfigLayerLoader::~ConfigLayerLoader() = default;
LayerType ConfigLayerLoader::GetLayer() const
{
return m_layer;
}
Layer::Layer(LayerType type) : m_layer(type)
{
}
Layer::Layer(std::unique_ptr<ConfigLayerLoader> loader)
: m_layer(loader->GetLayer()), m_loader(std::move(loader))
{
Load();
}
Layer::~Layer()
{
Save();
}
bool Layer::Exists(const ConfigLocation& location) const
{
const auto iter = m_map.find(location);
return iter != m_map.end() && iter->second.has_value();
}
bool Layer::DeleteKey(const ConfigLocation& location)
{
m_is_dirty = true;
bool had_value = m_map[location].has_value();
m_map[location].reset();
return had_value;
}
void Layer::DeleteAllKeys()
{
m_is_dirty = true;
for (auto& pair : m_map)
{
pair.second.reset();
}
}
Section Layer::GetSection(System system, const std::string& section)
{
return Section{m_map.lower_bound(ConfigLocation{system, section, ""}),
m_map.lower_bound(ConfigLocation{system, section + '\001', ""})};
}
ConstSection Layer::GetSection(System system, const std::string& section) const
{
return ConstSection{m_map.lower_bound(ConfigLocation{system, section, ""}),
m_map.lower_bound(ConfigLocation{system, section + '\001', ""})};
}
void Layer::Load()
{
if (m_loader)
m_loader->Load(this);
m_is_dirty = false;
}
void Layer::Save()
{
if (!m_loader || !m_is_dirty)
return;
m_loader->Save(this);
m_is_dirty = false;
}
LayerType Layer::GetLayer() const
{
return m_layer;
}
const LayerMap& Layer::GetLayerMap() const
{
return m_map;
}
}