From b937df3ca64f96723110fa3915e5269873faee7e Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 7 Mar 2024 12:46:00 -0500 Subject: [PATCH] allow storage of extra data inside metadata --- CMakeLists.txt | 2 +- include/blt/std/array.h | 53 ++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 56333e9..826c2c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) 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_TARGET BLT) diff --git a/include/blt/std/array.h b/include/blt/std/array.h index 35d5ef1..6c4e11c 100644 --- a/include/blt/std/array.h +++ b/include/blt/std/array.h @@ -29,8 +29,35 @@ namespace blt { -//#define ALIGN_TO(x, size) (((x) + size - 1) & ~(size - 1)) - template + template + struct metadata_template_t; + + template<> + struct metadata_template_t + { + // size in number of elements! + blt::size_t size; + + explicit metadata_template_t(blt::size_t size): size(size) + {} + }; + + template + 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 class array { public: @@ -39,14 +66,8 @@ namespace blt using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; private: - struct metadata_t - { - // size in number of elements! - blt::size_t size; - - explicit metadata_t(blt::size_t size): size(size) - {} - } metadata; + using metadata_t = metadata_template_t; + metadata_t metadata; 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) { auto aligned_ptr = std::align(alignof(array), sizeof(array), ptr, size); - return new (aligned_ptr) array {size}; + return new(aligned_ptr) array{size}; } array(const array&) = delete; @@ -186,6 +207,16 @@ namespace blt return reverse_iterator{cbegin()}; } + constexpr inline metadata_t& get_metadata() + { + return metadata; + } + + constexpr inline const metadata_t& get_metadata() const + { + return metadata; + } + ~array() = default; };