2023-11-24 13:45:14 -05:00
|
|
|
/*
|
|
|
|
* <Short Description>
|
|
|
|
* Copyright (C) 2023 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_UTILITY_H
|
|
|
|
#define BLT_UTILITY_H
|
|
|
|
|
2023-12-09 21:21:14 -05:00
|
|
|
#include <string>
|
2023-12-28 12:28:10 -05:00
|
|
|
#include <blt/compatibility.h>
|
2023-11-24 13:45:14 -05:00
|
|
|
|
2023-12-07 15:59:26 -05:00
|
|
|
#if defined(__GNUC__)
|
2023-12-08 20:11:58 -05:00
|
|
|
|
2023-12-07 15:59:26 -05:00
|
|
|
#include <cxxabi.h>
|
|
|
|
|
2023-12-08 20:11:58 -05:00
|
|
|
namespace blt
|
|
|
|
{
|
|
|
|
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
|
2023-12-07 15:59:26 -05:00
|
|
|
{
|
2023-12-08 20:11:58 -05:00
|
|
|
int status;
|
|
|
|
// only defined for GNU C++11?
|
|
|
|
char* demangled_name = abi::__cxa_demangle(str.c_str(), nullptr, nullptr, &status);
|
|
|
|
if (demangled_name == nullptr)
|
|
|
|
return str;
|
|
|
|
std::string ret_name = demangled_name;
|
|
|
|
std::free(demangled_name);
|
|
|
|
return ret_name;
|
2023-12-07 15:59:26 -05:00
|
|
|
}
|
2023-12-08 20:11:58 -05:00
|
|
|
}
|
2023-12-07 15:59:26 -05:00
|
|
|
#else
|
2023-12-08 20:11:58 -05:00
|
|
|
namespace blt
|
|
|
|
{
|
|
|
|
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
|
2023-12-07 15:59:26 -05:00
|
|
|
{
|
2023-12-08 20:11:58 -05:00
|
|
|
return str;
|
2023-12-07 15:59:26 -05:00
|
|
|
}
|
2023-12-08 20:11:58 -05:00
|
|
|
}
|
2023-12-07 15:59:26 -05:00
|
|
|
#endif
|
|
|
|
|
2023-11-24 13:45:14 -05:00
|
|
|
namespace blt
|
|
|
|
{
|
2023-12-09 13:44:16 -05:00
|
|
|
template<typename T>
|
|
|
|
static BLT_CPP20_CONSTEXPR inline std::string type_string()
|
|
|
|
{
|
|
|
|
return demangle(typeid(T).name());
|
|
|
|
}
|
|
|
|
|
2023-12-08 20:11:58 -05:00
|
|
|
template<typename TYPE_ITR>
|
2023-12-08 20:44:45 -05:00
|
|
|
class enumerator
|
2023-11-24 13:45:14 -05:00
|
|
|
{
|
2023-12-08 20:11:58 -05:00
|
|
|
public:
|
2023-12-08 20:44:45 -05:00
|
|
|
class iterator
|
2023-12-08 20:11:58 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
using value_type = typename TYPE_ITR::value_type;
|
|
|
|
using difference_type = typename TYPE_ITR::difference_type;
|
|
|
|
using pointer = typename TYPE_ITR::pointer;
|
|
|
|
using reference = typename TYPE_ITR::reference;
|
|
|
|
private:
|
|
|
|
size_t index = 0;
|
2023-12-08 20:44:45 -05:00
|
|
|
TYPE_ITR current;
|
2023-12-08 20:11:58 -05:00
|
|
|
public:
|
2023-12-09 13:44:16 -05:00
|
|
|
explicit iterator(TYPE_ITR current): current(std::move(current))
|
|
|
|
{}
|
2023-12-08 20:44:45 -05:00
|
|
|
|
|
|
|
iterator& operator++()
|
|
|
|
{
|
2023-12-09 13:44:16 -05:00
|
|
|
++index;
|
2023-12-08 20:44:45 -05:00
|
|
|
++current;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-12-09 13:44:16 -05:00
|
|
|
bool operator==(iterator other) const
|
2023-12-08 20:44:45 -05:00
|
|
|
{
|
2023-12-09 13:44:16 -05:00
|
|
|
return current == other.current;
|
2023-12-08 20:44:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(iterator other) const
|
2023-12-09 13:44:16 -05:00
|
|
|
{
|
|
|
|
return current != other.current;
|
|
|
|
}
|
2023-12-08 20:44:45 -05:00
|
|
|
|
|
|
|
std::pair<size_t, const reference> operator*() const
|
|
|
|
{
|
|
|
|
return {index, *current};
|
|
|
|
};
|
2023-12-09 13:44:16 -05:00
|
|
|
|
|
|
|
std::pair<size_t, reference> operator*()
|
|
|
|
{
|
|
|
|
return {index, *current};
|
|
|
|
};
|
2023-12-08 20:11:58 -05:00
|
|
|
};
|
2023-12-08 20:44:45 -05:00
|
|
|
|
2023-12-09 13:44:16 -05:00
|
|
|
explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end))
|
2023-12-08 20:44:45 -05:00
|
|
|
{}
|
|
|
|
|
|
|
|
iterator begin()
|
|
|
|
{
|
|
|
|
return begin_;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end()
|
|
|
|
{
|
|
|
|
return end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
iterator begin_;
|
|
|
|
iterator end_;
|
2023-11-24 13:45:14 -05:00
|
|
|
};
|
2023-12-08 20:44:45 -05:00
|
|
|
|
|
|
|
template<typename T>
|
2023-12-09 13:44:16 -05:00
|
|
|
static inline auto enumerate(const T& container)
|
|
|
|
{
|
|
|
|
return enumerator{container.begin(), container.end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline auto enumerate(T& container)
|
2023-12-08 20:44:45 -05:00
|
|
|
{
|
|
|
|
return enumerator{container.begin(), container.end()};
|
|
|
|
}
|
2023-12-10 13:21:26 -05:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct range
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct range_itr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
using difference_type = T;
|
|
|
|
using value_type = T;
|
|
|
|
using pointer = T*;
|
|
|
|
using reference = T&;
|
|
|
|
private:
|
|
|
|
T current;
|
|
|
|
bool forward;
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit range_itr(T current, bool forward): current(current), forward(forward)
|
|
|
|
{}
|
|
|
|
|
|
|
|
value_type operator*() const
|
|
|
|
{ return current; }
|
|
|
|
|
|
|
|
value_type operator->()
|
|
|
|
{ return current; }
|
|
|
|
|
|
|
|
range_itr& operator++()
|
|
|
|
{
|
|
|
|
if (forward)
|
|
|
|
current++;
|
|
|
|
else
|
|
|
|
current--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
range_itr& operator--()
|
|
|
|
{
|
|
|
|
if (forward)
|
|
|
|
current--;
|
|
|
|
else
|
|
|
|
current++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator==(const range_itr& a, const range_itr& b)
|
|
|
|
{
|
|
|
|
return a.current == b.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const range_itr& a, const range_itr& b)
|
|
|
|
{
|
|
|
|
return a.current != b.current;
|
|
|
|
}
|
|
|
|
};
|
2024-02-01 15:51:54 -05:00
|
|
|
|
2023-12-10 13:21:26 -05:00
|
|
|
private:
|
|
|
|
T _begin;
|
|
|
|
T _end;
|
|
|
|
T offset = 0;
|
|
|
|
public:
|
|
|
|
range(T begin, T end): _begin(begin), _end(end), offset(end < begin ? 1 : 0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
range_itr begin()
|
|
|
|
{
|
|
|
|
return range_itr(_begin - offset, offset == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
range_itr end()
|
|
|
|
{
|
|
|
|
// not sure if i like this
|
|
|
|
return range_itr(_end - offset, offset == 0);
|
|
|
|
}
|
|
|
|
};
|
2024-02-01 15:51:54 -05:00
|
|
|
|
|
|
|
template<typename T, typename E>
|
|
|
|
class expected
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<typename V>
|
|
|
|
inline static constexpr bool isCopyMovable()
|
|
|
|
{
|
|
|
|
return std::is_move_constructible_v<V> && std::is_copy_constructible_v<V>
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<T> t;
|
|
|
|
std::optional<E> e;
|
|
|
|
public:
|
|
|
|
constexpr expected() noexcept: t(T())
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr explicit expected(const T& t): t(t)
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr explicit expected(T&& t): t(std::forward(t))
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr explicit expected(const E& e): e(e)
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr explicit expected(E&& e): e(std::forward(e))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr explicit operator bool() const noexcept
|
|
|
|
{
|
|
|
|
return t.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr inline bool has_value() const noexcept
|
|
|
|
{
|
|
|
|
return t.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr T& value()&
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const T& value() const&
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr T&& value()&&
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const T&& value() const&&
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const E& error() const& noexcept
|
|
|
|
{
|
|
|
|
return e.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr E& error()& noexcept
|
|
|
|
{
|
|
|
|
return e.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const E&& error() const&& noexcept
|
|
|
|
{
|
|
|
|
return e.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr E&& error()&& noexcept
|
|
|
|
{
|
|
|
|
return e.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class U, std::enable_if_t<std::is_convertible_v<U, T> && std::is_copy_constructible_v<T>, int*> = 0>
|
|
|
|
constexpr T value_or(U&& default_value) const&
|
|
|
|
{
|
|
|
|
return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class U, std::enable_if_t<std::is_convertible_v<U, T> && std::is_move_constructible_v<T>, int*> = 0 >
|
|
|
|
constexpr T value_or(U&& default_value)&&
|
|
|
|
{
|
|
|
|
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T* operator->() const noexcept
|
|
|
|
{
|
|
|
|
return &t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T* operator->() noexcept
|
|
|
|
{
|
|
|
|
return &t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T& operator*() const& noexcept
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T& operator*()& noexcept
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline const T&& operator*() const&& noexcept
|
|
|
|
{
|
|
|
|
return t.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline T&& operator*()&& noexcept
|
|
|
|
{
|
|
|
|
return std::move(t.value());
|
|
|
|
}
|
|
|
|
};
|
2023-12-11 17:52:24 -05:00
|
|
|
|
|
|
|
//#define BLT_LAMBDA(type, var, code) [](const type& var) -> auto { return code; }
|
|
|
|
//#define BLT_LAMBDA(var, code) [](var) -> auto { return code; }
|
|
|
|
|
2024-01-24 17:03:20 -05:00
|
|
|
/*
|
|
|
|
* std::visit(blt::lambda_visitor{
|
|
|
|
* lambdas...
|
|
|
|
* }, data_variant);
|
|
|
|
*/
|
|
|
|
|
2023-12-11 17:52:24 -05:00
|
|
|
// TODO: WTF
|
2023-12-11 16:58:20 -05:00
|
|
|
template<class... TLambdas>
|
2024-02-01 15:51:54 -05:00
|
|
|
struct lambda_visitor : TLambdas ...
|
|
|
|
{
|
2023-12-11 16:58:20 -05:00
|
|
|
using TLambdas::operator()...;
|
|
|
|
};
|
2024-02-01 15:51:54 -05:00
|
|
|
|
2023-12-11 16:58:20 -05:00
|
|
|
#if __cplusplus < 202002L
|
|
|
|
|
|
|
|
// explicit deduction guide (not needed as of C++20)
|
|
|
|
template<class... TLambdas>
|
|
|
|
lambda_visitor(TLambdas...) -> lambda_visitor<TLambdas...>;
|
|
|
|
|
|
|
|
#endif
|
2023-12-08 20:44:45 -05:00
|
|
|
|
2023-11-26 17:35:06 -05:00
|
|
|
|
|
|
|
#if defined(__GNUC__) || defined(__llvm__)
|
|
|
|
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))
|
|
|
|
#else
|
|
|
|
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
|
|
|
#define BLT_ATTRIB_NO_INLINE __declspec(noinline)
|
|
|
|
#else
|
|
|
|
#define BLT_ATTRIB_NO_INLINE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<typename T>
|
2023-12-28 12:33:01 -05:00
|
|
|
BLT_ATTRIB_NO_INLINE void black_box(const T& val)
|
2023-11-26 17:35:06 -05:00
|
|
|
{
|
2023-12-09 13:44:16 -05:00
|
|
|
static volatile void* hell;
|
2023-11-26 17:35:06 -05:00
|
|
|
hell = (void*) &val;
|
2023-11-26 18:05:38 -05:00
|
|
|
(void) hell;
|
2023-11-26 17:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2023-12-28 12:33:01 -05:00
|
|
|
BLT_ATTRIB_NO_INLINE const T& black_box_ret(const T& val)
|
2023-11-26 17:35:06 -05:00
|
|
|
{
|
2023-12-09 13:44:16 -05:00
|
|
|
static volatile void* hell;
|
2023-11-26 17:35:06 -05:00
|
|
|
hell = (void*) &val;
|
2023-11-26 18:05:38 -05:00
|
|
|
(void) hell;
|
2023-11-26 17:35:06 -05:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:45:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_UTILITY_H
|