Backport SettingsInterface

This commit is contained in:
Connor McLaughlin
2022-07-09 00:14:48 +10:00
parent 0c0bd4c995
commit 6a6ab6529a
13 changed files with 749 additions and 113 deletions

View File

@ -30,6 +30,8 @@ add_library(common
gl/texture.h
hash_combine.h
heap_array.h
layered_settings_interface.cpp
layered_settings_interface.h
log.cpp
log.h
make_array.h
@ -43,6 +45,7 @@ add_library(common
progress_callback.h
rectangle.h
scope_guard.h
settings_interface.h
string.cpp
string.h
string_util.cpp

View File

@ -45,6 +45,7 @@
<ExcludedFromBuild Condition="'$(BuildingForUWP)'=='true'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="image.h" />
<ClInclude Include="layered_settings_interface.h" />
<ClInclude Include="log.h" />
<ClInclude Include="lru_cache.h" />
<ClInclude Include="make_array.h" />
@ -55,6 +56,7 @@
<ClInclude Include="progress_callback.h" />
<ClInclude Include="rectangle.h" />
<ClInclude Include="scope_guard.h" />
<ClInclude Include="settings_interface.h" />
<ClInclude Include="string.h" />
<ClInclude Include="string_util.h" />
<ClInclude Include="thirdparty\StackWalker.h">
@ -116,6 +118,7 @@
<ExcludedFromBuild Condition="'$(BuildingForUWP)'=='true'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="image.cpp" />
<ClCompile Include="layered_settings_interface.cpp" />
<ClCompile Include="log.cpp" />
<ClCompile Include="md5_digest.cpp" />
<ClCompile Include="minizip_helpers.cpp" />

View File

@ -133,6 +133,8 @@
<ClInclude Include="gl\loader.h">
<Filter>gl</Filter>
</ClInclude>
<ClInclude Include="settings_interface.h" />
<ClInclude Include="layered_settings_interface.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="gl\program.cpp">
@ -244,6 +246,7 @@
<ClCompile Include="vulkan\loader.cpp">
<Filter>vulkan</Filter>
</ClCompile>
<ClCompile Include="layered_settings_interface.cpp" />
</ItemGroup>
<ItemGroup>
<Natvis Include="bitfield.natvis" />

View File

@ -0,0 +1,189 @@
#include "layered_settings_interface.h"
#include "common/assert.h"
LayeredSettingsInterface::LayeredSettingsInterface() = default;
LayeredSettingsInterface::~LayeredSettingsInterface() = default;
bool LayeredSettingsInterface::Save()
{
Panic("Attempting to save layered settings interface");
return false;
}
void LayeredSettingsInterface::Clear()
{
Panic("Attempting to clear layered settings interface");
}
bool LayeredSettingsInterface::GetIntValue(const char* section, const char* key, s32* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetIntValue(section, key, value))
return true;
}
}
return false;
}
bool LayeredSettingsInterface::GetUIntValue(const char* section, const char* key, u32* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetUIntValue(section, key, value))
return true;
}
}
return false;
}
bool LayeredSettingsInterface::GetFloatValue(const char* section, const char* key, float* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetFloatValue(section, key, value))
return true;
}
}
return false;
}
bool LayeredSettingsInterface::GetDoubleValue(const char* section, const char* key, double* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetDoubleValue(section, key, value))
return true;
}
}
return false;
}
bool LayeredSettingsInterface::GetBoolValue(const char* section, const char* key, bool* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetBoolValue(section, key, value))
return true;
}
}
return false;
}
bool LayeredSettingsInterface::GetStringValue(const char* section, const char* key, std::string* value) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->GetStringValue(section, key, value))
return true;
}
}
return false;
}
void LayeredSettingsInterface::SetIntValue(const char* section, const char* key, int value)
{
Panic("Attempt to call SetIntValue() on layered settings interface");
}
void LayeredSettingsInterface::SetUIntValue(const char* section, const char* key, u32 value)
{
Panic("Attempt to call SetUIntValue() on layered settings interface");
}
void LayeredSettingsInterface::SetFloatValue(const char* section, const char* key, float value)
{
Panic("Attempt to call SetFloatValue() on layered settings interface");
}
void LayeredSettingsInterface::SetDoubleValue(const char* section, const char* key, double value)
{
Panic("Attempt to call SetDoubleValue() on layered settings interface");
}
void LayeredSettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
{
Panic("Attempt to call SetBoolValue() on layered settings interface");
}
void LayeredSettingsInterface::SetStringValue(const char* section, const char* key, const char* value)
{
Panic("Attempt to call SetStringValue() on layered settings interface");
}
bool LayeredSettingsInterface::ContainsValue(const char* section, const char* key) const
{
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
if (sif->ContainsValue(key, section))
return true;
}
}
return false;
}
void LayeredSettingsInterface::DeleteValue(const char* section, const char* key)
{
Panic("Attempt to call DeleteValue() on layered settings interface");
}
void LayeredSettingsInterface::ClearSection(const char* section)
{
Panic("Attempt to call ClearSection() on layered settings interface");
}
std::vector<std::string> LayeredSettingsInterface::GetStringList(const char* section, const char* key) const
{
std::vector<std::string> ret;
for (u32 layer = FIRST_LAYER; layer <= LAST_LAYER; layer++)
{
if (SettingsInterface* sif = m_layers[layer]; sif != nullptr)
{
ret = sif->GetStringList(section, key);
if (!ret.empty())
break;
}
}
return ret;
}
void LayeredSettingsInterface::SetStringList(const char* section, const char* key,
const std::vector<std::string>& items)
{
Panic("Attempt to call SetStringList() on layered settings interface");
}
bool LayeredSettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item)
{
Panic("Attempt to call RemoveFromStringList() on layered settings interface");
return false;
}
bool LayeredSettingsInterface::AddToStringList(const char* section, const char* key, const char* item)
{
Panic("Attempt to call AddToStringList() on layered settings interface");
return true;
}

