VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::HandleBuffer< T, Handle, MaskSplit > Class Template Reference

TODO broken don't use. More...

#include <THandleBuffer.hpp>

Inheritance diagram for tklb::HandleBuffer< T, Handle, MaskSplit >:
Collaboration diagram for tklb::HandleBuffer< T, Handle, MaskSplit >:

Public Member Functions

 HandleBuffer (Handle T::*member)
 Construct a new Handle Buffer object. More...
 
template<class Func >
void forEach (const Func &&func)
 
T * at (const Handle &handle)
 
Handle create ()
 
Handle push (const T &object)
 add element, may trigger resize More...
 
bool pop (const Handle &handle, T *destination=nullptr)
 
bool remove (const Size index)
 
bool remove (const T &object)
 
- Public Member Functions inherited from tklb::HeapBuffer< T, true >
 HeapBuffer ()=default
 
 HeapBuffer (const Size size)
 Setup the buffer with a size. More...
 
 HeapBuffer (const HeapBuffer< T, Alignment2, Allocator2, Size2 > &source)
 Copy Constructor, calls set() Failed allocations have to be checked. More...
 
 HeapBuffer (const T *data, Size size)
 
 HeapBuffer (HeapBuffer &&source)
 Move contructor, will mark the original buffer as injected. More...
 
 HeapBuffer (const HeapBuffer *)=delete
 
HeapBufferoperator= (HeapBuffer &&source)
 
HeapBufferoperator= (const HeapBuffer &)=delete
 
 ~HeapBuffer ()
 
bool set (const HeapBuffer< T, Alignment2, Allocator2, Size2 > &source)
 Resizes and copies the contents of the source Buffer This will do a memory::copy,so none of the object contructors will called. More...
 
bool set (const T *data, Size size)
 Set data from array. More...
 
void inject (T *mem, const Size size, const Size realSize=0)
 Provide foreign memory to borrow. More...
 
void inject (const T *mem, const Size size, const Size realSize=0)
 Provide const foreign memory to use Using non const accessors will cause assertions in debug mode. More...
 
void disown ()
 The memory is no longer manager by this instance. More...
 
bool empty () const
 
T * data ()
 
const T * data () const
 
const T & operator[] (const Size index) const
 
T & operator[] (const Size index)
 
const T * begin () const
 
T * begin ()
 
const T * end () const
 
T * end ()
 
T & last ()
 
T & first ()
 
bool reserve (const Size size)
 Will make sure the desired space is allocated. More...
 
bool resize (const Size size, const bool downsize=true)
 Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon as an allocation happens. More...
 
bool push (const T &object)
 Push the object to the back of the buffer TODO tklb move version. More...
 
bool pop (T *object)
 Get the last element in the buffer. More...
 
bool remove (const Size index)
 Removes the object at a given index and fills the gap with another Will never shrink the buffer/allocate. More...
 
bool remove (const T &object)
 If a == comparisin is possible the object itself can be used. More...
 
bool remove (const T *object)
 Otherwise use memory location. More...
 
bool destroyPointers ()
 If T is a pointer type, delete will be called for all pointers in buffer and resize(0) is called. More...
 
void clear ()
 Clears the buffer but doesn't free the memory. More...
 
bool injected () const
 Whether the memory is managed by the instance or is borrowed. More...
 
Size size () const
 Returns the amount of elements in the container. More...
 
Size reserved () const
 Returns the real allocated size in elements. More...
 
Size allocated () const
 Returns the size of the allocated space. More...
 

Static Public Attributes

static constexpr Handle InvalidHandle = ~0
 Since 0 is a valid handle, this is used to indicate a invalid one. More...
 
- Static Public Attributes inherited from tklb::HeapBuffer< T, true >
static constexpr size_t Alignment
 
static constexpr size_t ChunkSize
 

Private Types

using Base = HeapBuffer< T, true >
 
using Size = typename Base::Size
 

Private Attributes

Size mLastFree = 0
 
