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 <string>
31#include <vector>
32
33namespace vcl {
34
36
37/*
38 * std::array
39 */
40
41template<typename T, std::size_t N>
42void serialize(std::ostream& os, const std::array<T, N>& a)
43{
44 if constexpr (Serializable<T>) {
45 for (const T& v : a) {
46 v.serialize(os);
47 }
48 }
49 else {
50 for (const T& e : a) {
51 serialize(os, e);
52 }
53 }
54}
55
56/*
57 * std::string
58 */
59
60inline void serialize(std::ostream& os, const std::string& s)
61{
62 std::size_t size = s.size();
63 serialize(os, size);
64 serializeN(os, s.data(), size);
65}
66
67/*
68 * std::vector
69 */
70
71template<typename T>
72void serialize(std::ostream& os, const std::vector<T>& v)
73{
74 std::size_t size = v.size();
75 serialize(os, size);
76 if constexpr (Serializable<T>) {
77 for (const T& e : v) {
78 e.serialize(os);
79 }
80 }
81 else {
82 for (const T& e : v) {
83 serialize(os, e);
84 }
85 }
86}
87
88template<typename T>
89void serialize(std::ostream& os, const std::vector<std::any>& v)
90{
91 std::size_t size = v.size();
92 serialize(os, size);
93 if constexpr (Serializable<T>) {
94 for (const std::any& e : v) {
95 std::any_cast<T>(e).serialize(os);
96 }
97 }
98 else {
99 for (const std::any& e : v) {
100 serialize(os, std::any_cast<T>(e));
101 }
102 }
103}
104
105} // namespace vcl
106
107#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