View File

@ -0,0 +1,62 @@
#pragma once
#include "settings_interface.h"
#include <array>
class LayeredSettingsInterface final : public SettingsInterface
{
public:
enum Layer : u32
{
LAYER_CMDLINE,
LAYER_GAME,
LAYER_INPUT,
LAYER_BASE,
NUM_LAYERS
};
LayeredSettingsInterface();
~LayeredSettingsInterface() override;
SettingsInterface* GetLayer(Layer layer) const { return m_layers[layer]; }
void SetLayer(Layer layer, SettingsInterface* sif) { m_layers[layer] = sif; }
bool Save() override;
void Clear() override;
bool GetIntValue(const char* section, const char* key, s32* value) const override;
bool GetUIntValue(const char* section, const char* key, u32* value) const override;
bool GetFloatValue(const char* section, const char* key, float* value) const override;
bool GetDoubleValue(const char* section, const char* key, double* value) const override;
bool GetBoolValue(const char* section, const char* key, bool* value) const override;
bool GetStringValue(const char* section, const char* key, std::string* value) const override;
void SetIntValue(const char* section, const char* key, s32 value) override;
void SetUIntValue(const char* section, const char* key, u32 value) override;
void SetFloatValue(const char* section, const char* key, float value) override;
void SetDoubleValue(const char* section, const char* key, double value) override;
void SetBoolValue(const char* section, const char* key, bool value) override;
void SetStringValue(const char* section, const char* key, const char* value) override;
bool ContainsValue(const char* section, const char* key) const override;
void DeleteValue(const char* section, const char* key) override;
void ClearSection(const char* section) override;
std::vector<std::string> GetStringList(const char* section, const char* key) const override;
void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) override;
bool RemoveFromStringList(const char* section, const char* key, const char* item) override;
bool AddToStringList(const char* section, const char* key, const char* item) override;
// default parameter overloads
using SettingsInterface::GetBoolValue;
using SettingsInterface::GetDoubleValue;
using SettingsInterface::GetFloatValue;
using SettingsInterface::GetIntValue;
using SettingsInterface::GetStringValue;
using SettingsInterface::GetUIntValue;
private:
static constexpr Layer FIRST_LAYER = LAYER_CMDLINE;
static constexpr Layer LAST_LAYER = LAYER_BASE;
std::array<SettingsInterface*, NUM_LAYERS> m_layers{};
};

View File

