BLT/include/blt/std/array.h

227 lines
6.7 KiB
C
Raw Normal View History

2024-03-07 09:00:19 -05:00
/*
* <Short Description>
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_ARRAY_H
#define BLT_ARRAY_H
#include <type_traits>
#include <blt/std/types.h>
#include <blt/std/memory_util.h>
#include <stdexcept>
#include <iterator>
2024-03-07 11:38:09 -05:00
#include <memory>
#include "logging.h"
2024-03-07 09:00:19 -05:00
namespace blt
{
template<typename MetaExtra>
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
{
static_assert(std::is_trivially_copyable_v<Extra> && "Must be raw type!");
2024-03-07 12:55:12 -05:00
Extra extra;
// size in number of elements!
blt::size_t size;
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>
2024-03-07 09:00:19 -05:00
class array
{
public:
using iterator = blt::ptr_iterator<T>;
using const_iterator = blt::ptr_iterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private:
using metadata_t = metadata_template_t<Extra>;
metadata_t metadata;
2024-03-07 09:00:19 -05:00
2024-03-07 11:38:09 -05:00
static constexpr blt::size_t ALIGNMENT = std::max(sizeof(metadata_t), alignof(T));
inline T* _data()
{
return reinterpret_cast<T*>(reinterpret_cast<blt::u8*>(this) + ALIGNMENT);
}
2024-03-07 09:00:19 -05:00
/**
* constructs an array out of a block of memory of size bytes
* @param size number of bytes available in the memory allocated to this array.
*/
2024-03-07 11:38:09 -05:00
explicit array(blt::size_t size): metadata((size - sizeof(metadata)) / sizeof(T))
2024-03-07 09:00:19 -05:00
{}
2024-03-07 11:38:09 -05:00
public:
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<T>{size};
2024-03-07 11:38:09 -05:00
}
array(const array&) = delete;
array(array&&) = delete;
array& operator=(const array&) = delete;
array& operator=(array&&) = delete;
2024-03-07 09:00:19 -05:00
inline T& operator[](blt::size_t index)
{
2024-03-07 11:38:09 -05:00
return _data()[index];
2024-03-07 09:00:19 -05:00
}
inline const T& operator[](blt::size_t index) const
{
2024-03-07 11:38:09 -05:00
return _data()[index];
2024-03-07 09:00:19 -05:00
}
[[nodiscard]] inline T& at(blt::size_t index)
{
if (index > size())
throw std::runtime_error("Index " + std::to_string(index) += " is outside the bounds of this array!");
2024-03-07 11:38:09 -05:00
return _data()[index];
2024-03-07 09:00:19 -05:00
}
[[nodiscard]] inline const T& at(blt::size_t index) const
{
if (index > size())
throw std::runtime_error("Index " + std::to_string(index) += " is outside the bounds of this array!");
2024-03-07 11:38:09 -05:00
return _data()[index];
2024-03-07 09:00:19 -05:00
}
[[nodiscard]] inline T* data()
{
2024-03-07 11:38:09 -05:00
return _data();
2024-03-07 09:00:19 -05:00
}
[[nodiscard]] inline T* data() const
{
2024-03-07 11:38:09 -05:00
return _data();
2024-03-07 09:00:19 -05:00
}
[[nodiscard]] inline blt::size_t size() const
{
return metadata.size;
}
[[nodiscard]] inline blt::size_t size_bytes() const
{
return (metadata.size * sizeof(T)) + sizeof(metadata);
}
constexpr inline T* operator*()
{
2024-03-07 11:38:09 -05:00
return data();
2024-03-07 09:00:19 -05:00
}
constexpr inline T& front()
{
2024-03-07 11:38:09 -05:00
return *_data();
2024-03-07 09:00:19 -05:00
}
constexpr inline const T& front() const
{
2024-03-07 11:38:09 -05:00
return *data();
2024-03-07 09:00:19 -05:00
}
constexpr inline T& back()
{
2024-03-07 11:38:09 -05:00
return data()[size() - 1];
2024-03-07 09:00:19 -05:00
}
constexpr inline const T& back() const
{
2024-03-07 11:38:09 -05:00
return data()[size() - 1];
2024-03-07 09:00:19 -05:00
}
constexpr inline iterator begin() const noexcept
{
return data();
}
constexpr inline iterator end() const noexcept
{
return data() + size();
}
constexpr inline const_iterator cbegin() const noexcept
{
return data();
}
constexpr inline const_iterator cend() const noexcept
{
return data() + size();
}
constexpr inline reverse_iterator rbegin() const noexcept
{
return reverse_iterator{end()};
}
constexpr inline reverse_iterator rend() const noexcept
{
return reverse_iterator{begin()};
}
constexpr inline const_iterator crbegin() const noexcept
{
return const_reverse_iterator{cend()};
}
constexpr inline reverse_iterator crend() const noexcept
{
return reverse_iterator{cbegin()};
}
2024-03-07 11:38:09 -05:00
constexpr inline metadata_t& get_metadata()
{
return metadata;
}
constexpr inline const metadata_t& get_metadata() const
{
return metadata;
}
2024-03-07 11:38:09 -05:00
~array() = default;
2024-03-07 09:00:19 -05:00
};
}
#endif //BLT_ARRAY_H