Skip to content

Instantly share code, notes, and snippets.

@bit-hack
Created July 22, 2017 00:52
Show Gist options
  • Save bit-hack/a9752c6a9b43b15afa3e43e3d41cbbe9 to your computer and use it in GitHub Desktop.
Save bit-hack/a9752c6a9b43b15afa3e43e3d41cbbe9 to your computer and use it in GitHub Desktop.
heap allocated array wrapper
#pragma once
#include <memory>
#include <cstddef>
template <typename type_t>
struct dynarray_t {
dynarray_t(size_t elements)
: _elements(elements)
, _array(std::move(std::make_unique<type_t[]>(elements)))
{
}
const size_t element_size() const {
return sizeof(type_t);
}
const size_t size() const {
return _elements;
}
type_t & operator [] (const size_t index) {
assert(index < _elements);
return _array.get()[index];
}
const type_t & operator [] (const size_t index) const {
assert(index < _elements);
return _array.get()[index];
}
explicit operator type_t* () {
return _array.get();
}
explicit operator const type_t* () const {
return _array.get();
}
operator void* () {
return (void*)_array.get();
}
operator const void* () const {
return (const void*) _array.get();
}
type_t * data() {
return _array.get();
}
const type_t * data() const {
return _array.get();
}
protected:
const size_t _elements;
std::unique_ptr<type_t[]> _array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment