CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ScopeExit.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include <type_traits>
11#include <utility>
12
13namespace CeresEngine {
14
15 template<typename Func> struct ScopeExit {
17
18 ScopeExit(Func&& func) noexcept(std::is_nothrow_move_constructible_v<Func>) : func(std::move(func)) {}
19
20 ScopeExit(const ScopeExit&) = delete;
21 ScopeExit& operator=(const ScopeExit&) = delete;
22
23 ScopeExit(ScopeExit&& rhs) noexcept(std::is_nothrow_move_constructible_v<Func>) : func(std::move(rhs.func)) {}
25
27 // On scope exit we call the destructor.
28 func();
29 }
30 };
31
32 template<typename Func> auto ce_scope_exit(Func&& func) noexcept { // NOLINT
33 return ScopeExit(std::forward<Func>(func));
34 }
35
36 template<typename T, typename Func> auto ce_scope_exit(T* ptr, Func func) noexcept { // NOLINT
37 return ce_scope_exit([ptr, func]() { return (ptr->*func)(); });
38 }
39
40#define CE_SCOPE_EXIT const ::CeresEngine::ScopeExit CE_TOKENPASTE2(_scopeExit, __COUNTER__) = [&]() noexcept
41
42} // namespace CeresEngine
Definition Application.hpp:19
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
auto ce_scope_exit(Func &&func) noexcept
Definition ScopeExit.hpp:32
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Definition ScopeExit.hpp:15
ScopeExit(const ScopeExit &)=delete
ScopeExit(Func &&func) noexcept(std::is_nothrow_move_constructible_v< Func >)
Definition ScopeExit.hpp:18
ScopeExit(ScopeExit &&rhs) noexcept(std::is_nothrow_move_constructible_v< Func >)
Definition ScopeExit.hpp:23
ScopeExit & operator=(const ScopeExit &)=delete
ScopeExit & operator=(ScopeExit &&)=delete
~ScopeExit() noexcept
Definition ScopeExit.hpp:26
Func func
Definition ScopeExit.hpp:16