Ocean
Loading...
Searching...
No Matches
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
13namespace 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 */
21template <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
108template <typename T>
109inline ScopedValueT<T>::ScopedValueT(T& target, const T& delayedValue) :
110 valueTarget(target),
111 valueDelayed(delayedValue)
112{
113 // nothing to do here
114}
115
116template <typename T>
117inline ScopedValueT<T>::ScopedValueT(T& target, T&& delayedValue) :
118 valueTarget(target),
119 valueDelayed(std::move(delayedValue))
120{
121 // nothing to do here
122}
123
124template <typename T>
125inline ScopedValueT<T>::ScopedValueT(T& target, const T& delayedValue, const T& immediateValue) :
126 valueTarget(target),
127 valueDelayed(delayedValue)
128{
129 target = immediateValue;
130}
131
132template <typename T>
133inline 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
140template <typename T>
142{
143 valueTarget = valueDelayed;
144}
145
146template <typename T>
147inline void ScopedValueT<T>::setDelayed(const T& value)
148{
149 valueDelayed = value;
150}
151
152template <typename T>
153inline void ScopedValueT<T>::setDelayed(T&& value)
154{
155 valueDelayed = std::move(value);
156}
157
158template <typename T>
159inline void ScopedValueT<T>::setImmediately(const T& value)
160{
161 valueTarget = value;
162}
163
164template <typename T>
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()
Destructs the scoped value object.
Definition ScopedValue.h:141
ScopedValueT & operator=(const ScopedValueT< T > &value)=delete
Disabled copy operator.
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