OpenStructure
binary_data_source.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_IO_BINARY_DATA_SOURCE_HH
20 #define OST_IO_BINARY_DATA_SOURCE_HH
21 /*
22  Author: Marco Biasini
23 
24  minimalistic binary serialization interface
25  */
26 
27 #include <iostream>
28 #include <boost/type_traits/is_floating_point.hpp>
29 #include <boost/type_traits/is_integral.hpp>
30 
31 #include <ost/io/module_config.hh>
32 namespace ost { namespace io {
33 
35 public:
36  BinaryDataSource(std::istream& stream)
37  : stream_(stream) {
38  }
39  template <typename SERIALIZABLE>
40  BinaryDataSource& operator >> (SERIALIZABLE& object) {
41  Serialize(*this, object);
42  return *this;
43  }
44  template <typename SERIALIZABLE>
45  BinaryDataSource& operator & (SERIALIZABLE& object) {
46  return this->operator>>(object);
47  }
48  std::istream& Stream() {
49  return stream_;
50  }
51 
52  bool IsSource() { return true; }
53 private:
54  std::istream& stream_;
55 };
56 
57 namespace detail {
58 
59 template <bool B, typename T>
60 struct SerializeHelper;
61 
62 template <typename T>
63 struct SerializeHelper<false, T> {
64  SerializeHelper(BinaryDataSource& source, T& value)
65  {
66  value.Serialize(source);
67  }
68 };
69 
70 template <typename T>
71 struct SerializeHelper<true, T> {
72  SerializeHelper(BinaryDataSource& source, T& value)
73  {
74  source.Stream().read(reinterpret_cast<char*>(&value), sizeof(T));
75  }
76 };
77 }
78 
79 template <typename T>
80 void Serialize(BinaryDataSource& source, T& value)
81 {
82  detail::SerializeHelper<boost::is_integral<T>::value ||
83  boost::is_floating_point<T>::value,
84  T>(source, value);
85 }
86 
87 inline void RawSerialize(BinaryDataSource& sink,
88  char* value, size_t size)
89 {
90  sink.Stream().read(value, size);
91 }
92 
93 inline void Serialize(BinaryDataSource& source, String& str)
94 {
95  static char tmp_buf[1024];
96  size_t str_len;
97  source.Stream().read(reinterpret_cast<char*>(&str_len), sizeof(size_t));
98  source.Stream().read(tmp_buf, str_len);
99  str.reserve(str_len+1);
100  str.assign(tmp_buf, str_len);
101 }
102 }}
103 
104 #endif