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 <string>
31#include <vector>
32
33namespace vcl {
34
36
37/*
38 * std::array
39 */
40
41template<typename T, std::size_t N>
42void deserialize(std::istream& is, std::array<T, N>& a)
43{
44 if constexpr (Serializable<T>) {
45 for (T& v : a) {
46 v.deserialize(is);
47 }
48 }
49 else {
50 for (T& e : a) {
51 deserialize(is, e);
52 }
53 }
54}
55
56/*
57 * std::string
58 */
59
60inline void deserialize(std::istream& is, std::string& s)
61{
62 std::size_t size;
63 deserialize(is, size);
64 s.resize(size);
65 deserializeN(is, s.data(), size);
66}
67
68/*
69 * std::vector
70 */
71
72template<typename T>
73void deserialize(std::istream& is, std::vector<T>& v)
74{
75 std::size_t size;
76 deserialize(is, size);
77 v.resize(size);
78 if constexpr (Serializable<T>) {
79 for (T& e : v) {
80 e.deserialize(is);
81 }
82 }
83 else {
84 for (T& e : v) {
85 deserialize(is, e);
86 }
87 }
88}
89
90template<typename T>
91void deserialize(std::istream& is, std::vector<std::any>& v)
92{
93 std::size_t size;
94 deserialize(is, size);
95 v.resize(size);
96 if constexpr (Serializable<T>) {
97 for (std::any& e : v) {
98 T obj;
99 obj.deserialize(is);
100 e = std::move(obj);
101 }
102 }
103 else {
104 for (std::any& e : v) {
105 T obj;
106 deserialize(is, obj);
107 e = std::move(obj);
108 }
109 }
110}
111
112} // namespace vcl
113
114#endif // VCL_BASE_SERIALIZATION_STL_DESERIALIZE_H
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:476