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> && std::is_trivial_v<MsgEvent>);
31static_assert(sizeof(MsgEvent) + sizeof(std::int64_t) == 1536);
32
33template <typename DataT>
34void emplace_event(MsgEvent& ev, int type) noexcept
35{
36 static_assert(alignof(DataT) <= 8);
37 static_assert(std::is_nothrow_default_constructible_v<DataT>);
38 static_assert(std::is_trivially_copyable_v<DataT>);
39 ev.type = type;
40 ::new (ev.data) DataT{};
41}
42
43template <typename DataT, typename... ArgsT>
44void emplace_event(MsgEvent& ev, int type, ArgsT&&... args) noexcept
45{
46 static_assert(alignof(DataT) <= 8);
47 static_assert(std::is_nothrow_constructible_v<DataT, ArgsT...>);
48 static_assert(std::is_trivially_copyable_v<DataT>);
49 ev.type = type;
50 ::new (ev.data) DataT{std::forward<ArgsT>(args)...};
51}
52
53template <typename DataT>
54const DataT& data(const MsgEvent& ev) noexcept
55{
56 return *reinterpret_cast<const DataT*>(ev.data);
57}
58
59template <typename DataT>
60DataT& data(MsgEvent& ev) noexcept
61{
62 return *reinterpret_cast<DataT*>(ev.data);
63}
64
65} // namespace io
66} // namespace toolbox
67
68#endif // TOOLBOX_IO_EVENT_HPP
const DataT & data(const MsgEvent &ev) noexcept
Definition Event.hpp:54
void emplace_event(MsgEvent &ev, int type) noexcept
Definition Event.hpp:34
constexpr auto bind() noexcept
Definition Slot.hpp:92
char data[1524]
Definition Event.hpp:28