Config: Remove recursive layer

This commit is contained in:
MerryMage
2017-10-29 19:17:58 +00:00
parent f612569718
commit c8f970e2b0
13 changed files with 30 additions and 139 deletions

View File

@ -17,11 +17,6 @@ static std::list<ConfigChangedCallback> s_callbacks;
void InvokeConfigChangedCallbacks();
Section* GetOrCreateSection(System system, const std::string& section_name)
{
return s_layers[LayerType::Meta]->GetOrCreateSection(system, section_name);
}
Layers* GetLayers()
{
return &s_layers;
@ -83,8 +78,6 @@ void Init()
{
// These layers contain temporary values
ClearCurrentRunLayer();
// This layer always has to exist
s_layers[LayerType::Meta] = std::make_unique<RecursiveLayer>();
}
void Shutdown()
@ -129,7 +122,6 @@ const std::string& GetLayerName(LayerType layer)
{LayerType::Movie, "Movie"},
{LayerType::CommandLine, "Command Line"},
{LayerType::CurrentRun, "Current Run"},
{LayerType::Meta, "Top"},
};
return layer_to_name.at(layer);
}

View File

@ -36,9 +36,6 @@ struct ConfigInfo
using Layers = std::map<LayerType, std::unique_ptr<Layer>>;
using ConfigChangedCallback = std::function<void()>;
// Common function used for getting configuration
Section* GetOrCreateSection(System system, const std::string& section_name);
// Layer management
Layers* GetLayers();
void AddLayer(std::unique_ptr<Layer> layer);
@ -66,13 +63,15 @@ LayerType GetActiveLayerForConfig(const ConfigLocation&);
template <typename T>
T Get(LayerType layer, const ConfigInfo<T>& info)
{
if (layer == LayerType::Meta)
return Get(info);
return GetLayer(layer)->Get(info);
}
template <typename T>
T Get(const ConfigInfo<T>& info)
{
return Get(LayerType::Meta, info);
return GetLayer(GetActiveLayerForConfig(info.location))->Get(info);
}
template <typename T>

View File

@ -34,7 +34,6 @@ enum class System
};
constexpr std::array<LayerType, 7> SEARCH_ORDER{{
// Skip the meta layer
LayerType::CurrentRun, LayerType::CommandLine, LayerType::Movie, LayerType::Netplay,
LayerType::LocalGame, LayerType::GlobalGame, LayerType::Base,
}};

View File

@ -67,15 +67,7 @@ Section* Layer::GetOrCreateSection(System system, const std::string& section_nam
Section* section = GetSection(system, section_name);
if (!section)
{
if (m_layer == LayerType::Meta)
{
m_sections[system].emplace_back(
std::make_unique<RecursiveSection>(m_layer, system, section_name));
}
else
{
m_sections[system].emplace_back(std::make_unique<Section>(m_layer, system, section_name));
}
m_sections[system].emplace_back(std::make_unique<Section>(m_layer, system, section_name));
section = m_sections[system].back().get();
}
return section;
@ -124,26 +116,4 @@ void Layer::ClearDirty()
[](auto& section) { section->ClearDirty(); });
});
}
RecursiveLayer::RecursiveLayer() : Layer(LayerType::Meta)
{
}
Section* RecursiveLayer::GetSection(System system, const std::string& section_name)
{
// Always queries backwards recursively, so it doesn't matter if it exists or not on this layer
return GetOrCreateSection(system, section_name);
}
Section* RecursiveLayer::GetOrCreateSection(System system, const std::string& section_name)
{
Section* section = Layer::GetSection(system, section_name);
if (!section)
{
m_sections[system].emplace_back(
std::make_unique<RecursiveSection>(m_layer, system, section_name));
section = m_sections[system].back().get();
}
return section;
}
}

View File

@ -84,12 +84,4 @@ protected:
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;
};
}
} // namespace Config

View File

@ -246,51 +246,4 @@ void Section::ClearDirty()
{
m_dirty = false;
}
RecursiveSection::RecursiveSection(LayerType layer, System system, const std::string& name)
: Section(layer, system, name)
{
}
bool RecursiveSection::Exists(const std::string& key) const
{
auto layers_it = Config::GetLayers()->find(LayerType::Meta);
do
{
const Section* layer_section = layers_it->second->GetSection(m_system, m_name);
if (layer_section && layer_section->Exists(key))
{
return true;
}
} while (--layers_it != Config::GetLayers()->end());
return false;
}
bool RecursiveSection::Get(const std::string& key, std::string* value,
const std::string& default_value) const
{
for (auto layer_id : SEARCH_ORDER)
{
auto layers_it = Config::GetLayers()->find(layer_id);
if (layers_it == Config::GetLayers()->end())
continue;
const Section* layer_section = layers_it->second->GetSection(m_system, m_name);
if (layer_section && layer_section->Exists(key))
{
return layer_section->Get(key, value, default_value);
}
}
return Section::Get(key, value, default_value);
}
void RecursiveSection::Set(const std::string& key, const std::string& value)
{
// The RecursiveSection can't set since it is used to recursively get values from the layer
// map.
// It is only a part of the meta layer, and the meta layer isn't allowed to set any values.
_assert_msg_(COMMON, false, "Don't try to set values here!");
}
}

View File

@ -96,18 +96,4 @@ protected:
std::vector<std::string> m_lines;
};
// Only to be used with the meta-layer
class RecursiveSection final : public Section
{
public:
RecursiveSection(LayerType layer, System system, const std::string& name);
bool Exists(const std::string& key) const override;
bool Get(const std::string& key, std::string* value,
const std::string& default_value = NULL_STRING) const override;
void Set(const std::string& key, const std::string& value) override;
};
}