Brett 2024-03-07 09:00:19 -05:00
parent 7444103897
commit eaad38e588
6 changed files with 241 additions and 64 deletions

1
cloc.sh Executable file
View File

@ -0,0 +1 @@
cloc --exclude-list-file=exclude.txt include src

1
exclude.txt Normal file
View File

@ -0,0 +1 @@
include/blt/unicode_emoji.h

170
include/blt/std/array.h Normal file
View File

@ -0,0 +1,170 @@
/*
* <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>
namespace blt
{
template<typename T = void>
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:
struct metadata
{
// size in number of elements!
blt::size_t size;
explicit metadata(blt::size_t size): size(size)
{}
} metadata;
T* _data = static_cast<T*>(static_cast<blt::u8*>(this) + sizeof(metadata));
public:
/**
* 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.
*/
explicit array(blt::size_t size): metadata( (size - sizeof(metadata)) / sizeof(T))
{}
inline T& operator[](blt::size_t index)
{
return _data[index];
}
inline const T& operator[](blt::size_t index) const
{
return _data[index];
}
[[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!");
return _data[index];
}
[[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!");
return _data[index];
}
[[nodiscard]] inline T* data()
{
return _data;
}
[[nodiscard]] inline T* data() const
{
return _data;
}
[[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*()
{
return _data;
}
constexpr inline T& front()
{
return *_data;
}
constexpr inline const T& front() const
{
return *_data;
}
constexpr inline T& back()
{
return _data[size() - 1];
}
constexpr inline const T& back() const
{
return _data[size() - 1];
}
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()};
}
};
}
#endif //BLT_ARRAY_H

View File

@ -22,65 +22,6 @@
namespace blt
{
template<typename V>
struct ptr_iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = V;
using pointer = value_type*;
using reference = value_type&;
explicit ptr_iterator(V* v): _v(v)
{}
reference operator*() const
{ return *_v; }
pointer operator->()
{ return _v; }
ptr_iterator& operator++()
{
_v++;
return *this;
}
ptr_iterator& operator--()
{
_v--;
return *this;
}
ptr_iterator operator++(int)
{
auto tmp = *this;
++(*this);
return tmp;
}
ptr_iterator operator--(int)
{
auto tmp = *this;
--(*this);
return tmp;
}
friend bool operator==(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v == b._v;
}
friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v != b._v;
}
private:
V* _v;
};
template<typename T, bool = std::is_copy_constructible_v<T> || std::is_copy_assignable_v<T>>
class scoped_buffer;

View File

@ -28,8 +28,9 @@
#if defined(__clang__) || defined(__llvm__) || defined(__GNUC__) || defined(__GNUG__)
#if defined(__GNUC__) || defined(__GNUG__)
#include <byteswap.h>
#define SWAP16(val) bswap_16(val)
#define SWAP32(val) bswap_32(val)
#define SWAP64(val) bswap_64(val)
@ -127,6 +128,69 @@ namespace blt::mem
std::memcpy(&r, &type, sizeof(type));
return r;
}
}
namespace blt
{
template<typename V>
struct ptr_iterator
{
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = V;
using pointer = value_type*;
using reference = value_type&;
explicit ptr_iterator(V* v): _v(v)
{}
reference operator*() const
{ return *_v; }
pointer operator->()
{ return _v; }
ptr_iterator& operator++()
{
_v++;
return *this;
}
ptr_iterator& operator--()
{
_v--;
return *this;
}
ptr_iterator operator++(int)
{
auto tmp = *this;
++(*this);
return tmp;
}
ptr_iterator operator--(int)
{
auto tmp = *this;
--(*this);
return tmp;
}
friend bool operator==(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v == b._v;
}
friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v != b._v;
}
private:
V* _v;
};
}
#endif //BLT_MEMORY_UTIL_H

View File

@ -108,12 +108,12 @@ namespace blt
return buffer_;
}
constexpr inline iterator begin() noexcept
constexpr inline iterator begin() const noexcept
{
return data();
}
constexpr inline iterator end() noexcept
constexpr inline iterator end() const noexcept
{
return data() + size();
}
@ -128,12 +128,12 @@ namespace blt
return data() + size();
}
constexpr inline reverse_iterator rbegin() noexcept
constexpr inline reverse_iterator rbegin() const noexcept
{
return reverse_iterator{end()};
}
constexpr inline reverse_iterator rend() noexcept
constexpr inline reverse_iterator rend() const noexcept
{
return reverse_iterator{begin()};
}