OSDN Git Service

First version
[st-ro/stro.git] / 3rdparty / yaml-cpp / src / setting.h
1 #ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3
4 #if defined(_MSC_VER) ||                                            \
5     (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
6      (__GNUC__ >= 4))  // GCC supports "pragma once" correctly since 3.4
7 #pragma once
8 #endif
9
10 #include <memory>
11 #include <vector>
12 #include "yaml-cpp/noncopyable.h"
13
14 namespace YAML {
15 class SettingChangeBase;
16
17 template <typename T>
18 class Setting {
19  public:
20   Setting() : m_value() {}
21
22   const T get() const { return m_value; }
23   std::unique_ptr<SettingChangeBase> set(const T& value);
24   void restore(const Setting<T>& oldSetting) { m_value = oldSetting.get(); }
25
26  private:
27   T m_value;
28 };
29
30 class SettingChangeBase {
31  public:
32   virtual ~SettingChangeBase() {}
33   virtual void pop() = 0;
34 };
35
36 template <typename T>
37 class SettingChange : public SettingChangeBase {
38  public:
39   SettingChange(Setting<T>* pSetting) : m_pCurSetting(pSetting) {
40     // copy old setting to save its state
41     m_oldSetting = *pSetting;
42   }
43
44   virtual void pop() { m_pCurSetting->restore(m_oldSetting); }
45
46  private:
47   Setting<T>* m_pCurSetting;
48   Setting<T> m_oldSetting;
49 };
50
51 template <typename T>
52 inline std::unique_ptr<SettingChangeBase> Setting<T>::set(const T& value) {
53   std::unique_ptr<SettingChangeBase> pChange(new SettingChange<T>(this));
54   m_value = value;
55   return pChange;
56 }
57
58 class SettingChanges : private noncopyable {
59  public:
60   SettingChanges() {}
61   ~SettingChanges() { clear(); }
62
63   void clear() {
64     restore();
65     m_settingChanges.clear();
66   }
67
68   void restore() {
69     for (setting_changes::const_iterator it = m_settingChanges.begin();
70          it != m_settingChanges.end(); ++it)
71       (*it)->pop();
72   }
73
74   void push(std::unique_ptr<SettingChangeBase> pSettingChange) {
75     m_settingChanges.push_back(std::move(pSettingChange));
76   }
77
78   // like std::unique_ptr - assignment is transfer of ownership
79   SettingChanges& operator=(SettingChanges&& rhs) {
80     if (this == &rhs)
81       return *this;
82
83     clear();
84     std::swap(m_settingChanges, rhs.m_settingChanges);
85
86     return *this;
87   }
88
89  private:
90   typedef std::vector<std::unique_ptr<SettingChangeBase>> setting_changes;
91   setting_changes m_settingChanges;
92 };
93 }
94
95 #endif  // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66