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