Lots of documentation.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-09-22 19:32:40 +02:00
parent 95f1e818e7
commit 31b63d56db
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
15 changed files with 2724 additions and 33 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
/cmake-build-debug-coverage/ /cmake-build-debug-coverage/
/cmake-build-release/ /cmake-build-release/
/build-release-windows/ /build-release-windows/
/.idea/ /.idea/
/docs/

2531
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
#include "../Assert.hpp" #include "../Assert.hpp"
namespace ArbUt { namespace ArbUt {
/// @brief Wrapper around unordered_map, allowing safer access and adding several helper methods.
template <class KeyT, class ValueT> class Dictionary { template <class KeyT, class ValueT> class Dictionary {
private: private:
std::unordered_map<KeyT, ValueT> _map; std::unordered_map<KeyT, ValueT> _map;
@ -14,11 +15,15 @@ namespace ArbUt {
public: public:
Dictionary() : _map() {} Dictionary() : _map() {}
/// @brief Initialises a dictionary with a certain capacity.
explicit Dictionary(size_t capacity) : _map(capacity) {} explicit Dictionary(size_t capacity) : _map(capacity) {}
explicit Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {} /// @brief Initialises a dictionary from an initializer_list.
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {}
/// @brief Removes all items from the dictionary.
inline void Clear() noexcept { _map.clear(); } inline void Clear() noexcept { _map.clear(); }
/// @brief Inserts a new item in the dictionary. This will throw if the dictionary already contains the key.
inline void Insert(const KeyT& key, const ValueT& value) { inline void Insert(const KeyT& key, const ValueT& value) {
[[maybe_unused]] const auto& v = _map.insert({key, value}); [[maybe_unused]] const auto& v = _map.insert({key, value});
#ifndef NO_ASSERT #ifndef NO_ASSERT
@ -27,8 +32,10 @@ namespace ArbUt {
#endif #endif
} }
/// @brief Sets a key in the dictionary to a specific value.
inline void Set(const KeyT& key, const ValueT& value) { _map[key] = value; } inline void Set(const KeyT& key, const ValueT& value) { _map[key] = value; }
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline ValueT& Get(const KeyT& key) { [[nodiscard]] inline ValueT& Get(const KeyT& key) {
#ifndef NO_ASSERT #ifndef NO_ASSERT
return _map.at(key); return _map.at(key);
@ -36,9 +43,11 @@ namespace ArbUt {
return _map[key]; return _map[key];
#endif #endif
} }
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); } [[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); }
/// @brief Try to get an item from the dictionary using a key. Returns false if no item is found, and out will
/// not be touched in that case.
inline bool TryGet(const KeyT& key, ValueT& out) const noexcept { inline bool TryGet(const KeyT& key, ValueT& out) const noexcept {
const auto& find = _map.find(key); const auto& find = _map.find(key);
if (find == _map.end()) { if (find == _map.end()) {
@ -48,22 +57,33 @@ namespace ArbUt {
return true; return true;
} }
/// @brief Removes an item with a certain key from the dictionary
inline void Remove(const KeyT& key) { _map.erase(key); } inline void Remove(const KeyT& key) { _map.erase(key); }
/// @brief Returns the number of items in the dictionary.
[[nodiscard]] inline size_t Count() const noexcept { return _map.size(); } [[nodiscard]] inline size_t Count() const noexcept { return _map.size(); }
/// @brief Checks whether the dictionary contains a specific key.
inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); } inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); }
/// @brief Indexing operator to get a value from the dictionary using a key.
inline ValueT& operator[](const KeyT& key) { return Get(key); } inline ValueT& operator[](const KeyT& key) { return Get(key); }
/// @brief Indexing operator to get a value from the dictionary using a key.
inline const ValueT& operator[](const KeyT& key) const { return Get(key); } inline const ValueT& operator[](const KeyT& key) const { return Get(key); }
/// @brief returns an iterator to the beginning of the specified bucket
iterator begin() noexcept { return _map.begin(); } iterator begin() noexcept { return _map.begin(); }
/// @brief returns an iterator to the beginning of the specified bucket
const_iterator begin() const noexcept { return _map.begin(); } const_iterator begin() const noexcept { return _map.begin(); }
/// @brief returns an iterator to the end of the specified bucket
iterator end() noexcept { return _map.end(); } iterator end() noexcept { return _map.end(); }
/// @brief returns an iterator to the end of the specified bucket
const_iterator end() const { return _map.end(); } const_iterator end() const { return _map.end(); }
/// @brief returns the backing unordered_map.
const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return _map; } const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return _map; }
/// @brief returns the backing unordered_map.
std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return _map; } std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return _map; }
}; };
} }

