libluna+libos+libui: Move Action to libluna and make it usable in the kernel
Some checks failed
Build and test / build (push) Failing after 1m21s

This commit adds an error-propagating constructor for Action and Function, which makes them usable in the kernel.
This commit is contained in:
apio 2024-10-19 21:25:17 +02:00
parent c21fc2a297
commit 53f8a583dc
Signed by: apio
GPG Key ID: B8A7D06E42258954
14 changed files with 321 additions and 282 deletions

View File

@ -8,7 +8,7 @@
*/
#pragma once
#include <os/Action.h>
#include <luna/Action.h>
#include <ui/Widget.h>
namespace ui
@ -19,7 +19,7 @@ namespace ui
Button(Rect rect);
void set_widget(Widget& widget);
void set_action(os::Action&& action);
void set_action(Action&& action);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -32,6 +32,6 @@ namespace ui
bool m_hovered { false };
bool m_clicked { false };
Widget* m_child;
os::Action m_action;
Action m_action;
};
}

View File

@ -8,7 +8,7 @@
*/
#pragma once
#include <os/Action.h>
#include <luna/Action.h>
#include <ui/Window.h>
namespace ui
@ -17,6 +17,6 @@ namespace ui
{
Result<void> show_message(StringView title, StringView message);
Result<void> show_input_dialog(StringView title, StringView message, os::Function<StringView> callback);
Result<void> show_input_dialog(StringView title, StringView message, Function<StringView> callback);
}
}

View File

@ -8,7 +8,7 @@
*/
#pragma once
#include <os/Action.h>
#include <luna/Action.h>
#include <ui/Font.h>
#include <ui/TextInput.h>
@ -27,7 +27,7 @@ namespace ui
StringView data();
void on_submit(os::Function<StringView>&& action)
void on_submit(Function<StringView>&& action)
{
m_on_submit_action = move(action);
m_has_on_submit_action = true;
@ -36,7 +36,7 @@ namespace ui
private:
SharedPtr<ui::Font> m_font;
os::Function<StringView> m_on_submit_action;
Function<StringView> m_on_submit_action;
bool m_has_on_submit_action { false };
};
}

View File

@ -73,14 +73,14 @@ namespace ui
Result<ui::EventResult> handle_mouse_buttons(ui::Point position, int buttons);
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request);
Result<void> add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, os::Function<ui::Shortcut>&& action);
Result<void> add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, Function<ui::Shortcut>&& action);
int id() const
{
return m_id;
}
void on_close(os::Action&& action)
void on_close(Action&& action)
{
m_on_close_action = move(action);
m_has_on_close_action = true;
@ -99,13 +99,13 @@ namespace ui
Option<int> m_old_mouse_buttons;
bool m_decorated { false };
os::Action m_on_close_action;
Action m_on_close_action;
bool m_has_on_close_action { false };
struct ShortcutAction
{
bool intercept;
os::Function<Shortcut> action;
Function<Shortcut> action;
};
HashMap<Shortcut, ShortcutAction> m_shortcuts;

View File

@ -24,7 +24,7 @@ namespace ui
widget.set_parent(this);
}
void Button::set_action(os::Action&& action)
void Button::set_action(Action&& action)
{
m_action = move(action);
}

View File

