136 lines
3.7 KiB
C++
136 lines
3.7 KiB
C++
// =====================================================================================================================
|
|
// fennec, a free and open source game engine
|
|
// Copyright © 2025 Medusa Slockbower
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
// =====================================================================================================================
|
|
|
|
///
|
|
/// \file function.h
|
|
/// \brief
|
|
///
|
|
///
|
|
/// \details
|
|
/// \author Medusa Slockbower
|
|
///
|
|
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
|
|
///
|
|
///
|
|
|
|
|
|
#ifndef FENNEC_LANG_FUNCTION_H
|
|
#define FENNEC_LANG_FUNCTION_H
|
|
|
|
#include <fennec/lang/types.h>
|
|
#include <fennec/lang/assert.h>
|
|
#include <fennec/lang/utility.h>
|
|
|
|
#include <fennec/lang/detail/_function.h>
|
|
|
|
namespace fennec
|
|
{
|
|
///
|
|
/// \brief a class capable of holding a function or non-capturing lambda
|
|
template<typename> class function;
|
|
|
|
///
|
|
/// \brief a class capable of holding a function or non-capturing lambda
|
|
/// \tparam ReturnT The return type of the function
|
|
/// \tparam ArgsT The argument types of the function
|
|
template<typename ReturnT, typename...ArgsT>
|
|
class function<ReturnT(ArgsT...)> {
|
|
public:
|
|
///
|
|
/// \brief default constructor
|
|
constexpr function() noexcept
|
|
: call(nullptr) {
|
|
}
|
|
|
|
///
|
|
/// \brief destructor
|
|
constexpr ~function() = default;
|
|
|
|
///
|
|
/// \brief copy constructor
|
|
/// \param func the function to copy
|
|
constexpr function(const function& func) noexcept = default;
|
|
|
|
///
|
|
/// \brief move constructor
|
|
/// \param func the function to take ownership of
|
|
constexpr function(function&& func) noexcept = default;
|
|
|
|
///
|
|
/// \brief function constructor
|
|
/// \param func the function to capture
|
|
constexpr function(ReturnT (*func)(ArgsT...))
|
|
: call(func) {
|
|
}
|
|
|
|
///
|
|
/// \brief null constructor
|
|
constexpr function(nullptr_t) noexcept : function() {}
|
|
|
|
///
|
|
/// \brief copy assignment
|
|
/// \param func the function to copy
|
|
/// \returns a reference to self
|
|
constexpr function& operator=(const function& func) = default;
|
|
|
|
///
|
|
/// \brief move assignment
|
|
/// \param func the function to capture
|
|
/// \returns a reference to self
|
|
constexpr function& operator=(function&& func) = default;
|
|
|
|
///
|
|
/// \brief null assignment
|
|
/// \returns a reference to self
|
|
constexpr function& operator=(nullptr_t) {
|
|
call = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
///
|
|
/// \brief function assignment
|
|
/// \param func the function to capture
|
|
/// \returns a reference to self
|
|
constexpr function& operator=(ReturnT (*func)(ArgsT...)) {
|
|
call = func;
|
|
return *this;
|
|
}
|
|
|
|
///
|
|
/// \brief implicit bool check
|
|
/// \returns \f$true\f$ if a function is captured, \f$false\f$ otherwise
|
|
constexpr operator bool() const noexcept {
|
|
return call != nullptr;
|
|
}
|
|
|
|
///
|
|
/// \brief function call operator
|
|
/// \param args the arguments to call the function with
|
|
/// \returns the result of the function call
|
|
ReturnT operator()(ArgsT...args) const noexcept {
|
|
assertf(call != nullptr, "Attempted to call a null function object!");
|
|
return call(fennec::forward<ArgsT>(args)...);
|
|
}
|
|
|
|
private:
|
|
ReturnT (*call)(ArgsT&&...) { nullptr };
|
|
};
|
|
|
|
}
|
|
|
|
#endif // FENNEC_LANG_FUNCTION_H
|