Visual Computing Library
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_IO_SERIALIZATION_SERIALIZE_H
24#define VCL_IO_SERIALIZATION_SERIALIZE_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 <ostream>
34#include <vector>
35
36namespace vcl {
37
48template<IsNotClass T>
49void serialize(std::ostream& os, const T& data, std::endian endian)
50{
51 if (endian != std::endian::native) {
52 T swapped = detail::swapEndian(data);
53 os.write(reinterpret_cast<const char*>(&swapped), sizeof(T));
54 }
55 else {
56 os.write(reinterpret_cast<const char*>(&data), sizeof(T));
57 }
58}
59
74template<typename T>
75void serializeN(
76 std::ostream& os,
77 const 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 serialize(os, data[i], endian);
83 }
84}
85
86template<typename T, typename... Others>
87void serialize(std::ostream& os, const T& data, const Others&... others)
88{
89 if constexpr (Serializable<T>) {
90 data.serialize(os);
91 }
92 else {
93 serialize(os, data, std::endian::little);
94 }
95 if constexpr (sizeof...(Others) > 0) {
96 serialize(os, others...);
97 }
98}
99
101
102/*
103 * std::array
104 */
105
106template<typename T, std::size_t N>
107void serialize(std::ostream& os, const std::array<T, N>& a)
108{
109 if constexpr (Serializable<T>) {
110 for (const T& v : a) {
111 v.serialize(os);
112 }
113 }
114 else {
115 for (const T& e : a) {
116 serialize(os, e);
117 }
118 }
119}
120
121/*
122 * std::string
123 */
124
125inline void serialize(std::ostream& os, const std::string& s)
126{
127 std::size_t size = s.size();
128 serialize(os, size);
129 serializeN(os, s.data(), size);
130}
131
132/*
133 * std::vector
134 */
135
136template<typename T>
137void serialize(std::ostream& os, const std::vector<T>& v)
138{
139 std::size_t size = v.size();
140 serialize(os, size);
141 if constexpr (Serializable<T>) {
142 for (const T& e : v) {
143 e.serialize(os);
144 }
145 }
146 else {
147 for (const T& e : v) {
148 serialize(os, e);
149 }
150 }
151}
152
153} // namespace vcl
154
155#endif // VCL_IO_SERIALIZATION_SERIALIZE_H