Cbeam
Loading...
Searching...
No Matches
map.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/buffer.hpp> // for buffer (ptr only), cbeam::container::buffer
29#include <cbeam/serialization/traits.hpp> // for cbeam::serialization::traits, cbeam::serialization::serialized_object
30
31#include <cstdint> // for uint8_t
32#include <cstring> // for memcpy, size_t, std::memcpy
33
34#include <map> // for std::map
35
37{
44 template <typename Map>
46 {
56 static void serialize(const Map& map, container::buffer& stream)
57 {
58 size_t size = map.size();
59 stream.append(reinterpret_cast<const char*>(&size), sizeof(size));
60
61 for (const auto& pair : map)
62 {
65 }
66 }
67
77 static void deserialize(serialized_object& it, Map& map)
78 {
79 map.clear();
80
81 size_t size;
82 uint8_t* localIt = reinterpret_cast<uint8_t*>(it);
83 std::memcpy(&size, localIt, sizeof(size));
84 localIt += sizeof(size);
85 it = localIt;
86
87 for (size_t i = 0; i < size; ++i)
88 {
89 typename Map::key_type key;
90 typename Map::mapped_type value;
93 map.insert({key, value});
94 }
95 }
96 };
97
106 template <typename Key, typename Value>
107 struct traits<std::map<Key, Value>>
108 {
109 static inline void serialize(const std::map<Key, Value>& map, container::buffer& stream)
110 {
112 }
113
114 static inline void deserialize(serialized_object& it, std::map<Key, Value>& map)
115 {
117 }
118 };
119
123 template <typename Key, typename Value>
124 struct traits<const std::map<Key, Value>>
125 {
126 static inline void serialize(const std::map<Key, Value>& map, container::buffer& stream)
127 {
129 }
130 };
131}
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
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
Provides serialization and deserialization logic for standard map-like containers.
Definition map.hpp:46
static void deserialize(serialized_object &it, Map &map)
Deserializes map data from the buffer into a Map instance, replacing its current contents.
Definition map.hpp:77
static void serialize(const Map &map, container::buffer &stream)
Serializes all key-value pairs of the map into a binary format appended to stream.
Definition map.hpp:56
static void serialize(const std::map< Key, Value > &map, container::buffer &stream)
Definition map.hpp:126
static void deserialize(serialized_object &it, std::map< Key, Value > &map)
Definition map.hpp:114
static void serialize(const std::map< Key, Value > &map, container::buffer &stream)
Definition map.hpp:109
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
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