Size mOffset
 Offset to the member variable which is used to identify an object. More...
 

Static Private Attributes

static constexpr Handle MaskId = (1 << (MaskSplit)) - 1
 
static constexpr Handle MaskIndex = ~MaskId
 

Additional Inherited Members

- Public Types inherited from tklb::HeapBuffer< T, true >
using Size = unsigned int
 everything using the heapbuffer uses this type Always needs to be unsigned! More...
 
- Static Public Member Functions inherited from tklb::HeapBuffer< T, true >
static Size closestChunkSize (Size size, Size chunk)
 
static bool isAligned (const void *ptr)
 

Detailed Description

template<class T, typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
class tklb::HandleBuffer< T, Handle, MaskSplit >

TODO broken don't use.

A buffer to access contents via a Handle with validity checks

Type stored in here needs to store it's own id in a Handle member.

Template Parameters
Ttype to store
Handletype to index elements
MaskSplitAt what bit to split the Handle for validation

Definition at line 15 of file THandleBuffer.hpp.

Member Typedef Documentation

◆ Base

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
using tklb::HandleBuffer< T, Handle, MaskSplit >::Base = HeapBuffer<T, true>
private

Definition at line 17 of file THandleBuffer.hpp.

◆ Size

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
using tklb::HandleBuffer< T, Handle, MaskSplit >::Size = typename Base::Size
private

Definition at line 18 of file THandleBuffer.hpp.

Constructor & Destructor Documentation

◆ HandleBuffer()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
tklb::HandleBuffer< T, Handle, MaskSplit >::HandleBuffer ( Handle T::*  member)
inline

Construct a new Handle Buffer object.

Parameters
memberPointer to the member variable used for validation

Definition at line 41 of file THandleBuffer.hpp.

41 {
42 // nice
43 mOffset = Size(((char*) &(((T*)nullptr)->*member)) - ((char*)nullptr));
44 }
Size mOffset
Offset to the member variable which is used to identify an object.
typename Base::Size Size

Member Function Documentation

◆ at()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
T * tklb::HandleBuffer< T, Handle, MaskSplit >::at ( const Handle &  handle)
inline

Definition at line 53 of file THandleBuffer.hpp.

53 {
54 const Handle index = (handle & MaskIndex) >> MaskSplit;
55 if (index == mLastFree) { return nullptr; } // element deleted
56 if (Base::size() <= index) { return nullptr; } // out of range
57 T* element = Base::data() + index;
58 const Handle refernceId = handle & MaskId;
59 const Handle id = (*((Handle*)(((char*) element) + mOffset))) & MaskId;
60 if (id != refernceId) { return nullptr; } // Id mismatch, so object was replaced
61 return element;
62 }
static constexpr Handle MaskIndex
static constexpr Handle MaskId
Size size() const
Returns the amount of elements in the container.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
Handle tklb::HandleBuffer< T, Handle, MaskSplit >::create ( )
inline

Definition at line 64 of file THandleBuffer.hpp.

64 {
65 Size index = 0;
66 const Size s = Base::size();
67 if (s <= mLastFree || mLastFree < 0) {
68 index = s;
69 Base::resize(s + 1);
70 }
71 Size zero = 0; // should underflow
72 mLastFree = zero - 1; // make sure free slot is not in size any more
73 Handle ret = index << MaskSplit;
74 Handle id = (*((Handle*)(((char*) (Base::data() + index)) + mOffset))) & MaskId;
75 ret = ret | id;
76 return ret;
77 }
bool resize(const Size size, const bool downsize=true)
Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon ...
Here is the call graph for this function:

◆ forEach()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
template<class Func >
void tklb::HandleBuffer< T, Handle, MaskSplit >::forEach ( const Func &&  func)
inline

Definition at line 47 of file THandleBuffer.hpp.

47 {
48 for (Size i = 0; i < Base::size(); i++) {
49 // if (*(Base::data() + i))
50 }
51 }
Here is the call graph for this function:

