VTK
dox/Common/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 // vtkVariant serialization code
00061 //----------------------------------------------------------------------------
00062 
00063 template<typename Archiver>
00064 void save(Archiver& ar, const vtkVariant& variant,
00065           const unsigned int vtkNotUsed(version))
00066 {
00067   if (!variant.IsValid())
00068     {
00069     char null = 0;
00070     ar & null;
00071     return;
00072     }
00073 
00074   // Output the type
00075   char Type = variant.GetType();
00076   ar & Type;
00077 
00078   // Output the value
00079 #define VTK_VARIANT_SAVE(Value,Type,Function)   \
00080    case Value:                                  \
00081      {                                          \
00082        Type value = variant.Function();         \
00083        ar & value;                              \
00084      }                                          \
00085      return
00086 
00087   switch (Type)
00088     {
00089     VTK_VARIANT_SAVE(VTK_STRING,vtkStdString,ToString);
00090     VTK_VARIANT_SAVE(VTK_FLOAT,float,ToFloat);
00091     VTK_VARIANT_SAVE(VTK_DOUBLE,double,ToDouble);
00092     VTK_VARIANT_SAVE(VTK_CHAR,char,ToChar);
00093     VTK_VARIANT_SAVE(VTK_UNSIGNED_CHAR,unsigned char,ToUnsignedChar);
00094     VTK_VARIANT_SAVE(VTK_SHORT,short,ToShort);
00095     VTK_VARIANT_SAVE(VTK_UNSIGNED_SHORT,unsigned short,ToUnsignedShort);
00096     VTK_VARIANT_SAVE(VTK_INT,int,ToInt);
00097     VTK_VARIANT_SAVE(VTK_UNSIGNED_INT,unsigned int,ToUnsignedInt);
00098     VTK_VARIANT_SAVE(VTK_LONG,long,ToLong);
00099     VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG,unsigned long,ToUnsignedLong);
00100 #if defined(VTK_TYPE_USE___INT64)
00101     VTK_VARIANT_SAVE(VTK___INT64,vtkTypeInt64,ToTypeInt64);
00102     VTK_VARIANT_SAVE(VTK_UNSIGNED___INT64,vtkTypeUInt64,ToTypeUInt64);
00103 #endif
00104 #if defined(VTK_TYPE_USE_LONG_LONG)
00105     VTK_VARIANT_SAVE(VTK_LONG_LONG,long long,ToLongLong);
00106     VTK_VARIANT_SAVE(VTK_UNSIGNED_LONG_LONG,unsigned long long,
00107                      ToUnsignedLongLong);
00108 #endif
00109     default:
00110       cerr << "cannot serialize variant with type " << variant.GetType()
00111            << '\n';
00112     }
00113 #undef VTK_VARIANT_SAVE
00114 }
00115 
00116 template<typename Archiver>
00117 void load(Archiver& ar, vtkVariant& variant,
00118           const unsigned int vtkNotUsed(version))
00119 {
00120   char Type;
00121   ar & Type;
00122 
00123 #define VTK_VARIANT_LOAD(Value,Type)            \
00124     case Value:                                 \
00125       {                                         \
00126         Type value;                             \
00127         ar & value;                             \
00128         variant = vtkVariant(value);            \
00129       }                                         \
00130       return
00131 
00132   switch (Type)
00133     {
00134     case 0: variant = vtkVariant(); return;
00135     VTK_VARIANT_LOAD(VTK_STRING,vtkStdString);
00136     VTK_VARIANT_LOAD(VTK_FLOAT,float);
00137     VTK_VARIANT_LOAD(VTK_DOUBLE,double);
00138     VTK_VARIANT_LOAD(VTK_CHAR,char);
00139     VTK_VARIANT_LOAD(VTK_UNSIGNED_CHAR,unsigned char);
00140     VTK_VARIANT_LOAD(VTK_SHORT,short);
00141     VTK_VARIANT_LOAD(VTK_UNSIGNED_SHORT,unsigned short);
00142     VTK_VARIANT_LOAD(VTK_INT,int);
00143     VTK_VARIANT_LOAD(VTK_UNSIGNED_INT,unsigned int);
00144     VTK_VARIANT_LOAD(VTK_LONG,long);
00145     VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG,unsigned long);
00146 #if defined(VTK_TYPE_USE___INT64)
00147     VTK_VARIANT_LOAD(VTK___INT64,vtkTypeInt64);
00148     VTK_VARIANT_LOAD(VTK_UNSIGNED___INT64,vtkTypeUInt64);
00149 #endif
00150 #if defined(VTK_TYPE_USE_LONG_LONG)
00151     VTK_VARIANT_LOAD(VTK_LONG_LONG,long long);
00152     VTK_VARIANT_LOAD(VTK_UNSIGNED_LONG_LONG,unsigned long long);
00153 #endif
00154     default:
00155       cerr << "cannot deserialize variant with type " << Type << '\n';
00156       variant = vtkVariant();
00157     }
00158 #undef VTK_VARIANT_LOAD
00159 }
00160 
00161 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariant)
00162 
00163 //----------------------------------------------------------------------------
00164 // vtkVariantArray serialization code
00165 //----------------------------------------------------------------------------
00166 
00167 template<typename Archiver>
00168 void save(Archiver& ar, const vtkVariantArray& c_array,
00169           const unsigned int vtkNotUsed(version))
00170 {
00171   vtkVariantArray& array = const_cast<vtkVariantArray&>(c_array);
00172 
00173   // Array name
00174   vtkStdString name;
00175   if(array.GetName()!=NULL) name=array.GetName();
00176   ar & name;
00177 
00178   // Array data
00179   vtkIdType n = array.GetNumberOfTuples();
00180   ar & n;
00181   for (vtkIdType i = 0; i < n; ++i)
00182     {
00183     ar & array.GetValue(i);
00184     }
00185 }
00186 
00187 template<typename Archiver>
00188 void load(Archiver& ar, vtkVariantArray& array,
00189           const unsigned int vtkNotUsed(version))
00190 {
00191   // Array name
00192   vtkStdString name;
00193   ar & name;
00194   array.SetName(name.c_str());
00195 
00196   if(name.empty())
00197     {
00198     array.SetName(0);
00199     }
00200   else
00201     {
00202     array.SetName(name.c_str());
00203     }
00204 
00205   // Array data
00206   vtkIdType n;
00207   ar & n;
00208   array.SetNumberOfTuples(n);
00209   vtkVariant value;
00210   for (vtkIdType i = 0; i < n; ++i)
00211     {
00212     ar & value;
00213     array.SetValue(i, value);
00214     }
00215 }
00216 
00217 BOOST_SERIALIZATION_SPLIT_FREE(vtkVariantArray)
00218 
00219 #endif