Advertisement
Guest User

attempt at boost serialization variables_map and variable_value

a guest
Nov 11th, 2010
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <boost/program_options/variables_map.hpp>
  4. #include <boost/serialization/map.hpp>
  5.  
  6. #include <iostream>
  7.  
  8. namespace boost {
  9.    namespace serialization {
  10.       template<class Archive>
  11.       inline void save_construct_data(Archive & ar,
  12.                                       const boost::program_options::variable_value* t,
  13.                                       const unsigned int file_version) {
  14.          std::cerr << __PRETTY_FUNCTION__ << std::endl;
  15.          // save data required to construct instance
  16.          ar << t->defaulted();
  17.          ar << t->value();
  18.       }
  19.      
  20.       template<class Archive>
  21.       inline void load_construct_data(Archive & ar,
  22.                                       boost::program_options::variable_value* t,
  23.                                       const unsigned int file_version) {
  24.          std::cerr << __PRETTY_FUNCTION__ << std::endl;
  25.          // retrieve data from archive required to construct new instance
  26.          bool defaulted;
  27.          boost::any value;
  28.          ar >> defaulted;
  29.          ar >> value;
  30.          std::cerr << value.type().name();
  31.          // invoke inplace constructor to initialize instance of my_class
  32.          ::new(t)boost::program_options::variable_value(value, defaulted);
  33.       }
  34.      
  35.       template<class Archive>
  36.       inline void save(Archive & ar,
  37.                        const boost::program_options::variable_value &t,
  38.                        const unsigned int /* file_version */) {
  39.          ar & t.value(); // TODO(jayen): figure out how to serialize boost::any
  40.          ar & t.defaulted();
  41.       }
  42.       template<class Archive>
  43.       inline void load(Archive & ar,
  44.                        boost::program_options::variable_value &t,
  45.                        const unsigned int /* file_version */) {
  46.          boost::any v;
  47.          ar & v;
  48.          bool defaulted;
  49.          ar & defaulted;
  50.          boost::program_options::variable_value vv(v, defaulted);
  51.          memcpy(&t, &vv, sizeof(vv));
  52.       }
  53.       template<class Archive>
  54.       inline void serialize(Archive & ar,
  55.                             boost::program_options::variable_value &t,
  56.                             const unsigned int file_version) {
  57.          // we should have nothing to do, as it's all in the constructor, but
  58.          // boost serialization of pairs uses the default constructor and not
  59.          // the above stuff.
  60.          boost::serialization::split_free(ar, t, file_version);
  61.       }
  62.      
  63.       // just use the superclass's serialize function
  64.       template<class Archive>
  65.       inline void serialize(Archive & ar,
  66.                             boost::program_options::variables_map &t,
  67.                             const unsigned int file_version) {
  68.          serialize(ar, (std::map<std::string,
  69.                         boost::program_options::variable_value> &)t,
  70.                    file_version);
  71.       }
  72.    }  // serialization
  73. }  // namespace boost
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement