fix doc example typo
[boost.git] / boost / mpi / detail / packed_iprimitive.hpp
blob03d1c80782a2279f8d685cfedd87e9036a40f91f
1 // (C) Copyright 2005 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_PACKED_IPRIMITIVE_HPP
10 #define BOOST_MPI_PACKED_IPRIMITIVE_HPP
12 #include <boost/mpi/config.hpp>
13 #include <iostream>
14 #include <cstddef> // size_t
15 #include <boost/config.hpp>
16 #include <boost/mpi/datatype.hpp>
17 #include <boost/mpi/exception.hpp>
18 #include <boost/assert.hpp>
19 #include <boost/serialization/array.hpp>
20 #include <boost/serialization/detail/get_data.hpp>
21 #include <vector>
22 #include <boost/mpi/allocator.hpp>
24 namespace boost { namespace mpi {
26 /// deserialization using MPI_Unpack
28 class BOOST_MPI_DECL packed_iprimitive
30 public:
31 /// the type of the buffer from which the data is unpacked upon deserialization
32 typedef std::vector<char, allocator<char> > buffer_type;
34 packed_iprimitive(buffer_type & b, MPI_Comm const & comm, int position = 0)
35 : buffer_(b),
36 comm(comm),
37 position(position)
41 void* address ()
43 return &buffer_[0];
46 void const* address () const
48 return &buffer_[0];
51 const std::size_t& size() const
53 return size_ = buffer_.size();
56 void resize(std::size_t s)
58 buffer_.resize(s);
61 void load_binary(void *address, std::size_t count)
63 load_impl(address,MPI_BYTE,count);
66 // fast saving of arrays of fundamental types
67 template<class T>
68 void load_array(serialization::array<T> const& x, unsigned int /* file_version */)
70 if (x.count())
71 load_impl(x.address(), get_mpi_datatype(*x.address()), x.count());
74 template<class T>
75 void load(serialization::array<T> const& x)
77 load_array(x,0u);
80 typedef is_mpi_datatype<mpl::_1> use_array_optimization;
82 // default saving of primitives.
83 template<class T>
84 void load( T & t)
86 load_impl(&t, get_mpi_datatype(t), 1);
89 void load( std::string & s)
91 unsigned int l;
92 load(l);
93 // borland de-allocator fixup
94 #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
95 if(NULL != s.data())
96 #endif
97 s.resize(l);
98 // note breaking a rule here - could be a problem on some platform
99 load_impl(const_cast<char *>(s.data()),MPI_CHAR,l);
102 private:
104 void load_impl(void * p, MPI_Datatype t, int l)
106 BOOST_MPI_CHECK_RESULT(MPI_Unpack,
107 (const_cast<char*>(boost::serialization::detail::get_data(buffer_)), buffer_.size(), &position, p, l, t, comm));
110 buffer_type & buffer_;
111 mutable std::size_t size_;
112 MPI_Comm comm;
113 int position;
116 } } // end namespace boost::mpi
118 #endif // BOOST_MPI_PACKED_IPRIMITIVE_HPP