Cbeam
Loading...
Searching...
No Matches
direct.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/container/stable_reference_buffer.hpp> // for cbeam::container::stable_reference_buffer
29#include <cbeam/logging/log_manager.hpp> // for CBEAM_LOG
30#include <cbeam/serialization/traits.hpp> // for cbeam::serialization::serialized_object, cbeam::serialization::traits
31
32#include <cassert> // for assert
33
34#include <exception> // for std::exception
35#include <string> // for std::string_literals::operator""s, std::operator+, std::string_literals
36
38{
49 template <typename T>
50 static inline container::stable_reference_buffer serialize(const T& instance) noexcept
51 {
52 using namespace std::string_literals;
54
55 try
56 {
57 traits<T>::serialize(instance, byte_stream);
58 }
59 catch (const std::exception& ex)
60 {
61 CBEAM_LOG("cbeam::serialization: Exception while serializing: "s + ex.what());
62 }
63 catch (...)
64 {
65 CBEAM_LOG("cbeam::serialization: Unknown exception while serializing"s);
66 }
67
68 return byte_stream;
69 }
70
81 template <typename T>
82 static inline T deserialize(serialized_object& it)
83 {
84 T result;
85 traits<T>::deserialize(it, result);
86 return result;
87 }
88
99 template <typename T>
100 static inline T deserialize(const serialized_object& it)
101 {
102 T result;
103 serialized_object it2 = it;
104 traits<T>::deserialize(it2, result);
105 return result;
106 }
107}
Manages memory buffers with stable reference counting, optimized for shared library contexts.
Definition stable_reference_buffer.hpp:63
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Implements traits-based serialization for complex data types, including standard containers and custo...
Definition direct.hpp:38
void * serialized_object
Represents a serialized value in memory.
Definition traits.hpp:42
static void serialize(const T &val, container::buffer &stream)
Required to serialize an object of type T into a shared_buffer stream.
Definition traits.hpp:63
static void deserialize(serialized_object &it, T &val)
Required to deserialize an object of type T from a serialized memory block, incrementing the iterator...
Definition traits.hpp:77