Visual Computing Library  devel
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_BASE_SERIALIZATION_DESERIALIZE_H
24#define VCL_BASE_SERIALIZATION_DESERIALIZE_H
25
26#include "endian.h"
27
28#include <vclib/base/concepts/serialization.h>
29#include <vclib/base/concepts/types.h>
30
31#include <bit>
32#include <istream>
33
34namespace vcl {
35
48template<IsNotClass T>
49void deserialize(std::istream& is, T& data, std::endian endian)
50{
51 is.read(reinterpret_cast<char*>(&data), sizeof(T));
52 if (endian != std::endian::native) {
53 data = detail::swapEndian(data);
54 }
55}
56
72template<typename T>
73void deserializeN(
74 std::istream& is,
75 T* data,
76 std::size_t size,
77 std::endian endian = std::endian::little)
78{
79 for (std::size_t i = 0; i < size; ++i) {
80 deserialize(is, data[i], endian);
81 }
82}
83
84template<typename T, typename... Others>
85void deserialize(std::istream& is, T& data, Others&... others)
86{
87 if constexpr (Serializable<T>) {
88 data.deserialize(is);
89 }
90 else {
91 deserialize(is, data, std::endian::little);
92 }
93 if constexpr (sizeof...(Others) > 0) {
94 deserialize(is, others...);
95 }
96}
97
98} // namespace vcl
99
100#endif // VCL_BASE_SERIALIZATION_DESERIALIZE_H
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:476