mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 07:50:04 +00:00
Settings variable
This commit is contained in:
parent
65fd47968a
commit
db665ed9ec
5 changed files with 266 additions and 12 deletions
|
|
@ -109,10 +109,12 @@ set(HDR_REQUEST source/scwx/qt/request/nexrad_file_request.hpp)
|
||||||
set(SRC_REQUEST source/scwx/qt/request/nexrad_file_request.cpp)
|
set(SRC_REQUEST source/scwx/qt/request/nexrad_file_request.cpp)
|
||||||
set(HDR_SETTINGS source/scwx/qt/settings/general_settings.hpp
|
set(HDR_SETTINGS source/scwx/qt/settings/general_settings.hpp
|
||||||
source/scwx/qt/settings/map_settings.hpp
|
source/scwx/qt/settings/map_settings.hpp
|
||||||
source/scwx/qt/settings/palette_settings.hpp)
|
source/scwx/qt/settings/palette_settings.hpp
|
||||||
|
source/scwx/qt/settings/settings_variable.hpp)
|
||||||
set(SRC_SETTINGS source/scwx/qt/settings/general_settings.cpp
|
set(SRC_SETTINGS source/scwx/qt/settings/general_settings.cpp
|
||||||
source/scwx/qt/settings/map_settings.cpp
|
source/scwx/qt/settings/map_settings.cpp
|
||||||
source/scwx/qt/settings/palette_settings.cpp)
|
source/scwx/qt/settings/palette_settings.cpp
|
||||||
|
source/scwx/qt/settings/settings_variable.cpp)
|
||||||
set(HDR_TYPES source/scwx/qt/types/qt_types.hpp
|
set(HDR_TYPES source/scwx/qt/types/qt_types.hpp
|
||||||
source/scwx/qt/types/radar_product_record.hpp
|
source/scwx/qt/types/radar_product_record.hpp
|
||||||
source/scwx/qt/types/text_event_key.hpp)
|
source/scwx/qt/types/text_event_key.hpp)
|
||||||
|
|
|
||||||
159
scwx-qt/source/scwx/qt/settings/settings_variable.cpp
Normal file
159
scwx-qt/source/scwx/qt/settings/settings_variable.cpp
Normal file
|
|
@ -0,0 +1,159 @@
|
||||||
|
#include <scwx/qt/settings/settings_variable.hpp>
|
||||||
|
#include <scwx/util/logger.hpp>
|
||||||
|
|
||||||
|
namespace scwx
|
||||||
|
{
|
||||||
|
namespace qt
|
||||||
|
{
|
||||||
|
namespace settings
|
||||||
|
{
|
||||||
|
|
||||||
|
template class SettingsVariable<bool>;
|
||||||
|
template class SettingsVariable<int64_t>;
|
||||||
|
template class SettingsVariable<std::string>;
|
||||||
|
template class SettingsVariable<std::vector<int64_t>>;
|
||||||
|
|
||||||
|
static const std::string logPrefix_ = "scwx::qt::settings::settings_variable";
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class SettingsVariable<T>::Impl
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit Impl(const std::string& name) : name_ {name} {}
|
||||||
|
|
||||||
|
~Impl() {}
|
||||||
|
|
||||||
|
bool Validate(const T& value);
|
||||||
|
|
||||||
|
const std::string name_;
|
||||||
|
T value_ {};
|
||||||
|
T default_ {};
|
||||||
|
std::optional<T> staged_ {};
|
||||||
|
std::optional<T> minimum_ {};
|
||||||
|
std::optional<T> maximum_ {};
|
||||||
|
std::function<bool(const T&)> validator_ {nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
SettingsVariable<T>::SettingsVariable(const std::string& name) :
|
||||||
|
p(std::make_unique<Impl>(name))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
SettingsVariable<T>::~SettingsVariable() = default;
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
SettingsVariable<T>::SettingsVariable(SettingsVariable&&) noexcept = default;
|
||||||
|
template<class T>
|
||||||
|
SettingsVariable<T>&
|
||||||
|
SettingsVariable<T>::operator=(SettingsVariable&&) noexcept = default;
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
std::string SettingsVariable<T>::name() const
|
||||||
|
{
|
||||||
|
return p->name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T SettingsVariable<T>::GetValue() const
|
||||||
|
{
|
||||||
|
return p->value_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool SettingsVariable<T>::SetValue(const T& value)
|
||||||
|
{
|
||||||
|
bool validated = false;
|
||||||
|
|
||||||
|
if (p->Validate(value))
|
||||||
|
{
|
||||||
|
p->value_ = value;
|
||||||
|
validated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool SettingsVariable<T>::SetValueOrDefault(const T& value)
|
||||||
|
{
|
||||||
|
bool validated = false;
|
||||||
|
|
||||||
|
if (p->Validate(value))
|
||||||
|
{
|
||||||
|
p->value_ = value;
|
||||||
|
validated = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p->value_ = p->default_;
|
||||||
|
}
|
||||||
|
|
||||||
|
return validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool SettingsVariable<T>::StageValue(const T& value)
|
||||||
|
{
|
||||||
|
bool validated = false;
|
||||||
|
|
||||||
|
if (p->Validate(value))
|
||||||
|
{
|
||||||
|
p->staged_ = value;
|
||||||
|
validated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return validated;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void SettingsVariable<T>::Commit()
|
||||||
|
{
|
||||||
|
if (p->staged_.has_value())
|
||||||
|
{
|
||||||
|
p->value_ = std::move(p->staged_.value());
|
||||||
|
p->staged_.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T SettingsVariable<T>::GetDefault() const
|
||||||
|
{
|
||||||
|
return p->default_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void SettingsVariable<T>::SetDefault(const T& value)
|
||||||
|
{
|
||||||
|
p->default_ = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void SettingsVariable<T>::SetMinimum(const T& value)
|
||||||
|
{
|
||||||
|
p->minimum_ = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void SettingsVariable<T>::SetMaximum(const T& value)
|
||||||
|
{
|
||||||
|
p->maximum_ = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void SettingsVariable<T>::SetValidator(std::function<bool(const T&)> validator)
|
||||||
|
{
|
||||||
|
p->validator_ = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool SettingsVariable<T>::Impl::Validate(const T& value)
|
||||||
|
{
|
||||||
|
return ((!minimum_.has_value() || value >= minimum_) && // Validate minimum
|
||||||
|
(!maximum_.has_value() || value <= maximum_) && // Validate maximum
|
||||||
|
(validator_ == nullptr || validator_(value))); // User-validation
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace settings
|
||||||
|
} // namespace qt
|
||||||
|
} // namespace scwx
|
||||||
56
scwx-qt/source/scwx/qt/settings/settings_variable.hpp
Normal file
56
scwx-qt/source/scwx/qt/settings/settings_variable.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace scwx
|
||||||
|
{
|
||||||
|
namespace qt
|
||||||
|
{
|
||||||
|
namespace settings
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class SettingsVariable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit SettingsVariable(const std::string& name);
|
||||||
|
~SettingsVariable();
|
||||||
|
|
||||||
|
SettingsVariable(const SettingsVariable&) = delete;
|
||||||
|
SettingsVariable& operator=(const SettingsVariable&) = delete;
|
||||||
|
|
||||||
|
SettingsVariable(SettingsVariable&&) noexcept;
|
||||||
|
SettingsVariable& operator=(SettingsVariable&&) noexcept;
|
||||||
|
|
||||||
|
std::string name() const;
|
||||||
|
|
||||||
|
T GetValue() const;
|
||||||
|
bool SetValue(const T& value);
|
||||||
|
bool SetValueOrDefault(const T& value);
|
||||||
|
|
||||||
|
bool StageValue(const T& value);
|
||||||
|
void Commit();
|
||||||
|
|
||||||
|
T GetDefault() const;
|
||||||
|
|
||||||
|
void SetDefault(const T& value);
|
||||||
|
void SetMinimum(const T& value);
|
||||||
|
void SetMaximum(const T& value);
|
||||||
|
void SetValidator(std::function<bool(const T&)> validator);
|
||||||
|
|
||||||
|
private:
|
||||||
|
class Impl;
|
||||||
|
std::unique_ptr<Impl> p;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Instantiated templates:
|
||||||
|
// template class SettingsVariable<bool>;
|
||||||
|
// template class SettingsVariable<int64_t>;
|
||||||
|
// template class SettingsVariable<std::string>;
|
||||||
|
// template class SettingsVariable<std::vector<int64_t>>;
|
||||||
|
|
||||||
|
} // namespace settings
|
||||||
|
} // namespace qt
|
||||||
|
} // namespace scwx
|
||||||
34
test/source/scwx/qt/settings/settings_variable.test.cpp
Normal file
34
test/source/scwx/qt/settings/settings_variable.test.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include <scwx/qt/settings/settings_variable.hpp>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
namespace scwx
|
||||||
|
{
|
||||||
|
namespace qt
|
||||||
|
{
|
||||||
|
namespace settings
|
||||||
|
{
|
||||||
|
|
||||||
|
TEST(SettingsVariableTest, String)
|
||||||
|
{
|
||||||
|
SettingsVariable<std::string> stringVariable {"string"};
|
||||||
|
stringVariable.SetDefault("Default");
|
||||||
|
stringVariable.SetValidator([](const std::string& value)
|
||||||
|
{ return !value.empty(); });
|
||||||
|
stringVariable.SetValue("Hello World");
|
||||||
|
|
||||||
|
EXPECT_EQ(stringVariable.name(), "string");
|
||||||
|
EXPECT_EQ(stringVariable.GetValue(), "Hello World");
|
||||||
|
EXPECT_EQ(stringVariable.SetValue(""), false);
|
||||||
|
EXPECT_EQ(stringVariable.GetValue(), "Hello World");
|
||||||
|
EXPECT_EQ(stringVariable.SetValueOrDefault(""), false);
|
||||||
|
EXPECT_EQ(stringVariable.GetValue(), "Default");
|
||||||
|
EXPECT_EQ(stringVariable.SetValue("Value 1"), true);
|
||||||
|
EXPECT_EQ(stringVariable.GetValue(), "Value 1");
|
||||||
|
EXPECT_EQ(stringVariable.SetValueOrDefault("Value 2"), true);
|
||||||
|
EXPECT_EQ(stringVariable.GetValue(), "Value 2");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace settings
|
||||||
|
} // namespace qt
|
||||||
|
} // namespace scwx
|
||||||
|
|
@ -23,6 +23,7 @@ set(SRC_QT_CONFIG_TESTS source/scwx/qt/config/county_database.test.cpp
|
||||||
source/scwx/qt/config/radar_site.test.cpp)
|
source/scwx/qt/config/radar_site.test.cpp)
|
||||||
set(SRC_QT_MANAGER_TESTS source/scwx/qt/manager/settings_manager.test.cpp)
|
set(SRC_QT_MANAGER_TESTS source/scwx/qt/manager/settings_manager.test.cpp)
|
||||||
set(SRC_QT_MODEL_TESTS source/scwx/qt/model/imgui_context_model.test.cpp)
|
set(SRC_QT_MODEL_TESTS source/scwx/qt/model/imgui_context_model.test.cpp)
|
||||||
|
set(SRC_QT_SETTINGS_TESTS source/scwx/qt/settings/settings_variable.test.cpp)
|
||||||
set(SRC_UTIL_TESTS source/scwx/util/float.test.cpp
|
set(SRC_UTIL_TESTS source/scwx/util/float.test.cpp
|
||||||
source/scwx/util/rangebuf.test.cpp
|
source/scwx/util/rangebuf.test.cpp
|
||||||
source/scwx/util/streams.test.cpp
|
source/scwx/util/streams.test.cpp
|
||||||
|
|
@ -41,20 +42,22 @@ add_executable(wxtest ${SRC_MAIN}
|
||||||
${SRC_QT_CONFIG_TESTS}
|
${SRC_QT_CONFIG_TESTS}
|
||||||
${SRC_QT_MANAGER_TESTS}
|
${SRC_QT_MANAGER_TESTS}
|
||||||
${SRC_QT_MODEL_TESTS}
|
${SRC_QT_MODEL_TESTS}
|
||||||
|
${SRC_QT_SETTINGS_TESTS}
|
||||||
${SRC_UTIL_TESTS}
|
${SRC_UTIL_TESTS}
|
||||||
${SRC_WSR88D_TESTS}
|
${SRC_WSR88D_TESTS}
|
||||||
${CMAKE_FILES})
|
${CMAKE_FILES})
|
||||||
|
|
||||||
source_group("Source Files\\main" FILES ${SRC_MAIN})
|
source_group("Source Files\\main" FILES ${SRC_MAIN})
|
||||||
source_group("Source Files\\awips" FILES ${SRC_AWIPS_TESTS})
|
source_group("Source Files\\awips" FILES ${SRC_AWIPS_TESTS})
|
||||||
source_group("Source Files\\common" FILES ${SRC_COMMON_TESTS})
|
source_group("Source Files\\common" FILES ${SRC_COMMON_TESTS})
|
||||||
source_group("Source Files\\network" FILES ${SRC_NETWORK_TESTS})
|
source_group("Source Files\\network" FILES ${SRC_NETWORK_TESTS})
|
||||||
source_group("Source Files\\provider" FILES ${SRC_PROVIDER_TESTS})
|
source_group("Source Files\\provider" FILES ${SRC_PROVIDER_TESTS})
|
||||||
source_group("Source Files\\qt\\config" FILES ${SRC_QT_CONFIG_TESTS})
|
source_group("Source Files\\qt\\config" FILES ${SRC_QT_CONFIG_TESTS})
|
||||||
source_group("Source Files\\qt\\manager" FILES ${SRC_QT_MANAGER_TESTS})
|
source_group("Source Files\\qt\\manager" FILES ${SRC_QT_MANAGER_TESTS})
|
||||||
source_group("Source Files\\qt\\model" FILES ${SRC_QT_MODEL_TESTS})
|
source_group("Source Files\\qt\\model" FILES ${SRC_QT_MODEL_TESTS})
|
||||||
source_group("Source Files\\util" FILES ${SRC_UTIL_TESTS})
|
source_group("Source Files\\qt\\settings" FILES ${SRC_QT_SETTINGS_TESTS})
|
||||||
source_group("Source Files\\wsr88d" FILES ${SRC_WSR88D_TESTS})
|
source_group("Source Files\\util" FILES ${SRC_UTIL_TESTS})
|
||||||
|
source_group("Source Files\\wsr88d" FILES ${SRC_WSR88D_TESTS})
|
||||||
|
|
||||||
target_include_directories(wxtest PRIVATE ${GTest_INCLUDE_DIRS})
|
target_include_directories(wxtest PRIVATE ${GTest_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue