Added Constructors and Destructor to dynarray
This commit is contained in:
@@ -30,7 +30,10 @@
|
||||
|
||||
#ifndef FENNEC_CONTAINERS_DYNARRAY_H
|
||||
#define FENNEC_CONTAINERS_DYNARRAY_H
|
||||
|
||||
#include <fennec/lang/utility.h>
|
||||
#include <fennec/memory/allocator.h>
|
||||
#include <fennec/memory/new.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
@@ -42,10 +45,45 @@ public:
|
||||
using element_t = T;
|
||||
using alloc_t = Alloc;
|
||||
|
||||
///
|
||||
/// \brief Default Constructor, initializes an empty allocation.
|
||||
dynarray() : _alloc(), _size(0) {}
|
||||
dynarray(size_t size) : _alloc(size), _size(size) { }
|
||||
|
||||
///
|
||||
/// \breif Alloc Constructor, initalize empty allocation with allocator instance.
|
||||
/// \param alloc An allocator object to copy, for instances where the allocator needs to be initialized with some data.
|
||||
dynarray(const alloc_t& alloc) : _alloc(alloc), _size(0) {}
|
||||
|
||||
///
|
||||
/// \brief Create an allocation with a size of `n` elements. All elements are initialized with the default constructor.
|
||||
dynarray(size_t n) : _alloc(n), _size(n)
|
||||
{
|
||||
element_t* addr = _alloc.data();
|
||||
for(; n > 0; --n, ++addr) { fennec::construct(addr); }
|
||||
}
|
||||
|
||||
///
|
||||
/// \brief Create an allocation of size `n`, with each element constructed using the copy constructor
|
||||
/// \brief n the number of elements
|
||||
dynarray(size_t n, const element_t& val)
|
||||
{
|
||||
element_t* addr = _alloc.data();
|
||||
for(; n > 0; --n, ++addr) { fennec::construct(addr, val); }
|
||||
}
|
||||
|
||||
template<typename...ArgsT>
|
||||
dynarray(size_t n, ArgsT...args)
|
||||
{
|
||||
element_t* addr = _alloc.data();
|
||||
for(; n > 0; --n, ++addr) { fennec::construct(addr, args...); }
|
||||
}
|
||||
|
||||
~dynarray()
|
||||
{
|
||||
element_t* addr = _alloc.data();
|
||||
for(; n > 0; --n, ++addr) { fennec::destruct(addr); }
|
||||
}
|
||||
|
||||
private:
|
||||
allocation<element_t, alloc_t> _alloc;
|
||||
size_t _size;
|
||||
|
||||
@@ -93,8 +93,8 @@
|
||||
|
||||
// Difficult and Inconsistent without intrinsics
|
||||
#if __has_builtin(__is_constructible)
|
||||
# define FENNEC_BUILTIN_CAN_CONSTRUCT 1
|
||||
# define FENNEC_BUILTIN_CAN_CONSTRUCT(type, args...) __is_constructible(type, args)
|
||||
# define FENNEC_HAS_BUILTIN_CAN_CONSTRUCT 1
|
||||
# define FENNEC_BUILTIN_CAN_CONSTRUCT(type, ...) __is_constructible(type, __VA_ARGS__)
|
||||
#else
|
||||
# define FENNEC_HAS_BUILTIN_CAN_CONSTRUCT 0
|
||||
#endif
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef FENNEC_MEMORY_ALLOCATOR_H
|
||||
#define FENNEC_MEMORY_ALLOCATOR_H
|
||||
|
||||
#include <type_traits>
|
||||
#include <fennec/memory/ptr_traits.h>
|
||||
#include <fennec/lang/conditional_types.h>
|
||||
#include <fennec/lang/types.h>
|
||||
@@ -279,9 +278,14 @@ public:
|
||||
|
||||
///
|
||||
/// \brief Getter for the number of elements `n` of type `T` that the allocation can hold.
|
||||
/// \return the size of the allocation in elements
|
||||
/// \returns the size of the allocation in elements
|
||||
constexpr size_t capacity() const { return _capacity; }
|
||||
|
||||
///
|
||||
/// \brief Getter for the real pointer to the allocated block of memory
|
||||
/// \returns Pointer to the allocated memory.
|
||||
constexpr value_t* data() { return _data; }
|
||||
|
||||
private:
|
||||
alloc_t _alloc; // Allocator object
|
||||
value_t* _data; // Handle for the memory block
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <new>
|
||||
|
||||
#include <fennec/lang/types.h>
|
||||
#include <fennec/lang/utility.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
@@ -36,6 +37,15 @@ enum class align_t : size_t;
|
||||
/// \brief Type to handle explicit nothrow definitions
|
||||
struct nothrow_t { explicit nothrow_t() noexcept { } };
|
||||
|
||||
template<typename TypeT> void construct(TypeT* ptr)
|
||||
{ ptr->TypeT(); }
|
||||
|
||||
template<typename TypeT, typename...ArgsT> void construct(TypeT* ptr, ArgsT...args)
|
||||
{ ptr->TypeT(fennec::forward(args)...); }
|
||||
|
||||
template<typename TypeT> void destruct(TypeT* ptr)
|
||||
{ ptr->~TypeT(); }
|
||||
|
||||
}
|
||||
|
||||
#endif // FENNEC_MEMORY_NEW_H
|
||||
|
||||
Reference in New Issue
Block a user