Thrill  0.1
buffer_reader.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/buffer_reader.hpp
3  *
4  * Look at the Doxygen below....
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2013-2015 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #pragma once
14 #ifndef THRILL_NET_BUFFER_READER_HEADER
15 #define THRILL_NET_BUFFER_READER_HEADER
16 
20 
21 #include <algorithm>
22 #include <string>
23 
24 namespace thrill {
25 namespace net {
26 
27 //! \addtogroup net_layer
28 //! \{
29 
30 /*!
31  * BufferReader represents a BufferRef with an additional cursor with
32  * which the memory can be read incrementally.
33  */
35  : public BufferRef,
36  public common::ItemReaderToolsBase<BufferReader>
37 {
38 private:
39  //! Current read cursor
40  size_t cursor_ = 0;
41 
42 public:
43  //! \name Construction
44  //! \{
45 
46  //! Constructor, assign memory area from BinaryBuilder.
47  BufferReader(const BufferRef& br) // NOLINT
48  : BufferRef(br)
49  { }
50 
51  //! Constructor, assign memory area from pointer and length.
52  BufferReader(const void* data, size_t n)
53  : BufferRef(data, n)
54  { }
55 
56  //! Constructor, assign memory area from string, does NOT copy.
57  explicit BufferReader(const std::string& str)
58  : BufferRef(str)
59  { }
60 
61  //! \}
62 
63  //! \name Size Accessors
64  //! \{
65 
66  //! Return the current read cursor.
67  size_t cursor() const {
68  return cursor_;
69  }
70 
71  //! Return the number of bytes still available at the cursor.
72  bool available(size_t size) const {
73  return cursor_ + size <= size_;
74  }
75 
76  //! Return true if the cursor is at the end of the buffer.
77  bool empty() const {
78  return cursor_ == size_;
79  }
80 
81  size_t Size() const {
82  return size_;
83  }
84 
85  //! Indicates if the reader was initialized with a nullptr and size 0
86  bool IsNull() const {
87  return data_ == nullptr;
88  }
89 
90  //! \}
91 
92  //! \name Cursor Movement and Checks
93  //! \{
94 
95  //! Reset the read cursor.
97  cursor_ = 0;
98  return *this;
99  }
100 
101  //! Throws a std::underflow_error unless n bytes are available at the
102  //! cursor.
103  void CheckAvailable(size_t n) const {
104  if (!available(n))
105  throw std::underflow_error("BufferReader underrun");
106  }
107 
108  //! Advance the cursor given number of bytes without reading them.
109  BufferReader& Skip(size_t n) {
110  CheckAvailable(n);
111  cursor_ += n;
112 
113  return *this;
114  }
115 
116  //! \}
117 
118  //! \name Cursor Reading Methods
119  //! \{
120 
121  //! Fetch a number of unstructured bytes from the buffer, advancing the
122  //! cursor.
123  BufferReader& Read(void* outdata, size_t datalen) {
124  CheckAvailable(datalen);
125 
126  Byte* coutdata = reinterpret_cast<Byte*>(outdata);
127  std::copy(data_ + cursor_, data_ + cursor_ + datalen, coutdata);
128  cursor_ += datalen;
129 
130  return *this;
131  }
132 
133  //! Fetch a number of unstructured bytes from the buffer as std::string,
134  //! advancing the cursor.
135  std::string Read(size_t datalen) {
136  CheckAvailable(datalen);
137  std::string out(
138  reinterpret_cast<const char*>(data_ + cursor_), datalen);
139  cursor_ += datalen;
140  return out;
141  }
142 
143  //! Fetch a single item of the template type Type from the buffer,
144  //! advancing the cursor. Be careful with implicit type conversions!
145  template <typename Type>
146  Type Get() {
147  static_assert(
149  "You only want to Get() trivially copyable types as raw values.");
150 
151  CheckAvailable(sizeof(Type));
152 
153  Type ret = *reinterpret_cast<const Type*>(data_ + cursor_);
154  cursor_ += sizeof(Type);
155 
156  return ret;
157  }
158 
159  //! Fetch a single byte from the buffer, advancing the cursor.
161  return Get<uint8_t>();
162  }
163 
164  //! Fetch a single item of the template type Type from the buffer,
165  //! advancing the cursor. Be careful with implicit type conversions!
166  template <typename Type>
168  return Get<Type>();
169  }
170 
171  //! Fetch a BufferRef to a binary string or blob which was Put via
172  //! Put_string(). Does NOT copy the data.
174  uint64_t len = GetVarint();
175  // save object
176  BufferRef br(data_ + cursor_, len);
177  // skip over sub block data
178  Skip(len);
179  return br;
180  }
181 
182  //! \}
183 };
184 
185 //! \}
186 
187 } // namespace net
188 } // namespace thrill
189 
190 #endif // !THRILL_NET_BUFFER_READER_HEADER
191 
192 /******************************************************************************/
uint64_t GetVarint()
Fetch a 64-bit varint from the reader at the cursor.
void CheckAvailable(size_t n) const
bool IsNull() const
Indicates if the reader was initialized with a nullptr and size 0.
BufferReader & Read(void *outdata, size_t datalen)
std::string Read(size_t datalen)
const void * data() const
Return a pointer to the currently kept memory area.
Definition: buffer_ref.hpp:68
size_t cursor() const
Return the current read cursor.
Type
VFS object type.
Definition: file_io.hpp:52
CRTP class to enhance item/memory reader classes with Varint decoding and String decoding.
size_t cursor_
Current read cursor.
Byte GetByte()
Fetch a single byte from the buffer, advancing the cursor.
BufferReader(const std::string &str)
Constructor, assign memory area from string, does NOT copy.
BufferReader represents a BufferRef with an additional cursor with which the memory can be read incre...
const Byte * data_
Allocated buffer pointer.
Definition: buffer_ref.hpp:40
BufferReader(const BufferRef &br)
Constructor, assign memory area from BinaryBuilder.
std::is_trivially_copyable< T > is_trivially_copyable
Definition: defines.hpp:54
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
BufferReader(const void *data, size_t n)
Constructor, assign memory area from pointer and length.
unsigned char Byte
type used to store the bytes
Definition: buffer_ref.hpp:37
BufferRef represents a reference to a memory area as pointer and valid length.
Definition: buffer_ref.hpp:33
bool empty() const
Return true if the cursor is at the end of the buffer.
size_t size() const
Return the currently valid length in bytes.
Definition: buffer_ref.hpp:72
BufferReader & Rewind()
Reset the read cursor.
bool available(size_t size) const
Return the number of bytes still available at the cursor.
size_t size_
Size of valid data.
Definition: buffer_ref.hpp:43
BufferReader & Skip(size_t n)
Advance the cursor given number of bytes without reading them.