00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef BINARY_DATA_ARCHIVE_HPP_
00042 #define BINARY_DATA_ARCHIVE_HPP_
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00068 #include <cassert>
00069 #include <istream>
00070 #include <ostream>
00071 #include <streambuf>
00072 #include <cstring>
00073 #include <boost/serialization/serialization.hpp>
00074 #include <boost/serialization/is_bitwise_serializable.hpp>
00075 #include <boost/archive/detail/iserializer.hpp>
00076 #include <boost/archive/detail/oserializer.hpp>
00077 #include <boost/archive/archive_exception.hpp>
00078 #include <boost/config.hpp>
00079 #include <boost/mpl/bool.hpp>
00080
00081
00082 #include <boost/version.hpp>
00083
00084 namespace RTT
00085 {
00086 namespace mqueue
00087 {
00088
00095 class binary_data_iarchive
00096 {
00097 std::streambuf& m_sb;
00098 int data_read;
00099 public:
00100 typedef char Elem;
00101 typedef binary_data_iarchive Archive;
00102
00106 typedef boost::mpl::bool_<true> is_loading;
00110 typedef boost::mpl::bool_<false> is_saving;
00111
00116 binary_data_iarchive(std::streambuf& bsb) :
00117 m_sb(bsb), data_read(0)
00118 {
00119 }
00120
00125 binary_data_iarchive(std::istream& is) :
00126 m_sb(*is.rdbuf()), data_read(0)
00127 {
00128 }
00129
00134 unsigned int get_library_version() { return 0; }
00135
00141 void reset_object_address(const void * new_address, const void * old_address) {}
00142
00146 void delete_created_pointers() {}
00147
00153 template<class T>
00154 const boost::archive::detail::basic_pointer_iserializer *
00155 register_type(T * = NULL) {return 0;}
00156
00163 void load_object(
00164 void *t,
00165 const boost::archive::detail::basic_iserializer & bis
00166 ) {
00167 assert(false);
00168 }
00169
00174 template<class T>
00175 void load_override(T & t, BOOST_PFTO int){
00176 load_a_type(t, boost::mpl::bool_<boost::serialization::implementation_level<T>::value == boost::serialization::primitive_type>() );
00177
00178 }
00179
00185 #if 0
00186 void load_override(const boost::serialization::nvp<boost::serialization::collection_size_type> & t, int){
00187 size_t x=0;
00188 * this >> x;
00189 t.value() = boost::serialization::collection_size_type(x);
00190 }
00191 #endif
00192 template<class T>
00193 void load_override(const boost::serialization::nvp<T> & t, int){
00194 T& x(t.value());
00195 * this >> x;
00196 }
00197
00203 template<class T>
00204 void load_override(const boost::serialization::array<T> &t, int)
00205 {
00206 boost::serialization::array<T> tmp(t.address(), t.count());
00207 *this >> tmp;
00208 }
00209
00215 template<class T>
00216 binary_data_iarchive &operator>>(T &t){
00217 this->load_override(t, 0);
00218 return * this;
00219 }
00220
00226 template<class T>
00227 binary_data_iarchive &operator&(T &t){
00228 return this->operator>>(t);
00229 }
00230
00236 void load_binary(void *address, std::size_t count)
00237 {
00238
00239 std::streamsize s = count / sizeof(Elem);
00240 std::streamsize scount = m_sb.sgetn(
00241 static_cast<Elem *> (address), s);
00242 if (scount != static_cast<std::streamsize> (s))
00243 #if BOOST_VERSION >= 104400
00244 boost::serialization::throw_exception(
00245 boost::archive::archive_exception(
00246 boost::archive::archive_exception::input_stream_error));
00247 #else
00248 boost::serialization::throw_exception(
00249 boost::archive::archive_exception(
00250 boost::archive::archive_exception::stream_error));
00251 #endif
00252
00253 s = count % sizeof(Elem);
00254 if (0 < s)
00255 {
00256
00257
00258
00259
00260 Elem t;
00261 scount = m_sb.sgetn(&t, 1);
00262 if (scount != 1)
00263 #if BOOST_VERSION >= 104400
00264 boost::serialization::throw_exception(
00265 boost::archive::archive_exception(
00266 boost::archive::archive_exception::input_stream_error));
00267 #else
00268 boost::serialization::throw_exception(
00269 boost::archive::archive_exception(
00270 boost::archive::archive_exception::stream_error));
00271 #endif
00272 std::memcpy(static_cast<char*> (address) + (count - s), &t,
00273 s);
00274 }
00275 data_read += count;
00276 }
00277
00283 template<class T>
00284 binary_data_iarchive &load_a_type(T &t,boost::mpl::true_){
00285 load_binary(&t, sizeof(T));
00286 return *this;
00287 }
00288
00294 template<class T>
00295 binary_data_iarchive &load_a_type(T &t,boost::mpl::false_){
00296 #if BOOST_VERSION >= 104100
00297 boost::archive::detail::load_non_pointer_type<binary_data_iarchive>::load_only::invoke(*this,t);
00298 #else
00299 boost::archive::detail::load_non_pointer_type<binary_data_iarchive,T>::load_only::invoke(*this,t);
00300 #endif
00301 return *this;
00302 }
00303
00308 struct use_array_optimization {
00309 template <class T>
00310 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
00311 struct apply {
00312 typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable<T>::type type;
00313 };
00314 #else
00315 struct apply : public boost::serialization::is_bitwise_serializable<T> {};
00316 #endif
00317 };
00318
00322 template<class ValueType>
00323 void load_array(boost::serialization::array<ValueType>& a,
00324 unsigned int)
00325 {
00326 load_binary(a.address(), a.count()
00327 * sizeof(ValueType));
00328 }
00329
00333 int getArchiveSize() { return data_read; }
00334 };
00335
00341 class binary_data_oarchive
00342 {
00343 std::streambuf & m_sb;
00344 int data_written;
00345 bool mdo_save;
00346 public:
00347 typedef char Elem;
00351 typedef boost::mpl::bool_<false> is_loading;
00355 typedef boost::mpl::bool_<true> is_saving;
00356
00365 binary_data_oarchive(std::ostream& os,bool do_save = true) :
00366 m_sb(*os.rdbuf()), data_written(0), mdo_save(do_save)
00367 {
00368 }
00369
00378 binary_data_oarchive(std::streambuf& sb,bool do_save = true) :
00379 m_sb(sb), data_written(0), mdo_save(do_save)
00380 {
00381 }
00382
00387 unsigned int get_library_version() { return 0; }
00388
00394 template<class T>
00395 const boost::archive::detail::basic_pointer_iserializer *
00396 register_type(T * = NULL) {return 0;}
00397
00403 void save_object(
00404 const void *x,
00405 const boost::archive::detail::basic_oserializer & bos
00406 ) {
00407 assert(false);
00408
00409 }
00410
00416 template<class T>
00417 binary_data_oarchive &operator<<(T const &t){
00418 return save_a_type(t,boost::mpl::bool_< boost::serialization::implementation_level<T>::value == boost::serialization::primitive_type>() );
00419 }
00420
00426 template<class T>
00427 binary_data_oarchive &operator&(T const &t){
00428 return this->operator<<(t);
00429 }
00430
00436 inline void save_binary(const void *address, std::size_t count)
00437 {
00438
00439 count = (count + sizeof(Elem) - 1) / sizeof(Elem);
00440 if (mdo_save) {
00441 std::streamsize scount = m_sb.sputn(
00442 static_cast<const Elem *> (address), count);
00443 if (count != static_cast<std::size_t> (scount))
00444 #if BOOST_VERSION >= 104400
00445 boost::serialization::throw_exception(
00446 boost::archive::archive_exception(
00447 boost::archive::archive_exception::output_stream_error));
00448 #else
00449 boost::serialization::throw_exception(
00450 boost::archive::archive_exception(
00451 boost::archive::archive_exception::stream_error));
00452 #endif
00453 }
00454 data_written += count;
00455 }
00456
00462 template<class T>
00463 binary_data_oarchive &save_a_type(T const &t,boost::mpl::true_){
00464 save_binary(&t, sizeof(T));
00465 return *this;
00466 }
00467
00473 template<class T>
00474 binary_data_oarchive &save_a_type(T const &t,boost::mpl::false_){
00475 #if BOOST_VERSION >= 104100
00476 boost::archive::detail::save_non_pointer_type<binary_data_oarchive>::save_only::invoke(*this,t);
00477 #else
00478 boost::archive::detail::save_non_pointer_type<binary_data_oarchive,T>::save_only::invoke(*this,t);
00479 #endif
00480 return *this;
00481 }
00482
00487 struct use_array_optimization {
00488 template <class T>
00489 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
00490 struct apply {
00491 typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable<T>::type type;
00492 };
00493 #else
00494 struct apply : public boost::serialization::is_bitwise_serializable<T> {};
00495 #endif
00496 };
00497
00501 template<class ValueType>
00502 void save_array(boost::serialization::array<ValueType> const& a,
00503 unsigned int)
00504 {
00505 save_binary(a.address(), a.count()
00506 * sizeof(ValueType));
00507 }
00508
00512 int getArchiveSize() { return data_written; }
00513 };
00514 }
00515 }
00516
00517 #endif