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_OPRIMITIVE_HPP
10 #define BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP
14 #include <cstddef> // size_t
15 #include <boost/config.hpp>
17 #include <boost/serialization/array.hpp>
18 #include <boost/serialization/is_bitwise_serializable.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/mpl/assert.hpp>
22 #include <boost/mpi/allocator.hpp>
23 #include <boost/mpl/always.hpp>
24 #include <boost/type_traits/remove_const.hpp>
26 namespace boost { namespace mpi {
28 /// serialization using binary copy into a buffer
30 class BOOST_MPI_DECL binary_buffer_oprimitive
33 /// the type of the buffer into which the data is packed upon serialization
34 typedef std::vector<char, allocator<char> > buffer_type;
36 binary_buffer_oprimitive(buffer_type & b, MPI_Comm const &)
41 void const * address() const
43 return &buffer_.front();
46 const std::size_t& size() const
48 return size_ = buffer_.size();
51 void save_binary(void const *address, std::size_t count)
53 save_impl(address,count);
56 // fast saving of arrays
58 void save_array(serialization::array_wrapper<T> const& x, unsigned int /* file_version */)
61 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
63 save_impl(x.address(), x.count()*sizeof(T));
67 void save(serialization::array_wrapper<T> const& x)
72 typedef serialization::is_bitwise_serializable<mpl::_1> use_array_optimization;
74 // default saving of primitives.
76 void save(const T & t)
78 BOOST_MPL_ASSERT((serialization::is_bitwise_serializable<BOOST_DEDUCED_TYPENAME remove_const<T>::type>));
79 save_impl(&t, sizeof(T));
82 template<class CharType>
83 void save(const std::basic_string<CharType> &s)
85 unsigned int l = static_cast<unsigned int>(s.size());
87 save_impl(s.data(),s.size());
92 void save_impl(void const * p, int l)
94 char const* ptr = reinterpret_cast<char const*>(p);
95 buffer_.insert(buffer_.end(),ptr,ptr+l);
99 mutable std::size_t size_;
102 } } // end namespace boost::mpi
104 #endif // BOOST_MPI_BINARY_BUFFER_OPRIMITIVE_HPP