OpenStructure
Loading...
Searching...
No Matches
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-2020 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
30
31namespace ost { namespace io {
32
34public:
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; }
52private:
53 std::ostream& stream_;
54};
55
56namespace detail {
57
58template <bool B, typename T>
59struct SerializeWriteHelper;
60
61template <typename T>
62struct SerializeWriteHelper<false, T> {
63 SerializeWriteHelper(BinaryDataSink& sink, T& value)
64 {
65 value.Serialize(sink);
66 }
67};
68
69template <typename T>
70struct SerializeWriteHelper<true, T> {
71 SerializeWriteHelper(BinaryDataSink& sink, T& value)
72 {
73 sink.Stream().write(reinterpret_cast<char*>(&value), sizeof(T));
74 }
75};
76}
77
78template <typename T>
79void 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
87inline void RawSerialize(BinaryDataSink& sink, char* value, size_t size)
88{
89 sink.Stream().write(reinterpret_cast<char*>(value), size);
90}
91
92inline 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
BinaryDataSink & operator<<(SERIALIZABLE object)
BinaryDataSink(std::ostream &stream)
BinaryDataSink & operator&(SERIALIZABLE object)
std::string String
Definition base.hh:54
void Serialize(BinaryDataSink &sink, const T &value)
void RawSerialize(BinaryDataSink &sink, char *value, size_t size)
Definition base.dox:1