Ocean
ScopedValue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_SCOPED_VALUE_H
9 #define META_OCEAN_BASE_SCOPED_VALUE_H
10 
11 #include "ocean/base/Base.h"
12 
13 namespace Ocean
14 {
15 
16 /**
17  * This class implements a scoped value that allows to change a specified value at the end of a scope.
18  * @tparam T Data type of the value to be set
19  * @ingroup base
20  */
21 template <typename T>
23 {
24  public:
25 
26  /**
27  * Creates a new scoped value object.
28  * @param target The target value that will be changed at the end of the surrounding scope
29  * @param delayedValue Value that will be assigned at the end of the surrounding scope
30  */
31  inline ScopedValueT(T& target, const T& delayedValue);
32 
33  /**
34  * Creates a new scoped value object.
35  * @param target The target value that will be changed at the end of the surrounding scope
36  * @param delayedValue Value that will be assigned at the end of the surrounding scope
37  */
38  inline ScopedValueT(T& target, T&& delayedValue);
39 
40  /**
41  * Creates a new scoped value object.
42  * @param target The target value that will be changed at the end of the surrounding scope
43  * @param delayedValue Value that will be assigned at the end of the surrounding scope
44  * @param immediateValue Value that will be assigned directly inside the constructor
45  */
46  inline ScopedValueT(T& target, const T& delayedValue, const T& immediateValue);
47 
48  /**
49  * Creates a new scoped value object.
50  * @param target The target value that will be changed at the end of the surrounding scope
51  * @param delayedValue Value that will be assigned at the end of the surrounding scope
52  * @param immediateValue Value that will be assigned directly inside the constructor
53  */
54  inline ScopedValueT(T& target, T&& delayedValue, T&& immediateValue);
55 
56  /**
57  * Destructs the scoped value object.
58  */
59  inline ~ScopedValueT();
60 
61  /**
62  * Changes the value that will be assigned at the end of the surrounding scope.
63  * @param value The value to be changed
64  */
65  inline void setDelayed(const T& value);
66 
67  /**
68  * Changes the value that will be assigned at the end of the surrounding scope.
69  * @param value The value to be changed
70  */
71  inline void setDelayed(T&& value);
72 
73  /**
74  * Immediately changes the target value, the modification is not applied at the end of the surrounding scope
75  * @param value The value to be changed
76  */
77  inline void setImmediately(const T& value);
78 
79  /**
80  * Immediately changes the target value, the modification is not applied at the end of the surrounding scope
81  * @param value The value to be changed
82  */
83  inline void setImmediately(T&& value);
84 
85  protected:
86 
87  /**
88  * Disabled copy constructor.
89  * @param value The value which would be copied
90  */
91  ScopedValueT(const ScopedValueT<T>& value) = delete;
92 
93  /**
94  * Disabled copy operator.
95  * @param value The value which would be copied
96  */
97  ScopedValueT& operator=(const ScopedValueT<T>& value) = delete;
98 
99  protected:
100 
101  /// Target value that will be changed at the end of the surrounding scope.
103 
104  /// Value that will be assigned at the end of the surrounding scope.
106 };
107 
108 template <typename T>
109 inline ScopedValueT<T>::ScopedValueT(T& target, const T& delayedValue) :
110  valueTarget(target),
111  valueDelayed(delayedValue)
112 {
113  // nothing to do here
114 }
115 
116 template <typename T>
117 inline ScopedValueT<T>::ScopedValueT(T& target, T&& delayedValue) :
118  valueTarget(target),
119  valueDelayed(std::move(delayedValue))
120 {
121  // nothing to do here
122 }
123 
124 template <typename T>
125 inline ScopedValueT<T>::ScopedValueT(T& target, const T& delayedValue, const T& immediateValue) :
126  valueTarget(target),
127  valueDelayed(delayedValue)
128 {
129  target = immediateValue;
130 }
131 
132 template <typename T>
133 inline ScopedValueT<T>::ScopedValueT(T& target, T&& delayedValue, T&& immediateValue) :
134  valueTarget(target),
135  valueDelayed(std::move(delayedValue))
136 {
137  target = std::move(immediateValue);
138 }
139 
140 template <typename T>
142 {
143  valueTarget = valueDelayed;
144 }
145 
146 template <typename T>
147 inline void ScopedValueT<T>::setDelayed(const T& value)
148 {
149  valueDelayed = value;
150 }
151 
152 template <typename T>
153 inline void ScopedValueT<T>::setDelayed(T&& value)
154 {
155  valueDelayed = std::move(value);
156 }
157 
158 template <typename T>
159 inline void ScopedValueT<T>::setImmediately(const T& value)
160 {
161  valueTarget = value;
162 }
163 
164 template <typename T>
165 inline void ScopedValueT<T>::setImmediately(T&& value)
166 {
167  valueTarget = std::move(value);
168 }
169 
170 }
171 
172 #endif // META_OCEAN_BASE_SCOPED_VALUE_H
This class implements a scoped value that allows to change a specified value at the end of a scope.
Definition: ScopedValue.h:23
ScopedValueT(const ScopedValueT< T > &value)=delete
Disabled copy constructor.
ScopedValueT(T &target, const T &delayedValue)
Creates a new scoped value object.
Definition: ScopedValue.h:109
T valueDelayed
Value that will be assigned at the end of the surrounding scope.
Definition: ScopedValue.h:105
ScopedValueT & operator=(const ScopedValueT< T > &value)=delete
Disabled copy operator.
~ScopedValueT()
Destructs the scoped value object.
Definition: ScopedValue.h:141
T & valueTarget
Target value that will be changed at the end of the surrounding scope.
Definition: ScopedValue.h:102
void setImmediately(const T &value)
Immediately changes the target value, the modification is not applied at the end of the surrounding s...
Definition: ScopedValue.h:159
void setDelayed(const T &value)
Changes the value that will be assigned at the end of the surrounding scope.
Definition: ScopedValue.h:147
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15