@ -0,0 +1,213 @@
#pragma once
#include "types.h"
#include <optional>
#include <string>
#include <vector>
class SettingsInterface
{
public:
virtual ~SettingsInterface() = default;
virtual bool Save() = 0;
virtual void Clear() = 0;
virtual bool GetIntValue(const char* section, const char* key, s32* value) const = 0;
virtual bool GetUIntValue(const char* section, const char* key, u32* value) const = 0;
virtual bool GetFloatValue(const char* section, const char* key, float* value) const = 0;
virtual bool GetDoubleValue(const char* section, const char* key, double* value) const = 0;
virtual bool GetBoolValue(const char* section, const char* key, bool* value) const = 0;
virtual bool GetStringValue(const char* section, const char* key, std::string* value) const = 0;
virtual void SetIntValue(const char* section, const char* key, s32 value) = 0;
virtual void SetUIntValue(const char* section, const char* key, u32 value) = 0;
virtual void SetFloatValue(const char* section, const char* key, float value) = 0;
virtual void SetDoubleValue(const char* section, const char* key, double value) = 0;
virtual void SetBoolValue(const char* section, const char* key, bool value) = 0;
virtual void SetStringValue(const char* section, const char* key, const char* value) = 0;
virtual std::vector<std::string> GetStringList(const char* section, const char* key) const = 0;
virtual void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) = 0;
virtual bool RemoveFromStringList(const char* section, const char* key, const char* item) = 0;
virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0;
virtual bool ContainsValue(const char* section, const char* key) const = 0;
virtual void DeleteValue(const char* section, const char* key) = 0;
virtual void ClearSection(const char* section) = 0;
ALWAYS_INLINE s32 GetIntValue(const char* section, const char* key, s32 default_value = 0) const
{
s32 value;
return GetIntValue(section, key, &value) ? value : default_value;
}
ALWAYS_INLINE u32 GetUIntValue(const char* section, const char* key, u32 default_value = 0) const
{
u32 value;
return GetUIntValue(section, key, &value) ? value : default_value;
}
ALWAYS_INLINE float GetFloatValue(const char* section, const char* key, float default_value = 0.0f) const
{
float value;
return GetFloatValue(section, key, &value) ? value : default_value;
}
ALWAYS_INLINE double GetDoubleValue(const char* section, const char* key, double default_value = 0.0) const
{
double value;
return GetDoubleValue(section, key, &value) ? value : default_value;
}
ALWAYS_INLINE bool GetBoolValue(const char* section, const char* key, bool default_value = false) const
{
bool value;
return GetBoolValue(section, key, &value) ? value : default_value;
}
ALWAYS_INLINE std::string GetStringValue(const char* section, const char* key, const char* default_value = "") const
{
std::string value;
if (!GetStringValue(section, key, &value))
value.assign(default_value);
return value;
}
ALWAYS_INLINE std::optional<s32> GetOptionalIntValue(const char* section, const char* key,
std::optional<s32> default_value = std::nullopt)
{
s32 ret;
return GetIntValue(section, key, &ret) ? std::optional<s32>(ret) : default_value;
}
ALWAYS_INLINE std::optional<u32> GetOptionalUIntValue(const char* section, const char* key,
std::optional<u32> default_value = std::nullopt)
{
u32 ret;
return GetUIntValue(section, key, &ret) ? std::optional<u32>(ret) : default_value;
}
ALWAYS_INLINE std::optional<float> GetOptionalFloatValue(const char* section, const char* key,
std::optional<float> default_value = std::nullopt)
{
float ret;
return GetFloatValue(section, key, &ret) ? std::optional<float>(ret) : default_value;
}
ALWAYS_INLINE std::optional<double> GetOptionalDoubleValue(const char* section, const char* key,
std::optional<double> default_value = std::nullopt)
{
double ret;
return GetDoubleValue(section, key, &ret) ? std::optional<double>(ret) : default_value;
}
ALWAYS_INLINE std::optional<bool> GetOptionalBoolValue(const char* section, const char* key,
std::optional<bool> default_value = std::nullopt)
{
bool ret;
return GetBoolValue(section, key, &ret) ? std::optional<bool>(ret) : default_value;
}
ALWAYS_INLINE std::optional<std::string>
GetOptionalStringValue(const char* section, const char* key,
std::optional<const char*> default_value = std::nullopt) const
{
std::string ret;
return GetStringValue(section, key, &ret) ? std::optional<std::string>(ret) : default_value;
}
ALWAYS_INLINE void SetOptionalIntValue(const char* section, const char* key, const std::optional<s32>& value)
{
value.has_value() ? SetIntValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void SetOptionalUIntValue(const char* section, const char* key, const std::optional<u32>& value)
{
value.has_value() ? SetUIntValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void SetOptionalFloatValue(const char* section, const char* key, const std::optional<float>& value)
{
value.has_value() ? SetFloatValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void SetOptionalDoubleValue(const char* section, const char* key, const std::optional<double>& value)
{
value.has_value() ? SetDoubleValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void SetOptionalBoolValue(const char* section, const char* key, const std::optional<bool>& value)
{
value.has_value() ? SetBoolValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void SetOptionalStringValue(const char* section, const char* key,
const std::optional<const char*>& value)
{
value.has_value() ? SetStringValue(section, key, value.value()) : DeleteValue(section, key);
}
ALWAYS_INLINE void CopyBoolValue(const SettingsInterface& si, const char* section, const char* key)
{
bool value;
if (si.GetBoolValue(section, key, &value))
SetBoolValue(section, key, value);
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyIntValue(const SettingsInterface& si, const char* section, const char* key)
{
s32 value;
if (si.GetIntValue(section, key, &value))
SetIntValue(section, key, value);
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyUIntValue(const SettingsInterface& si, const char* section, const char* key)
{
u32 value;
if (si.GetUIntValue(section, key, &value))
SetUIntValue(section, key, value);
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyFloatValue(const SettingsInterface& si, const char* section, const char* key)
{
float value;
if (si.GetFloatValue(section, key, &value))
SetFloatValue(section, key, value);
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyDoubleValue(const SettingsInterface& si, const char* section, const char* key)
{
double value;
if (si.GetDoubleValue(section, key, &value))
SetDoubleValue(section, key, value);
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyStringValue(const SettingsInterface& si, const char* section, const char* key)
{
std::string value;
if (si.GetStringValue(section, key, &value))
SetStringValue(section, key, value.c_str());
else
DeleteValue(section, key);
}
ALWAYS_INLINE void CopyStringListValue(const SettingsInterface& si, const char* section, const char* key)
{
std::vector<std::string> value(si.GetStringList(section, key));
if (!value.empty())
SetStringList(section, key, value);
else
DeleteValue(section, key);
}
};