General Utility Library for C++17 26.5.0
finalizer.h
Go to the documentation of this file.
1
26#ifndef GUL17_FINALIZER_H_
27#define GUL17_FINALIZER_H_
28
29#include <type_traits>
30#include <utility>
31
32#include "gul17/internal.h"
33
34namespace gul17 {
35
107template <typename F>
109{
110 F action_;
111 bool invoke_ {true};
112
113public:
127 : action_(std::move(f))
128 {}
131 : action_{ std::move(other.action_) }
132 , invoke_{ other.invoke_ }
133 {
134 // don't call callback on moved from FinalAction
135 other.invoke_ = false;
136 }
139 if (this != &other) {
140 this->action_ = std::move(other.action_);
141 this->invoke_ = std::move(other.invoke_);
142 // don't call callback on moved from FinalAction
143 other.invoke_ = false;
144 }
145 return *this;
146 }
147
148 FinalAction() = delete;
149 FinalAction(const FinalAction&) = delete;
151
155 if (invoke_)
156 action_();
157 }
158};
159
171template <typename F>
173 return FinalAction<typename std::decay_t<F>>(std::forward<typename std::decay_t<F>>(f));
174}
175
177
178} // namespace gul17
179
180#endif
181
182// vi:et:sts=4:sw=4:ts=4
FinalAction allows us to execute something if the FinalAction object leaves the scope.
Definition finalizer.h:109
FinalAction(FinalAction &&other) noexcept
Move constructor.
Definition finalizer.h:130
FinalAction(F f) noexcept
Creates a new FinalAction object.
Definition finalizer.h:126
FinalAction()=delete
FinalAction is not is_default_constructible.
FinalAction(const FinalAction &)=delete
FinalAction is not copyable.
FinalAction & operator=(const FinalAction &)=delete
FinalAction is not copyable.
FinalAction & operator=(FinalAction &&other) noexcept
Move assignment operator.
Definition finalizer.h:138
~FinalAction() noexcept
Destructor Calls action except when in move contexts.
Definition finalizer.h:154
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29