scide: implement selectionLength for openDocument
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / src / setting.h
blob806ccdae96d19a020fe93f0c759c1d053a03acd4
1 #ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
4 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5 #pragma once
6 #endif
9 #include <memory>
10 #include <vector>
11 #include "yaml-cpp/noncopyable.h"
13 namespace YAML
15 class SettingChangeBase;
17 template <typename T>
18 class Setting
20 public:
21 Setting(): m_value() {}
23 const T get() const { return m_value; }
24 std::auto_ptr <SettingChangeBase> set(const T& value);
25 void restore(const Setting<T>& oldSetting) {
26 m_value = oldSetting.get();
29 private:
30 T m_value;
33 class SettingChangeBase
35 public:
36 virtual ~SettingChangeBase() {}
37 virtual void pop() = 0;
40 template <typename T>
41 class SettingChange: public SettingChangeBase
43 public:
44 SettingChange(Setting<T> *pSetting): m_pCurSetting(pSetting) {
45 // copy old setting to save its state
46 m_oldSetting = *pSetting;
49 virtual void pop() {
50 m_pCurSetting->restore(m_oldSetting);
53 private:
54 Setting<T> *m_pCurSetting;
55 Setting<T> m_oldSetting;
58 template <typename T>
59 inline std::auto_ptr <SettingChangeBase> Setting<T>::set(const T& value) {
60 std::auto_ptr <SettingChangeBase> pChange(new SettingChange<T> (this));
61 m_value = value;
62 return pChange;
65 class SettingChanges: private noncopyable
67 public:
68 SettingChanges() {}
69 ~SettingChanges() { clear(); }
71 void clear() {
72 restore();
74 for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
75 delete *it;
76 m_settingChanges.clear();
79 void restore() {
80 for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
81 (*it)->pop();
84 void push(std::auto_ptr <SettingChangeBase> pSettingChange) {
85 m_settingChanges.push_back(pSettingChange.release());
88 // like std::auto_ptr - assignment is transfer of ownership
89 SettingChanges& operator = (SettingChanges& rhs) {
90 if(this == &rhs)
91 return *this;
93 clear();
94 m_settingChanges = rhs.m_settingChanges;
95 rhs.m_settingChanges.clear();
96 return *this;
99 private:
100 typedef std::vector <SettingChangeBase *> setting_changes;
101 setting_changes m_settingChanges;
105 #endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66