Honeycomb  0.1
Component-Model Framework
Buffer.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Math/Alge/Alge.h"
5 
6 namespace honey
7 {
8 
10 
16 template<class T>
17 class Buffer
18 {
19 public:
20  typedef T value_type;
21  typedef szt size_type;
23  typedef T& reference;
24  typedef T* pointer;
25  typedef T* iterator;
26  typedef std::reverse_iterator<iterator> reverse_iterator;
27 
29  Buffer() { reset(); }
31  Buffer(T* data, szt size) { reset(data, size); }
33  template<class U>
34  Buffer(vector<U>& list) { reset(list.data(), list.size()); }
35  template<class U>
36  Buffer(const vector<U>& list) { reset(list.data(), list.size()); }
38  template<class U, szt N>
39  Buffer(array<U,N>& list) { reset(list.data(), list.size()); }
40  template<class U, szt N>
41  Buffer(const array<U,N>& list) { reset(list.data(), list.size()); }
42  Buffer(const Buffer& rhs) { operator=(rhs); }
43  Buffer(Buffer&& rhs) { operator=(move(rhs)); }
44 
45  Buffer& operator=(const Buffer& rhs) { reset(rhs._data, rhs._size); return *this; }
46  Buffer& operator=(Buffer&& rhs) { reset(rhs._data, rhs._size); rhs.reset(); return *this; }
47 
49  T& at(szt pos) const { if (pos >= _size) throw std::out_of_range("invalid buffer index"); return _data[pos]; }
51  T& operator[](szt pos) const { return _data[pos]; }
53  T& front() const { return _data[0]; }
55  T& back() const { return _data[_size - 1]; }
57  T* data() const { return _data; }
59 
62  Buffer slice(szt pos, optional<szt> size = optnull) const { szt begin = Alge::clamp(pos, 0, _size), end = size ? Alge::clamp(pos + *size, 0, _size) : _size; return Buffer(_data + begin, end - begin); }
63 
65  T* begin() const { return _data; }
67  T* end() const { return _data + _size; }
69  reverse_iterator rbegin() const { return reverse_iterator(_data + _size); }
71  reverse_iterator rend() const { return reverse_iterator(_data); }
72 
74  bool empty() const { return !_data || !_size; }
76  explicit operator bool() const { return !empty(); }
78  szt size() const { return _size; }
79 
81  bool operator==(const Buffer& rhs) const { return _data == rhs._data && _size == rhs._size; }
82  bool operator!=(const Buffer& rhs) const { return !operator==(rhs); }
83 
85  void reset() { _data = nullptr; _size = 0; }
87  void reset(T* data, szt size) { _data = data; _size = size; }
88 
90  operator Buffer<const T>() const { return Buffer<const T>(_data, _size); }
91 
92 private:
93  T* _data;
94  szt _size;
95 };
96 
97 }
A contiguous region of referenced (not owned by object) memory.
Definition: Buffer.h:17
std::reverse_iterator< iterator > reverse_iterator
Definition: Buffer.h:26
Buffer & operator=(Buffer &&rhs)
Definition: Buffer.h:46
sdt difference_type
Definition: Buffer.h:22
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
bool operator!=(const Buffer &rhs) const
Definition: Buffer.h:82
void reset()
Set data to null with zero size.
Definition: Buffer.h:85
T * pointer
Definition: Buffer.h:24
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
szt size_type
Definition: Buffer.h:21
bool empty() const
Checks whether the buffer does not have any elements.
Definition: Buffer.h:74
Buffer & operator=(const Buffer &rhs)
Definition: Buffer.h:45
Buffer slice(szt pos, optional< szt > size=optnull) const
Get a view of this memory region in range [pos, pos+size)
Definition: Buffer.h:62
Buffer(const Buffer &rhs)
Definition: Buffer.h:42
T & reference
Definition: Buffer.h:23
T value_type
Definition: Buffer.h:20
static std::common_type< Num, Num2, Num3 >::type clamp(Num val, Num2 min, Num3 max)
Ensure that a number is within a range.
Definition: Alge.h:99
T * end() const
Returns an iterator to the end.
Definition: Buffer.h:67
Buffer(vector< U > &list)
Construct from vector with compatible memory reference type.
Definition: Buffer.h:34
Buffer(Buffer &&rhs)
Definition: Buffer.h:43
Buffer(const vector< U > &list)
Definition: Buffer.h:36
szt size() const
Returns the number of elements.
Definition: Buffer.h:78
bool operator==(const Buffer &rhs) const
Check if buffers reference same memory region.
Definition: Buffer.h:81
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
T & operator[](szt pos) const
Access element without bounds checking.
Definition: Buffer.h:51
reverse_iterator rbegin() const
Returns a reverse iterator to the beginning.
Definition: Buffer.h:69
Buffer(T *data, szt size)
Construct from memory reference and number of contiguous elements in region.
Definition: Buffer.h:31
Buffer()
Construct empty buffer with null data and zero size.
Definition: Buffer.h:29
reverse_iterator rend() const
Returns a reverse iterator to the end.
Definition: Buffer.h:71
Buffer(const array< U, N > &list)
Definition: Buffer.h:41
T * iterator
Definition: Buffer.h:25
T * begin() const
Returns an iterator to the beginning.
Definition: Buffer.h:65
T & at(szt pos) const
Access element with bounds checking, throws std::out_of_range.
Definition: Buffer.h:49
T & front() const
Returns the first element without bounds checking.
Definition: Buffer.h:53
T & back() const
Returns the last element without bounds checking.
Definition: Buffer.h:55
Buffer(array< U, N > &list)
Construct from array with compatible memory reference type.
Definition: Buffer.h:39
Global Honeycomb namespace.
void reset(T *data, szt size)
Set new data and size.
Definition: Buffer.h:87
T * data() const
Get the referenced memory.
Definition: Buffer.h:57