View File

@ -7,6 +7,7 @@
#include "../Exception.hpp" #include "../Exception.hpp"
namespace ArbUt { namespace ArbUt {
/// @brief Wrapper around vector, allowing safer access, with several helper methods inserted.
template <class ValueT> class List { template <class ValueT> class List {
private: private:
std::vector<ValueT> _vector; std::vector<ValueT> _vector;
@ -17,12 +18,23 @@ namespace ArbUt {
public: public:
List() noexcept : _vector() {} List() noexcept : _vector() {}
/// @brief Creates a list with a reserved capacity.
/// @param capacity The number of spaces the list should reserve.
explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); } explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); }
/// @brief Initialise the list from an initializer_list.
/// @param l The initializer_list to initialise from.
List(const std::initializer_list<ValueT>& l) noexcept : _vector(l) {} List(const std::initializer_list<ValueT>& l) noexcept : _vector(l) {}
/// @brief Initialise a list from a memory range.
/// @param begin A pointer to the beginning of the memory range.
/// @param end A pointer to the end of the memory range.
List(const ValueT* begin, const ValueT* end) noexcept : _vector(begin, end) {} List(const ValueT* begin, const ValueT* end) noexcept : _vector(begin, end) {}
/// @brief Removes all items from the list.
inline void Clear() noexcept { _vector.clear(); } inline void Clear() noexcept { _vector.clear(); }
/// @brief Gets an item at a position. Asserts whether the index is within bounds.
/// @param index The index requested.
/// @return A reference to the item at the given index.
inline reference At(size_t index) { inline reference At(size_t index) {
#ifndef NO_ASSERT #ifndef NO_ASSERT
if (index >= _vector.size()) { if (index >= _vector.size()) {
@ -34,6 +46,9 @@ namespace ArbUt {
return _vector.at(index); return _vector.at(index);
} }
/// @brief Gets an item at a position. Asserts whether the index is within bounds.
/// @param index The index requested.
/// @return A reference to the item at the given index.
inline const const_reference& At(size_t index) const { inline const const_reference& At(size_t index) const {
#ifndef NO_ASSERT #ifndef NO_ASSERT
if (index >= _vector.size()) { if (index >= _vector.size()) {
@ -45,6 +60,10 @@ namespace ArbUt {
return _vector.at(index); return _vector.at(index);
} }
/// @brief Sets an item to a given position. Asserts whether the index is within bounds
/// @param index The index requested.
/// @param value The value of the item.
/// @return The previous item at the given position.
inline const const_reference& Set(size_t index, const ValueT& value) { inline const const_reference& Set(size_t index, const ValueT& value) {
#ifndef NO_ASSERT #ifndef NO_ASSERT
if (index >= _vector.size()) { if (index >= _vector.size()) {
@ -58,13 +77,16 @@ namespace ArbUt {
return prev; return prev;
} }
/// @brief Check whether an item exist in the list.
/// @param value A reference to the item we're checking for.
/// @return Whether the item exists in the list.
inline bool Contains(const ValueT& value) const { inline bool Contains(const ValueT& value) const {
return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); return std::find(_vector.begin(), _vector.end(), value) != _vector.end();
} }
/// Find the index of the first occurrence of a value in the list, return -1 if none is found. /// @brief Find the index of the first occurrence of a value in the list, return -1 if none is found.
/// \param value The value we want the index for. /// @param value The value we want the index for.
/// \return The index of the first occurrence of the value in the list, or -1 if none is found. /// @return The index of the first occurrence of the value in the list, or -1 if none is found.
inline size_t IndexOf(const ValueT& value) const noexcept { inline size_t IndexOf(const ValueT& value) const noexcept {
const auto& it = std::find(_vector.begin(), _vector.end(), value); const auto& it = std::find(_vector.begin(), _vector.end(), value);
if (it == _vector.end()) if (it == _vector.end())
@ -72,30 +94,66 @@ namespace ArbUt {
return std::distance(_vector.begin(), it); return std::distance(_vector.begin(), it);
} }
/// @brief Adds an item to the end of the list.
/// @param value The item that needs to be added.
inline void Append(const ValueT& value) { _vector.push_back(value); } inline void Append(const ValueT& value) { _vector.push_back(value); }
/// @brief Instantiate an item at the end of the list.
/// @tparam parameters Type argument of constructor args.
/// @param pars The arguments of the constructor for the item.
template <class... parameters> inline void CreateBack(parameters... pars) { _vector.emplace_back(pars...); } template <class... parameters> inline void CreateBack(parameters... pars) { _vector.emplace_back(pars...); }
/// @brief Inserts an item into a position in the list.
/// @param index The index the item should be.
/// @param value The item that should be in the position.
inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); } inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
/// @brief Returns the item at an index.
/// @param index The index we're looking for.
/// @return The item at the given index.
inline reference operator[](size_t index) { return At(index); } inline reference operator[](size_t index) { return At(index); }
/// @brief Returns the item at an index.
/// @param index The index we're looking for.
/// @return The item at the given index.
inline const const_reference& operator[](size_t index) const { return At(index); } inline const const_reference& operator[](size_t index) const { return At(index); }
/// @brief Returns the number of items in the list.
/// @return The number of items in the list.
inline size_t Count() const noexcept { return _vector.size(); } inline size_t Count() const noexcept { return _vector.size(); }
/// @brief Reserve a number of spaces for the list to use.
/// @param size A value that is greater or equal to the current size of the list.
inline void Reserve(size_t size) { _vector.reserve(size); } inline void Reserve(size_t size) { _vector.reserve(size); }
/// @brief Resize the list to a certain size.
/// @param size The new size the list should be.
inline void Resize(size_t size) { _vector.resize(size); } inline void Resize(size_t size) { _vector.resize(size); }
/// @brief Resize the list to a certain size.
/// @param size The new size the list should be.
/// @param defaultValue The default value new items should be instantiated with.
inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); } inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); }
/// @brief Remove an item at an index.
/// @param index The index to remove the item from.
inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); } inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); }
/// @brief returns an iterator to the beginning of the specified bucket
iterator begin() noexcept { return _vector.begin(); } iterator begin() noexcept { return _vector.begin(); }
/// @brief returns an iterator to the beginning of the specified bucket
const_iterator begin() const noexcept { return _vector.begin(); } const_iterator begin() const noexcept { return _vector.begin(); }
/// @brief returns an iterator to the end of the specified bucket
iterator end() noexcept { return _vector.end(); } iterator end() noexcept { return _vector.end(); }
/// @brief returns an iterator to the end of the specified bucket
const_iterator end() const noexcept { return _vector.end(); } const_iterator end() const noexcept { return _vector.end(); }
/// @brief Return a raw pointer to the beginning of the list.
/// @return A raw array pointer to the beginning of the list.
const ValueT* RawData() const noexcept { return _vector.data(); } const ValueT* RawData() const noexcept { return _vector.data(); }
/// @brief Returns a std::vector representation of the current list.
/// @return A std::vector representation of the current list.
const std::vector<ValueT>& GetStdList() const noexcept { return _vector; } const std::vector<ValueT>& GetStdList() const noexcept { return _vector; }
/// @brief Returns a std::vector representation of the current list.
/// @return A std::vector representation of the current list.
std::vector<ValueT>& GetStdList() noexcept { return _vector; } std::vector<ValueT>& GetStdList() noexcept { return _vector; }
}; };
} }

