conduit 0.6.0
Modern C++23 header-only event-dispatching / event-transport library
Loading...
Searching...
No Matches
builder.hpp
Go to the documentation of this file.
1#pragma once
2
6
8#include <conduit/event.hpp>
9#include <conduit/flags.hpp>
10#include <conduit/metadata.hpp>
11
12#include <ulid/ulid.h>
13
14#include <chrono>
15#include <concepts>
16#include <memory>
17#include <string>
18#include <utility>
19
20#include <parcel/parcel.h>
21
22namespace conduit {
23
24template <typename T>
26 static_assert(std::derived_from<T, parcel::ICell>,
27 "EventBuilder<T>: T must derive from conduit::Event<T, Name>");
28
29public:
30 explicit EventBuilder(T payload) : payload_(std::move(payload)) {
32 }
33
34 [[nodiscard]] EventBuilder& id(const ulid::Ulid value) {
35 core_.id = value;
36 id_set_ = true;
37 return *this;
38 }
39
40 [[nodiscard]] EventBuilder& correlation_id(ulid::Ulid value) {
41 core_.correlation_id = value;
42 return *this;
43 }
44 [[nodiscard]] EventBuilder& causation_id(ulid::Ulid value) {
45 core_.causation_id = value;
46 return *this;
47 }
48
49 [[nodiscard]] EventBuilder& metadata(std::string key, md::Value value) {
50 core_.metadata.insert_or_assign(std::move(key), std::move(value));
51 return *this;
52 }
53 [[nodiscard]] EventBuilder& metadata(Metadata md) {
54 core_.metadata = std::move(md);
55 return *this;
56 }
57
58 [[nodiscard]] EventBuilder& created_at(const std::chrono::system_clock::time_point tp) {
59 core_.timestamps.created_at = tp;
60 created_at_set_ = true;
61 return *this;
62 }
63
64 template <typename F>
65 [[nodiscard]] EventBuilder& flag() {
66 core_.flags.template insert<F>();
67 return *this;
68 }
69
70 template <typename... Fs>
71 [[nodiscard]] EventBuilder& flags() {
72 (core_.flags.template insert<Fs>(), ...);
73 return *this;
74 }
75
76 [[nodiscard]] EventEnvelope build() {
77 if (!id_set_) {
78 core_.id = ulid::generate();
79 }
80 if (!created_at_set_) {
81 core_.timestamps.created_at = std::chrono::system_clock::now();
82 }
83 auto payload_cell = std::make_shared<T>(std::move(payload_));
84 return EventEnvelope(std::move(core_), parcel::cell_t{std::move(payload_cell)});
85 }
86
87 operator EventEnvelope() {
88 return build();
89 } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
90
91private:
93 T payload_;
94 bool id_set_ = false;
95 bool created_at_set_ = false;
96};
97
98template <typename T>
99[[nodiscard]] inline EventBuilder<T> event(T payload) {
100 return EventBuilder<T>(std::move(payload));
101}
102
103template <typename T, typename... Args>
104[[nodiscard]] inline EventBuilder<T> make_event(Args&&... args) {
105 return EventBuilder<T>(T(std::forward<Args>(args)...));
106}
107
108} // namespace conduit
Definition builder.hpp:25
EventBuilder & flag()
Definition builder.hpp:65
EventBuilder(T payload)
Definition builder.hpp:30
EventBuilder & flags()
Definition builder.hpp:71
EventBuilder & id(const ulid::Ulid value)
Definition builder.hpp:34
EventBuilder & metadata(std::string key, md::Value value)
Definition builder.hpp:49
EventBuilder & causation_id(ulid::Ulid value)
Definition builder.hpp:44
EventEnvelope build()
Definition builder.hpp:76
EventBuilder & created_at(const std::chrono::system_clock::time_point tp)
Definition builder.hpp:58
EventBuilder & correlation_id(ulid::Ulid value)
Definition builder.hpp:40
EventBuilder & metadata(Metadata md)
Definition builder.hpp:53
Polymorphic envelope cell.
Definition envelope.hpp:62
EventEnvelope — a parcel cell carrying conduit's envelope metadata plus a polymorphic payload cell.
Event<Self, Name> library base built on parcel::SelfStructCell.
Conduit flag tags, built atop comms::Flag / comms::FlagSet.
Envelope metadata container and lifecycle timestamps.
flags::FlagSet collect_default_flags()
Definition event.hpp:86
Definition builder.hpp:22
EventBuilder< T > make_event(Args &&... args)
Definition builder.hpp:104
md::Metadata Metadata
Envelope metadata: a typed JSON-shaped key/value tree.
Definition metadata.hpp:20
EventBuilder< T > event(T payload)
Definition builder.hpp:99
std::chrono::system_clock::time_point created_at
Definition metadata.hpp:24
Internal core shared between envelope copies — accessors return references into this struct so transp...
Definition envelope.hpp:32
std::optional< ulid::Ulid > correlation_id
Definition envelope.hpp:37
std::optional< ulid::Ulid > causation_id
Definition envelope.hpp:38
ulid::Ulid id
Definition envelope.hpp:33
flags::FlagSet flags
Definition envelope.hpp:34
Metadata metadata
Definition envelope.hpp:35
Timestamps timestamps
Definition envelope.hpp:36