OpenStructure
binary_data_sink.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_SINK_HH
20 #define OST_IO_BINARY_DATA_SINK_HH
21 /*
22  Author: Marco Biasini
23 
24  minimalistic binary serialization interface
25  */
26 
27 #include <iostream>
28 
29 #include <ost/io/module_config.hh>
30 
31 namespace ost { namespace io {
32 
34 public:
35  BinaryDataSink(std::ostream& stream)
36  : stream_(stream) {
37  }
38  template <typename SERIALIZABLE>
39  BinaryDataSink& operator << (SERIALIZABLE object) {
40  Serialize(*this, object);
41  return *this;
42  }
43  template <typename SERIALIZABLE>
44  BinaryDataSink& operator & (SERIALIZABLE object) {
45  return this->operator<<(object);
46  }
47  std::ostream& Stream() {
48  return stream_;
49  }
50 
51  bool IsSource() { return false; }
52 private:
53  std::ostream& stream_;
54 };
55 
56 namespace detail {
57 
58 template <bool B, typename T>
59 struct SerializeWriteHelper;
60 
61 template <typename T>
62 struct SerializeWriteHelper<false, T> {
63  SerializeWriteHelper(BinaryDataSink& sink, T& value)
64  {
65  value.Serialize(sink);
66  }
67 };
68 
69 template <typename T>
70 struct SerializeWriteHelper<true, T> {
71  SerializeWriteHelper(BinaryDataSink& sink, T& value)
72  {
73  sink.Stream().write(reinterpret_cast<char*>(&value), sizeof(T));
74  }
75 };
76 }
77 
78 template <typename T>
79 void Serialize(BinaryDataSink& sink, const T& value)
80 {
81  detail::SerializeWriteHelper<boost::is_integral<T>::value ||
82  boost::is_floating_point<T>::value,
83  T>(sink, const_cast<T&>(value));
84 }
85 
86 
87 inline void RawSerialize(BinaryDataSink& sink, char* value, size_t size)
88 {
89  sink.Stream().write(reinterpret_cast<char*>(value), size);
90 }
91 
92 inline void Serialize(BinaryDataSink& sink, const String& str) {
93  size_t str_len=str.length();
94  sink.Stream().write(reinterpret_cast<char*>(&str_len), sizeof(size_t));
95  sink.Stream().write(str.c_str(), str.length());
96 }
97 }}
98 
99 #endif