Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_EVENT_HPP
18#define TOOLBOX_IO_EVENT_HPP
19
20#include <cstdint>
21#include <utility>
22
23namespace toolbox {
24inline namespace io {
25
26struct MsgEvent {
27 int type;
28 char data[1524];
29};
30static_assert(std::is_standard_layout_v<MsgEvent>
31 && std::is_trivially_default_constructible_v<MsgEvent>
32 && std::is_trivially_copyable_v<MsgEvent>);
33static_assert(sizeof(MsgEvent) + sizeof(std::int64_t) == 1536);
34
35template <typename DataT>
36void emplace_event(MsgEvent& ev, int type) noexcept
37{
38 static_assert(alignof(DataT) <= 8);
39 static_assert(std::is_nothrow_default_constructible_v<DataT>);
40 static_assert(std::is_trivially_copyable_v<DataT>);
41 ev.type = type;
42 ::new (ev.data) DataT{};
43}
44
45template <typename DataT, typename... ArgsT>
46void emplace_event(MsgEvent& ev, int type, ArgsT&&... args) noexcept
47{
48 static_assert(alignof(DataT) <= 8);
49 static_assert(std::is_nothrow_constructible_v<DataT, ArgsT...>);
50 static_assert(std::is_trivially_copyable_v<DataT>);
51 ev.type = type;
52 ::new (ev.data) DataT{std::forward<ArgsT>(args)...};
53}
54
55template <typename DataT>
56const DataT& data(const MsgEvent& ev) noexcept
57{
58 return *reinterpret_cast<const DataT*>(ev.data);
59}
60
61template <typename DataT>
62DataT& data(MsgEvent& ev) noexcept
63{
64 return *reinterpret_cast<DataT*>(ev.data);
65}
66
67} // namespace io
68} // namespace toolbox
69
70#endif // TOOLBOX_IO_EVENT_HPP
const DataT & data(const MsgEvent &ev) noexcept
Definition Event.hpp:56
void emplace_event(MsgEvent &ev, int type) noexcept
Definition Event.hpp:36
constexpr auto bind() noexcept
Definition Slot.hpp:97
char data[1524]
Definition Event.hpp:28