VTK  9.3.0
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
34//----------------------------------------------------------------------------
35// vtkStdString serialization code
36//----------------------------------------------------------------------------
37template <typename Archiver>
38void serialize(Archiver& ar, vtkStdString& str, const unsigned int vtkNotUsed(version))
39{
40 ar& boost::serialization::base_object<std::string>(str);
41}
42
43//----------------------------------------------------------------------------
44
45template <typename Archiver>
46void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))
47{
48 ar& str;
49}
50
51template <typename Archiver>
52void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))
53{
54 std::string utf8;
55 ar& utf8;
56 str = utf8;
57}
58
59//----------------------------------------------------------------------------
60// vtkVariant serialization code
61//----------------------------------------------------------------------------
62
63template <typename Archiver>
64void save(Archiver& ar, const vtkVariant& variant, const unsigned int vtkNotUsed(version))
65{
66 if (!variant.IsValid())
67 {
68 char null = 0;
69 ar& null;
70 return;
71 }
72
73 // Output the type
74 char Type = variant.GetType();
75 ar& Type;
76
77 // Output the value
78#define VTK_VARIANT_SAVE(Value, Type, Function) \
79 case Value: \
80 { \
81 Type value = variant.Function(); \
82 ar& value; \
83 } \
84 return
85
86 switch (Type)
87 {
89 VTK_VARIANT_SAVE(VTK_FLOAT, float, ToFloat);
90 VTK_VARIANT_SAVE(VTK_DOUBLE, double, ToDouble);
91 VTK_VARIANT_SAVE(VTK_CHAR, char, ToChar);
92 VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR, unsigned char, ToUnsignedChar);
93 VTK_VARIANT_SAVE(VTK_SHORT, short, ToShort);
94 VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT, unsigned short, ToUnsignedShort);
95 VTK_VARIANT_SAVE(VTK_INT, int, ToInt);
96 VTK_VARIANT_SAVE(VTK_UNSIGNED_INT, unsigned int, ToUnsignedInt);
97 VTK_VARIANT_SAVE(VTK_LONG, long, ToLong);
98 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG, unsigned long, ToUnsignedLong);
99 VTK_VARIANT_SAVE(VTK_LONG_LONG, long long, ToLongLong);
100 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG, unsigned long long, ToUnsignedLongLong);
101 default:
102 cerr << "cannot serialize variant with type " << variant.GetType() << '\n';
103 }
104#undef VTK_VARIANT_SAVE
105}
106
107template <typename Archiver>
108void load(Archiver& ar, vtkVariant& variant, const unsigned int vtkNotUsed(version))
109{
110 char Type;
111 ar& Type;
112
113#define VTK_VARIANT_LOAD(Value, Type) \
114 case Value: \
115 { \
116 Type value; \
117 ar& value; \
118 variant = vtkVariant(value); \
119 } \
120 return
121
122 switch (Type)
123 {
124 case 0:
125 variant = vtkVariant();
126 return;
131 VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR, unsigned char);
133 VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT, unsigned short);
135 VTK_VARIANT_LOAD(VTK_UNSIGNED_INT, unsigned int);
137 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG, unsigned long);
139 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG, unsigned long long);
140 default:
141 cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
142 variant = vtkVariant();
143 }
144#undef VTK_VARIANT_LOAD
145}
146
147BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
148
149//----------------------------------------------------------------------------
150// vtkVariantArray serialization code
151//----------------------------------------------------------------------------
152
153template <typename Archiver>
154void save(Archiver& ar, const vtkVariantArray& c_array, const unsigned int vtkNotUsed(version))
155{
156 vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
157
158 // Array name
159 vtkStdString name;
160 if (array.GetName() != nullptr)
161 name = array.GetName();
162 ar& name;
163
164 // Array data
165 vtkIdType n = array.GetNumberOfTuples();
166 ar& n;
167 for (vtkIdType i = 0; i < n; ++i)
168 {
169 ar& array.GetValue(i);
170 }
171}
172
173template <typename Archiver>
174void load(Archiver& ar, vtkVariantArray& array, const unsigned int vtkNotUsed(version))
175{
176 // Array name
177 vtkStdString name;
178 ar& name;
179 array.SetName(name.c_str());
180
181 if (name.empty())
182 {
183 array.SetName(nullptr);
184 }
185 else
186 {
187 array.SetName(name.c_str());
188 }
189
190 // Array data
191 vtkIdType n;
192 ar& n;
193 array.SetNumberOfTuples(n);
194 vtkVariant value;
195 for (vtkIdType i = 0; i < n; ++i)
196 {
197 ar& value;
198 array.SetValue(i, value);
199 }
200}
201
202BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
203
204#endif
205// 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:53
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))