@ -41,7 +41,7 @@ namespace ui::Dialog
return {};
}
Result<void> show_input_dialog(StringView title, StringView message, os::Function<StringView> callback)
Result<void> show_input_dialog(StringView title, StringView message, Function<StringView> callback)
{
auto rect = ui::App::the().main_window()->canvas().rect();
int text_length = (int)message.length() * ui::Font::default_font()->width();

View File

@ -224,8 +224,7 @@ namespace ui
return m_main_widget->handle_key_event(request);
}
Result<void> Window::add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept,
os::Function<ui::Shortcut>&& action)
Result<void> Window::add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, Function<ui::Shortcut>&& action)
{
TRY(m_shortcuts.try_set(shortcut, { intercept, move(action) }));

View File

@ -40,7 +40,7 @@ set(SOURCES
)
add_library(luna-freestanding ${FREESTANDING_SOURCES})
target_compile_definitions(luna-freestanding PRIVATE USE_FREESTANDING)
target_compile_definitions(luna-freestanding PUBLIC USE_FREESTANDING)
target_compile_options(luna-freestanding PRIVATE ${COMMON_FLAGS})
target_compile_options(luna-freestanding PRIVATE -nostdlib -mcmodel=kernel -ffreestanding)

View File

@ -0,0 +1,300 @@
/**
* @file Action.h
* @author apio (cloudapio.eu)
* @brief Wrapper for callable objects, like function pointers or lambdas.
*
* @copyright Copyright (c) 2024, the Luna authors.
*
*/
#pragma once
#include <luna/Heap.h>
#include <luna/SharedPtr.h>
/**
* @brief Wrapper for callable objects with no arguments.
*
* 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.
*/
class Action
{
public:
/**
* @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.
*/
Action()
{
m_action = nullptr;
}
/**
* @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.
*/
Action(Action&& other) : m_action(other.m_action)
{
other.m_action = {};
}
/**
* @brief Construct a new Action object, copying it from another one.
*
* @param other The old Action object to copy from.
*/
Action(Action& other) : m_action(other.m_action)
{
}
#ifndef USE_FREESTANDING
/**
* @brief Construct a new Action object, copying it from another one.
*
* @param other The old Action object to copy from.
*/
Action(const Action& other) : m_action(other.m_action)
{
// FIXME: Doesn't actually copy?
}
/**
* @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.
*/
template <typename T> Action(T&& function)
{
m_action =
adopt_shared_if_nonnull((ActionBase*)new (std::nothrow) ActionImpl<T>(move(function))).release_value();
}
/**
* @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=().
*/
template <typename T> Action& operator=(T&& function)
{
m_action =
adopt_shared_if_nonnull((ActionBase*)new (std::nothrow) ActionImpl<T>(move(function))).release_value();
return *this;
}
#endif
/**
* @brief Construct a new Action object, wrapping a callable object. This version propagates errors, and thus can
* be used in freestanding contexts, unlike the constructors above.
*
* @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.
*/
template <typename T> static Result<Action> wrap(T&& function)
{
auto base = TRY(adopt_shared_if_nonnull((ActionBase*)new (std::nothrow) ActionImpl<T>(move(function))));
return Action(base);
}
/**
* @brief Call the underlying object.
*/
void operator()()
{
expect(m_action, "Action called with no underlying callable object");
return m_action->call();
}
private:
class ActionBase : public Shareable
{
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()
{
m_function();
}
private:
T m_function;
};
Action(SharedPtr<ActionBase> action) : m_action(action)
{
}
SharedPtr<ActionBase> m_action;
};
/**
* @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 Function type acts as a callable wrapper for such functions, allowing them to be passed as
* arguments to any function.
*/
template <class... Args> class Function
{
public:
/**
* @brief Construct a new empty Function 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.
*/
Function()
{
m_action = nullptr;
}
/**
* @brief Construct a new Function object, moving it from another one.
*
* @param other The old Function object to move from. This object will become invalid.
*/
Function(Function<Args...>&& other) : m_action(other.m_action)
{
other.m_action = {};
}
/**
* @brief Construct a new Function object, copying it from another one.
*
* @param other The old Function object to copy from.
*/
Function(Function<Args...>& other) : m_action(other.m_action)
{
}
#ifndef USE_FREESTANDING
/**
* @brief Construct a new Function object, copying it from another one.
*
* @param other The old Function object to copy from.
*/
Function(const Function<Args...>& other) : m_action(other.m_action)
{
// FIXME: Doesn't actually copy?
}
/**
* @brief Construct a new Function 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.
*/
template <typename T> Function(T&& function)
{
m_action =
adopt_shared_if_nonnull((FunctionBase*)new (std::nothrow) FunctionImpl<T>(move(function))).release_value();
}
/**
* @brief Assign a new callable object to this Function.
*
* @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=().
*/
template <typename T> Function& operator=(T&& function)
{
m_action =
adopt_shared_if_nonnull((FunctionBase*)new (std::nothrow) FunctionImpl<T>(move(function))).release_value();
return *this;
}
#endif
/**
* @brief Construct a new Function object, wrapping a callable object. This version propagates errors, and thus can
* be used in freestanding contexts, unlike the constructors above.
*
* @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.
*/
template <typename T> static Result<Function> wrap(T&& function)
{
auto base = TRY(adopt_shared_if_nonnull((FunctionBase*)new (std::nothrow) FunctionImpl<T>(move(function))));
return Function(base);
}
/**
* @brief Call the underlying object.
*/
void operator()(Args... args)
{
expect(m_action, "Function called with no underlying callable object");
return m_action->call(args...);
}
/**
* @brief Call the underlying object.
*/
void operator()(Args... args) const
{
expect(m_action, "Function called with no underlying callable object");
return m_action->call(args...);
}
private:
class FunctionBase : public Shareable
{
public:
virtual void call(Args... args) = 0;
virtual void call(Args... args) const = 0;
virtual ~FunctionBase() = default;
};
template <typename T> class FunctionImpl final : public FunctionBase
{
public:
FunctionImpl(FunctionImpl<T>&& function) : m_function(move(function.m_function))
{
}
FunctionImpl(T&& function) : m_function(move(function))
{
}
void call(Args... args) override
{
m_function(args...);
}
void call(Args... args) const override
{
m_function(args...);
}
private:
T m_function;
};
Function(SharedPtr<FunctionBase> action) : m_action(action)
{
}
SharedPtr<FunctionBase> m_action;
};

View File

@ -1,260 +0,0 @@
/**
* @file Action.h
* @author apio (cloudapio.eu)
* @brief Wrapper for callable objects, like function pointers or lambdas.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
#include <luna/Heap.h>
#include <luna/SharedPtr.h>
namespace os
{
/**
* @brief Wrapper for callable objects with no arguments.
*
* 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.
*/
class Action
{
public:
/**
* @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.
*/
Action()
{
m_action = nullptr;
}
/**
* @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.
*/
Action(Action&& other) : m_action(other.m_action)
{
other.m_action = {};
}
/**
* @brief Construct a new Action object, copying it from another one.
*
* @param other The old Action object to copy from.
*/
Action(Action& other) : m_action(other.m_action)
{
}
/**
* @brief Construct a new Action object, copying it from another one.
*
* @param other The old Action object to copy from.
*/
Action(const Action& other) : m_action(other.m_action)
{
}
/**
* @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.
*/
template <typename T> Action(T&& function)
{
m_action =
adopt_shared_if_nonnull((ActionBase*)new (std::nothrow) ActionImpl<T>(move(function))).release_value();
}
/**
* @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=().
*/
template <typename T> Action& operator=(T&& function)
{
m_action =
adopt_shared_if_nonnull((ActionBase*)new (std::nothrow) ActionImpl<T>(move(function))).release_value();
return *this;
}
/**
* @brief Call the underlying object.
*/
void operator()()
{
expect(m_action, "os::Action called with no underlying callable object");
return m_action->call();
}
private:
class ActionBase : public Shareable
{
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()
{
m_function();
}
private:
T m_function;
};
SharedPtr<ActionBase> m_action;
};
/**
* @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 Function type acts as a callable wrapper for such functions, allowing them to be passed as
* arguments to any function.
*/
template <class... Args> class Function
{
public:
/**
* @brief Construct a new empty Function 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.
*/
Function()
{
m_action = nullptr;
}
/**
* @brief Construct a new Function object, moving it from another one.
*
* @param other The old Function object to move from. This object will become invalid.
*/
Function(Function<Args...>&& other) : m_action(other.m_action)
{
other.m_action = {};
}
/**
* @brief Construct a new Function object, copying it from another one.
*
* @param other The old Function object to copy from.
*/
Function(Function<Args...>& other) : m_action(other.m_action)
{
}
/**
* @brief Construct a new Function object, copying it from another one.
*
* @param other The old Function object to copy from.
*/
Function(const Function<Args...>& other) : m_action(other.m_action)
{
}
/**
* @brief Construct a new Function 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.
*/
template <typename T> Function(T&& function)
{
m_action = adopt_shared_if_nonnull((FunctionBase*)new (std::nothrow) FunctionImpl<T>(move(function)))
.release_value();
}
/**
* @brief Assign a new callable object to this Function.
*
* @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=().
*/
template <typename T> Function& operator=(T&& function)
{
m_action = adopt_shared_if_nonnull((FunctionBase*)new (std::nothrow) FunctionImpl<T>(move(function)))
.release_value();
return *this;
}
/**
* @brief Call the underlying object.
*/
void operator()(Args... args)
{
expect(m_action, "os::Function called with no underlying callable object");
return m_action->call(args...);
}
/**
* @brief Call the underlying object.
*/
void operator()(Args... args) const
{
expect(m_action, "os::Function called with no underlying callable object");
return m_action->call(args...);
}
private:
class FunctionBase : public Shareable
{
public:
virtual void call(Args... args) = 0;
virtual void call(Args... args) const = 0;
virtual ~FunctionBase() = default;
};
template <typename T> class FunctionImpl final : public FunctionBase
{
public:
FunctionImpl(FunctionImpl<T>&& function) : m_function(move(function.m_function))
{
}
FunctionImpl(T&& function) : m_function(move(function))
{
}
void call(Args... args) override
{
m_function(args...);
}
void call(Args... args) const override
{
m_function(args...);
}
private:
T m_function;
};
SharedPtr<FunctionBase> m_action;
};
}

View File

@ -8,9 +8,9 @@
*/
#pragma once
#include <luna/Action.h>
#include <luna/HashMap.h>
#include <luna/Vector.h>
#include <os/Action.h>
#include <os/Timer.h>
#include <sys/poll.h>

View File

@ -8,7 +8,7 @@
*/
#pragma once
#include <os/Action.h>
#include <luna/Action.h>
#include <os/LocalClient.h>
#include <os/LocalServer.h>
@ -382,7 +382,7 @@ namespace os
* @return true The child is ready.
* @return false The method timed out.
*/
static bool run_and_wait(os::Action&& action, int timeout = -1);
static bool run_and_wait(Action&& action, int timeout = -1);
};
/**

View File

@ -8,9 +8,9 @@
*/
#pragma once
#include <luna/Action.h>
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <os/Action.h>
#include <time.h>
namespace os

View File

@ -124,7 +124,7 @@ namespace os::IPC
return result > 0;
}
bool Notifier::run_and_wait(os::Action&& action, int timeout)
bool Notifier::run_and_wait(Action&& action, int timeout)
{
auto notifier = create();
notifier.hook();