stellarlib 0.1.0
Loading...
Searching...
No Matches
command_queue.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_ECS_COMMAND_QUEUE_HPP
25#define STELLARLIB_ECS_COMMAND_QUEUE_HPP
26
27#include <stellarlib/ecs/stack_vector.hpp>
28#include <stellarlib/ext/memory.hpp>
29
30#include <memory>
31#include <utility>
32
33namespace stellarlib::ecs::internal
34{
35class command_queue final : ext::arena_allocator
36{
37public:
38 [[nodiscard]]
39 command_queue() noexcept;
40
41 [[nodiscard]]
42 constexpr command_queue(const command_queue &) noexcept = delete;
43
44 [[nodiscard]]
45 command_queue(command_queue &&) noexcept;
46
47 constexpr auto operator=(const command_queue &) noexcept
48 -> command_queue & = delete;
49
50 auto operator=(command_queue &&) noexcept
51 -> command_queue &;
52
53 ~command_queue() noexcept;
54
55 template <typename Callback>
56 constexpr void enqueue(Callback &&callback) noexcept
57 {
58 _commands.push(
59 [] (auto callback) noexcept -> void {
60 (*static_cast<Callback *>(callback))();
61 std::destroy_at(static_cast<const Callback *>(callback));
62 },
63 std::construct_at(allocate<Callback>(), std::forward<Callback>(callback))
64 );
65 }
66
67 void execute() noexcept;
68
69private:
70 struct command final
71 {
72 void (*execute)(void *) noexcept;
73 void *callback;
74 };
75
76 stack_vector<command> _commands;
77};
78}
79
80#endif
constexpr auto allocate() noexcept
Returns uninitialized memory for type T.
Definition memory.hpp:363