conduit 0.6.0
Modern C++23 header-only event-dispatching / event-transport library
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1#pragma once
2
6
8#include <conduit/event.hpp>
10
11#include <cstdint>
12#include <memory>
13#include <mutex>
14#include <shared_mutex>
15#include <span>
16#include <vector>
17
18#include <parcel/parcel.h>
19
20namespace conduit {
21
33public:
35 auto reg = std::make_shared<parcel::ParcelRegistry>();
36 reg->register_cells<EventEnvelope>();
37 registry_ = std::move(reg);
38 }
39
41 const auto snap = other.snapshot();
42 registry_ = std::make_shared<parcel::ParcelRegistry>(*snap);
43 }
44
45 EventRegistry(EventRegistry&&) noexcept = delete;
46 EventRegistry& operator=(const EventRegistry&) = delete;
47 EventRegistry& operator=(EventRegistry&&) noexcept = delete;
48 ~EventRegistry() = default;
49
52 template <typename T>
54 add_descriptor(T::descriptor());
55 return *this;
56 }
57
61 EventRegistry& add_descriptor(parcel::cell_type_descriptor_t desc) {
62 const auto current = snapshot();
63 auto next = std::make_shared<parcel::ParcelRegistry>(*current);
64 next->register_kind(std::move(desc));
65 std::unique_lock lock(mu_);
66 registry_ = std::move(next);
67 return *this;
68 }
69
70 [[nodiscard]] EventEnvelope decode_json(parcel::json_t const& j) const {
71 const auto reg = snapshot();
72 try {
73 const auto cell = EventEnvelope::from_json(j, *reg);
74 const auto env = parcel::cell_cast<EventEnvelope>(cell);
75 return *env;
76 } catch (const parcel::ParcelException& e) {
77 throw SerializationError{e.what()};
78 }
79 }
80
81 [[nodiscard]] EventEnvelope decode_cbor(std::span<const std::uint8_t> bytes) const {
82 try {
83 const auto j = parcel::json_t::from_cbor(bytes);
84 return decode_json(j);
85 } catch (const parcel::ParcelException& e) {
86 throw SerializationError{e.what()};
87 } catch (const SerializationError&) {
88 throw;
89 } catch (const std::exception& e) {
90 throw SerializationError{e.what()};
91 }
92 }
93
94 [[nodiscard]] EventEnvelope decode_cbor(std::span<const char> bytes) const {
95 try {
96 const auto j = parcel::json_t::from_cbor(bytes);
97 return decode_json(j);
98 } catch (const parcel::ParcelException& e) {
99 throw SerializationError{e.what()};
100 } catch (const SerializationError&) {
101 throw;
102 } catch (const std::exception& e) {
103 throw SerializationError{e.what()};
104 }
105 }
106
110 [[nodiscard]] std::shared_ptr<const parcel::ParcelRegistry>
111 parcel_registry_snapshot() const noexcept {
112 return snapshot();
113 }
114
115private:
116 [[nodiscard]] std::shared_ptr<parcel::ParcelRegistry> snapshot() const {
117 std::shared_lock lock(mu_);
118 return registry_;
119 }
120
121 mutable std::shared_mutex mu_;
122 std::shared_ptr<parcel::ParcelRegistry> registry_;
123};
124
125[[nodiscard]] inline parcel::json_t encode_json(const EventEnvelope& env) {
126 return env.to_json();
127}
128
129[[nodiscard]] inline std::vector<char> encode_cbor(const EventEnvelope& env) {
130 std::vector<char> out;
131 parcel::json_t::to_cbor(env.to_json(), out);
132 return out;
133}
134
135namespace serialization {
136
140
141using ::conduit::encode_cbor;
142using ::conduit::encode_json;
143
144} // namespace serialization
145
146} // namespace conduit
Polymorphic envelope cell.
Definition envelope.hpp:62
parcel::json_t to_json() const override
Definition envelope.hpp:194
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &reg)
Definition envelope.hpp:246
Registers event cells for wire decoding.
Definition serialization.hpp:32
EventEnvelope decode_cbor(std::span< const std::uint8_t > bytes) const
Definition serialization.hpp:81
EventRegistry()
Definition serialization.hpp:34
EventRegistry(EventRegistry &&) noexcept=delete
EventRegistry(const EventRegistry &other)
Definition serialization.hpp:40
EventRegistry & add()
Register the descriptor for an event type T (i.e.
Definition serialization.hpp:53
EventRegistry & add_descriptor(parcel::cell_type_descriptor_t desc)
Register a previously-resolved cell descriptor.
Definition serialization.hpp:61
EventEnvelope decode_cbor(std::span< const char > bytes) const
Definition serialization.hpp:94
EventEnvelope decode_json(parcel::json_t const &j) const
Definition serialization.hpp:70
std::shared_ptr< const parcel::ParcelRegistry > parcel_registry_snapshot() const noexcept
Snapshot accessor for the underlying parcel registry.
Definition serialization.hpp:111
Raised by envelope/cell deserialization when the wire data is malformed.
Definition exception.hpp:56
EventEnvelope — a parcel cell carrying conduit's envelope metadata plus a polymorphic payload cell.
Event<Self, Name> library base built on parcel::SelfStructCell.
Root exception hierarchy for the conduit library.
Definition builder.hpp:22
std::vector< char > encode_cbor(const EventEnvelope &env)
Definition serialization.hpp:129
parcel::json_t encode_json(const EventEnvelope &env)
Definition serialization.hpp:125