Pigweed
 
Loading...
Searching...
No Matches
pw::allocator::WithBuffer< T, kBufferSize, kAlignment > Class Template Reference

#include <buffer.h>

Public Member Functions

ByteSpan as_bytes ()
 
std::byte * data ()
 
size_t size () const
 
T & operator* ()
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 

Static Public Attributes

static constexpr size_t kCapacity = kBufferSize
 

Detailed Description

template<typename T, size_t kBufferSize, size_t kAlignment = 1>
class pw::allocator::WithBuffer< T, kBufferSize, kAlignment >

Associates a default-constructed type with a memory buffer.

Although the type is arbitrary, the intended purpose of of this class is to provide allocators with memory to use, e.g. when testing.

This class uses composition instead of inheritance in order to allow the wrapped type's destructor to reference the memory without risk of a use-after-free. As a result, the specific methods of the wrapped type are not directly accesible. Instead, they can be accessed using the * and -> operators, e.g.

allocator->MethodSpecificToMyAllocator();
Definition: buffer.h:52

Note that this class does NOT initialize the allocator, since initialization is not specified as part of the Allocator interface and may vary from allocator to allocator. As a result, typical usage includes deriving a class that initializes the wrapped allocator with the buffer in a constructor. See AllocatorForTest for an example.

Template Parameters
TThe wrapped object.
kBufferSizeThe size of the backing memory, in bytes.
kAlignmentBuffer memory will be aligned to this alignment boundary.

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