allow storage of extra data inside metadata

v1
Brett 2024-03-07 12:46:00 -05:00
parent 19857f3b2b
commit b937df3ca6
2 changed files with 43 additions and 12 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 0.14.1) set(BLT_VERSION 0.14.2)
set(BLT_TEST_VERSION 0.0.1) set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -29,8 +29,35 @@
namespace blt namespace blt
{ {
//#define ALIGN_TO(x, size) (((x) + size - 1) & ~(size - 1)) template<typename MetaExtra>
template<typename T = void> struct metadata_template_t;
template<>
struct metadata_template_t<void>
{
// size in number of elements!
blt::size_t size;
explicit metadata_template_t(blt::size_t size): size(size)
{}
};
template<typename Extra>
struct metadata_template_t
{
// size in number of elements!
blt::size_t size;
Extra extra;
explicit metadata_template_t(blt::size_t size): size(size)
{}
};
/**
* @tparam T type to store inside
* @tparam Extra any extra data to store. void will result in zero size increase.
*/
template<typename T = void, typename Extra = void>
class array class array
{ {
public: public:
@ -39,14 +66,8 @@ namespace blt
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private: private:
struct metadata_t using metadata_t = metadata_template_t<Extra>;
{ metadata_t metadata;
// size in number of elements!
blt::size_t size;
explicit metadata_t(blt::size_t size): size(size)
{}
} metadata;
static constexpr blt::size_t ALIGNMENT = std::max(sizeof(metadata_t), alignof(T)); static constexpr blt::size_t ALIGNMENT = std::max(sizeof(metadata_t), alignof(T));
@ -66,7 +87,7 @@ namespace blt
inline static array* construct(void* ptr, blt::size_t size) inline static array* construct(void* ptr, blt::size_t size)
{ {
auto aligned_ptr = std::align(alignof(array), sizeof(array), ptr, size); auto aligned_ptr = std::align(alignof(array), sizeof(array), ptr, size);
return new (aligned_ptr) array<T> {size}; return new(aligned_ptr) array<T>{size};
} }
array(const array&) = delete; array(const array&) = delete;
@ -186,6 +207,16 @@ namespace blt
return reverse_iterator{cbegin()}; return reverse_iterator{cbegin()};
} }
constexpr inline metadata_t& get_metadata()
{
return metadata;
}
constexpr inline const metadata_t& get_metadata() const
{
return metadata;
}
~array() = default; ~array() = default;
}; };