Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
binary_stream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__UTIL__BINARY_STREAM__
34#define __H__UTIL__BINARY_STREAM__
35
36#include <iostream>
37#include <vector>
38#include "vector_util.h"
39
40namespace ug
41{
42
45
48
57class BinaryStreamBuffer : public std::streambuf
58{
59 public:
61
63 void clear();
64
66 void reserve(size_t newSize);
67
69
70 void resize(size_t newSize);
71
73 void reset();
74
76 inline char_type* buffer() {return reinterpret_cast<char_type*>(GetDataPtr(m_dataBuf));}
77
78 inline const char_type* buffer() const {return reinterpret_cast<const char_type*>(GetDataPtr(m_dataBuf));}
79
81 size_t size() const;
82
84 void write_jump(size_t jumpSize);
85
87 void read_jump(size_t jumpSize);
88
90 size_t get_read_pos() const;
91
92 // implementation of virtual std::streambuf methods.
93 virtual int_type overflow(int_type c = traits_type::eof());
94
95 virtual int_type underflow();
96
97 protected:
99 inline char_type* end() {return buffer() + m_dataBuf.size();}
100 inline const char_type* end() const {return buffer() + m_dataBuf.size();}
101
102 protected:
103 std::vector<unsigned char> m_dataBuf;
104};
105
108class BinaryStream : public std::iostream
109{
110 public:
111 BinaryStream() : std::iostream(&m_streamBuf) {}
112 BinaryStream(size_t newSize) : std::iostream(&m_streamBuf) {resize(newSize);}
113
114 inline void clear() //< clears the data and resets the read and write positions.
115 {m_streamBuf.clear();}
116
117 inline void resize(size_t newSize) //< resizes the data-buffer but does not alter read and write positions.
118 {m_streamBuf.resize(newSize);}
119
120 inline void reset() //< set read- and write-positions to the start of the buffer.
121 {m_streamBuf.reset();}
122
123 inline void* buffer() //< returns a pointer to the front of the buffer.
124 {return m_streamBuf.buffer();}
125
126 inline size_t size() const//< returns the size of the buffer in bytes.
127 {return m_streamBuf.size();}
128
129 inline void write_jump(size_t jumpSize) //< advances the write-pointer by jumpSize bytes
130 {m_streamBuf.write_jump(jumpSize);}
131
132 inline void read_jump(size_t jumpSize) //< advances the read-pointer by jumpSize bytes
133 {m_streamBuf.read_jump(jumpSize);}
134
135 inline size_t read_pos() const
136 {return m_streamBuf.get_read_pos();}
137
139
142 inline bool can_read_more()
143 {return m_streamBuf.get_read_pos() < size();}
144 protected:
146};
147
148// end group ugbase_common_io
150
151}// end of namespace
152
153#endif
A special version of a std::streambuf, writes data directly into a buffer that is accessible at any t...
Definition binary_stream.h:58
virtual int_type underflow()
Definition binary_stream.cpp:131
const char_type * end() const
Definition binary_stream.h:100
void write_jump(size_t jumpSize)
advances the write-pointer by jumpSize bytes
Definition binary_stream.cpp:104
BinaryStreamBuffer()
Definition binary_stream.cpp:40
size_t get_read_pos() const
returns the read-position
Definition binary_stream.cpp:114
void resize(size_t newSize)
resizes the data-buffer but does not alter read and write positions.
Definition binary_stream.cpp:75
void clear()
clears the data and resets the read and write positions.
Definition binary_stream.cpp:45
const char_type * buffer() const
Definition binary_stream.h:78
char_type * buffer()
returns a pointer to the front of the buffer or NULL if the buffer is empty.
Definition binary_stream.h:76
std::vector< unsigned char > m_dataBuf
Definition binary_stream.h:103
virtual int_type overflow(int_type c=traits_type::eof())
Definition binary_stream.cpp:119
char_type * end()
returns pointer to the first entry behind the buffer.
Definition binary_stream.h:99
size_t size() const
returns the size of the buffer in bytes.
Definition binary_stream.cpp:50
void read_jump(size_t jumpSize)
advances the read-pointer by jumpSize bytes
Definition binary_stream.cpp:109
void reserve(size_t newSize)
Similar to resize, however, doesn't alter the read-area.
Definition binary_stream.cpp:57
void reset()
set read- and write-positions to the start of the buffer.
Definition binary_stream.cpp:96
a specialzation of std::iostream, that uses a
Definition binary_stream.h:109
BinaryStream(size_t newSize)
Definition binary_stream.h:112
BinaryStream()
Definition binary_stream.h:111
size_t read_pos() const
Definition binary_stream.h:135
bool can_read_more()
returns true if there is more data left to read.
Definition binary_stream.h:142
size_t size() const
Definition binary_stream.h:126
void write_jump(size_t jumpSize)
Definition binary_stream.h:129
void read_jump(size_t jumpSize)
Definition binary_stream.h:132
void resize(size_t newSize)
Definition binary_stream.h:117
void reset()
Definition binary_stream.h:120
void * buffer()
Definition binary_stream.h:123
void clear()
Definition binary_stream.h:114
BinaryStreamBuffer m_streamBuf
Definition binary_stream.h:145
T * GetDataPtr(std::vector< T > &v)
Returns a pointer to the array which is managed by the std::vector.
Definition vector_util.h:51
Definition smart_pointer.h:814
the ug namespace