setting.h
Go to the documentation of this file.
1 #ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
2 #define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
3 
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
7 
8 
9 #include <memory>
10 #include <vector>
12 
13 namespace YAML_PM
14 {
15  class SettingChangeBase;
16 
17  template <typename T>
18  class Setting
19  {
20  public:
21  Setting(): m_value() {}
22 
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();
27  }
28 
29  private:
31  };
32 
34  {
35  public:
36  virtual ~SettingChangeBase() {}
37  virtual void pop() = 0;
38  };
39 
40  template <typename T>
42  {
43  public:
44  SettingChange(Setting<T> *pSetting): m_pCurSetting(pSetting) {
45  // copy old setting to save its state
46  m_oldSetting = *pSetting;
47  }
48 
49  virtual void pop() {
50  m_pCurSetting->restore(m_oldSetting);
51  }
52 
53  private:
56  };
57 
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;
63  }
64 
65  class SettingChanges: private noncopyable
66  {
67  public:
70 
71  void clear() {
72  restore();
73 
74  for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
75  delete *it;
76  m_settingChanges.clear();
77  }
78 
79  void restore() {
80  for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
81  (*it)->pop();
82  }
83 
84  void push(std::auto_ptr <SettingChangeBase> pSettingChange) {
85  m_settingChanges.push_back(pSettingChange.release());
86  }
87 
88  // like std::auto_ptr - assignment is transfer of ownership
90  if(this == &rhs)
91  return *this;
92 
93  clear();
95  rhs.m_settingChanges.clear();
96  return *this;
97  }
98 
99  private:
100  typedef std::vector <SettingChangeBase *> setting_changes;
102  };
103 }
104 
105 #endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
YAML_PM::SettingChanges::SettingChanges
SettingChanges()
Definition: setting.h:68
YAML_PM::SettingChangeBase::pop
virtual void pop()=0
YAML_PM::SettingChanges::push
void push(std::auto_ptr< SettingChangeBase > pSettingChange)
Definition: setting.h:84
YAML_PM::SettingChanges::~SettingChanges
~SettingChanges()
Definition: setting.h:69
YAML_PM::SettingChange::m_oldSetting
Setting< T > m_oldSetting
Definition: setting.h:55
YAML_PM::SettingChange
Definition: setting.h:41
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::Setting
Definition: setting.h:18
YAML_PM::SettingChanges::restore
void restore()
Definition: setting.h:79
YAML_PM::SettingChange::m_pCurSetting
Setting< T > * m_pCurSetting
Definition: setting.h:54
YAML_PM::SettingChangeBase::~SettingChangeBase
virtual ~SettingChangeBase()
Definition: setting.h:36
YAML_PM::Setting::Setting
Setting()
Definition: setting.h:21
YAML_PM::SettingChanges::operator=
SettingChanges & operator=(SettingChanges &rhs)
Definition: setting.h:89
YAML_PM::Setting::restore
void restore(const Setting< T > &oldSetting)
Definition: setting.h:25
YAML_PM::SettingChangeBase
Definition: setting.h:33
YAML_PM::SettingChange::SettingChange
SettingChange(Setting< T > *pSetting)
Definition: setting.h:44
noncopyable.h
YAML_PM::SettingChanges::setting_changes
std::vector< SettingChangeBase * > setting_changes
Definition: setting.h:100
YAML_PM::SettingChanges::m_settingChanges
setting_changes m_settingChanges
Definition: setting.h:101
YAML_PM::SettingChanges::clear
void clear()
Definition: setting.h:71
YAML_PM::SettingChanges
Definition: setting.h:65
YAML_PM::Setting::get
const T get() const
Definition: setting.h:23
YAML_PM::SettingChange::pop
virtual void pop()
Definition: setting.h:49
YAML_PM::Setting::set
std::auto_ptr< SettingChangeBase > set(const T &value)
Definition: setting.h:59
YAML_PM::noncopyable
Definition: noncopyable.h:13
YAML_PM::Setting::m_value
T m_value
Definition: setting.h:30


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:41