VTK
dox/Infovis/Boost/vtkVariantBoostSerialization.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkVariantBoostSerialization.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 /*
00016  * Copyright (C) 2008 The Trustees of Indiana University.
00017  * Use, modification and distribution is subject to the Boost Software
00018  * License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
00019  */
00032 #ifndef __vtkVariantBoostSerialization_h
00033 #define __vtkVariantBoostSerialization_h
00034 
00035 #include "vtkSetGet.h"
00036 #include "vtkType.h"
00037 #include "vtkVariant.h"
00038 #include "vtkVariantArray.h"
00039 
00040 // This include fixes header-ordering issues in Boost.Serialization
00041 // prior to Boost 1.35.0.
00042 #include <boost/archive/binary_oarchive.hpp>
00043 
00044 #include <boost/serialization/base_object.hpp>
00045 #include <boost/serialization/export.hpp>
00046 #include <boost/serialization/extended_type_info_no_rtti.hpp>
00047 #include <boost/serialization/split_free.hpp>
00048 
00049 //----------------------------------------------------------------------------
00050 // vtkStdString serialization code
00051 //----------------------------------------------------------------------------
00052 template<typename Archiver>
00053 void serialize(Archiver& ar, vtkStdString& str,
00054                const unsigned int vtkNotUsed(version))
00055 {
00056   ar & boost::serialization::base_object<std::string>(str);
00057 }
00058 
00059 //----------------------------------------------------------------------------
00060 // vtkUnicodeString serialization code
00061 //----------------------------------------------------------------------------
00062 
00063 template<typename Archiver>
00064 void save(Archiver& ar, const vtkUnicodeString& str,
00065           const unsigned int vtkNotUsed(version))
00066 {
00067   std::string utf8(str.utf8_str());
00068   ar & utf8;
00069 }
00070 
00071 template<typename Archiver>
00072 void load(Archiver& ar, vtkUnicodeString& str,
00073           const unsigned int vtkNotUsed(version))
00074 {
00075   std::string utf8;
00076   ar & utf8;
00077   str = vtkUnicodeString::from_utf8(utf8);
00078 }
00079 
00080 BOOST_SERIALIZATION_SPLIT_FREE(vtkUnicodeString)
00081 
00082 
00083 //----------------------------------------------------------------------------
00084 // vtkVariant serialization code
00085 //----------------------------------------------------------------------------
00086 
00087 template<typename Archiver>
00088 void save(Archiver& ar, const vtkVariant& variant,
00089           const unsigned int vtkNotUsed(version))
00090 {
00091   if (!variant.IsValid())
00092     {
00093     char null = 0;
00094     ar & null;
00095     return;
00096     }
00097 
00098   // Output the type
00099   char Type = variant.GetType();
00100   ar & Type;
00101 
00102   // Output the value
00103 #define VTK_VARIANT_SAVE(Value,Type,Function)   \
00104    case Value:                                  \
00105      {                                          \
00106        Type value = variant.Function();         \
00107        ar & value;                              \
00108      }                                          \
00109      return
00110 
00111   switch (Type)
00112     {
00113     VTK_VARIANT_SAVE(VTK_STRING,vtkStdString,ToString);
00114     VTK_VARIANT_SAVE(VTK_UNICODE_STRING,vtkUnicodeString,ToUnicodeString);
00115     VTK_VARIANT_SAVE(VTK_FLOAT,float,ToFloat);
00116     VTK_VARIANT_SAVE(VTK_DOUBLE,double,ToDouble);
00117     VTK_VARIANT_SAVE(VTK_CHAR,char,ToChar);
00118     VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR,unsigned char,ToUnsignedChar);
00119     VTK_VARIANT_SAVE(VTK_SHORT,short,ToShort);
00120     VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT,unsigned short,ToUnsignedShort);
00121     VTK_VARIANT_SAVE(VTK_INT,int,ToInt);
00122     VTK_VARIANT_SAVE(VTK_UNSIGNED_INT,unsigned int,ToUnsignedInt);
00123     VTK_VARIANT_SAVE(VTK_LONG,long,ToLong);
00124     VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG,unsigned long,ToUnsignedLong);
00125 #if defined(VTK_TYPE_USE___INT64)
00126     VTK_VARIANT_SAVE(VTK___INT64,vtkTypeInt64,ToTypeInt64);
00127     VTK_VARIANT_SAVE(VTK_UNSIGNED___INT64,vtkTypeUInt64,ToTypeUInt64);
00128 #endif
00129 #if defined(VTK_TYPE_USE_LONG_LONG)
00130     VTK_VARIANT_SAVE(VTK_LONG_LONG,long long,ToLongLong);
00131     VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG,unsigned long long,
00132                      ToUnsignedLongLong);
00133 #endif
00134     default:
00135       cerr << "cannot serialize variant with type " << variant.GetType()
00136            << '\n';
00137     }
00138 #undef VTK_VARIANT_SAVE
00139 }
00140 
00141 template<typename Archiver>
00142 void load(Archiver& ar, vtkVariant& variant,
00143           const unsigned int vtkNotUsed(version))
00144 {
00145   char Type;
00146   ar & Type;
00147 
00148 #define VTK_VARIANT_LOAD(Value,Type)            \
00149     case Value:                                 \
00150       {                                         \
00151         Type value;                             \
00152         ar & value;                             \
00153         variant = vtkVariant(value);            \
00154       }                                         \
00155       return
00156 
00157   switch (Type)
00158     {
00159     case 0: variant = vtkVariant(); return;
00160     VTK_VARIANT_LOAD(VTK_STRING,vtkStdString);
00161     VTK_VARIANT_LOAD(VTK_UNICODE_STRING,vtkUnicodeString);
00162     VTK_VARIANT_LOAD(VTK_FLOAT,float);
00163     VTK_VARIANT_LOAD(VTK_DOUBLE,double);
00164     VTK_VARIANT_LOAD(VTK_CHAR,char);
00165     VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR,unsigned char);
00166     VTK_VARIANT_LOAD(VTK_SHORT,short);
00167     VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT,unsigned short);
00168     VTK_VARIANT_LOAD(VTK_INT,int);
00169     VTK_VARIANT_LOAD(VTK_UNSIGNED_INT,unsigned int);
00170     VTK_VARIANT_LOAD(VTK_LONG,long);
00171     VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG,unsigned long);
00172 #if defined(VTK_TYPE_USE___INT64)
00173     VTK_VARIANT_LOAD(VTK___INT64,vtkTypeInt64);
00174     VTK_VARIANT_LOAD(VTK_UNSIGNED___INT64,vtkTypeUInt64);
00175 #endif
00176 #if defined(VTK_TYPE_USE_LONG_LONG)
00177     VTK_VARIANT_LOAD(VTK_LONG_LONG,long long);
00178     VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG,unsigned long long);
00179 #endif
00180     default:
00181       cerr << "cannot deserialize variant with type " << static_cast<unsigned int>(Type) << '\n';
00182       variant = vtkVariant();
00183     }
00184 #undef VTK_VARIANT_LOAD
00185 }
00186 
00187 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
00188 
00189 //----------------------------------------------------------------------------
00190 // vtkVariantArray serialization code
00191 //----------------------------------------------------------------------------
00192 
00193 template<typename Archiver>
00194 void save(Archiver& ar, const vtkVariantArray& c_array,
00195           const unsigned int vtkNotUsed(version))
00196 {
00197   vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
00198 
00199   // Array name
00200   vtkStdString name;
00201   if(array.GetName()!=NULL) name=array.GetName();
00202   ar & name;
00203 
00204   // Array data
00205   vtkIdType n = array.GetNumberOfTuples();
00206   ar & n;
00207   for (vtkIdType i = 0; i < n; ++i)
00208     {
00209     ar & array.GetValue(i);
00210     }
00211 }
00212 
00213 template<typename Archiver>
00214 void load(Archiver& ar, vtkVariantArray& array,
00215           const unsigned int vtkNotUsed(version))
00216 {
00217   // Array name
00218   vtkStdString name;
00219   ar & name;
00220   array.SetName(name.c_str());
00221 
00222   if(name.empty())
00223     {
00224     array.SetName(0);
00225     }
00226   else
00227     {
00228     array.SetName(name.c_str());
00229     }
00230 
00231   // Array data
00232   vtkIdType n;
00233   ar & n;
00234   array.SetNumberOfTuples(n);
00235   vtkVariant value;
00236   for (vtkIdType i = 0; i < n; ++i)
00237     {
00238     ar & value;
00239     array.SetValue(i, value);
00240     }
00241 }
00242 
00243 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
00244 
00245 #endif
00246 // VTK-HeaderTest-Exclude: vtkVariantBoostSerialization.h