2023-10-07 13:31:50 +00:00
|
|
|
/**
|
|
|
|
* @file Action.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief Wrapper for callable objects, like function pointers or lambdas.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-09-27 16:51:38 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/Heap.h>
|
|
|
|
|
2023-10-07 13:31:50 +00:00
|
|
|
namespace os
|
2023-09-27 16:51:38 +00:00
|
|
|
{
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Wrapper for callable objects.
|
|
|
|
*
|
|
|
|
* Certain callable types such as capture lambdas don't have a named type, thus they can't be passed to functions as
|
|
|
|
* arguments. The Action type acts as a callable wrapper for such functions, allowing them to be passed as arguments
|
|
|
|
* to any function.
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
class Action
|
|
|
|
{
|
|
|
|
public:
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Construct a new empty Action object.
|
|
|
|
*
|
|
|
|
* Since it is not wrapping any callable object, this object is invalid and attempting to invoke its
|
|
|
|
* (nonexistent) callable object will result in a crash at runtime.
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
Action()
|
|
|
|
{
|
|
|
|
m_action = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Action object, moving it from another one.
|
|
|
|
*
|
|
|
|
* @param other The old Action object to move from. This object will become invalid.
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
Action(Action&& other) : m_action(other.m_action)
|
|
|
|
{
|
|
|
|
other.m_action = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Construct a new Action object from a callable object.
|
|
|
|
*
|
|
|
|
* @tparam T The type of the callable object. Since this can be guessed by template deduction, the object
|
|
|
|
* doesn't actually need a type name.
|
|
|
|
* @param function The callable object to wrap.
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
template <typename T> Action(T&& function)
|
|
|
|
{
|
|
|
|
m_action = new (std::nothrow) ActionImpl<T>(move(function));
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Assign a new callable object to this Action.
|
|
|
|
*
|
|
|
|
* @tparam T The type of the callable object. Since this can be guessed by template deduction, the object
|
|
|
|
* doesn't actually need a type name.
|
|
|
|
* @param function The callable object to wrap.
|
|
|
|
* @return Action& A reference to this object, as required by operator=().
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
template <typename T> Action& operator=(T&& function)
|
|
|
|
{
|
|
|
|
if (m_action) delete m_action;
|
|
|
|
m_action = new (std::nothrow) ActionImpl<T>(move(function));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:11:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Call the underlying object.
|
|
|
|
*/
|
2023-09-27 16:51:38 +00:00
|
|
|
void operator()()
|
|
|
|
{
|
2023-10-10 20:11:12 +00:00
|
|
|
expect(m_action, "os::Action called with no underlying callable object");
|
2023-09-27 16:51:38 +00:00
|
|
|
return m_action->call();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Action()
|
|
|
|
{
|
|
|
|
if (m_action) delete m_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class ActionBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void call() = 0;
|
|
|
|
virtual ~ActionBase() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> class ActionImpl final : public ActionBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ActionImpl(T&& function) : m_function(move(function))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void call()
|
|
|
|
{
|
|
|
|
return m_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_function;
|
|
|
|
};
|
|
|
|
|
|
|
|
ActionBase* m_action;
|
|
|
|
};
|
|
|
|
}
|