/* * * 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 . */ #ifndef BLT_UTILITY_H #define BLT_UTILITY_H #include #include #include #include #if defined(__GNUC__) #include namespace blt { static inline std::string demangle(const std::string& str) { 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; } } #else namespace blt { static inline std::string demangle(const std::string& str) { return str; } } #endif namespace blt { template static BLT_CPP20_CONSTEXPR inline std::string type_string() { return demangle(typeid(T).name()); } template class enumerator { public: class iterator { 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; TYPE_ITR current; public: explicit iterator(TYPE_ITR current): current(std::move(current)) {} iterator& operator++() { ++index; ++current; return *this; } bool operator==(iterator other) const { return current == other.current; } bool operator!=(iterator other) const { return current != other.current; } std::pair operator*() const { return {index, *current}; }; std::pair operator*() { return {index, *current}; }; }; explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin)), end_(std::move(end)) {} iterator begin() { return begin_; } iterator end() { return end_; } private: iterator begin_; iterator end_; }; template static inline auto enumerate(const T& container) { return enumerator{container.begin(), container.end()}; } template static inline auto enumerate(T& container) { return enumerator{container.begin(), container.end()}; } template 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; } }; 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); } }; struct unexpect_t { explicit unexpect_t() = default; }; inline constexpr unexpect_t unexpect{}; template using remove_cvref_t = std::remove_reference_t>; template class unexpected { private: E e; public: constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; template, unexpected> && !std::is_same_v, std::in_place_t> && std::is_constructible_v, bool> = true> constexpr explicit unexpected(Err&& e): e(std::forward(e)) {} template, bool> = true> constexpr explicit unexpected(std::in_place_t, Args&& ... args): e(std::forward(args)...) {} template&, Args...>, bool> = true> constexpr explicit unexpected(std::in_place_t, std::initializer_list il, Args&& ... args): e(il, std::forward(args)...) {} constexpr const E& error() const& noexcept { return e; } constexpr E& error()& noexcept { return e; } constexpr const E&& error() const&& noexcept { return e; } constexpr E&& error()&& noexcept { return e; } constexpr void swap(unexpected& other) noexcept(std::is_nothrow_swappable_v) { std::swap(error(), other.error()); } template inline friend constexpr bool operator==(const unexpected& x, const unexpected & y) { return x.error() == y.error(); } friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))) {} }; template unexpected(E) -> unexpected; template class bad_expected_access : public std::exception { private: E e; public: explicit bad_expected_access(E e): e(std::move(e)) {} const E& error() const& noexcept { return e; } E& error()& noexcept { return e; } const E&& error() const&& noexcept { return e; } E&& error()&& noexcept { return e; } [[nodiscard]] const char* what() const noexcept override { return "blt::expected does not contain a value!"; } }; template> class expected { protected: std::variant v; template inline static constexpr bool eight_insanity_v = std::is_constructible_v&> || std::is_constructible_v> || std::is_constructible_v&> || std::is_constructible_v> || std::is_convertible_v&, T> || std::is_convertible_v, T> || std::is_convertible_v&, T> || std::is_convertible_v, T>; template inline static constexpr bool four_insanity_v = std::is_constructible_v, expected&> || std::is_constructible_v, expected> || std::is_constructible_v, const expected&> || std::is_constructible_v, const expected>; public: template, bool> = true> constexpr expected() noexcept: v(T()) {} constexpr expected(const expected& copy) = delete; constexpr expected(expected&& move) noexcept: v(move ? std::move(*move) : std::move(move.error())) {} /* * (4)...(5) */ template, class GF = const G&, std::enable_if_t< (!std::is_convertible_v || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> constexpr explicit expected(const expected& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> constexpr explicit expected(expected&& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template, class GF = const G&, std::enable_if_t< (std::is_convertible_v && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> constexpr expected(const expected& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> constexpr expected(expected&& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} /* * (6) */ template && !std::is_same_v, void> && !std::is_same_v, std::in_place_t> && !std::is_same_v> && std::is_constructible_v && !std::is_same_v, unexpected> && !std::is_same_v, expected>, bool> = true> constexpr explicit expected(U&& v): v(T(std::forward(v))) {} template && !std::is_same_v, void> && !std::is_same_v, std::in_place_t> && !std::is_same_v> && std::is_constructible_v && !std::is_same_v, unexpected> && !std::is_same_v, expected>, bool> = true> constexpr expected(U&& v): v(T(std::forward(v))) {} /* * (7) */ template>, std::enable_if_t< !std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr explicit expected(const unexpected& e): v(std::forward(e.error())) {} template>, std::enable_if_t< std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr expected(const unexpected& e): v(std::forward(e.error())) {} /* * (8) */ template>, std::enable_if_t< !std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr explicit expected(unexpected&& e): v(std::forward(e.error())) {} template>, std::enable_if_t< std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr expected(unexpected&& e): v(std::forward(e.error())) {} /* * (9)...(13) */ template, bool> = true> constexpr explicit expected(std::in_place_t, Args&& ... args): v(T(std::forward(args)...)) {} template&, Args...>, bool> = true> constexpr explicit expected(std::in_place_t, std::initializer_list il, Args&& ... args): v(T(il, std::forward(args)...)) {} // template, void>, bool> = true> // constexpr explicit expected(std::in_place_t) noexcept: v(T()) // {} template, bool> = true> constexpr explicit expected(unexpect_t, Args&& ... args): v(E(std::forward(args)...)) {} template&, Args...>, bool> = true> constexpr explicit expected(unexpect_t, std::initializer_list il, Args&& ... args): v(E(il, std::forward(args)...)) {} expected& operator=(const expected& copy) = delete; expected& operator=(expected&& move) = default; [[nodiscard]] constexpr explicit operator bool() const noexcept { return std::holds_alternative(v); } [[nodiscard]] constexpr inline bool has_value() const noexcept { return std::holds_alternative(v); } constexpr T& value()& { if (*this) return std::get(v); else throw bad_expected_access(std::as_const(error())); } constexpr const T& value() const& { if (*this) return std::get(v); else throw bad_expected_access(std::as_const(error())); } constexpr T&& value()&& { if (*this) return std::get(v); else throw bad_expected_access(std::move(error())); } constexpr const T&& value() const&& { if (*this) return std::get(v); else throw bad_expected_access(std::move(error())); } constexpr const E& error() const& noexcept { return std::get(v); } constexpr E& error()& noexcept { return std::get(v); } constexpr const E&& error() const&& noexcept { return std::get(v); } constexpr E&& error()&& noexcept { return std::get(v); } template && std::is_copy_constructible_v, bool> = true> constexpr T value_or(U&& default_value) const& { return bool(*this) ? **this : static_cast(std::forward(default_value)); } template && std::is_move_constructible_v, bool> = true> constexpr T value_or(U&& default_value)&& { return bool(*this) ? std::move(**this) : static_cast(std::forward(default_value)); } constexpr inline const T* operator->() const noexcept { return &std::get(v); } constexpr inline T* operator->() noexcept { return &std::get(v); } constexpr inline const T& operator*() const& noexcept { return std::get(v); } constexpr inline T& operator*()& noexcept { return std::get(v); } constexpr inline const T&& operator*() const&& noexcept { return std::move(std::get(v)); } constexpr inline T&& operator*()&& noexcept { return std::move(std::get(v)); } }; template class expected : expected { public: using expected::expected; constexpr expected(const expected& copy): expected::v(copy ? *copy : copy.error()) {} expected& operator=(const expected& copy) = default; }; //#define BLT_LAMBDA(type, var, code) [](const type& var) -> auto { return code; } //#define BLT_LAMBDA(var, code) [](var) -> auto { return code; } /* * std::visit(blt::lambda_visitor{ * lambdas... * }, data_variant); */ // TODO: WTF template struct lambda_visitor : TLambdas ... { using TLambdas::operator()...; }; #if __cplusplus < 202002L // explicit deduction guide (not needed as of C++20) template lambda_visitor(TLambdas...) -> lambda_visitor; #endif #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 BLT_ATTRIB_NO_INLINE void black_box(const T& val) { static volatile void* hell; hell = (void*) &val; (void) hell; } template BLT_ATTRIB_NO_INLINE const T& black_box_ret(const T& val) { static volatile void* hell; hell = (void*) &val; (void) hell; return val; } } #endif //BLT_UTILITY_H