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:
69  ~SettingChanges() { clear(); }
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
89  SettingChanges& operator = (SettingChanges& rhs) {
90  if(this == &rhs)
91  return *this;
92 
93  clear();
94  m_settingChanges = rhs.m_settingChanges;
95  rhs.m_settingChanges.clear();
96  return *this;
97  }
98 
99  private:
100  typedef std::vector <SettingChangeBase *> setting_changes;
101  setting_changes m_settingChanges;
102  };
103 }
104 
105 #endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
void push(std::auto_ptr< SettingChangeBase > pSettingChange)
Definition: setting.h:84
std::auto_ptr< SettingChangeBase > set(const T &value)
Definition: setting.h:59
std::vector< SettingChangeBase * > setting_changes
Definition: setting.h:100
SettingChange(Setting< T > *pSetting)
Definition: setting.h:44
virtual ~SettingChangeBase()
Definition: setting.h:36
Setting< T > * m_pCurSetting
Definition: setting.h:54
void restore(const Setting< T > &oldSetting)
Definition: setting.h:25
setting_changes m_settingChanges
Definition: setting.h:101
Setting< T > m_oldSetting
Definition: setting.h:55
const T get() const
Definition: setting.h:23
virtual void pop()
Definition: setting.h:49


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:03