Visual Computing Library  devel
Loading...
Searching...
No Matches
stl_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_STL_SERIALIZE_H
24#define VCL_BASE_SERIALIZATION_STL_SERIALIZE_H
25
26#include "serialize.h"
27
28#include <any>
29#include <array>
30#include <map>
31#include <string>
32#include <unordered_map>
33#include <vector>
34
35namespace vcl {
36
38
39/*
40 * std::string
41 */
42
43inline void serialize(std::ostream& os, const std::string& s)
44{
45 std::size_t size = s.size();
46 serialize(os, size);
47 serializeN(os, s.data(), size);
48}
49
50/*
51 * std::array
52 */
53
54template<typename T, std::size_t N>
55void serialize(std::ostream& os, const std::array<T, N>& a)
56{
57 if constexpr (Serializable<T>) {
58 for (const T& v : a) {
59 v.serialize(os);
60 }
61 }
62 else {
63 for (const T& e : a) {
64 serialize(os, e);
65 }
66 }
67}
68
69/*
70 * std::map
71 */
72
73template<typename K, typename V>
74void serialize(std::ostream& os, const std::map<K, V>& m)
75{
76 std::size_t size = m.size();
77 serialize(os, size);
78 for (const auto& [key, value] : m) {
79 if constexpr (Serializable<K>) {
80 key.serialize(os);
81 }
82 else {
83 serialize(os, key);
84 }
85 if constexpr (Serializable<V>) {
86 value.serialize(os);
87 }
88 else {
89 serialize(os, value);
90 }
91 }
92}
93
94/*
95 * std::unordered_map
96 */
97
98template<typename K, typename V>
99void serialize(std::ostream& os, const std::unordered_map<K, V>& m)
100{
101 std::size_t size = m.size();
102 serialize(os, size);
103 for (const auto& [key, value] : m) {
104 if constexpr (Serializable<K>) {
105 key.serialize(os);
106 }
107 else {
108 serialize(os, key);
109 }
110 if constexpr (Serializable<V>) {
111 value.serialize(os);
112 }
113 else {
114 serialize(os, value);
115 }
116 }
117}
118
119/*
120 * std::vector
121 */
122
123template<typename T>
124void serialize(std::ostream& os, const std::vector<T>& v)
125{
126 std::size_t size = v.size();
127 serialize(os, size);
128 if constexpr (Serializable<T>) {
129 for (const T& e : v) {
130 e.serialize(os);
131 }
132 }
133 else {
134 for (const T& e : v) {
135 serialize(os, e);
136 }
137 }
138}
139
140template<typename T>
141void serialize(std::ostream& os, const std::vector<std::any>& v)
142{
143 std::size_t size = v.size();
144 serialize(os, size);
145 if constexpr (Serializable<T>) {
146 for (const std::any& e : v) {
147 std::any_cast<T>(e).serialize(os);
148 }
149 }
150 else {
151 for (const std::any& e : v) {
152 serialize(os, std::any_cast<T>(e));
153 }
154 }
155}
156
157} // namespace vcl
158
159#endif // VCL_BASE_SERIALIZATION_STL_SERIALIZE_H
void serialize(std::ostream &os) const
Serializes the box to the given output stream.
Definition box.h:466
PointT size() const
Computes the size of the box.
Definition box.h:267