fennec
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31
32#ifndef FENNEC_LANG_FUNCTION_H
33#define FENNEC_LANG_FUNCTION_H
34
35#include <fennec/lang/types.h>
36#include <fennec/lang/assert.h>
37#include <fennec/lang/utility.h>
38
39#include <fennec/lang/detail/_function.h>
40
41namespace fennec
42{
43
44template<typename> class function;
45
46template<typename ReturnT, typename...ArgsT>
47class function<ReturnT(ArgsT...)> {
48public:
49 constexpr function() noexcept = default;
50 constexpr ~function() = default;
51
52 constexpr function(const function&) noexcept = default;
53 constexpr function( function&&) noexcept = default;
54
55 constexpr function(ReturnT (*func)(ArgsT...))
56 : call(func) {
57 }
58
59 constexpr function(nullptr_t) noexcept : function() {}
60
61 constexpr function& operator=(const function&) = default;
62 constexpr function& operator=(function&&) = default;
63
64 constexpr function& operator=(nullptr_t) {
65 call = nullptr;
66 return *this;
67 }
68
69 constexpr function& operator=(ReturnT (*func)(ArgsT...)) {
70 call = func;
71 return *this;
72 }
73
74 constexpr operator bool() const noexcept { return call != nullptr; }
75
76 ReturnT operator()(ArgsT...args) const noexcept {
77 assertf(call != nullptr, "Attempted to call a null function object!");
78 return call(fennec::forward<ArgsT>(args)...);
79 }
80
81private:
82 ReturnT (*call)(ArgsT&&...) { nullptr };
83};
84
85}
86
87#endif // FENNEC_LANG_FUNCTION_H
Assertions
Types
Utility