ug4
binary_buffer_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-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__UG__binary_buffer_impl__
34 #define __H__UG__binary_buffer_impl__
35 
36 #include <cassert>
37 #include <cstring>
38 #include "vector_util.h"
39 
40 namespace ug
41 {
42 
43 inline size_t BinaryBuffer::capacity() const
44 {
45  return m_data.size();
46 }
47 
48 inline size_t BinaryBuffer::read_pos() const
49 {
50  return m_readPos;
51 }
52 
53 inline size_t BinaryBuffer::write_pos() const
54 {
55  return m_writePos;
56 }
57 
58 inline void BinaryBuffer::read(char* buf, size_t size)
59 {
60 // make sure that we only read valid data
61  assert(m_readPos + size <= m_data.size());
62  assert(m_readPos + size <= m_writePos);
63 
64 // copy the data
65  memcpy(buf, &m_data.front() + m_readPos, size);
66 
67 // adjust read-pos
68  m_readPos += size;
69 }
70 
71 inline void BinaryBuffer::write(const char* buf, size_t size)
72 {
73 // make sure that our data buffer is big enough
74  if(m_writePos + size > m_data.size()){
75  // if the size of the data to be written exceeds the current
76  // data-buffers size, then we increase the data buffer by
77  // the size of the now written data.
78  // If not, then we'll increase the data-buffer by simply doubling
79  // its memory, to minimize reallocation costs (this is just a
80  // heuristic)
81  if(size > m_data.size())
82  m_data.resize(m_data.size() + size);
83  else
84  m_data.resize(m_data.size() * 2);
85  }
86 
87 // copy the data
88  memcpy(&m_data.front() + m_writePos, buf, size);
89 
90 // adjust write pos
91  m_writePos += size;
92 }
93 
94 inline char* BinaryBuffer::buffer()
95 {
96  return GetDataPtr(m_data);
97 }
98 
99 inline bool BinaryBuffer::eof()
100 {
101  return m_readPos >= m_writePos;
102 }
103 
104 }// end of namespace
105 
106 #endif
size_t read_pos() const
returns the current read-pos (in bytes)
Definition: binary_buffer_impl.h:48
char * buffer()
returns the raw buffer pointer or NULL if the buffer is empty (capacity() == 0)
Definition: binary_buffer_impl.h:94
size_t capacity() const
returns the capacity (reserved memory) of the buffer
Definition: binary_buffer_impl.h:43
size_t m_readPos
Definition: binary_buffer.h:106
void read(char *buf, size_t size)
reads data of the given size (in bytes)
Definition: binary_buffer_impl.h:58
size_t m_writePos
Definition: binary_buffer.h:107
size_t write_pos() const
returns the current write-pos (in bytes)
Definition: binary_buffer_impl.h:53
std::vector< char > m_data
Definition: binary_buffer.h:105
bool eof()
returns true if the read-position reached the write-position
Definition: binary_buffer_impl.h:99
void write(const char *buf, size_t size)
writes data of the given size (in bytes)
Definition: binary_buffer_impl.h:71
T * GetDataPtr(std::vector< T > &v)
Returns a pointer to the array which is managed by the std::vector.
Definition: vector_util.h:51
the ug namespace