Kea 2.7.5
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
13namespace isc {
14namespace util {
15
31template<typename ValueType>
32class StagedValue : public boost::noncopyable {
33public:
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
97private:
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
This class implements set/commit mechanism for a single object.
void revert()
Reverts any modifications since last commit.
StagedValue & operator=(const ValueType &value)
Assignment operator.
void commit()
Commits a value.
void setValue(const ValueType &new_value)
Sets new value.
const ValueType & getValue() const
Retrieves current value.
void reset()
Resets value to defaults.
StagedValue()
Constructor.
Defines the logger used by the top-level component of kea-lfc.