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  */
32 #ifndef vtkVariantBoostSerialization_h
33 #define vtkVariantBoostSerialization_h
34 
35 #include "vtkSetGet.h"
36 #include "vtkType.h"
37 #include "vtkVariant.h"
38 #include "vtkVariantArray.h"
39 
40 // This include fixes header-ordering issues in Boost.Serialization
41 // prior to Boost 1.35.0.
42 #include <boost/archive/binary_oarchive.hpp>
43 
44 #include <boost/serialization/base_object.hpp>
45 #include <boost/serialization/export.hpp>
46 #include <boost/serialization/extended_type_info_no_rtti.hpp>
47 #include <boost/serialization/split_free.hpp>
48 
49 //----------------------------------------------------------------------------
50 // vtkStdString serialization code
51 //----------------------------------------------------------------------------
52 template<typename Archiver>
53 void serialize(Archiver& ar, vtkStdString& str,
54  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,
65  const unsigned int vtkNotUsed(version))
66 {
67  std::string utf8(str.utf8_str());
68  ar & utf8;
69 }
70 
71 template<typename Archiver>
72 void load(Archiver& ar, vtkUnicodeString& str,
73  const unsigned int vtkNotUsed(version))
74 {
75  std::string utf8;
76  ar & utf8;
77  str = vtkUnicodeString::from_utf8(utf8);
78 }
79 
80 BOOST_SERIALIZATION_SPLIT_FREE(vtkUnicodeString)
81 
82 
83 //----------------------------------------------------------------------------
84 // vtkVariant serialization code
85 //----------------------------------------------------------------------------
86 
87 template<typename Archiver>
88 void save(Archiver& ar, const vtkVariant& variant,
89  const unsigned int vtkNotUsed(version))
90 {
91  if (!variant.IsValid())
92  {
93  char null = 0;
94  ar & null;
95  return;
96  }
97 
98  // Output the type
99  char Type = variant.GetType();
100  ar & Type;
101 
102  // Output the value
103 #define VTK_VARIANT_SAVE(Value,Type,Function) \
104  case Value: \
105  { \
106  Type value = variant.Function(); \
107  ar & value; \
108  } \
109  return
110 
111  switch (Type)
112  {
115  VTK_VARIANT_SAVE(VTK_FLOAT,float,ToFloat);
116  VTK_VARIANT_SAVE(VTK_DOUBLE,double,ToDouble);
117  VTK_VARIANT_SAVE(VTK_CHAR,char,ToChar);
118  VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR,unsigned char,ToUnsignedChar);
119  VTK_VARIANT_SAVE(VTK_SHORT,short,ToShort);
120  VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT,unsigned short,ToUnsignedShort);
121  VTK_VARIANT_SAVE(VTK_INT,int,ToInt);
122  VTK_VARIANT_SAVE(VTK_UNSIGNED_INT,unsigned int,ToUnsignedInt);
123  VTK_VARIANT_SAVE(VTK_LONG,long,ToLong);
124  VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG,unsigned long,ToUnsignedLong);
125 #if defined(VTK_TYPE_USE___INT64)
126  VTK_VARIANT_SAVE(VTK___INT64,vtkTypeInt64,ToTypeInt64);
127  VTK_VARIANT_SAVE(VTK_UNSIGNED___INT64,vtkTypeUInt64,ToTypeUInt64);
128 #endif
129 #if defined(VTK_TYPE_USE_LONG_LONG)
130  VTK_VARIANT_SAVE(VTK_LONG_LONG,long long,ToLongLong);
131  VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG,unsigned long long,
132  ToUnsignedLongLong);
133 #endif
134  default:
135  cerr << "cannot serialize variant with type " << variant.GetType()
136  << '\n';
137  }
138 #undef VTK_VARIANT_SAVE
139 }
140 
141 template<typename Archiver>
142 void load(Archiver& ar, vtkVariant& variant,
143  const unsigned int vtkNotUsed(version))
144 {
145  char Type;
146  ar & Type;
147 
148 #define VTK_VARIANT_LOAD(Value,Type) \
149  case Value: \
150  { \
151  Type value; \
152  ar & value; \
153  variant = vtkVariant(value); \
154  } \
155  return
156 
157  switch (Type)
158  {
159  case 0: variant = vtkVariant(); return;
165  VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR,unsigned char);
167  VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT,unsigned short);
169  VTK_VARIANT_LOAD(VTK_UNSIGNED_INT,unsigned int);
171  VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG,unsigned long);
172 #if defined(VTK_TYPE_USE___INT64)
173  VTK_VARIANT_LOAD(VTK___INT64,vtkTypeInt64);
174  VTK_VARIANT_LOAD(VTK_UNSIGNED___INT64,vtkTypeUInt64);
175 #endif
176 #if defined(VTK_TYPE_USE_LONG_LONG)
177  VTK_VARIANT_LOAD(VTK_LONG_LONG,long long);
178  VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG,unsigned long long);
179 #endif
180  default:
181  cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
182  variant = vtkVariant();
183  }
184 #undef VTK_VARIANT_LOAD
185 }
186 
187 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
188 
189 //----------------------------------------------------------------------------
190 // vtkVariantArray serialization code
191 //----------------------------------------------------------------------------
192 
193 template<typename Archiver>
194 void save(Archiver& ar, const vtkVariantArray& c_array,
195  const unsigned int vtkNotUsed(version))
196 {
197  vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
198 
199  // Array name
201  if(array.GetName()!=NULL) name=array.GetName();
202  ar & name;
203 
204  // Array data
205  vtkIdType n = array.GetNumberOfTuples();
206  ar & n;
207  for (vtkIdType i = 0; i < n; ++i)
208  {
209  ar & array.GetValue(i);
210  }
211 }
212 
213 template<typename Archiver>
214 void load(Archiver& ar, vtkVariantArray& array,
215  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
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:46
#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:32
void serialize(Archiver &ar, vtkStdString &str, const unsigned int vtkNotUsed(version))
An array holding vtkVariants.
vtkIdType GetNumberOfTuples()
#define VTK_UNSIGNED_SHORT
Definition: vtkType.h:30
int vtkIdType
Definition: vtkType.h:275
#define VTK_UNICODE_STRING
Definition: vtkType.h:60
#define VTK_VARIANT_LOAD(Value, Type)
#define VTK_LONG_LONG
Definition: vtkType.h:45
#define VTK_UNSIGNED___INT64
Definition: vtkType.h:53
A atomic type representing the union of many types.
Definition: vtkVariant.h:78
#define VTK_STRING
Definition: vtkType.h:41
virtual void SetName(const char *)
#define VTK_DOUBLE
Definition: vtkType.h:36
#define VTK_FLOAT
Definition: vtkType.h:35
static vtkUnicodeString from_utf8(const char *)
const char * utf8_str() const
#define VTK_SHORT
Definition: vtkType.h:29
virtual char * GetName()
#define VTK_CHAR
Definition: vtkType.h:26
#define VTK_LONG
Definition: vtkType.h:33
void SetValue(vtkIdType id, vtkVariant value)
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
#define VTK_UNSIGNED_CHAR
Definition: vtkType.h:28
virtual void SetNumberOfTuples(vtkIdType number)
#define VTK_UNSIGNED_LONG
Definition: vtkType.h:34
vtkVariant & GetValue(vtkIdType id) const
#define VTK_UNSIGNED_LONG_LONG
Definition: vtkType.h:46
#define VTK_INT
Definition: vtkType.h:31
#define VTK___INT64
Definition: vtkType.h:49
String class that stores Unicode text.