Ocean
Loading...
Searching...
No Matches
ScopedFunction.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_FUNCTION_H
9#define META_OCEAN_BASE_SCOPED_FUNCTION_H
10
11#include "ocean/base/Base.h"
12
13#include <functional>
14
15namespace Ocean
16{
17
18/**
19 * This class holds a function which will be invoked once the object is disposed.
20 * @tparam T The data type of the function
21 * @ingroup base
22 */
23template <typename T>
25{
26 public:
27
28 /**
29 * Creates an object without function.
30 */
31 ScopedFunctionT() = default;
32
33 /**
34 * Creates a new object with a given function.
35 * @param function The function to be invoked once this object is disposed
36 */
37 explicit ScopedFunctionT(T&& function) noexcept;
38
39 /**
40 * Move constructor.
41 * @param scopedFunction Object to be moved
42 */
43 ScopedFunctionT(ScopedFunctionT<T>&& scopedFunction) noexcept;
44
45 /**
46 * Destructs this object and invoked the function if this object holds function.
47 */
49
50 /**
51 * Explicitly released this object.
52 * In case this object holds a valid function, the function is invoked and then invalidated so that the function will not be invoked again when this object is disposed.
53 */
54 void release();
55
56 /**
57 * Revokes the function call.
58 * In case this object holds a valid function, the function is invalidated without calling the function.
59 */
60 void revoke();
61
62 /**
63 * Move operator.
64 * @param scopedFunction The object to be moved
65 * @return Reference to this object
66 */
67 ScopedFunctionT& operator=(ScopedFunctionT<T>&& scopedFunction) noexcept;
68
69 protected:
70
71 /**
72 * Disabled copy constructor.
73 */
75
76 /**
77 * Disabled assign operator.
78 * @return Reference to this object
79 */
81
82 protected:
83
84 /// The function to be invoked.
85 T function_ = T();
86};
87
88/**
89 * Definition of a scoped function with void return parameter.
90 * @ingroup base
91 */
92typedef ScopedFunctionT<std::function<void()>> ScopedFunctionVoid;
93
94template <typename T>
96 function_(std::move(function))
97{
98 // nothing to do here
99}
100
101template <typename T>
103{
104 *this = std::move(scopedFunction);
105}
106
107template <typename T>
109{
110 release();
111}
112
113template <typename T>
115{
116 if (function_)
117 {
118 function_();
119
120 function_ = T();
121 }
122}
123
124template <typename T>
126{
127 function_ = T();
128}
129
130template <typename T>
132{
133 if (this != &scopedFunction)
134 {
135 release();
136
137 function_ = std::move(scopedFunction.function_);
138 scopedFunction.function_ = T();
139 }
140
141 return *this;
142}
143
144}
145
146#endif // META_OCEAN_BASE_SCOPED_OBJECT_H
This class holds a function which will be invoked once the object is disposed.
Definition ScopedFunction.h:25
ScopedFunctionT< T > & operator=(const ScopedFunctionT &)=delete
Disabled assign operator.
void release()
Explicitly released this object.
Definition ScopedFunction.h:114
T function_
The function to be invoked.
Definition ScopedFunction.h:85
ScopedFunctionT & operator=(ScopedFunctionT< T > &&scopedFunction) noexcept
Move operator.
Definition ScopedFunction.h:131
ScopedFunctionT(const ScopedFunctionT &)=delete
Disabled copy constructor.
~ScopedFunctionT()
Destructs this object and invoked the function if this object holds function.
Definition ScopedFunction.h:108
void revoke()
Revokes the function call.
Definition ScopedFunction.h:125
ScopedFunctionT()=default
Creates an object without function.
ScopedFunctionT< std::function< void()> > ScopedFunctionVoid
Definition of a scoped function with void return parameter.
Definition ScopedFunction.h:92
The namespace covering the entire Ocean framework.
Definition Accessor.h:15