Visual Computing Library  devel
Loading...
Searching...
No Matches
stl_deserialize.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BASE_SERIALIZATION_STL_DESERIALIZE_H
24#define VCL_BASE_SERIALIZATION_STL_DESERIALIZE_H
25
26#include "deserialize.h"
27
28#include <any>
29#include <array>
30#include <map>
31#include <string>
32#include <unordered_map>
33#include <vector>
34
35namespace vcl {
36
38
39/*
40 * std::string
41 */
42
43inline void deserialize(std::istream& is, std::string& s)
44{
45 std::size_t size;
46 deserialize(is, size);
47 s.resize(size);
48 deserializeN(is, s.data(), size);
49}
50
51/*
52 * std::array
53 */
54
55template<typename T, std::size_t N>
56void deserialize(std::istream& is, std::array<T, N>& a)
57{
58 if constexpr (Serializable<T>) {
59 for (T& v : a) {
60 v.deserialize(is);
61 }
62 }
63 else {
64 for (T& e : a) {
65 deserialize(is, e);
66 }
67 }
68}
69
70/*
71 * map
72 */
73
74template<typename K, typename V>
75void deserialize(std::istream& is, std::map<K, V>& m)
76{
77 m.clear();
78 std::size_t size;
79 deserialize(is, size);
80 for (std::size_t i = 0; i < size; ++i) {
81 K key;
82 if constexpr (Serializable<K>) {
83 key.deserialize(is);
84 }
85 else {
86 deserialize(is, key);
87 }
88
89 V value;
90 if constexpr (Serializable<V>) {
91 value.deserialize(is);
92 }
93 else {
94 deserialize(is, value);
95 }
96 m.insert({std::move(key), std::move(value)});
97 }
98}
99
100/*
101 * unordered_map
102 */
103
104template<typename K, typename V>
105void deserialize(std::istream& is, std::unordered_map<K, V>& m)
106{
107 m.clear();
108 std::size_t size;
109 deserialize(is, size);
110 for (std::size_t i = 0; i < size; ++i) {
111 K key;
112 if constexpr (Serializable<K>) {
113 key.deserialize(is);
114 }
115 else {
116 deserialize(is, key);
117 }
118
119 V value;
120 if constexpr (Serializable<V>) {
121 value.deserialize(is);
122 }
123 else {
124 deserialize(is, value);
125 }
126 m.insert({std::move(key), std::move(value)});
127 }
128}
129
130/*
131 * std::vector
132 */
133
134template<typename T>
135void deserialize(std::istream& is, std::vector<T>& v)
136{
137 std::size_t size;
138 deserialize(is, size);
139 v.resize(size);
140 if constexpr (Serializable<T>) {
141 for (T& e : v) {
142 e.deserialize(is);
143 }
144 }
145 else {
146 for (T& e : v) {
147 deserialize(is, e);
148 }
149 }
150}
151
152template<typename T>
153void deserialize(std::istream& is, std::vector<std::any>& v)
154{
155 std::size_t size;
156 deserialize(is, size);
157 v.resize(size);
158 if constexpr (Serializable<T>) {
159 for (std::any& e : v) {
160 T obj;
161 obj.deserialize(is);
162 e = std::move(obj);
163 }
164 }
165 else {
166 for (std::any& e : v) {
167 T obj;
168 deserialize(is, obj);
169 e = std::move(obj);
170 }
171 }
172}
173
174} // namespace vcl
175
176#endif // VCL_BASE_SERIALIZATION_STL_DESERIALIZE_H
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:476