unsafe_serialize.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2006 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. // This file contains the "unsafe_serialize" routine, which transforms
  8. // types they may not be serializable (such as void*) into
  9. // serializable equivalents.
  10. #ifndef BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP
  11. #define BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP
  12. #include <boost/mpi/datatype.hpp>
  13. #include <boost/serialization/is_bitwise_serializable.hpp>
  14. #include <boost/mpl/bool.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/cstdint.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/type_traits.hpp>
  19. #include <utility>
  20. BOOST_IS_BITWISE_SERIALIZABLE(void*)
  21. namespace boost { namespace mpi {
  22. template<> struct is_mpi_datatype<void*> : mpl::true_ { };
  23. } } // end namespace boost::mpi
  24. namespace boost {
  25. typedef mpl::if_c<(sizeof(int) == sizeof(void*)),
  26. int,
  27. mpl::if_c<(sizeof(long) == sizeof(void*)),
  28. long,
  29. mpl::if_c<(sizeof(void*) <= sizeof(boost::intmax_t)),
  30. boost::intmax_t,
  31. void>::type
  32. >::type
  33. >::type ptr_serialize_type;
  34. BOOST_STATIC_ASSERT ((!boost::is_void<ptr_serialize_type>::value));
  35. template<typename T> inline T& unsafe_serialize(T& x) { return x; }
  36. inline ptr_serialize_type& unsafe_serialize(void*& x)
  37. { return reinterpret_cast<ptr_serialize_type&>(x); }
  38. // Force Boost.MPI to serialize a void* like a ptr_serialize_type
  39. namespace mpi {
  40. template<> inline MPI_Datatype get_mpi_datatype<void*>(void* const& x)
  41. {
  42. return get_mpi_datatype<ptr_serialize_type>();
  43. }
  44. }
  45. template<typename T, typename U>
  46. struct unsafe_pair
  47. {
  48. unsafe_pair() { }
  49. unsafe_pair(const T& t, const U& u) : first(t), second(u) { }
  50. unsafe_pair(const std::pair<T, U>& p) : first(p.first), second(p.second) { }
  51. T first;
  52. U second;
  53. template<typename Archiver>
  54. void serialize(Archiver& ar, const unsigned /*version*/)
  55. {
  56. ar & unsafe_serialize(first) & unsafe_serialize(second);
  57. }
  58. };
  59. template<typename T, typename U>
  60. bool operator<(unsafe_pair<T,U> const& x, unsafe_pair<T,U> const& y)
  61. {
  62. return std::make_pair(x.first, x.second) <
  63. std::make_pair(y.first, y.second);
  64. }
  65. } // end namespace boost
  66. #endif // BOOST_PROPERTY_MAP_UNSAFE_SERIALIZE_HPP