MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Deferred.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <utility>
7
8namespace mxl::lib
9{
14 template<typename F>
16 {
17 public:
18 Deferred(Deferred<F> const&) = delete;
19 Deferred(Deferred<F>&&) = delete;
20 Deferred& operator=(Deferred<F> const&) = delete;
22
25 constexpr ~Deferred() noexcept(std::is_nothrow_invocable_v<F>)
26 {
27 _f();
28 }
29
30 private:
33 constexpr Deferred(F&& f)
34 : _f(std::forward<F>(f))
35 {}
36
39 template<typename N>
40 friend constexpr Deferred<N> defer(N&& f) noexcept;
41
42 private:
43 F _f;
44 };
45
46 template<typename F>
47 [[nodiscard("The value returned from defer() must not be discarded. Discarding it calls the function passed to defer() right away.")]]
48 constexpr Deferred<F> defer(F&& f) noexcept
49 {
50 return Deferred{std::forward<F>(f)};
51 }
52}
Helper class that is created by a call to.
Definition Deferred.hpp:16
constexpr ~Deferred() noexcept(std::is_nothrow_invocable_v< F >)
Calls the deferred function.
Definition Deferred.hpp:25
constexpr Deferred(F &&f)
The construtor is private, an can only be called from defer().
Definition Deferred.hpp:33
Deferred(Deferred< F > &&)=delete
Deferred & operator=(Deferred< F > const &)=delete
friend constexpr Deferred< N > defer(N &&f) noexcept
Allow Deferred to be constructed from defer().
Deferred & operator=(Deferred< F > &&)=delete
Deferred(Deferred< F > const &)=delete
constexpr Deferred< F > defer(F &&f) noexcept
Definition Deferred.hpp:48