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