From a570e494bca43c30d7ec91d293051c0c818509b5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 23 Jun 2022 17:05:43 +0200 Subject: feat: add statusline mode styling --- src/util/ranges.hpp | 47 +++++++++++++++++++++++++++++++++++++ src/util/ranges_impl.hpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/util/ranges.hpp create mode 100644 src/util/ranges_impl.hpp (limited to 'src/util') diff --git a/src/util/ranges.hpp b/src/util/ranges.hpp new file mode 100644 index 0000000..c47c7b5 --- /dev/null +++ b/src/util/ranges.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +template +class IotaViewIterator +{ +public: + constexpr explicit IotaViewIterator(Value value) noexcept; + + constexpr auto operator++() noexcept -> const IotaViewIterator &; + constexpr auto operator++(int) noexcept -> IotaViewIterator; + + constexpr auto operator*() const noexcept -> Value; + + constexpr auto operator==(const IotaViewIterator &rhs) const noexcept -> bool; + constexpr auto operator!=(const IotaViewIterator &rhs) const noexcept -> bool; + +private: + Value _value; +}; + +/** + * A range factory that generates a sequence of elements by repeatedly incrementing an + * initial value. + * + * This class was created because C++20 ranges is a complete shitshow in Clang. + * https://github.com/llvm/llvm-project/issues/52696 + */ +template +requires std::equality_comparable_with && std::copyable +class IotaView +{ +public: + constexpr IotaView(Value value, Bound bound) noexcept; + + [[nodiscard]] constexpr auto begin() const noexcept -> IotaViewIterator; + + [[nodiscard]] constexpr auto end() const noexcept -> IotaViewIterator; + +private: + Value _value; + Bound _bound; +}; + +#include "ranges_impl.hpp" diff --git a/src/util/ranges_impl.hpp b/src/util/ranges_impl.hpp new file mode 100644 index 0000000..b18a11c --- /dev/null +++ b/src/util/ranges_impl.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include "ranges.hpp" + +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define IOTA_VIEW_ITERATOR(return_type) \ + template \ + constexpr return_type IotaViewIterator + +IOTA_VIEW_ITERATOR()::IotaViewIterator(Value value) noexcept : _value(value) {} + +IOTA_VIEW_ITERATOR(auto)::operator++() noexcept -> const IotaViewIterator & +{ + ++_value; + + return *this; +} + +IOTA_VIEW_ITERATOR(auto)::operator++(int) noexcept -> IotaViewIterator +{ + auto copy = *this; + + ++(*this); + + return copy; +} + +IOTA_VIEW_ITERATOR(auto)::operator*() const noexcept -> Value +{ + return _value; +} + +IOTA_VIEW_ITERATOR(auto)::operator==(const IotaViewIterator &rhs) const noexcept -> bool +{ + return _value == rhs._value; +} + +IOTA_VIEW_ITERATOR(auto)::operator!=(const IotaViewIterator &rhs) const noexcept -> bool +{ + return !(*this == rhs); +} + +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define IOTA_VIEW(return_type) \ + template \ + requires std::equality_comparable_with && std::copyable \ + constexpr return_type IotaView + +IOTA_VIEW()::IotaView(Value value, Bound bound) noexcept : _value(value), _bound(bound) {} + +IOTA_VIEW(auto)::begin() const noexcept -> IotaViewIterator +{ + return IotaViewIterator(_value); +} + +IOTA_VIEW(auto)::end() const noexcept -> IotaViewIterator +{ + return IotaViewIterator(_bound); +} + -- cgit v1.2.3-18-g5258