Thrill  0.1
fixed_buffer_builder.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/net/fixed_buffer_builder.hpp
3  *
4  * FixedBufferBuilder is like BufferBuilder except that constructs data blocks
5  * with FIXED length content.
6  *
7  * Part of Project Thrill - http://project-thrill.org
8  *
9  * Copyright (C) 2013-2015 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
12  ******************************************************************************/
13 
14 #pragma once
15 #ifndef THRILL_NET_FIXED_BUFFER_BUILDER_HEADER
16 #define THRILL_NET_FIXED_BUFFER_BUILDER_HEADER
17 
19 
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <cstdlib>
24 #include <stdexcept>
25 #include <string>
26 
27 namespace thrill {
28 namespace net {
29 
30 //! \addtogroup net_layer
31 //! \{
32 
33 /*!
34  * Represents a FIXED length area of memory, which can be modified by appending
35  * integral data types via Put() and other basic operations.
36  */
37 template <size_t Capacity>
39  : public common::ItemWriterToolsBase<FixedBufferBuilder<Capacity> >
40 {
41 private:
42  //! type used to store the bytes
43  using Byte = unsigned char;
44 
45  //! simple pointer iterators
46  using iterator = Byte *;
47  //! simple pointer iterators
48  using const_iterator = const Byte *;
49  //! simple pointer references
50  using reference = Byte&;
51  //! simple pointer references
52  using const_reference = const Byte&;
53 
54  //! Allocated buffer.
55  std::array<Byte, Capacity> data_;
56 
57  //! Size of _valid_ data.
58  size_t size_ = 0;
59 
60 public:
61  //! \name Data, Size, and Capacity Accessors
62  //! \{
63 
64  //! Return a pointer to the currently kept memory area.
65  const Byte * data() const {
66  return data_.data();
67  }
68 
69  //! Return a writeable pointer to the currently kept memory area.
70  Byte * data() {
71  return data_.data();
72  }
73 
74  //! Return the currently used length in bytes.
75  size_t size() const {
76  return size_;
77  }
78 
79  //! Return the currently allocated buffer capacity.
80  size_t capacity() const {
81  return Capacity;
82  }
83 
84  //! \} //do not append empty end-of-stream buffer
85 
86  //! \name Buffer Growing, Clearing, and other Management
87  //! \{
88 
89  //! Clears the memory contents, does not deallocate the memory.
91  size_ = 0;
92  return *this;
93  }
94 
95  //! Set the valid bytes in the buffer, use if the buffer is filled
96  //! directly.
98  assert(n <= Capacity);
99  size_ = n;
100 
101  return *this;
102  }
103 
104  //! Explicit conversion to std::string (copies memory of course).
106  return std::string(reinterpret_cast<const char*>(data_.data()), size_);
107  }
108 
109  //! \}
110 
111  //! \name Appending Write Functions
112  //! \{
113 
114  //! Append a memory range to the buffer
115  FixedBufferBuilder& Append(const void* data, size_t len) {
116  assert(size_ + len <= Capacity);
117 
118  const Byte* cdata = reinterpret_cast<const Byte*>(data);
119  std::copy(cdata, cdata + len, data_.data() + size_);
120  size_ += len;
121 
122  return *this;
123  }
124 
125  //! Append to contents of a std::string, excluding the null (which isn't
126  //! contained in the string size anyway).
128  return Append(s.data(), s.size());
129  }
130 
131  //! Put (append) a single item of the template type T to the buffer. Be
132  //! careful with implicit type conversions!
133  template <typename Type>
134  FixedBufferBuilder& Put(const Type& item) {
135  static_assert(std::is_pod<Type>::value,
136  "You only want to Put() POD types as raw values.");
137 
138  assert(size_ + sizeof(Type) <= Capacity);
139 
140  *reinterpret_cast<Type*>(data_.data() + size_) = item;
141  size_ += sizeof(Type);
142 
143  return *this;
144  }
145 
146  //! Put a single byte to the buffer (used via CRTP from ItemWriterToolsBase)
148  return Put<uint8_t>(data);
149  }
150 
151  //! Put (append) a single item of the template type T to the buffer. Be
152  //! careful with implicit type conversions!
153  template <typename Type>
155  return Put<Type>(item);
156  }
157 
158  //! \}
159 
160  //! \name Access
161  //! \{
162 
163  //! return mutable iterator to first element
165  { return data_.data(); }
166  //! return constant iterator to first element
168  { return data_.data(); }
169 
170  //! return mutable iterator beyond last element
172  { return data_.data() + size_; }
173  //! return constant iterator beyond last element
175  { return data_.data() + size_; }
176 
177  //! return the i-th position of the vector
179  assert(i < size_);
180  return *(begin() + i);
181  }
182 
183  //! \}
184 };
185 
186 //! \}
187 
188 } // namespace net
189 } // namespace thrill
190 
191 #endif // !THRILL_NET_FIXED_BUFFER_BUILDER_HEADER
192 
193 /******************************************************************************/
FixedBufferBuilder & Clear()
Clears the memory contents, does not deallocate the memory.
FixedBufferBuilder & Append(const void *data, size_t len)
Append a memory range to the buffer.
unsigned char Byte
type used to store the bytes
size_t capacity() const
Return the currently allocated buffer capacity.
iterator end()
return mutable iterator beyond last element
size_t size_
Size of valid data.
Byte * data()
Return a writeable pointer to the currently kept memory area.
std::string ToString() const
Explicit conversion to std::string (copies memory of course).
Type
VFS object type.
Definition: file_io.hpp:52
size_t size() const
Return the currently used length in bytes.
FixedBufferBuilder & set_size(size_t n)
iterator begin()
return mutable iterator to first element
Byte * iterator
simple pointer iterators
Byte & reference
simple pointer references
const Byte & const_reference
simple pointer references
FixedBufferBuilder & AppendString(const std::string &s)
Represents a FIXED length area of memory, which can be modified by appending integral data types via ...
int value
Definition: gen_data.py:41
reference operator[](size_t i)
return the i-th position of the vector
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
std::array< Byte, Capacity > data_
Allocated buffer.
CRTP class to enhance item/memory writer classes with Varint encoding and String encoding.
FixedBufferBuilder & Put(const Type &item)
const_iterator begin() const
return constant iterator to first element
FixedBufferBuilder & PutByte(Byte data)
Put a single byte to the buffer (used via CRTP from ItemWriterToolsBase)
const_iterator end() const
return constant iterator beyond last element
const Byte * data() const
Return a pointer to the currently kept memory area.
const Byte * const_iterator
simple pointer iterators
FixedBufferBuilder & PutRaw(const Type &item)