bitset.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. * \file bitset.hpp
  3. * \brief Provides Boost.Serialization support for std::bitset
  4. * \author Brian Ravnsgaard Riis
  5. * \author Kenneth Riddile
  6. * \date 16.09.2004, updated 04.03.2009
  7. * \copyright 2004 Brian Ravnsgaard Riis
  8. * \license Boost Software License 1.0
  9. */
  10. #ifndef BOOST_SERIALIZATION_BITSET_HPP
  11. #define BOOST_SERIALIZATION_BITSET_HPP
  12. // MS compatible compilers support #pragma once
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <bitset>
  17. #include <cstddef> // size_t
  18. #include <boost/config.hpp>
  19. #include <boost/serialization/split_free.hpp>
  20. #include <boost/serialization/string.hpp>
  21. #include <boost/serialization/nvp.hpp>
  22. namespace boost{
  23. namespace serialization{
  24. template <class Archive, std::size_t size>
  25. inline void save(
  26. Archive & ar,
  27. std::bitset<size> const & t,
  28. const unsigned int /* version */
  29. ){
  30. const std::string bits = t.template to_string<
  31. std::string::value_type,
  32. std::string::traits_type,
  33. std::string::allocator_type
  34. >();
  35. ar << BOOST_SERIALIZATION_NVP( bits );
  36. }
  37. template <class Archive, std::size_t size>
  38. inline void load(
  39. Archive & ar,
  40. std::bitset<size> & t,
  41. const unsigned int /* version */
  42. ){
  43. std::string bits;
  44. ar >> BOOST_SERIALIZATION_NVP( bits );
  45. t = std::bitset<size>(bits);
  46. }
  47. template <class Archive, std::size_t size>
  48. inline void serialize(
  49. Archive & ar,
  50. std::bitset<size> & t,
  51. const unsigned int version
  52. ){
  53. boost::serialization::split_free( ar, t, version );
  54. }
  55. // don't track bitsets since that would trigger tracking
  56. // all over the program - which probably would be a surprise.
  57. // also, tracking would be hard to implement since, we're
  58. // serialization a representation of the data rather than
  59. // the data itself.
  60. template <std::size_t size>
  61. struct tracking_level<std::bitset<size> >
  62. : mpl::int_<track_never> {} ;
  63. } //serialization
  64. } //boost
  65. #endif // BOOST_SERIALIZATION_BITSET_HPP