◆ pop()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
bool tklb::HandleBuffer< T, Handle, MaskSplit >::pop ( const Handle &  handle,
T *  destination = nullptr 
)
inline

Definition at line 103 of file THandleBuffer.hpp.

103 {
104 T* element = at(handle);
105 if (element == nullptr) { return false; }
106 if (destination != nullptr) {
107 memory::copy(destination, element, sizeof(T));
108 }
109 return remove(*element);
110 }
bool remove(const Size index)
T * at(const Handle &handle)
static void copy(void *dst, const void *src, const size_t size)
memcpy wrapper
Definition: TMemoryUtil.hpp:14
Here is the call graph for this function:

◆ push()

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
Handle tklb::HandleBuffer< T, Handle, MaskSplit >::push ( const T &  object)
inline

add element, may trigger resize

Definition at line 82 of file THandleBuffer.hpp.

82 {
83 Handle ret = 0;
84 if (Base::size() <= mLastFree || mLastFree < 0) {
85 // no free slot
86 ret = Base::size();
87 if (!Base::push(object)) {
88 return InvalidHandle; // something went wrong
89 }
90 } else {
91 // use free slot
92 ret = mLastFree;
93 new (Base::data() + mLastFree) T(object);
94 }
95 ret = ret << MaskSplit;
96 Handle id = (*((Handle*)(((char*) &object) + mOffset))) & MaskId;
97 ret = ret | id;
98 Size zero = 0; // should underflow
99 mLastFree = zero - 1; // make sure free slot is not in size any more
100 return ret;
101 }
static constexpr Handle InvalidHandle
Since 0 is a valid handle, this is used to indicate a invalid one.
bool push(const T &object)
Push the object to the back of the buffer TODO tklb move version.
Here is the call graph for this function:

◆ remove() [1/2]

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
bool tklb::HandleBuffer< T, Handle, MaskSplit >::remove ( const Size  index)
inline

Definition at line 112 of file THandleBuffer.hpp.

112 {
113 if (Base::size() <= index) { return false; }
114 if (index == mLastFree) { return false; }
115 Base::data()[index].~T();
116 mLastFree = index;
117 return true;
118 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove() [2/2]

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
bool tklb::HandleBuffer< T, Handle, MaskSplit >::remove ( const T &  object)
inline

Definition at line 120 of file THandleBuffer.hpp.

120 {
121 T* arr = Base::data();
122 for (Size i = 0; i < Base::size(); i++) {
123 if ((arr + i) == &object) {
124 return remove(i);
125 }
126 }
127 return false;
128 }
Here is the call graph for this function:

Member Data Documentation

◆ InvalidHandle

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
constexpr Handle tklb::HandleBuffer< T, Handle, MaskSplit >::InvalidHandle = ~0
staticconstexpr

Since 0 is a valid handle, this is used to indicate a invalid one.

Definition at line 35 of file THandleBuffer.hpp.

◆ MaskId

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
constexpr Handle tklb::HandleBuffer< T, Handle, MaskSplit >::MaskId = (1 << (MaskSplit)) - 1
staticconstexprprivate

Definition at line 20 of file THandleBuffer.hpp.

◆ MaskIndex

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
constexpr Handle tklb::HandleBuffer< T, Handle, MaskSplit >::MaskIndex = ~MaskId
staticconstexprprivate

Definition at line 21 of file THandleBuffer.hpp.

◆ mLastFree

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
Size tklb::HandleBuffer< T, Handle, MaskSplit >::mLastFree = 0
private

Definition at line 23 of file THandleBuffer.hpp.

◆ mOffset

template<class T , typename Handle = unsigned int, int MaskSplit = sizeof(Handle) * 4>
Size tklb::HandleBuffer< T, Handle, MaskSplit >::mOffset
private

Offset to the member variable which is used to identify an object.

in bytes Should get optimized away since it depends on template arguments.

Definition at line 28 of file THandleBuffer.hpp.


The documentation for this class was generated from the following file: