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 (or object) to be set
19 * @tparam TValue Data type of the value to be used for the delayed (and immediate) assignment
20 * @ingroup base
21 */
22template <typename T, typename TValue = T>
24{
25 public:
26
27 /**
28 * Creates a new scoped value object.
29 * @param target The target value that will be changed at the end of the surrounding scope
30 * @param delayedValue Value that will be assigned at the end of the surrounding scope
31 */
32 inline ScopedValueT(T& target, const TValue& delayedValue);
33
34 /**
35 * Creates a new scoped value object.
36 * @param target The target value that will be changed at the end of the surrounding scope
37 * @param delayedValue Value that will be assigned at the end of the surrounding scope
38 */
39 inline ScopedValueT(T& target, TValue&& delayedValue);
40
41 /**
42 * Creates a new scoped value object.
43 * @param target The target value that will be changed at the end of the surrounding scope
44 * @param delayedValue Value that will be assigned at the end of the surrounding scope
45 * @param immediateValue Value that will be assigned directly inside the constructor
46 */
47 inline ScopedValueT(T& target, const TValue& delayedValue, const TValue& immediateValue);
48
49 /**
50 * Creates a new scoped value object.
51 * @param target The target value that will be changed at the end of the surrounding scope
52 * @param delayedValue Value that will be assigned at the end of the surrounding scope
53 * @param immediateValue Value that will be assigned directly inside the constructor
54 */
55 inline ScopedValueT(T& target, TValue&& delayedValue, TValue&& immediateValue);
56
57 /**
58 * Destructs the scoped value object.
59 */
60 inline ~ScopedValueT();
61
62 /**
63 * Changes the value that will be assigned at the end of the surrounding scope.
64 * @param value The value to be changed
65 */
66 inline void setDelayed(const TValue& value);
67
68 /**
69 * Changes the value that will be assigned at the end of the surrounding scope.
70 * @param value The value to be changed
71 */
72 inline void setDelayed(TValue&& value);
73
74 /**
75 * Immediately changes the target value, the modification is not applied at the end of the surrounding scope
76 * @param value The value to be changed
77 */
78 inline void setImmediately(const TValue& value);
79
80 /**
81 * Immediately changes the target value, the modification is not applied at the end of the surrounding scope
82 * @param value The value to be changed
83 */
84 inline void setImmediately(TValue&& value);
85
86 protected:
87
88 /**
89 * Disabled copy constructor.
90 * @param value The value which would be copied
91 */
92 ScopedValueT(const ScopedValueT<T>& value) = delete;
93
94 /**
95 * Disabled copy operator.
96 * @param value The value which would be copied
97 */
98 ScopedValueT& operator=(const ScopedValueT<T>& value) = delete;
99
100 protected:
101
102 /// Target value that will be changed at the end of the surrounding scope.
104
105 /// Value that will be assigned at the end of the surrounding scope.
106 TValue delayed_;
107};
108
109template <typename T, typename TValue>
110inline ScopedValueT<T, TValue>::ScopedValueT(T& target, const TValue& delayedValue) :
111 target_(target),
112 delayed_(delayedValue)
113{
114 // nothing to do here
115}
116
117template <typename T, typename TValue>
118inline ScopedValueT<T, TValue>::ScopedValueT(T& target, TValue&& delayedValue) :
119 target_(target),
120 delayed_(std::move(delayedValue))
121{
122 // nothing to do here
123}
124
125template <typename T, typename TValue>
126inline ScopedValueT<T, TValue>::ScopedValueT(T& target, const TValue& delayedValue, const TValue& immediateValue) :
127 target_(target),
128 delayed_(delayedValue)
129{
130 target = immediateValue;
131}
132
133template <typename T, typename TValue>
134inline ScopedValueT<T, TValue>::ScopedValueT(T& target, TValue&& delayedValue, TValue&& immediateValue) :
135 target_(target),
136 delayed_(std::move(delayedValue))
137{
138 target = std::move(immediateValue);
139}
140
141template <typename T, typename TValue>
143{
144 target_ = delayed_;
145}
146
147template <typename T, typename TValue>
148inline void ScopedValueT<T, TValue>::setDelayed(const TValue& value)
149{
150 delayed_ = value;
151}
152
153template <typename T, typename TValue>
154inline void ScopedValueT<T, TValue>::setDelayed(TValue&& value)
155{
156 delayed_ = std::move(value);
157}
158
159template <typename T, typename TValue>
160inline void ScopedValueT<T, TValue>::setImmediately(const TValue& value)
161{
162 target_ = value;
163}
164
165template <typename T, typename TValue>
167{
168 target_ = std::move(value);
169}
170
171}
172
173#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:24
ScopedValueT(const ScopedValueT< T > &value)=delete
Disabled copy constructor.
ScopedValueT(T &target, const TValue &delayedValue)
Creates a new scoped value object.
Definition ScopedValue.h:110
ScopedValueT & operator=(const ScopedValueT< T > &value)=delete
Disabled copy operator.
~ScopedValueT()
Destructs the scoped value object.
Definition ScopedValue.h:142
void setDelayed(const TValue &value)
Changes the value that will be assigned at the end of the surrounding scope.
Definition ScopedValue.h:148
TValue delayed_
Value that will be assigned at the end of the surrounding scope.
Definition ScopedValue.h:106
void setImmediately(const TValue &value)
Immediately changes the target value, the modification is not applied at the end of the surrounding s...
Definition ScopedValue.h:160
T & target_
Target value that will be changed at the end of the surrounding scope.
Definition ScopedValue.h:103
The namespace covering the entire Ocean framework.
Definition Accessor.h:15