binary_buffer_iprimitive.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // (C) Copyright 2005-2007 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_BINARY_BUFFER_IPRIMITIVE_HPP
  7. #define BOOST_MPI_BINARY_BUFFER_IPRIMITIVE_HPP
  8. #include <mpi.h>
  9. #include <iostream>
  10. #include <cstddef> // size_t
  11. #include <boost/config.hpp>
  12. #include <boost/mpi/exception.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/mpl/assert.hpp>
  15. #include <boost/serialization/array.hpp>
  16. #include <boost/serialization/is_bitwise_serializable.hpp>
  17. #include <vector>
  18. #include <boost/mpi/allocator.hpp>
  19. #include <cstring> // for memcpy
  20. #include <cassert>
  21. namespace boost { namespace mpi {
  22. /// deserialization using MPI_Unpack
  23. class BOOST_MPI_DECL binary_buffer_iprimitive
  24. {
  25. public:
  26. /// the type of the buffer from which the data is unpacked upon deserialization
  27. typedef std::vector<char, allocator<char> > buffer_type;
  28. binary_buffer_iprimitive(buffer_type & b, MPI_Comm const &, int position = 0)
  29. : buffer_(b),
  30. position(position)
  31. {
  32. }
  33. void* address ()
  34. {
  35. return detail::c_data(buffer_);
  36. }
  37. void const* address () const
  38. {
  39. return detail::c_data(buffer_);
  40. }
  41. const std::size_t& size() const
  42. {
  43. return size_ = buffer_.size();
  44. }
  45. void resize(std::size_t s)
  46. {
  47. buffer_.resize(s);
  48. }
  49. void load_binary(void *address, std::size_t count)
  50. {
  51. load_impl(address,count);
  52. }
  53. // fast saving of arrays of fundamental types
  54. template<class T>
  55. void load_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
  56. {
  57. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  58. if (x.count())
  59. load_impl(x.address(), sizeof(T)*x.count());
  60. }
  61. typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
  62. template<class T>
  63. void load(serialization::array_wrapper<T> const& x)
  64. {
  65. load_array(x,0u);
  66. }
  67. // default saving of primitives.
  68. template<class T>
  69. void load( T & t)
  70. {
  71. BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
  72. load_impl(&t, sizeof(T));
  73. }
  74. template<class CharType>
  75. void load(std::basic_string<CharType> & s)
  76. {
  77. unsigned int l;
  78. load(l);
  79. // borland de-allocator fixup
  80. #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
  81. if(NULL != s.data())
  82. #endif
  83. s.resize(l);
  84. // note breaking a rule here - could be a problem on some platform
  85. load_impl(const_cast<char *>(s.data()),l);
  86. }
  87. private:
  88. void load_impl(void * p, int l)
  89. {
  90. assert(position+l<=static_cast<int>(buffer_.size()));
  91. if (l)
  92. std::memcpy(p,&buffer_[position],l);
  93. position += l;
  94. }
  95. buffer_type & buffer_;
  96. mutable std::size_t size_;
  97. int position;
  98. };
  99. } } // end namespace boost::mpi
  100. #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP