VTK  9.1.0
vtkVariantBoostSerialization.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkVariantBoostSerialization.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
15/*
16 * Copyright (C) 2008 The Trustees of Indiana University.
17 * Use, modification and distribution is subject to the Boost Software
18 * License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
19 */
33#ifndef vtkVariantBoostSerialization_h
34#define vtkVariantBoostSerialization_h
35
36#include "vtkSetGet.h"
37#include "vtkType.h"
38#include "vtkVariant.h"
39#include "vtkVariantArray.h"
40
41// This include fixes header-ordering issues in Boost.Serialization
42// prior to Boost 1.35.0.
43#include <boost/archive/binary_oarchive.hpp>
44
45#include <boost/serialization/base_object.hpp>
46#include <boost/serialization/export.hpp>
47#include <boost/serialization/extended_type_info_no_rtti.hpp>
48#include <boost/serialization/split_free.hpp>
49
50//----------------------------------------------------------------------------
51// vtkStdString serialization code
52//----------------------------------------------------------------------------
53template <typename Archiver>
54void serialize(Archiver& ar, vtkStdString& str, const unsigned int vtkNotUsed(version))
55{
56 ar& boost::serialization::base_object<std::string>(str);
57}
58
59//----------------------------------------------------------------------------
60// vtkUnicodeString serialization code
61//----------------------------------------------------------------------------
62
63template <typename Archiver>
64void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))
65{
66 ar& str;
67}
68
69template <typename Archiver>
71 "Use void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))")
72void save(Archiver& ar, const vtkUnicodeString& str, const unsigned int vtkNotUsed(version))
73{
74 std::string utf8(str.utf8_str());
75 save(ar, utf8);
76}
77
78template <typename Archiver>
79void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))
80{
81 std::string utf8;
82 ar& utf8;
83 str = utf8;
84}
85
86template <typename Archiver>
88 "Use void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))")
89void load(Archiver& ar, vtkUnicodeString& str, const unsigned int vtkNotUsed(version))
90{
91 std::string utf8;
92 load(ar, utf8);
94}
95
96BOOST_SERIALIZATION_SPLIT_FREE(vtkUnicodeString)
97
98//----------------------------------------------------------------------------
99// vtkVariant serialization code
100//----------------------------------------------------------------------------
101
102template <typename Archiver>
103void save(Archiver& ar, const vtkVariant& variant, const unsigned int vtkNotUsed(version))
104{
105 if (!variant.IsValid())
106 {
107 char null = 0;
108 ar& null;
109 return;
110 }
111
112 // Output the type
113 char Type = variant.GetType();
114 ar& Type;
115
116 // Output the value
117#define VTK_VARIANT_SAVE(Value, Type, Function) \
118 case Value: \
119 { \
120 Type value = variant.Function(); \
121 ar& value; \
122 } \
123 return
124
125 switch (Type)
126 {
129 VTK_VARIANT_SAVE(VTK_FLOAT, float, ToFloat);
130 VTK_VARIANT_SAVE(VTK_DOUBLE, double, ToDouble);
131 VTK_VARIANT_SAVE(VTK_CHAR, char, ToChar);
132 VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR, unsigned char, ToUnsignedChar);
133 VTK_VARIANT_SAVE(VTK_SHORT, short, ToShort);
134 VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT, unsigned short, ToUnsignedShort);
135 VTK_VARIANT_SAVE(VTK_INT, int, ToInt);
136 VTK_VARIANT_SAVE(VTK_UNSIGNED_INT, unsigned int, ToUnsignedInt);
137 VTK_VARIANT_SAVE(VTK_LONG, long, ToLong);
138 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG, unsigned long, ToUnsignedLong);
139 VTK_VARIANT_SAVE(VTK_LONG_LONG, long long, ToLongLong);
140 VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG, unsigned long long, ToUnsignedLongLong);
141 default:
142 cerr << "cannot serialize variant with type " << variant.GetType() << '\n';
143 }
144#undef VTK_VARIANT_SAVE
145}
146
147template <typename Archiver>
148void load(Archiver& ar, vtkVariant& variant, const unsigned int vtkNotUsed(version))
149{
150 char Type;
151 ar& Type;
152
153#define VTK_VARIANT_LOAD(Value, Type) \
154 case Value: \
155 { \
156 Type value; \
157 ar& value; \
158 variant = vtkVariant(value); \
159 } \
160 return
161
162 switch (Type)
163 {
164 case 0:
165 variant = vtkVariant();
166 return;
172 VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR, unsigned char);
174 VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT, unsigned short);
176 VTK_VARIANT_LOAD(VTK_UNSIGNED_INT, unsigned int);
178 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG, unsigned long);
180 VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG, unsigned long long);
181 default:
182 cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
183 variant = vtkVariant();
184 }
185#undef VTK_VARIANT_LOAD
186}
187
188BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
189
190//----------------------------------------------------------------------------
191// vtkVariantArray serialization code
192//----------------------------------------------------------------------------
193
194template <typename Archiver>
195void save(Archiver& ar, const vtkVariantArray& c_array, const unsigned int vtkNotUsed(version))
196{
197 vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
198
199 // Array name
200 vtkStdString name;
201 if (array.GetName() != nullptr)
202 name = array.GetName();
203 ar& name;
204
205 // Array data
206 vtkIdType n = array.GetNumberOfTuples();
207 ar& n;
208 for (vtkIdType i = 0; i < n; ++i)
209 {
210 ar& array.GetValue(i);
211 }
212}
213
214template <typename Archiver>
215void load(Archiver& ar, vtkVariantArray& array, const unsigned int vtkNotUsed(version))
216{
217 // Array name
218 vtkStdString name;
219 ar& name;
220 array.SetName(name.c_str());
221
222 if (name.empty())
223 {
224 array.SetName(0);
225 }
226 else
227 {
228 array.SetName(name.c_str());
229 }
230
231 // Array data
232 vtkIdType n;
233 ar& n;
234 array.SetNumberOfTuples(n);
235 vtkVariant value;
236 for (vtkIdType i = 0; i < n; ++i)
237 {
238 ar& value;
239 array.SetValue(i, value);
240 }
241}
242
243BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
244
245#endif
246// 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.
Definition: vtkStdString.h:105
String class that stores Unicode text.
static vtkUnicodeString from_utf8(const char *)
Constructs a string from a null-terminated sequence of UTF-8 encoded characters.
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 atomic type representing the union of many types.
Definition: vtkVariant.h:145
unsigned int GetType() const
Get the type of the variant.
bool IsValid() const
Get whether the variant value is valid.
#define VTK_DEPRECATED_IN_9_1_0(reason)
#define VTK_SHORT
Definition: vtkType.h:48
int vtkIdType
Definition: vtkType.h:332
#define VTK_UNSIGNED_INT
Definition: vtkType.h:51
#define VTK_LONG_LONG
Definition: vtkType.h:63
#define VTK_UNICODE_STRING
Definition: vtkType.h:71
#define VTK_DOUBLE
Definition: vtkType.h:55
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:47
#define VTK_UNSIGNED_SHORT
Definition: vtkType.h:49
#define VTK_INT
Definition: vtkType.h:50
#define VTK_STRING
Definition: vtkType.h:60
#define VTK_FLOAT
Definition: vtkType.h:54
#define VTK_CHAR
Definition: vtkType.h:45
#define VTK_UNSIGNED_LONG
Definition: vtkType.h:53
#define VTK_UNSIGNED_LONG_LONG
Definition: vtkType.h:64
#define VTK_LONG
Definition: vtkType.h:52
#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))