File indexing completed on 2025-05-12 09:08:10
0001 #ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0002 #define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0003
0004 #if defined(_MSC_VER) || \
0005 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
0006 (__GNUC__ >= 4))
0007 #pragma once
0008 #endif
0009
0010 #include "ATOOLS/YAML/yaml-cpp/noexcept.h"
0011 #include <memory>
0012 #include <utility>
0013 #include <vector>
0014
0015 namespace SHERPA_YAML {
0016
0017 class SettingChangeBase {
0018 public:
0019 virtual ~SettingChangeBase() = default;
0020 virtual void pop() = 0;
0021 };
0022
0023 template <typename T>
0024 class Setting {
0025 public:
0026 Setting() : m_value() {}
0027 Setting(const T& value) : m_value() { set(value); }
0028
0029 const T get() const { return m_value; }
0030 std::unique_ptr<SettingChangeBase> set(const T& value);
0031 void restore(const Setting<T>& oldSetting) { m_value = oldSetting.get(); }
0032
0033 private:
0034 T m_value;
0035 };
0036
0037 template <typename T>
0038 class SettingChange : public SettingChangeBase {
0039 public:
0040 SettingChange(Setting<T>* pSetting)
0041 : m_pCurSetting(pSetting),
0042 m_oldSetting(*pSetting)
0043 {}
0044 SettingChange(const SettingChange&) = delete;
0045 SettingChange(SettingChange&&) = delete;
0046 SettingChange& operator=(const SettingChange&) = delete;
0047 SettingChange& operator=(SettingChange&&) = delete;
0048
0049 void pop() override { m_pCurSetting->restore(m_oldSetting); }
0050
0051 private:
0052 Setting<T>* m_pCurSetting;
0053 Setting<T> m_oldSetting;
0054 };
0055
0056 template <typename T>
0057 inline std::unique_ptr<SettingChangeBase> Setting<T>::set(const T& value) {
0058 std::unique_ptr<SettingChangeBase> pChange(new SettingChange<T>(this));
0059 m_value = value;
0060 return pChange;
0061 }
0062
0063 class SettingChanges {
0064 public:
0065 SettingChanges() : m_settingChanges{} {}
0066 SettingChanges(const SettingChanges&) = delete;
0067 SettingChanges(SettingChanges&&) YAML_CPP_NOEXCEPT = default;
0068 SettingChanges& operator=(const SettingChanges&) = delete;
0069 SettingChanges& operator=(SettingChanges&& rhs) YAML_CPP_NOEXCEPT {
0070 if (this == &rhs)
0071 return *this;
0072
0073 clear();
0074 std::swap(m_settingChanges, rhs.m_settingChanges);
0075
0076 return *this;
0077 }
0078 ~SettingChanges() { clear(); }
0079
0080 void clear() YAML_CPP_NOEXCEPT {
0081 restore();
0082 m_settingChanges.clear();
0083 }
0084
0085 void restore() YAML_CPP_NOEXCEPT {
0086 for (const auto& setting : m_settingChanges)
0087 setting->pop();
0088 }
0089
0090 void push(std::unique_ptr<SettingChangeBase> pSettingChange) {
0091 m_settingChanges.push_back(std::move(pSettingChange));
0092 }
0093
0094 private:
0095 using setting_changes = std::vector<std::unique_ptr<SettingChangeBase>>;
0096 setting_changes m_settingChanges;
0097 };
0098 }
0099
0100 #endif