VTK  9.0.20210616
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 //----------------------------------------------------------------------------
53 template <typename Archiver>
54 void 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 
63 template <typename Archiver>
64 void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))
65 {
66  ar& str;
67 }
68 
69 template <typename Archiver>
71  "Use void save(Archiver& ar, const std::string& str, const unsigned int vtkNotUsed(version))")
72 void 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 
78 template <typename Archiver>
79 void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))
80 {
81  std::string utf8;
82  ar& utf8;
83  str = utf8;
84 }
85 
86 template <typename Archiver>
88  "Use void load(Archiver& ar, std::string& str, const unsigned int vtkNotUsed(version))")
89 void load(Archiver& ar, vtkUnicodeString& str, const unsigned int vtkNotUsed(version))
90 {
91  std::string utf8;
92  load(ar, utf8);
93  str = vtkUnicodeString::from_utf8(utf8);
94 }
95 
96 BOOST_SERIALIZATION_SPLIT_FREE(vtkUnicodeString)
97 
98 //----------------------------------------------------------------------------
99 // vtkVariant serialization code
100 //----------------------------------------------------------------------------
101 
102 template <typename Archiver>
103 void 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 
147 template <typename Archiver>
148 void 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;
169  VTK_VARIANT_LOAD(VTK_FLOAT, float);
170  VTK_VARIANT_LOAD(VTK_DOUBLE, double);
171  VTK_VARIANT_LOAD(VTK_CHAR, char);
172  VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR, unsigned char);
173  VTK_VARIANT_LOAD(VTK_SHORT, short);
174  VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT, unsigned short);
176  VTK_VARIANT_LOAD(VTK_UNSIGNED_INT, unsigned int);
177  VTK_VARIANT_LOAD(VTK_LONG, long);
178  VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG, unsigned long);
179  VTK_VARIANT_LOAD(VTK_LONG_LONG, long 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 
188 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
189 
190 //----------------------------------------------------------------------------
191 // vtkVariantArray serialization code
192 //----------------------------------------------------------------------------
193 
194 template <typename Archiver>
195 void 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
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 
214 template <typename Archiver>
215 void load(Archiver& ar, vtkVariantArray& array, const unsigned int vtkNotUsed(version))
216 {
217  // Array 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);
236  for (vtkIdType i = 0; i < n; ++i)
237  {
238  ar& value;
239  array.SetValue(i, value);
240  }
241 }
242 
243 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
244 
245 #endif
246 // VTK-HeaderTest-Exclude: vtkVariantBoostSerialization.h
vtkAbstractArray::GetName
virtual char * GetName()
Set/get array's name.
vtkVariant.h
VTK_UNSIGNED_LONG_LONG
#define VTK_UNSIGNED_LONG_LONG
Definition: vtkType.h:64
VTK_VARIANT_LOAD
#define VTK_VARIANT_LOAD(Value, Type)
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkIdType
int vtkIdType
Definition: vtkType.h:332
VTK_UNSIGNED_INT
#define VTK_UNSIGNED_INT
Definition: vtkType.h:51
vtkVariantArray::GetValue
vtkVariant & GetValue(vtkIdType id) const
Get the data at a particular index.
VTK_UNICODE_STRING
#define VTK_UNICODE_STRING
Definition: vtkType.h:71
VTK_UNSIGNED_SHORT
#define VTK_UNSIGNED_SHORT
Definition: vtkType.h:49
vtkVariant::IsValid
bool IsValid() const
Get whether the variant value is valid.
serialize
void serialize(Archiver &ar, vtkStdString &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:54
vtkVariant::GetType
unsigned int GetType() const
Get the type of the variant.
VTK_DEPRECATED_IN_9_1_0
#define VTK_DEPRECATED_IN_9_1_0(reason)
Definition: vtkDeprecation.h:117
VTK_VARIANT_SAVE
#define VTK_VARIANT_SAVE(Value, Type, Function)
vtkVariantArray
An array holding vtkVariants.
Definition: vtkVariantArray.h:50
detail::Type
Type
Definition: TestQtCommon.h:19
VTK_LONG_LONG
#define VTK_LONG_LONG
Definition: vtkType.h:63
vtkUnicodeString::from_utf8
static vtkUnicodeString from_utf8(const char *)
Constructs a string from a null-terminated sequence of UTF-8 encoded characters.
vtkVariantArray::SetNumberOfTuples
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
vtkAbstractArray::GetNumberOfTuples
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
Definition: vtkAbstractArray.h:172
vtkType.h
load
void load(Archiver &ar, std::string &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:79
VTK_STRING
#define VTK_STRING
Definition: vtkType.h:60
VTK_CHAR
#define VTK_CHAR
Definition: vtkType.h:45
vtkVariantArray::SetValue
void SetValue(vtkIdType id, vtkVariant value)
Set the data at a particular index.
vtkUnicodeString
String class that stores Unicode text.
Definition: vtkUnicodeString.h:72
save
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:64
VTK_FLOAT
#define VTK_FLOAT
Definition: vtkType.h:54
VTK_DOUBLE
#define VTK_DOUBLE
Definition: vtkType.h:55
vtkVariant
A atomic type representing the union of many types.
Definition: vtkVariant.h:66
VTK_UNSIGNED_CHAR
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:47
VTK_LONG
#define VTK_LONG
Definition: vtkType.h:52
vtkX3D::name
@ name
Definition: vtkX3D.h:225
VTK_UNSIGNED_LONG
#define VTK_UNSIGNED_LONG
Definition: vtkType.h:53
vtkX3D::string
@ string
Definition: vtkX3D.h:496
VTK_SHORT
#define VTK_SHORT
Definition: vtkType.h:48
vtkVariantArray.h
vtkX3D::version
@ version
Definition: vtkX3D.h:532
VTK_INT
#define VTK_INT
Definition: vtkType.h:50
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:36
vtkAbstractArray::SetName
virtual void SetName(const char *)
Set/get array's name.