VTK: Parallel/Core/vtkMultiProcessStreamSerialization.h Source File

| VTK 9.3.1

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
31 #include "vtkMultiProcessStream.h"
32
33 #include <array>
34 #include <map>
35 #include <set>
36 #include <utility>
37 #include <vector>
38
39 VTK_ABI_NAMESPACE_BEGIN
40 template <typename T>
41 struct Serialization
42 {
43 static void Save(vtkMultiProcessStream& stream, const T& t) { stream << t; }
44 static void Load(vtkMultiProcessStream& stream, T& t) { stream >> t; }
45 };
46
47 template <typename ElementType>
48 struct 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 {
55 Serialization<ElementType>::Save(stream, elem);
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;
66 Serialization<ElementType>::Load(stream, elem);
67 set.insert(std::move(elem));
68 }
69 }
70 };
71
72 template <typename ElementType, std::size_t N>
73 struct 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 {
79 Serialization<ElementType>::Save(stream, elem);
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
92 template <typename T1, typename T2>
93 struct 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
108 template <typename T1, typename T2>
109 struct 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 {
116 Serialization<std::pair<T1, T2>>::Save(stream, pair);
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;
127 Serialization<std::pair<T1, T2>>::Load(stream, pair);
128 map.insert(std::move(pair));
129 }
130 }
131 };
132
133 template <typename ElementType>
134 struct 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 {
141 Serialization<ElementType>::Save(stream, elem);
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;
152 Serialization<ElementType>::Load(stream, elem);
153 vector.push_back(std::move(elem));
154 }
155 }
156 };
157
158 template <typename T>
159 inline vtkMultiProcessStream& operator<<(vtkMultiProcessStream& stream, const T& value)
160 {
161 Serialization<T>::Save(stream, value);
162 return stream;
163 }
164
165 template <typename T>
166 inline vtkMultiProcessStream& operator>>(vtkMultiProcessStream& stream, T& value)
167 {
168 Serialization<T>::Load(stream, value);
169 return stream;
170 }
171
172 VTK_ABI_NAMESPACE_END
173 #endif
174 // VTK-HeaderTest-Exclude: vtkMultiProcessStreamSerialization.h
vtkMultiProcessStream
stream used to pass data across processes using vtkMultiProcessController.
Definition: vtkMultiProcessStream.h:30
std
Definition: vtkSmartPointer.h:310
vtkX3D::vector
@ vector
Definition: vtkX3D.h:237
vtkX3D::value
@ value
Definition: vtkX3D.h:220
Serialization< std::array< ElementType, N > >::Save
static void Save(vtkMultiProcessStream &stream, const std::array< ElementType, N > &array)
Definition: vtkMultiProcessStreamSerialization.h:75
Serialization< std::array< ElementType, N > >::Load
static void Load(vtkMultiProcessStream &stream, std::array< ElementType, N > &array)
Definition: vtkMultiProcessStreamSerialization.h:83
Serialization< std::map< T1, T2 > >::Save
static void Save(vtkMultiProcessStream &stream, const std::map< T1, T2 > &map)
Definition: vtkMultiProcessStreamSerialization.h:111
Serialization< std::map< T1, T2 > >::Load
static void Load(vtkMultiProcessStream &stream, std::map< T1, T2 > &map)
Definition: vtkMultiProcessStreamSerialization.h:120
Serialization< std::pair< T1, T2 > >::Save
static void Save(vtkMultiProcessStream &stream, const std::pair< T1, T2 > &pair)
Definition: vtkMultiProcessStreamSerialization.h:95
Serialization< std::pair< T1, T2 > >::Load
static void Load(vtkMultiProcessStream &stream, std::pair< T1, T2 > &pair)
Definition: vtkMultiProcessStreamSerialization.h:101
Serialization< std::set< ElementType > >::Save
static void Save(vtkMultiProcessStream &stream, const std::set< ElementType > &set)
Definition: vtkMultiProcessStreamSerialization.h:50
Serialization< std::set< ElementType > >::Load
static void Load(vtkMultiProcessStream &stream, std::set< ElementType > &set)
Definition: vtkMultiProcessStreamSerialization.h:59
Serialization< std::vector< ElementType > >::Save
static void Save(vtkMultiProcessStream &stream, const std::vector< ElementType > &vector)
Definition: vtkMultiProcessStreamSerialization.h:136
Serialization< std::vector< ElementType > >::Load
static void Load(vtkMultiProcessStream &stream, std::vector< ElementType > &vector)
Definition: vtkMultiProcessStreamSerialization.h:145
Serialization
Definition: vtkMultiProcessStreamSerialization.h:42
Serialization::Save
static void Save(vtkMultiProcessStream &stream, const T &t)
Definition: vtkMultiProcessStreamSerialization.h:43
Serialization::Load
static void Load(vtkMultiProcessStream &stream, T &t)
Definition: vtkMultiProcessStreamSerialization.h:44
operator<<
vtkMultiProcessStream & operator<<(vtkMultiProcessStream &stream, const T &value)
Definition: vtkMultiProcessStreamSerialization.h:159
operator>>
vtkMultiProcessStream & operator>>(vtkMultiProcessStream &stream, T &value)
Definition: vtkMultiProcessStreamSerialization.h:166
vtkMultiProcessStream.h