VTK  9.4.20241112
vtkMultiProcessStreamSerialization.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
28#ifndef vtkMultiProcessSerialization_h
29#define vtkMultiProcessSerialization_h
30
32
33#include <array>
34#include <map>
35#include <set>
36#include <utility>
37#include <vector>
38
39VTK_ABI_NAMESPACE_BEGIN
40template <typename T>
42{
43 static void Save(vtkMultiProcessStream& stream, const T& t) { stream << t; }
44 static void Load(vtkMultiProcessStream& stream, T& t) { stream >> t; }
45};
46
47template <typename ElementType>
48struct Serialization<std::set<ElementType>>
49{
50 static void Save(vtkMultiProcessStream& stream, const std::set<ElementType>& set)
51 {
52 stream << static_cast<vtkTypeInt64>(set.size());
53 for (const auto& elem : set)
54 {
56 }
57 }
58
59 static void Load(vtkMultiProcessStream& stream, std::set<ElementType>& set)
60 {
61 vtkTypeInt64 count;
62 stream >> count;
63 for (vtkTypeInt64 cc = 0; cc < count; ++cc)
64 {
65 ElementType elem;
67 set.insert(std::move(elem));
68 }
69 }
70};
71
72template <typename ElementType, std::size_t N>
73struct Serialization<std::array<ElementType, N>>
74{
75 static void Save(vtkMultiProcessStream& stream, const std::array<ElementType, N>& array)
76 {
77 for (const auto& elem : array)
78 {
80 }
81 }
82
83 static void Load(vtkMultiProcessStream& stream, std::array<ElementType, N>& array)
84 {
85 for (std::size_t cc = 0; cc < N; ++cc)
86 {
87 Serialization<ElementType>::Load(stream, array[cc]);
88 }
89 }
90};
91
92template <typename T1, typename T2>
93struct Serialization<std::pair<T1, T2>>
94{
95 static void Save(vtkMultiProcessStream& stream, const std::pair<T1, T2>& pair)
96 {
97 Serialization<T1>::Save(stream, pair.first);
98 Serialization<T2>::Save(stream, pair.second);
99 }
100
101 static void Load(vtkMultiProcessStream& stream, std::pair<T1, T2>& pair)
102 {
103 Serialization<T1>::Load(stream, pair.first);
104 Serialization<T2>::Load(stream, pair.second);
105 }
106};
107
108template <typename T1, typename T2>
109struct Serialization<std::map<T1, T2>>
110{
111 static void Save(vtkMultiProcessStream& stream, const std::map<T1, T2>& map)
112 {
113 stream << static_cast<vtkTypeInt64>(map.size());
114 for (const auto& pair : map)
115 {
117 }
118 }
119
120 static void Load(vtkMultiProcessStream& stream, std::map<T1, T2>& map)
121 {
122 vtkTypeInt64 count;
123 stream >> count;
124 for (vtkTypeInt64 cc = 0; cc < count; ++cc)
125 {
126 std::pair<T1, T2> pair;
128 map.insert(std::move(pair));
129 }
130 }
131};
132
133template <typename ElementType>
134struct Serialization<std::vector<ElementType>>
135{
136 static void Save(vtkMultiProcessStream& stream, const std::vector<ElementType>& vector)
137 {
138 stream << static_cast<vtkTypeInt64>(vector.size());
139 for (const auto& elem : vector)
140 {
142 }
143 }
144
145 static void Load(vtkMultiProcessStream& stream, std::vector<ElementType>& vector)
146 {
147 vtkTypeInt64 count;
148 stream >> count;
149 for (vtkTypeInt64 cc = 0; cc < count; ++cc)
150 {
151 ElementType elem;
153 vector.push_back(std::move(elem));
154 }
155 }
156};
157
158template <typename T>
160{
161 Serialization<T>::Save(stream, value);
162 return stream;
163}
164
165template <typename T>
167{
168 Serialization<T>::Load(stream, value);
169 return stream;
170}
171
172VTK_ABI_NAMESPACE_END
173#endif
174// VTK-HeaderTest-Exclude: vtkMultiProcessStreamSerialization.h
stream used to pass data across processes using vtkMultiProcessController.
static void Save(vtkMultiProcessStream &stream, const std::array< ElementType, N > &array)
static void Load(vtkMultiProcessStream &stream, std::array< ElementType, N > &array)
static void Save(vtkMultiProcessStream &stream, const std::map< T1, T2 > &map)
static void Load(vtkMultiProcessStream &stream, std::map< T1, T2 > &map)
static void Save(vtkMultiProcessStream &stream, const std::pair< T1, T2 > &pair)
static void Load(vtkMultiProcessStream &stream, std::pair< T1, T2 > &pair)
static void Save(vtkMultiProcessStream &stream, const std::set< ElementType > &set)
static void Load(vtkMultiProcessStream &stream, std::set< ElementType > &set)
static void Save(vtkMultiProcessStream &stream, const std::vector< ElementType > &vector)
static void Load(vtkMultiProcessStream &stream, std::vector< ElementType > &vector)
static void Save(vtkMultiProcessStream &stream, const T &t)
static void Load(vtkMultiProcessStream &stream, T &t)
vtkMultiProcessStream & operator<<(vtkMultiProcessStream &stream, const T &value)
vtkMultiProcessStream & operator>>(vtkMultiProcessStream &stream, T &value)