Visual Computing Library  devel
Loading...
Searching...
No Matches
serialize.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_SERIALIZE_H
24#define VCL_BASE_SERIALIZATION_SERIALIZE_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 <ostream>
33
34namespace vcl {
35
46template<IsNotClass T>
47void serialize(std::ostream& os, const T& data, std::endian endian)
48{
49 if (endian != std::endian::native) {
50 T swapped = detail::swapEndian(data);
51 os.write(reinterpret_cast<const char*>(&swapped), sizeof(T));
52 }
53 else {
54 os.write(reinterpret_cast<const char*>(&data), sizeof(T));
55 }
56}
57
72template<typename T>
73void serializeN(
74 std::ostream& os,
75 const 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 serialize(os, data[i], endian);
81 }
82}
83
84template<typename T, typename... Others>
85void serialize(std::ostream& os, const T& data, const Others&... others)
86{
87 if constexpr (Serializable<T>) {
88 data.serialize(os);
89 }
90 else {
91 serialize(os, data, std::endian::little);
92 }
93 if constexpr (sizeof...(Others) > 0) {
94 serialize(os, others...);
95 }
96}
97
98} // namespace vcl
99
100#endif // VCL_BASE_SERIALIZATION_SERIALIZE_H
void serialize(std::ostream &os) const
Serializes the box to the given output stream.
Definition box.h:466