DF-0
DF-0: Data processing Framework with Zero-overhead
Loading...
Searching...
No Matches
message.h
1/**************************************************************************/
2/* */
3/* DF-0: Data processing Framework with Zero-overhead */
4/* */
5/* Copyright (c) DF-0 Team https://github.com/df0-io/df0 */
6/* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DF0-Commercial */
7/* */
8/**************************************************************************/
9
10#ifndef DF0_MESSAGE_H
11#define DF0_MESSAGE_H
12
13#include <concepts>
14#include <functional>
15#include <memory>
16#include <type_traits>
17#include <typeinfo>
18#include "type_id.h"
19
20namespace df0
21{
22
23enum class continuation { proceed, skip, stop };
25using message_sequence_streamer = std::function<continuation(const message_callbacks&)>;
26
27template <typename T>
28struct message;
29
31{
32 virtual ~message_base() = default;
33 virtual type_id object_type_id() const noexcept = 0;
34 virtual const std::type_info& object_type_info() const noexcept = 0;
35
36 template <typename T>
37 bool is() const noexcept
38 {
39 return object_type_id() == type_id_of<T>();
40 }
41 template <typename T>
42 const T& get() const
43 {
44 return static_cast<const message<T>&>(*this).object;
45 }
46
47 template <typename T>
48 T& get()
49 {
50 return static_cast<message<T>&>(*this).object;
51 }
52};
53
54template <typename T>
55struct message : message_base
56{
57 T object;
58 message(T&& object) : object(std::move(object)) {}
59 const std::type_info& object_type_info() const noexcept override { return typeid(T); }
60 type_id object_type_id() const noexcept override
61 {
62 return type_id_of<T>();
63 }
64};
65
66using message_ptr = std::shared_ptr<message_base>;
67
90{
91 std::function<continuation(message_ptr)> m_further;
92 std::function<continuation(message_ptr)> m_back;
93
94 continuation further(message_ptr msg) const { return m_further(std::move(msg)); }
95
96 template <typename T>
97 requires (!std::is_convertible_v<std::remove_cvref_t<T>, message_ptr>
98 && !std::derived_from<std::remove_cvref_t<T>, message_base>)
99 continuation further(T&& object) const { return m_further(std::make_shared<message<T>>(std::forward<T>(object))); }
100
101 continuation back(message_ptr msg) const { return m_back(std::move(msg)); }
102
103 template <typename T>
104 requires (!std::is_convertible_v<std::remove_cvref_t<T>, message_ptr>
105 && !std::derived_from<std::remove_cvref_t<T>, message_base>)
106 continuation back(T&& object) const { return m_back(std::make_shared<message<T>>(std::forward<T>(object))); }
107
108 continuation operator()(message_ptr msg) const { return further(std::move(msg)); }
109
110 template <typename T>
111 requires (!std::is_convertible_v<std::remove_cvref_t<T>, message_ptr>
112 && !std::derived_from<std::remove_cvref_t<T>, message_base>)
113 continuation operator()(T&& object) const { return further(std::forward<T>(object)); }
114};
115
116} // namespace df0
117
118#endif //DF0_MESSAGE_H
Strong type representing a compile-time type identity.
Definition type_id.h:27
Callback bundle used by pipeline elements to emit messages.
Definition message.h:90