Cbeam
Loading...
Searching...
No Matches
string.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/serialization/traits.hpp> // for cbeam::serialization::serialized_object, cbeam::serialization::traits
29
30#include <cstddef> // for std::size_t
31
32#include <iosfwd> // for std::stringstream
33#include <string> // for std::string
34
35namespace cbeam::container
36{
37 class buffer;
38}
39
41{
47 template <>
48 struct traits<std::string>
49 {
59 static inline void serialize(const std::string& str, container::buffer& stream)
60 {
61 std::size_t size = str.size();
62 stream.append(reinterpret_cast<const char*>(&size), sizeof(size));
63
64 for (const auto& c : str)
65 {
66 traits<char>::serialize(c, stream);
67 }
68 }
69
79 static inline void deserialize(serialized_object& it, std::string& str)
80 {
81 std::stringstream s;
82 str.clear();
83
84 char* localIt = reinterpret_cast<char*>(it);
85 std::size_t* size = reinterpret_cast<std::size_t*>(it);
86 localIt += sizeof(*size);
87
88 str = std::string(localIt, *size);
89 it = localIt + *size;
90 }
91 };
92}
Manages memory a byte buffer, offering dynamic appending. This class is designed for scenarios where ...
Definition buffer.hpp:50
virtual void append(const void *buffer_to_append, const std::size_t length_of_buffer)
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition buffer.hpp:96
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
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 std::string &str, container::buffer &stream)
Serializes a std::string into a binary buffer.
Definition string.hpp:59
static void deserialize(serialized_object &it, std::string &str)
Deserializes a std::string from a binary buffer.
Definition string.hpp:79
Defines the traits required for serializing and deserializing objects of type T.
Definition traits.hpp:52
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