diff options
Diffstat (limited to 'src/utils/cpp')
-rw-r--r-- | src/utils/cpp/atomic.hpp | 4 | ||||
-rw-r--r-- | src/utils/cpp/expected.hpp | 4 | ||||
-rw-r--r-- | src/utils/cpp/transformed_range.hpp | 46 | ||||
-rw-r--r-- | src/utils/cpp/type_safe_arithmetic.hpp | 94 |
4 files changed, 73 insertions, 75 deletions
diff --git a/src/utils/cpp/atomic.hpp b/src/utils/cpp/atomic.hpp index ec1c7dc0..94bda75f 100644 --- a/src/utils/cpp/atomic.hpp +++ b/src/utils/cpp/atomic.hpp @@ -26,7 +26,7 @@ // libcxx adds support for notify_*() and wait(). // [https://libcxx.llvm.org/docs/Cxx2aStatus.html] template <class T> -class atomic { +class atomic { // NOLINT(readability-identifier-naming) public: atomic() = default; explicit atomic(T value) : value_{std::move(value)} {} @@ -97,7 +97,7 @@ class atomic { // std::atomic<std::shared_ptr<T>>, once libcxx adds support for it. // [https://libcxx.llvm.org/docs/Cxx2aStatus.html] template <class T> -class atomic_shared_ptr { +class atomic_shared_ptr { // NOLINT(readability-identifier-naming) using ptr_t = std::shared_ptr<T>; public: diff --git a/src/utils/cpp/expected.hpp b/src/utils/cpp/expected.hpp index 823c4650..da3955b7 100644 --- a/src/utils/cpp/expected.hpp +++ b/src/utils/cpp/expected.hpp @@ -20,7 +20,7 @@ // TODO(modernize): replace this by std::unexpected once we switched to C++23 template <class E> -class unexpected { +class unexpected { // NOLINT(readability-identifier-naming) public: explicit unexpected(E error) : error_{std::move(error)} {} [[nodiscard]] auto error() && -> E { return std::move(error_); } @@ -31,7 +31,7 @@ class unexpected { // TODO(modernize): replace this by std::expected once we switched to C++23 template <class T, class E> -class expected { +class expected { // NOLINT(readability-identifier-naming) public: expected(T value) noexcept // NOLINT : value_{std::in_place_index<0>, std::move(value)} {} diff --git a/src/utils/cpp/transformed_range.hpp b/src/utils/cpp/transformed_range.hpp index f28029f3..cc3fe5d6 100644 --- a/src/utils/cpp/transformed_range.hpp +++ b/src/utils/cpp/transformed_range.hpp @@ -30,16 +30,16 @@ /// \brief Transform iterable sequence "on the fly" invoking the given /// transformation callback. If the callback throws an exception, /// std::terminate is called. -/// \tparam Iterator Type of the iterator of the sequence to be +/// \tparam TIterator Type of the iterator of the sequence to be /// transformed. /// \tparam Result Type of the transformation result. -template <typename Iterator, typename Result> +template <typename TIterator, typename Result> class TransformedRange final { public: using converter_t = - std::function<Result(typename Iterator::value_type const&)>; + std::function<Result(typename TIterator::value_type const&)>; - class iterator final { + class Iterator final { public: using value_type = std::remove_reference_t<Result>; using pointer = value_type*; @@ -47,9 +47,9 @@ class TransformedRange final { using difference_type = std::ptrdiff_t; using iterator_category = std::forward_iterator_tag; - iterator() noexcept = default; - iterator(Iterator iterator, converter_t c) noexcept - : iterator_(std::move(iterator)), c_(std::move(c)) {} + Iterator() noexcept = default; + Iterator(TIterator Iterator, converter_t c) noexcept + : iterator_(std::move(Iterator)), c_(std::move(c)) {} auto operator*() const noexcept -> decltype(auto) { try { @@ -59,35 +59,35 @@ class TransformedRange final { } } - auto operator++() noexcept -> iterator& { + auto operator++() noexcept -> Iterator& { ++iterator_; return *this; } - [[nodiscard]] friend auto operator==(iterator const& lhs, - iterator const& rhs) noexcept + [[nodiscard]] friend auto operator==(Iterator const& lhs, + Iterator const& rhs) noexcept -> bool { return lhs.iterator_ == rhs.iterator_; } - [[nodiscard]] friend auto operator!=(iterator const& lhs, - iterator const& rhs) noexcept + [[nodiscard]] friend auto operator!=(Iterator const& lhs, + Iterator const& rhs) noexcept -> bool { return not(lhs == rhs); } private: - Iterator iterator_{}; + TIterator iterator_{}; converter_t c_{}; }; - TransformedRange(Iterator begin, Iterator end, converter_t c) noexcept + TransformedRange(TIterator begin, TIterator end, converter_t c) noexcept : begin_{std::move(begin), std::move(c)}, end_{std::move(end), nullptr} {} - [[nodiscard]] auto begin() const noexcept -> iterator { return begin_; } - [[nodiscard]] auto end() const noexcept -> iterator { return end_; } - [[nodiscard]] auto size() const -> typename iterator::difference_type { + [[nodiscard]] auto begin() const noexcept -> Iterator { return begin_; } + [[nodiscard]] auto end() const noexcept -> Iterator { return end_; } + [[nodiscard]] auto size() const -> typename Iterator::difference_type { return std::distance(begin_, end_); } @@ -101,17 +101,17 @@ class TransformedRange final { } private: - iterator const begin_; - iterator const end_; + Iterator const begin_; + Iterator const end_; }; // User-defined deduction guide to help compiler dealing with generic lambdas // and invokable objects. -template <typename Iterator, +template <typename TIterator, typename Function, - typename IteratorValue = typename Iterator::value_type> -TransformedRange(Iterator, Iterator, Function) - -> TransformedRange<Iterator, + typename IteratorValue = typename TIterator::value_type> +TransformedRange(TIterator, TIterator, Function) + -> TransformedRange<TIterator, std::invoke_result_t<Function, IteratorValue>>; #endif // INCLUDED_SRC_OTHER_TOOLS_TRANSFORMED_RANGE_HPP diff --git a/src/utils/cpp/type_safe_arithmetic.hpp b/src/utils/cpp/type_safe_arithmetic.hpp index 690e9a86..52efd81c 100644 --- a/src/utils/cpp/type_safe_arithmetic.hpp +++ b/src/utils/cpp/type_safe_arithmetic.hpp @@ -20,16 +20,16 @@ #include "gsl/gsl" -/// \struct type_safe_arithmetic_tag +/// \struct TypeSafeArithmeticTag /// \brief Abstract tag defining types and limits for custom arithmetic types. /// Usage example: -/// struct my_type_tag : type_safe_arithmetic_tag<int, -2, +3> {}; -/// using my_type_t = type_safe_arithmetic<my_type_tag>; +/// struct my_type_tag : TypeSafeArithmeticTag<int, -2, +3> {}; +/// using my_type_t = TypeSafeArithmetic<my_type_tag>; template <typename T, T MIN_VALUE = std::numeric_limits<T>::lowest(), T MAX_VALUE = std::numeric_limits<T>::max(), T SMALLEST_VALUE = std::numeric_limits<T>::min()> -struct type_safe_arithmetic_tag { +struct TypeSafeArithmeticTag { static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type (integer or floating-point)"); @@ -44,11 +44,11 @@ struct type_safe_arithmetic_tag { static constexpr value_t smallest_value = SMALLEST_VALUE; }; -/// \class type_safe_arithmetic +/// \class TypeSafeArithmetic /// \brief Abstract class for defining custom arithmetic types. -/// \tparam TAG The actual \ref type_safe_arithmetic_tag +/// \tparam TAG The actual \ref TypeSafeArithmeticTag template <typename TAG> -class type_safe_arithmetic { +class TypeSafeArithmetic { typename TAG::value_t m_value{}; public: @@ -63,20 +63,19 @@ class type_safe_arithmetic { static constexpr value_t min_value = tag_t::min_value; static constexpr value_t smallest_value = tag_t::smallest_value; - constexpr type_safe_arithmetic() = default; + constexpr TypeSafeArithmetic() = default; // NOLINTNEXTLINE - constexpr /*explicit*/ type_safe_arithmetic(value_t value) { set(value); } + constexpr /*explicit*/ TypeSafeArithmetic(value_t value) { set(value); } - type_safe_arithmetic(type_safe_arithmetic const&) = default; - type_safe_arithmetic(type_safe_arithmetic&&) noexcept = default; - auto operator=(type_safe_arithmetic const&) -> type_safe_arithmetic& = - default; - auto operator=(type_safe_arithmetic&&) noexcept -> type_safe_arithmetic& = - default; - ~type_safe_arithmetic() = default; + TypeSafeArithmetic(TypeSafeArithmetic const&) = default; + TypeSafeArithmetic(TypeSafeArithmetic&&) noexcept = default; + auto operator=(TypeSafeArithmetic const&) -> TypeSafeArithmetic& = default; + auto operator=(TypeSafeArithmetic&&) noexcept -> TypeSafeArithmetic& = + default; + ~TypeSafeArithmetic() = default; - auto operator=(value_t value) -> type_safe_arithmetic& { + auto operator=(value_t value) -> TypeSafeArithmetic& { set(value); return *this; } @@ -96,115 +95,114 @@ class type_safe_arithmetic { }; // template <typename TAG> -// bool operator==(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator==(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return lhs.get() == rhs.get(); // } // // template <typename TAG> -// bool operator!=(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator!=(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return !(lhs == rhs); // } // // template <typename TAG> -// bool operator>(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator>(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return lhs.get() > rhs.get(); // } // // template <typename TAG> -// bool operator>=(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator>=(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return lhs.get() >= rhs.get(); // } // // template <typename TAG> -// bool operator<(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator<(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return lhs.get() < rhs.get(); // } // // template <typename TAG> -// bool operator<=(type_safe_arithmetic<TAG> lhs, type_safe_arithmetic<TAG> rhs) +// bool operator<=(TypeSafeArithmetic<TAG> lhs, TypeSafeArithmetic<TAG> rhs) // { // return lhs.get() <= rhs.get(); // } // // template <typename TAG> -// type_safe_arithmetic<TAG> operator+(type_safe_arithmetic<TAG> lhs, -// type_safe_arithmetic<TAG> rhs) { -// return type_safe_arithmetic<TAG>{lhs.get() + rhs.get()}; +// TypeSafeArithmetic<TAG> operator+(TypeSafeArithmetic<TAG> lhs, +// TypeSafeArithmetic<TAG> rhs) { +// return TypeSafeArithmetic<TAG>{lhs.get() + rhs.get()}; // } template <typename TAG> -auto operator+=(type_safe_arithmetic<TAG>& lhs, - type_safe_arithmetic<TAG> rhs) -> type_safe_arithmetic<TAG>& { +auto operator+=(TypeSafeArithmetic<TAG>& lhs, + TypeSafeArithmetic<TAG> rhs) -> TypeSafeArithmetic<TAG>& { lhs.set(lhs.get() + rhs.get()); return lhs; } // template <typename TAG> -// type_safe_arithmetic<TAG> operator-(type_safe_arithmetic<TAG> lhs, -// type_safe_arithmetic<TAG> rhs) { -// return type_safe_arithmetic<TAG>{lhs.get() - rhs.get()}; +// TypeSafeArithmetic<TAG> operator-(TypeSafeArithmetic<TAG> lhs, +// TypeSafeArithmetic<TAG> rhs) { +// return TypeSafeArithmetic<TAG>{lhs.get() - rhs.get()}; // } // // template <typename TAG> -// type_safe_arithmetic<TAG>& operator-=(type_safe_arithmetic<TAG>& lhs, -// type_safe_arithmetic<TAG> rhs) { +// TypeSafeArithmetic<TAG>& operator-=(TypeSafeArithmetic<TAG>& lhs, +// TypeSafeArithmetic<TAG> rhs) { // lhs.set(lhs.get() - rhs.get()); // return lhs; // } // // template <typename TAG> -// type_safe_arithmetic<TAG> operator*(type_safe_arithmetic<TAG> lhs, +// TypeSafeArithmetic<TAG> operator*(TypeSafeArithmetic<TAG> lhs, // typename TAG::value_t rhs) { -// return type_safe_arithmetic<TAG>{lhs.get() - rhs}; +// return TypeSafeArithmetic<TAG>{lhs.get() - rhs}; // } // // template <typename TAG> -// type_safe_arithmetic<TAG>& operator*=(type_safe_arithmetic<TAG>& lhs, +// TypeSafeArithmetic<TAG>& operator*=(TypeSafeArithmetic<TAG>& lhs, // typename TAG::value_t rhs) { // lhs.set(lhs.get() * rhs); // return lhs; // } // // template <typename TAG> -// type_safe_arithmetic<TAG> operator/(type_safe_arithmetic<TAG> lhs, +// TypeSafeArithmetic<TAG> operator/(TypeSafeArithmetic<TAG> lhs, // typename TAG::value_t rhs) { -// return type_safe_arithmetic<TAG>{lhs.get() / rhs}; +// return TypeSafeArithmetic<TAG>{lhs.get() / rhs}; // } // // template <typename TAG> -// type_safe_arithmetic<TAG>& operator/=(type_safe_arithmetic<TAG>& lhs, +// TypeSafeArithmetic<TAG>& operator/=(TypeSafeArithmetic<TAG>& lhs, // typename TAG::value_t rhs) { // lhs.set(lhs.get() / rhs); // return lhs; // } // // template <typename TAG> -// type_safe_arithmetic<TAG>& operator++(type_safe_arithmetic<TAG>& a) { -// return a += type_safe_arithmetic<TAG>{1}; +// TypeSafeArithmetic<TAG>& operator++(TypeSafeArithmetic<TAG>& a) { +// return a += TypeSafeArithmetic<TAG>{1}; // } template <typename TAG> -auto operator++(type_safe_arithmetic<TAG>& a, - int) -> type_safe_arithmetic<TAG> { +auto operator++(TypeSafeArithmetic<TAG>& a, int) -> TypeSafeArithmetic<TAG> { auto r = a; - a += type_safe_arithmetic<TAG>{1}; + a += TypeSafeArithmetic<TAG>{1}; return r; } // template <typename TAG> -// type_safe_arithmetic<TAG>& operator--(type_safe_arithmetic<TAG>& a) { -// return a -= type_safe_arithmetic<TAG>{1}; +// TypeSafeArithmetic<TAG>& operator--(TypeSafeArithmetic<TAG>& a) { +// return a -= TypeSafeArithmetic<TAG>{1}; // } // // template <typename TAG> -// type_safe_arithmetic<TAG> operator--(type_safe_arithmetic<TAG>& a, int) { +// TypeSafeArithmetic<TAG> operator--(TypeSafeArithmetic<TAG>& a, int) { // auto r = a; -// a += type_safe_arithmetic<TAG>{1}; +// a += TypeSafeArithmetic<TAG>{1}; // return r; // } |