1 // (C) Copyright 2005-2007 Matthias Troyer
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Matthias Troyer
9 #ifndef BOOST_MPI_BINARY_BUFFER_IPRIMITIVE_HPP
10 #define BOOST_MPI_BINARY_BUFFER_IPRIMITIVE_HPP
14 #include <cstddef> // size_t
15 #include <boost/config.hpp>
16 #include <boost/mpi/exception.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/mpl/assert.hpp>
19 #include <boost/serialization/array.hpp>
20 #include <boost/serialization/is_bitwise_serializable.hpp>
22 #include <boost/mpi/allocator.hpp>
23 #include <cstring> // for memcpy
26 namespace boost { namespace mpi {
28 /// deserialization using MPI_Unpack
30 class BOOST_MPI_DECL binary_buffer_iprimitive
33 /// the type of the buffer from which the data is unpacked upon deserialization
34 typedef std::vector<char, allocator<char> > buffer_type;
36 binary_buffer_iprimitive(buffer_type & b, MPI_Comm const &, int position = 0)
44 return &buffer_.front();
47 void const* address () const
49 return &buffer_.front();
52 const std::size_t& size() const
54 return size_ = buffer_.size();
57 void resize(std::size_t s)
62 void load_binary(void *address, std::size_t count)
64 load_impl(address,count);
67 // fast saving of arrays of fundamental types
69 void load_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
71 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
73 load_impl(x.address(), sizeof(T)*x.count());
76 typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
79 void load(serialization::array_wrapper<T> const& x)
84 // default saving of primitives.
88 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
89 load_impl(&t, sizeof(T));
92 template<class CharType>
93 void load(std::basic_string<CharType> & s)
97 // borland de-allocator fixup
98 #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
102 // note breaking a rule here - could be a problem on some platform
103 load_impl(const_cast<char *>(s.data()),l);
108 void load_impl(void * p, int l)
110 assert(position+l<=static_cast<int>(buffer_.size()));
112 std::memcpy(p,&buffer_[position],l);
116 buffer_type & buffer_;
117 mutable std::size_t size_;
121 } } // end namespace boost::mpi
123 #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP