VTK  9.4.20241222
vtkVariantBoostSerialization.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-FileCopyrightText: Copyright (C) 2008 The Trustees of Indiana University.
3// SPDX-License-Identifier: BSD-3-Clause AND BSL-1.0
17#ifndef vtkVariantBoostSerialization_h
18#define vtkVariantBoostSerialization_h
19
20#include "vtkSetGet.h"
21#include "vtkType.h"
22#include "vtkVariant.h"
23#include "vtkVariantArray.h"
24
25// This include fixes header-ordering issues in Boost.Serialization
26// prior to Boost 1.35.0.
27#include <boost/archive/binary_oarchive.hpp>
28
29#include <boost/serialization/base_object.hpp>
30#include <boost/serialization/export.hpp>
31#include <boost/serialization/extended_type_info_no_rtti.hpp>
32#include <boost/serialization/split_free.hpp>
33
34VTK_ABI_NAMESPACE_BEGIN
35
36//----------------------------------------------------------------------------
37// vtkStdString serialization code
38//----------------------------------------------------------------------------
39template <typename Archiver>
40void serialize(Archiver& ar, vtkStdString& str, const unsigned int vtkNotUsed(version))
41{
42 ar& boost::serialization::base_object<std::string>(str);
43}
44
45//----------------------------------------------------------------------------
46
47template <typename Archiver>
48void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))
49{
50 ar& str;
51}
52
53template <typename Archiver>
54void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))
55{
56 std::string utf8;
57 ar& utf8;
58 str = utf8;
59}
60
61//----------------------------------------------------------------------------
62// vtkVariant serialization code
63//----------------------------------------------------------------------------
64
65template <typename Archiver>
66void save(Archiver& ar, const vtkVariant& variant, const unsigned int vtkNotUsed(version))
67{
68 if (!variant.IsValid())
69 {
70 char null = 0;
71 ar& null;
72 return;
73 }
74
75 // Output the type
76 char Type = variant.GetType();
77 ar& Type;
78
79 // Output the value
80#define VTK_VARIANT_SAVE(Value, Type, Function) \
81 case Value: \
82 { \
83 Type value = variant.Function(); \
84 ar& value; \
85 } \
86 return
87
88 switch (Type)
89 {
91 VTK_VARIANT_SAVE(VTK_FLOAT, float, ToFloat);
92 VTK_VARIANT_SAVE(VTK_DOUBLE, double, ToDouble);
93 VTK_VARIANT_SAVE(VTK_CHAR, char, ToChar);
94 VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR, unsigned char, ToUnsignedChar);
95 VTK_VARIANT_SAVE(VTK_SHORT, short, ToShort);
96 VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT, unsigned short, ToUnsignedShort);
97 VTK_VARIANT_SAVE(VTK_INT, int, ToInt);
98 VTK_VARIANT_SAVE(VTK_UNSIGNED_INT, unsigned int, ToUnsignedInt);
99 VTK_VARIANT_SAVE(VTK_LONG, long, ToLong);
100 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG, unsigned long, ToUnsignedLong);
101 VTK_VARIANT_SAVE(VTK_LONG_LONG, long long, ToLongLong);
102 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG, unsigned long long, ToUnsignedLongLong);
103 default:
104 cerr << "cannot serialize variant with type " << variant.GetType() << '\n';
105 }
106#undef VTK_VARIANT_SAVE
107}
108
109template <typename Archiver>
110void load(Archiver& ar, vtkVariant& variant, const unsigned int vtkNotUsed(version))
111{
112 char Type;
113 ar& Type;
114
115#define VTK_VARIANT_LOAD(Value, Type) \
116 case Value: \
117 { \
118 Type value; \
119 ar& value; \
120 variant = vtkVariant(value); \
121 } \
122 return
123
124 switch (Type)
125 {
126 case 0:
127 variant = vtkVariant();
128 return;
133 VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR, unsigned char);
135 VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT, unsigned short);
137 VTK_VARIANT_LOAD(VTK_UNSIGNED_INT, unsigned int);
139 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG, unsigned long);
141 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG, unsigned long long);
142 default:
143 cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
144 variant = vtkVariant();
145 }
146#undef VTK_VARIANT_LOAD
147}
148
149VTK_ABI_NAMESPACE_END
150
151BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
152
153VTK_ABI_NAMESPACE_BEGIN
154
155//----------------------------------------------------------------------------
156// vtkVariantArray serialization code
157//----------------------------------------------------------------------------
158
159template <typename Archiver>
160void save(Archiver& ar, const vtkVariantArray& c_array, const unsigned int vtkNotUsed(version))
161{
162 vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
163
164 // Array name
165 vtkStdString name;
166 if (array.GetName() != nullptr)
167 name = array.GetName();
168 ar& name;
169
170 // Array data
171 vtkIdType n = array.GetNumberOfTuples();
172 ar& n;
173 for (vtkIdType i = 0; i < n; ++i)
174 {
175 ar& array.GetValue(i);
176 }
177}
178
179template <typename Archiver>
180void load(Archiver& ar, vtkVariantArray& array, const unsigned int vtkNotUsed(version))
181{
182 // Array name
183 vtkStdString name;
184 ar& name;
185 array.SetName(name.c_str());
186
187 if (name.empty())
188 {
189 array.SetName(nullptr);
190 }
191 else
192 {
193 array.SetName(name.c_str());
194 }
195
196 // Array data
197 vtkIdType n;
198 ar& n;
199 array.SetNumberOfTuples(n);
200 vtkVariant value;
201 for (vtkIdType i = 0; i < n; ++i)
202 {
203 ar& value;
204 array.SetValue(i, value);
205 }
206}
207
208VTK_ABI_NAMESPACE_END
209
210BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
211
212#endif
213// VTK-HeaderTest-Exclude: vtkVariantBoostSerialization.h
virtual char * GetName()
Set/get array's name.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
virtual void SetName(const char *)
Set/get array's name.
Wrapper around std::string to keep symbols short.
An array holding vtkVariants.
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
vtkVariant & GetValue(vtkIdType id) const
Get the data at a particular index.
void SetValue(vtkIdType id, vtkVariant value)
Set the data at a particular index.
A type representing the union of many types.
Definition vtkVariant.h:162
unsigned int GetType() const
Get the type of the variant.
bool IsValid() const
Get whether the variant value is valid.
#define VTK_SHORT
Definition vtkType.h:36
int vtkIdType
Definition vtkType.h:315
#define VTK_UNSIGNED_INT
Definition vtkType.h:39
#define VTK_LONG_LONG
Definition vtkType.h:51
#define VTK_DOUBLE
Definition vtkType.h:43
#define VTK_UNSIGNED_CHAR
Definition vtkType.h:35
#define VTK_UNSIGNED_SHORT
Definition vtkType.h:37
#define VTK_INT
Definition vtkType.h:38
#define VTK_STRING
Definition vtkType.h:48
#define VTK_FLOAT
Definition vtkType.h:42
#define VTK_CHAR
Definition vtkType.h:33
#define VTK_UNSIGNED_LONG
Definition vtkType.h:41
#define VTK_UNSIGNED_LONG_LONG
Definition vtkType.h:52
#define VTK_LONG
Definition vtkType.h:40
#define VTK_VARIANT_SAVE(Value, Type, Function)
void serialize(Archiver &ar, vtkStdString &str, const unsigned int vtkNotUsed(version))
#define VTK_VARIANT_LOAD(Value, Type)
void load(Archiver &ar, std::string &str, const unsigned int vtkNotUsed(version))
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))