Kea  1.9.9-git
staged_value.h
Go to the documentation of this file.
1 // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef STAGED_VALUE_H
8 #define STAGED_VALUE_H
9 
10 #include <boost/noncopyable.hpp>
11 #include <boost/shared_ptr.hpp>
12 
13 namespace isc {
14 namespace util {
15 
31 template<typename ValueType>
32 class StagedValue : public boost::noncopyable {
33 public:
34 
39  : staging_(new ValueType()), current_(new ValueType()),
40  modified_(false) {
41  }
42 
48  const ValueType& getValue() const {
49  return (modified_ ? *staging_ : *current_);
50  }
51 
55  void setValue(const ValueType& new_value) {
56  *staging_ = new_value;
57  modified_ = true;
58  }
59 
61  void commit() {
62  // Only apply changes if any modifications made.
63  if (modified_) {
64  current_ = staging_;
65  }
66  revert();
67  }
68 
70  void reset() {
71  revert();
72  current_.reset(new ValueType());
73  }
74 
76  void revert() {
77  staging_.reset(new ValueType());
78  modified_ = false;
79  }
80 
85  StagedValue& operator=(const ValueType& value) {
86  setValue(value);
87  return (*this);
88  }
89 
93  operator const ValueType&() const {
94  return (getValue());
95  }
96 
97 private:
98 
102  boost::shared_ptr<ValueType> staging_;
103 
107  boost::shared_ptr<ValueType> current_;
108 
111  bool modified_;
112 
113 };
114 
115 } // namespace isc::util
116 } // namespace isc
117 
118 #endif // STAGED_VALUE_H
StagedValue & operator=(const ValueType &value)
Assignment operator.
Definition: staged_value.h:85
void revert()
Reverts any modifications since last commit.
Definition: staged_value.h:76
void reset()
Resets value to defaults.
Definition: staged_value.h:70
StagedValue()
Constructor.
Definition: staged_value.h:38
const ValueType & getValue() const
Retrieves current value.
Definition: staged_value.h:48
void commit()
Commits a value.
Definition: staged_value.h:61
void setValue(const ValueType &new_value)
Sets new value.
Definition: staged_value.h:55
Defines the logger used by the top-level component of kea-dhcp-ddns.
This class implements set/commit mechanism for a single object.
Definition: staged_value.h:32