VTK
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 vtkUnicodeString& str, const unsigned int vtkNotUsed(version))
65 {
66  std::string utf8(str.utf8_str());
67  ar& utf8;
68 }
69 
70 template <typename Archiver>
71 void load(Archiver& ar, vtkUnicodeString& str, const unsigned int vtkNotUsed(version))
72 {
73  std::string utf8;
74  ar& utf8;
75  str = vtkUnicodeString::from_utf8(utf8);
76 }
77 
78 BOOST_SERIALIZATION_SPLIT_FREE(vtkUnicodeString)
79 
80 //----------------------------------------------------------------------------
81 // vtkVariant serialization code
82 //----------------------------------------------------------------------------
83 
84 template <typename Archiver>
85 void save(Archiver& ar, const vtkVariant& variant, const unsigned int vtkNotUsed(version))
86 {
87  if (!variant.IsValid())
88  {
89  char null = 0;
90  ar& null;
91  return;
92  }
93 
94  // Output the type
95  char Type = variant.GetType();
96  ar& Type;
97 
98  // Output the value
99 #define VTK_VARIANT_SAVE(Value, Type, Function) \
100  case Value: \
101  { \
102  Type value = variant.Function(); \
103  ar& value; \
104  } \
105  return
106 
107  switch (Type)
108  {
111  VTK_VARIANT_SAVE(VTK_FLOAT, float, ToFloat);
112  VTK_VARIANT_SAVE(VTK_DOUBLE, double, ToDouble);
113  VTK_VARIANT_SAVE(VTK_CHAR, char, ToChar);
114  VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR, unsigned char, ToUnsignedChar);
115  VTK_VARIANT_SAVE(VTK_SHORT, short, ToShort);
116  VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT, unsigned short, ToUnsignedShort);
117  VTK_VARIANT_SAVE(VTK_INT, int, ToInt);
118  VTK_VARIANT_SAVE(VTK_UNSIGNED_INT, unsigned int, ToUnsignedInt);
119  VTK_VARIANT_SAVE(VTK_LONG, long, ToLong);
120  VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG, unsigned long, ToUnsignedLong);
121  VTK_VARIANT_SAVE(VTK_LONG_LONG, long long, ToLongLong);
122  VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG, unsigned long long, ToUnsignedLongLong);
123  default:
124  cerr << "cannot serialize variant with type " << variant.GetType() << '\n';
125  }
126 #undef VTK_VARIANT_SAVE
127 }
128 
129 template <typename Archiver>
130 void load(Archiver& ar, vtkVariant& variant, const unsigned int vtkNotUsed(version))
131 {
132  char Type;
133  ar& Type;
134 
135 #define VTK_VARIANT_LOAD(Value, Type) \
136  case Value: \
137  { \
138  Type value; \
139  ar& value; \
140  variant = vtkVariant(value); \
141  } \
142  return
143 
144  switch (Type)
145  {
146  case 0:
147  variant = vtkVariant();
148  return;
151  VTK_VARIANT_LOAD(VTK_FLOAT, float);
152  VTK_VARIANT_LOAD(VTK_DOUBLE, double);
153  VTK_VARIANT_LOAD(VTK_CHAR, char);
154  VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR, unsigned char);
155  VTK_VARIANT_LOAD(VTK_SHORT, short);
156  VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT, unsigned short);
158  VTK_VARIANT_LOAD(VTK_UNSIGNED_INT, unsigned int);
159  VTK_VARIANT_LOAD(VTK_LONG, long);
160  VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG, unsigned long);
161  VTK_VARIANT_LOAD(VTK_LONG_LONG, long long);
162  VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG, unsigned long long);
163  default:
164  cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
165  variant = vtkVariant();
166  }
167 #undef VTK_VARIANT_LOAD
168 }
169 
170 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
171 
172 //----------------------------------------------------------------------------
173 // vtkVariantArray serialization code
174 //----------------------------------------------------------------------------
175 
176 template <typename Archiver>
177 void save(Archiver& ar, const vtkVariantArray& c_array, const unsigned int vtkNotUsed(version))
178 {
179  vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
180 
181  // Array name
183  if (array.GetName() != nullptr)
184  name = array.GetName();
185  ar& name;
186 
187  // Array data
188  vtkIdType n = array.GetNumberOfTuples();
189  ar& n;
190  for (vtkIdType i = 0; i < n; ++i)
191  {
192  ar& array.GetValue(i);
193  }
194 }
195 
196 template <typename Archiver>
197 void load(Archiver& ar, vtkVariantArray& array, const unsigned int vtkNotUsed(version))
198 {
199  // Array name
201  ar& name;
202  array.SetName(name.c_str());
203 
204  if (name.empty())
205  {
206  array.SetName(0);
207  }
208  else
209  {
210  array.SetName(name.c_str());
211  }
212 
213  // Array data
214  vtkIdType n;
215  ar& n;
216  array.SetNumberOfTuples(n);
218  for (vtkIdType i = 0; i < n; ++i)
219  {
220  ar& value;
221  array.SetValue(i, value);
222  }
223 }
224 
225 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
226 
227 #endif
228 // VTK-HeaderTest-Exclude: vtkVariantBoostSerialization.h
vtkAbstractArray::GetName
virtual char * GetName()
vtkVariant.h
VTK_UNSIGNED_LONG_LONG
#define VTK_UNSIGNED_LONG_LONG
Definition: vtkType.h:62
VTK_VARIANT_LOAD
#define VTK_VARIANT_LOAD(Value, Type)
vtkX3D::value
Definition: vtkX3D.h:226
vtkIdType
int vtkIdType
Definition: vtkType.h:343
VTK_UNSIGNED_INT
#define VTK_UNSIGNED_INT
Definition: vtkType.h:49
vtkVariantArray::GetValue
vtkVariant & GetValue(vtkIdType id) const
Get the data at a particular index.
VTK_UNICODE_STRING
#define VTK_UNICODE_STRING
Definition: vtkType.h:79
VTK_UNSIGNED_SHORT
#define VTK_UNSIGNED_SHORT
Definition: vtkType.h:47
vtkVariant::IsValid
bool IsValid() const
Get whether the variant value is valid.
save
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:64
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_VARIANT_SAVE
#define VTK_VARIANT_SAVE(Value, Type, Function)
vtkVariantArray
An array holding vtkVariants.
Definition: vtkVariantArray.h:49
detail::Type
Type
Definition: TestQtCommon.h:16
VTK_LONG_LONG
#define VTK_LONG_LONG
Definition: vtkType.h:61
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, vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:71
VTK_STRING
#define VTK_STRING
Definition: vtkType.h:58
VTK_CHAR
#define VTK_CHAR
Definition: vtkType.h:43
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:69
VTK_FLOAT
#define VTK_FLOAT
Definition: vtkType.h:52
vtkUnicodeString::utf8_str
const char * utf8_str() const
Returns the sequence as a null-terminated sequence of UTF-8 encoded characters.
VTK_DOUBLE
#define VTK_DOUBLE
Definition: vtkType.h:53
vtkVariant
A atomic type representing the union of many types.
Definition: vtkVariant.h:65
VTK_UNSIGNED_CHAR
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:45
VTK_LONG
#define VTK_LONG
Definition: vtkType.h:50
vtkX3D::name
Definition: vtkX3D.h:225
VTK_UNSIGNED_LONG
#define VTK_UNSIGNED_LONG
Definition: vtkType.h:51
vtkX3D::string
Definition: vtkX3D.h:496
VTK_SHORT
#define VTK_SHORT
Definition: vtkType.h:46
vtkVariantArray.h
vtkX3D::version
Definition: vtkX3D.h:532
VTK_INT
#define VTK_INT
Definition: vtkType.h:48
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
vtkAbstractArray::SetName
virtual void SetName(const char *)
Set/get array's name.