mpi_datatype_oarchive.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // (C) Copyright 2005 Matthias Troyer
  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: Matthias Troyer
  6. #ifndef BOOST_MPI_DETAIL_MPI_DATATYPE_OARCHIVE_HPP
  7. #define BOOST_MPI_DETAIL_MPI_DATATYPE_OARCHIVE_HPP
  8. #include <boost/type_traits/is_enum.hpp>
  9. #include <boost/mpl/bool.hpp>
  10. #include <boost/archive/detail/oserializer.hpp>
  11. #include <boost/archive/detail/auto_link_archive.hpp>
  12. #include <boost/archive/basic_archive.hpp>
  13. #include <boost/mpi/detail/ignore_skeleton_oarchive.hpp>
  14. #include <boost/mpi/detail/mpi_datatype_primitive.hpp>
  15. #include <boost/mpi/datatype_fwd.hpp>
  16. #include <boost/mpl/assert.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/integer.hpp>
  19. #include <boost/archive/detail/register_archive.hpp>
  20. namespace boost { namespace mpi { namespace detail {
  21. // an archive wrapper that stores only the data members but not the
  22. // special types defined by the serialization library
  23. // to define the data skeletons (classes, pointers, container sizes, ...)
  24. class mpi_datatype_oarchive
  25. : public mpi_datatype_primitive,
  26. public ignore_skeleton_oarchive<mpi_datatype_oarchive>
  27. {
  28. public:
  29. template <class T>
  30. mpi_datatype_oarchive(const T& x)
  31. : mpi_datatype_primitive(&x) // register address
  32. {
  33. BOOST_MPL_ASSERT((is_mpi_datatype<T>));
  34. *this << x; // serialize the object
  35. }
  36. template<class T>
  37. void save_override(T const& t)
  38. {
  39. save_enum(t,boost::is_enum<T>());
  40. }
  41. template<class T>
  42. void save_enum(T const& t, mpl::false_)
  43. {
  44. ignore_skeleton_oarchive<mpi_datatype_oarchive>::save_override(t);
  45. }
  46. template<class T>
  47. void save_enum(T const& t, mpl::true_)
  48. {
  49. // select the right sized integer for the enum
  50. typedef typename boost::uint_t<8*sizeof(T)>::least int_type;
  51. BOOST_STATIC_ASSERT((sizeof(T)==sizeof(int_type)));
  52. this->save(*reinterpret_cast<int_type const*>(&t));
  53. }
  54. };
  55. } } } // end namespace boost::mpi::detail
  56. // required by export
  57. BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::detail::mpi_datatype_oarchive)
  58. BOOST_SERIALIZATION_REGISTER_ARCHIVE(boost::mpi::detail::ignore_skeleton_oarchive<boost::mpi::detail::mpi_datatype_oarchive>)
  59. BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(boost::mpi::detail::mpi_datatype_oarchive)
  60. #endif // BOOST_MPI_DETAIL_MPI_DATATYPE_OARCHIVE_HPP