View File

@ -2,10 +2,10 @@
#include <cmath> #include <cmath>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <variant>
#include <vector> #include <vector>
#include "MacroUtils.hpp" #include "MacroUtils.hpp"
#include "StringView.hpp" #include "StringView.hpp"
#include <variant>
#define ENUM_VALUE(x, value) x = value, #define ENUM_VALUE(x, value) x = value,
# #

View File

@ -13,18 +13,21 @@
#endif #endif
namespace ArbUt { namespace ArbUt {
/// @brief Implementation of std::logic_error that gives a stack trace when thrown.
class Exception : std::logic_error { class Exception : std::logic_error {
#if !WINDOWS #if !WINDOWS
backward::StackTrace _stack; backward::StackTrace _stack;
#endif #endif
public: public:
/// @brief Throw an exception with specified message.
explicit Exception(const std::string& msg) : std::logic_error(msg) { explicit Exception(const std::string& msg) : std::logic_error(msg) {
#if !WINDOWS #if !WINDOWS
_stack.load_here(32); _stack.load_here(32);
#endif #endif
} }
/// @brief Copy operator.
Exception(const Exception& e) noexcept Exception(const Exception& e) noexcept
: std::logic_error(e.what()) : std::logic_error(e.what())
#if !WINDOWS #if !WINDOWS
@ -34,10 +37,14 @@ namespace ArbUt {
{ {
} }
/// @brief Returns the error message of the exception.
[[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { [[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override {
return logic_error::what(); return logic_error::what();
} }
/// @brief Returns the stacktrace of the exception.
/// @param depth The max number of stacks it should retrieve.
/// @param include_addr Whether or not the address should be shown if the line information could not be retrieved.
[[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6, [[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6,
[[maybe_unused]] bool include_addr = true) const { [[maybe_unused]] bool include_addr = true) const {
#if !WINDOWS #if !WINDOWS
@ -48,6 +55,10 @@ namespace ArbUt {
} }
#if !WINDOWS #if !WINDOWS
/// @brief Builds a string from a given stack.
/// @param stack The stack to build the string from.
/// @param depth The max number of stacks it should retrieve.
/// @param include_addr Whether or not the address should be shown if the line information could not be retrieved.
static std::string BuildStacktraceFromStack(const backward::StackTrace& stack, size_t depth = 6, static std::string BuildStacktraceFromStack(const backward::StackTrace& stack, size_t depth = 6,
bool include_addr = true) { bool include_addr = true) {
if (stack.size() == 0) { if (stack.size() == 0) {

View File

@ -5,45 +5,62 @@
#include "../Assert.hpp" #include "../Assert.hpp"
namespace ArbUt { namespace ArbUt {
/// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from /// @brief A borrowed pointer is used to indicate a pointer that is not owned by its holder.
/// another owning object that is assumed to always be kept alive during the entire lifetime of the borrowing /// @details A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed
/// object. /// from another owning object that is assumed to always be kept alive during the entire lifetime of the borrowing
// object.
template <class T> class BorrowedPtr { template <class T> class BorrowedPtr {
private: private:
T* _raw; T* _raw;
public: public:
/// @brief Initialise a BorrowedPtr with a null pointer.
inline BorrowedPtr<T>() noexcept : _raw(nullptr){}; inline BorrowedPtr<T>() noexcept : _raw(nullptr){};
/// @brief Initialise a BorrowedPtr with a specific raw pointer.
inline BorrowedPtr<T>(T* ptr) noexcept : _raw(ptr){}; inline BorrowedPtr<T>(T* ptr) noexcept : _raw(ptr){};
/// @brief Initialise a BorrowedPtr from a copy.
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) noexcept : _raw(other._raw){}; inline BorrowedPtr<T>(const BorrowedPtr<T>& other) noexcept : _raw(other._raw){};
/// @brief Initialise a BorrowedPtr with a std unique_ptr.
inline BorrowedPtr<T>(const std::unique_ptr<T>& other) noexcept : _raw(other.get()){}; inline BorrowedPtr<T>(const std::unique_ptr<T>& other) noexcept : _raw(other.get()){};
~BorrowedPtr() noexcept = default; ~BorrowedPtr() noexcept = default;
/// @brief Copy operator.
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) noexcept { inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) noexcept {
if(this == &rhs)
return *this;
_raw = rhs._raw; _raw = rhs._raw;
return *this; return *this;
} }
/// @brief Operator for access into underlying pointer.
/// @warning Note that this asserts that the underlying pointer is not null first, to prevent segfaults.
inline T* operator->() const { inline T* operator->() const {
AssertNotNull(_raw); AssertNotNull(_raw);
return _raw; return _raw;
} }
/// @brief Get the raw underlying pointer.
inline T* GetRaw() const noexcept { return _raw; } inline T* GetRaw() const noexcept { return _raw; }
/// @brief Check equality of two BorrowedPtr objects
inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _raw == rhs._raw; } inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _raw == rhs._raw; }
/// @brief Check equality of two BorrowedPtr objects
inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _raw != rhs._raw; } inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _raw != rhs._raw; }
/// @brief Returns whether the underlying pointer is a nullptr or not.
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; } [[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
/// @brief Returns a const version of the underlying pointer.
inline BorrowedPtr<const T> Const() const noexcept { return BorrowedPtr<const T>(_raw); } inline BorrowedPtr<const T> Const() const noexcept { return BorrowedPtr<const T>(_raw); }
/// @brief Casts the underlying pointer to another type using dynamic_cast.
template <class TCast> inline BorrowedPtr<TCast> As() const { template <class TCast> inline BorrowedPtr<TCast> As() const {
auto cast = dynamic_cast<TCast*>(_raw); auto cast = dynamic_cast<TCast*>(_raw);
return BorrowedPtr<TCast>(cast); return BorrowedPtr<TCast>(cast);
} }
/// @brief Try to cast the underlying pointer to another type using dynamic_cast.
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const noexcept { template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const noexcept {
auto cast = dynamic_cast<TCast*>(_raw); auto cast = dynamic_cast<TCast*>(_raw);
if (cast == nullptr) if (cast == nullptr)
@ -52,6 +69,7 @@ namespace ArbUt {
return true; return true;
} }
/// @brief Force cast the underlying pointer to another type using reinterpret_cast.
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const noexcept { template <class TCast> inline BorrowedPtr<TCast> ForceAs() const noexcept {
auto cast = reinterpret_cast<TCast*>(_raw); auto cast = reinterpret_cast<TCast*>(_raw);
return BorrowedPtr<TCast>(cast); return BorrowedPtr<TCast>(cast);
@ -60,6 +78,7 @@ namespace ArbUt {
} }
namespace std { namespace std {
/// @brief Helper class for allowing hashing of BorrowedPtr.
template <class T> struct hash<ArbUt::BorrowedPtr<T>> { template <class T> struct hash<ArbUt::BorrowedPtr<T>> {
std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); } std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); }
}; };

View File

@ -8,6 +8,7 @@
#include "BorrowedPtr.hpp" #include "BorrowedPtr.hpp"
namespace ArbUt { namespace ArbUt {
/// @brief Collection of pointers that is owned by the list. When the list exits scope, the destructor on all pointers will be called.
template <class ValueT> class UniquePtrList { template <class ValueT> class UniquePtrList {
private: private:
std::vector<ValueT*> _vector; std::vector<ValueT*> _vector;

View File

@ -8,7 +8,8 @@
#include "Assert.hpp" #include "Assert.hpp"
namespace ArbUt { namespace ArbUt {
/// @brief A helper class for getting random numbers.
/// @tparam RandomT A type for the desired random number generator.
template <class RandomT> class BaseRandom { template <class RandomT> class BaseRandom {
private: private:
uint_fast32_t _seed; uint_fast32_t _seed;
@ -22,62 +23,69 @@ namespace ArbUt {
.count()), .count()),
_rng(_seed), _distribution(0.0, 1.0) {} _rng(_seed), _distribution(0.0, 1.0) {}
/// @brief Instantiate random class with a specific seed.
/// @param seed The seed the random number generator should be instantiated with.
explicit inline constexpr BaseRandom(uint_fast32_t seed) noexcept explicit inline constexpr BaseRandom(uint_fast32_t seed) noexcept
: _seed(seed), _rng(seed), _distribution(0.0, 1.0){}; : _seed(seed), _rng(seed), _distribution(0.0, 1.0){};
/// @brief The random number generator that is backing the random class.
inline RandomT& GetRandomEngine() noexcept { return _rng; } inline RandomT& GetRandomEngine() noexcept { return _rng; }
/// Gets a random float between 0.0 and 1.0. /// @brief Gets a random float between 0.0 and 1.0.
[[nodiscard]] inline constexpr float GetFloat() noexcept { return static_cast<float>(GetDouble()); } [[nodiscard]] inline constexpr float GetFloat() noexcept { return static_cast<float>(GetDouble()); }
/// Gets a random double between 0.0 and 1.0. /// @brief Gets a random double between 0.0 and 1.0.
[[nodiscard]] inline constexpr double GetDouble() noexcept { return _distribution(_rng); } [[nodiscard]] inline constexpr double GetDouble() noexcept { return _distribution(_rng); }
/// Gets a random 32 bit integer /// @brief Gets a random 32 bit integer
[[nodiscard]] inline constexpr int32_t Get() noexcept { return static_cast<int32_t>(_rng()); } [[nodiscard]] inline constexpr int32_t Get() noexcept { return static_cast<int32_t>(_rng()); }
/// Gets a random 32 bit integer between 0, and given max parameter. /// @brief Gets a random 32 bit integer between 0, and given max parameter.
/// \param max The exclusive max value the random value should be. /// @param max The exclusive max value the random value should be.
[[nodiscard]] inline int32_t Get(int32_t max) { [[nodiscard]] inline int32_t Get(int32_t max) {
Assert(max > 0); Assert(max > 0);
std::uniform_int_distribution<int32_t> distribution(0, max - 1); std::uniform_int_distribution<int32_t> distribution(0, max - 1);
return distribution(_rng); return distribution(_rng);
} }
/// Gets a random 32 bit integer between given min and max parameters. /// @brief Gets a random 32 bit integer between given min and max parameters.
/// \param min The inclusive min value the random value should be. /// @param min The inclusive min value the random value should be.
/// \param max The exclusive max value the random value should be. /// @param max The exclusive max value the random value should be.
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) { [[nodiscard]] inline int32_t Get(int32_t min, int32_t max) {
Assert(max > min); Assert(max > min);
std::uniform_int_distribution<int32_t> distribution(min, max - 1); std::uniform_int_distribution<int32_t> distribution(min, max - 1);
return distribution(_rng); return distribution(_rng);
} }
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int. /// @brief Gets a random 32 bit unsigned integer between 0 and max unsigned int.
[[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept { return _rng(); } [[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept { return _rng(); }
/// Gets a random 32 bit unsigned integer between 0, and given max parameter. /// @brief Gets a random 32 bit unsigned integer between 0, and given max parameter.
/// \param max The exclusive max value the random value should be. /// @param max The exclusive max value the random value should be.
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) noexcept { [[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) noexcept {
std::uniform_int_distribution<uint32_t> distribution(0, max - 1); std::uniform_int_distribution<uint32_t> distribution(0, max - 1);
return distribution(_rng); return distribution(_rng);
} }
/// Gets a random 32 bit unsigned integer between given min and max parameters. /// @brief Gets a random 32 bit unsigned integer between given min and max parameters.
/// \param min The inclusive min value the random value should be. /// @param min The inclusive min value the random value should be.
/// \param max The exclusive max value the random value should be. /// @param max The exclusive max value the random value should be.
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) { [[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) {
Assert(max > min); Assert(max > min);
std::uniform_int_distribution<uint32_t> distribution(min, max - 1); std::uniform_int_distribution<uint32_t> distribution(min, max - 1);
return distribution(_rng); return distribution(_rng);
} }
/// @brief The seed the random class is generating from.
[[nodiscard]] inline constexpr uint_fast32_t GetSeed() const noexcept { return _seed; } [[nodiscard]] inline constexpr uint_fast32_t GetSeed() const noexcept { return _seed; }
}; };
/// @brief Implementation of the BaseRandom class with pcg32 as random number generator.
class Random : public BaseRandom<pcg32> { class Random : public BaseRandom<pcg32> {
public: public:
constexpr Random() noexcept : BaseRandom() {} constexpr Random() noexcept : BaseRandom() {}
/// @brief Instantiate random class with a specific seed.
/// @param seed The seed the random number generator should be instantiated with.
explicit constexpr Random(uint_fast32_t seed) noexcept : BaseRandom(seed) {} explicit constexpr Random(uint_fast32_t seed) noexcept : BaseRandom(seed) {}
}; };
} }

View File

@ -1,5 +1,7 @@
#include "SignalHandling.hpp" #include "SignalHandling.hpp"
#if !WINDOWS #if !WINDOWS
/// \cond SKIP
void (*ArbUt::SignalHandling::_callback)(const char*) = nullptr; void (*ArbUt::SignalHandling::_callback)(const char*) = nullptr;
/// \endcond
#endif #endif

View File

@ -164,4 +164,17 @@ namespace ArbUt {
void SetSignalCallback(void (*callback)(const char*)); void SetSignalCallback(void (*callback)(const char*));
} }
/// @class ArbUt::SignalHandling
/// @brief Handling of intercepting signals, allowing us to get stack traces and report them to calling libraries.
/// @fn ArbUt::SignalHandling::SignalHandling(const std::vector<int>& = std::vector<int>())
/// @brief Creates a signal handler
/// @fn ArbUt::SignalHandling::init()
/// @brief Whether or not the signal handler is initialised
/// @fn ArbUt::SignalHandling::loaded()
/// @brief Whether or not the signal handler is loaded
#endif // ARBUTILS_SIGNALHANDLING_HPP #endif // ARBUTILS_SIGNALHANDLING_HPP

View File

@ -2,34 +2,53 @@
#define ARBUTILS_BASICSTRINGVIEW_HPP #define ARBUTILS_BASICSTRINGVIEW_HPP
#include <iostream> #include <iostream>
#include <string> #include <string>
/// \defgroup Strings Strings
/// \brief Group of non-editable strings with faster hashing.
namespace ArbUt { namespace ArbUt {
/// \ingroup Strings
/// @brief Abstract base class of the string views.
class BasicStringView { class BasicStringView {
protected: protected:
size_t _length = 0; size_t _length = 0;
uint32_t _hash = 0; uint32_t _hash = 0;
/// @brief Construct a basic string view.
constexpr BasicStringView(size_t length, uint32_t hash) : _length(length), _hash(hash) {} constexpr BasicStringView(size_t length, uint32_t hash) : _length(length), _hash(hash) {}
public: public:
/// @brief The amount of characters of the string.
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } [[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; }
/// @brief The unique hash of the string.
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; } [[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; }
/// @brief The unique hash of the string.
[[nodiscard]] inline constexpr std::size_t operator()() const noexcept { return _hash; } [[nodiscard]] inline constexpr std::size_t operator()() const noexcept { return _hash; }
/// @brief The unique hash of the string.
[[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; } [[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; }
/// @brief Check whether two StringViews are equal
[[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept { [[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept {
return _hash == rhs._hash; return _hash == rhs._hash;
} }
/// @brief Check whether two StringViews are unequal
inline constexpr bool operator!=(const BasicStringView& rhs) const noexcept { return _hash != rhs._hash; } inline constexpr bool operator!=(const BasicStringView& rhs) const noexcept { return _hash != rhs._hash; }
/// @brief Check whether a string view has a length of 0.
inline constexpr bool IsEmpty() const noexcept { return Length() == 0; } inline constexpr bool IsEmpty() const noexcept { return Length() == 0; }
/// @brief The C string interpretation of the string.
[[nodiscard]] virtual constexpr const char* c_str() const noexcept = 0; [[nodiscard]] virtual constexpr const char* c_str() const noexcept = 0;
/// @brief The C++ std string interpretation of the string.
[[nodiscard]] virtual constexpr std::string_view std_str() const noexcept = 0; [[nodiscard]] virtual constexpr std::string_view std_str() const noexcept = 0;
/// @brief Check the equality between a std string_view and our StringViews.
virtual constexpr bool operator==(const std::string_view& rhs) const noexcept = 0; virtual constexpr bool operator==(const std::string_view& rhs) const noexcept = 0;
/// @brief Check the inequality between a std string_view and our StringViews.
virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0; virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0;
/// @brief Check the equality between a C string and our StringViews.
virtual constexpr bool operator==(const char* rhs) const noexcept = 0; virtual constexpr bool operator==(const char* rhs) const noexcept = 0;
/// @brief Check the equality between a C string and our StringViews.
virtual constexpr bool operator!=(const char* rhs) const noexcept = 0; virtual constexpr bool operator!=(const char* rhs) const noexcept = 0;
/// @brief Pipe the current string into an output stream.
friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) { friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) {
out << c.c_str(); out << c.c_str();
return out; return out;

View File

@ -37,6 +37,8 @@ static uint32_t constexpr Hash(char const* input) {
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; } static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
namespace ArbUt { namespace ArbUt {
/// \ingroup Strings
/// @brief A constant, non-editable string that allows copies to share the same string object.
class StringView final : public BasicStringView { class StringView final : public BasicStringView {
private: private:
static std::shared_ptr<__ConstStringCharHolder> __emptyString; static std::shared_ptr<__ConstStringCharHolder> __emptyString;
@ -91,6 +93,7 @@ namespace ArbUt {
} }
namespace std { namespace std {
/// @brief Helper class for hashing string views.
template <> struct hash<ArbUt::StringView> { template <> struct hash<ArbUt::StringView> {
constexpr std::size_t operator()(ArbUt::StringView const& s) const noexcept { return s.GetHash(); } constexpr std::size_t operator()(ArbUt::StringView const& s) const noexcept { return s.GetHash(); }
}; };

View File

@ -6,6 +6,8 @@
#include "StringView.hpp" #include "StringView.hpp"
namespace ArbUt { namespace ArbUt {
/// \ingroup Strings
/// @brief A literal representation of a string view. Used for compile time string processing.
class StringViewLiteral final : public BasicStringView { class StringViewLiteral final : public BasicStringView {
private: private:
const char* _str; const char* _str;
@ -14,23 +16,26 @@ namespace ArbUt {
constexpr StringViewLiteral(const char* str, size_t size) noexcept constexpr StringViewLiteral(const char* str, size_t size) noexcept
: BasicStringView(size, Hash(str)), _str(str) {} : BasicStringView(size, Hash(str)), _str(str) {}
constexpr StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){}; constexpr StringViewLiteral(const char* str) noexcept : StringViewLiteral(str, CalcLength(str)){};
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } [[nodiscard]] inline constexpr const char* c_str() const noexcept override { return _str; }
constexpr std::string_view std_str() const noexcept { return std::string_view(_str, _length); } [[nodiscard]] constexpr std::string_view std_str() const noexcept override {
return std::string_view(_str, _length);
}
constexpr std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); } constexpr std::size_t operator()(StringViewLiteral const& s) const noexcept { return s.GetHash(); }
inline operator StringView() const noexcept { return StringView(*this, _str, _length); } inline operator StringView() const noexcept { return StringView(*this, _str, _length); }
inline constexpr bool operator==(const std::string_view& rhs) const noexcept { inline constexpr bool operator==(const std::string_view& rhs) const noexcept override {
return _hash == Hash(rhs.data()); return _hash == Hash(rhs.data());
} }
inline constexpr bool operator!=(const std::string_view& rhs) const noexcept { inline constexpr bool operator!=(const std::string_view& rhs) const noexcept override {
return _hash != Hash(rhs.data()); return _hash != Hash(rhs.data());
} }
inline constexpr bool operator==(const char* rhs) const noexcept { return _hash == Hash(rhs); } inline constexpr bool operator==(const char* rhs) const noexcept override { return _hash == Hash(rhs); }
inline constexpr bool operator!=(const char* rhs) const noexcept { return _hash != Hash(rhs); } inline constexpr bool operator!=(const char* rhs) const noexcept override { return _hash != Hash(rhs); }
}; };
} }
namespace std { namespace std {
/// @brief Helper class for getting the hash of a string view literal.
template <> struct hash<ArbUt::StringViewLiteral> { template <> struct hash<ArbUt::StringViewLiteral> {
constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); } constexpr std::size_t operator()(ArbUt::StringViewLiteral const& s) const noexcept { return s.GetHash(); }
}; };

View File

@ -46,7 +46,7 @@ TEST_CASE("Literal stringview to non literal, then use", "[Utilities]") {
REQUIRE(strcmp(val.c_str(), "foobar") == 0); REQUIRE(strcmp(val.c_str(), "foobar") == 0);
} }
TEST_CASE("Pipe stringview into strean", "[Utilities]") { TEST_CASE("Pipe stringview into stream", "[Utilities]") {
std::stringstream ss; std::stringstream ss;
ArbUt::StringView val = "foo"; ArbUt::StringView val = "foo";
ss << val; ss << val;