Visual Computing Library
Loading...
Searching...
No Matches
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_IO_SERIALIZATION_DESERIALIZE_H
24#define VCL_IO_SERIALIZATION_DESERIALIZE_H
25
26#include "endian.h"
27
28#include <vclib/concepts/serialization.h>
29#include <vclib/concepts/types.h>
30
31#include <array>
32#include <bit>
33#include <istream>
34#include <vector>
35
36namespace vcl {
37
50template<IsNotClass T>
51void deserialize(std::istream& is, T& data, std::endian endian)
52{
53 is.read(reinterpret_cast<char*>(&data), sizeof(T));
54 if (endian != std::endian::native) {
55 data = detail::swapEndian(data);
56 }
57}
58
74template<typename T>
75void deserializeN(
76 std::istream& is,
77 T* data,
78 std::size_t size,
79 std::endian endian = std::endian::little)
80{
81 for (std::size_t i = 0; i < size; ++i) {
82 deserialize(is, data[i], endian);
83 }
84}
85
86template<typename T, typename... Others>
87void deserialize(std::istream& is, T& data, Others&... others)
88{
89 if constexpr (Serializable<T>) {
90 data.deserialize(is);
91 }
92 else {
93 deserialize(is, data, std::endian::little);
94 }
95 if constexpr (sizeof...(Others) > 0) {
96 deserialize(is, others...);
97 }
98}
99
101
102/*
103 * std::array
104 */
105
106template<typename T, std::size_t N>
107void deserialize(std::istream& is, std::array<T, N>& a)
108{
109 if constexpr (Serializable<T>) {
110 for (T& v : a) {
111 v.deserialize(is);
112 }
113 }
114 else {
115 for (T& e : a) {
116 deserialize(is, e);
117 }
118 }
119}
120
121/*
122 * std::string
123 */
124
125inline void deserialize(std::istream& is, std::string& s)
126{
127 std::size_t size;
128 deserialize(is, size);
129 s.resize(size);
130 deserializeN(is, s.data(), size);
131}
132
133/*
134 * std::vector
135 */
136
137template<typename T>
138void deserialize(std::istream& is, std::vector<T>& v)
139{
140 std::size_t size;
141 deserialize(is, size);
142 v.resize(size);
143 if constexpr (Serializable<T>) {
144 for (T& e : v) {
145 e.deserialize(is);
146 }
147 }
148 else {
149 for (T& e : v) {
150 deserialize(is, e);
151 }
152 }
153}
154
155} // namespace vcl
156
157#endif // VCL_IO_SERIALIZATION_DESERIALIZE_H