/Users/deen/code/yugabyte-db/src/yb/util/value_changer.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_UTIL_VALUE_CHANGER_H |
15 | | #define YB_UTIL_VALUE_CHANGER_H |
16 | | |
17 | | namespace yb { |
18 | | |
19 | | template <class T> |
20 | | class ValueChanger { |
21 | | public: |
22 | 0 | ValueChanger() : value_(nullptr) {} |
23 | | |
24 | | ValueChanger(T new_value, T* value) { |
25 | | Init(new_value, value); |
26 | | } |
27 | | |
28 | | ValueChanger(const ValueChanger&) = delete; |
29 | | void operator=(const ValueChanger&) = delete; |
30 | | |
31 | 0 | ~ValueChanger() { |
32 | 0 | Reset(); |
33 | 0 | } |
34 | | |
35 | 0 | void Init(T new_value, T* value) { |
36 | 0 | value_ = value; |
37 | 0 | original_value_ = *value; |
38 | 0 | *value = new_value; |
39 | 0 | } |
40 | | |
41 | 0 | void Reset() { |
42 | 0 | if (value_) { |
43 | 0 | *value_ = original_value_; |
44 | 0 | value_ = nullptr; |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | private: |
49 | | T* value_; |
50 | | T original_value_; |
51 | | }; |
52 | | |
53 | | } // namespace yb |
54 | | |
55 | | #endif // YB_UTIL_VALUE_CHANGER_H |