File indexing completed on 2025-01-18 09:40:57
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef BOOST_MPI_BINARY_BUFFER_IPRIMITIVE_HPP
0010 #define BOOST_MPI_BINARY_BUFFER_IPRIMITIVE_HPP
0011
0012 #include <mpi.h>
0013 #include <iostream>
0014 #include <cstddef> // size_t
0015 #include <boost/config.hpp>
0016 #include <boost/mpi/exception.hpp>
0017 #include <boost/assert.hpp>
0018 #include <boost/mpl/assert.hpp>
0019 #include <boost/serialization/array.hpp>
0020 #include <boost/serialization/is_bitwise_serializable.hpp>
0021 #include <vector>
0022 #include <boost/mpi/allocator.hpp>
0023 #include <cstring> // for memcpy
0024 #include <cassert>
0025
0026 namespace boost { namespace mpi {
0027
0028
0029
0030 class BOOST_MPI_DECL binary_buffer_iprimitive
0031 {
0032 public:
0033
0034 typedef std::vector<char, allocator<char> > buffer_type;
0035
0036 binary_buffer_iprimitive(buffer_type & b, MPI_Comm const &, int position = 0)
0037 : buffer_(b),
0038 position(position)
0039 {
0040 }
0041
0042 void* address ()
0043 {
0044 return detail::c_data(buffer_);
0045 }
0046
0047 void const* address () const
0048 {
0049 return detail::c_data(buffer_);
0050 }
0051
0052 const std::size_t& size() const
0053 {
0054 return size_ = buffer_.size();
0055 }
0056
0057 void resize(std::size_t s)
0058 {
0059 buffer_.resize(s);
0060 }
0061
0062 void load_binary(void *address, std::size_t count)
0063 {
0064 load_impl(address,count);
0065 }
0066
0067
0068 template<class T>
0069 void load_array(serialization::array_wrapper<T> const& x, unsigned int )
0070 {
0071 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
0072 if (x.count())
0073 load_impl(x.address(), sizeof(T)*x.count());
0074 }
0075
0076 typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
0077
0078 template<class T>
0079 void load(serialization::array_wrapper<T> const& x)
0080 {
0081 load_array(x,0u);
0082 }
0083
0084
0085 template<class T>
0086 void load( T & t)
0087 {
0088 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
0089 load_impl(&t, sizeof(T));
0090 }
0091
0092 template<class CharType>
0093 void load(std::basic_string<CharType> & s)
0094 {
0095 unsigned int l;
0096 load(l);
0097
0098 #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
0099 if(NULL != s.data())
0100 #endif
0101 s.resize(l);
0102
0103 load_impl(const_cast<CharType *>(s.data()), l*sizeof(CharType));
0104 }
0105
0106 private:
0107
0108 void load_impl(void * p, int l)
0109 {
0110 assert(position+l<=static_cast<int>(buffer_.size()));
0111 if (l)
0112 std::memcpy(p,&buffer_[position],l);
0113 position += l;
0114 }
0115
0116 buffer_type & buffer_;
0117 mutable std::size_t size_;
0118 int position;
0119 };
0120
0121 } }
0122
0123 #endif