diff options
| author | Martin Ridgers <[email protected]> | 2021-12-14 14:55:45 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-12-16 09:35:51 +0100 |
| commit | 3de8a7abe981d590c1b7db162f3b2250460adaea (patch) | |
| tree | f23a719353e34d1e0c124e8cc7e7c6ca529b0df2 /zencore/include | |
| parent | Use zen::Min() instead of std::min() (diff) | |
| download | zen-3de8a7abe981d590c1b7db162f3b2250460adaea.tar.xz zen-3de8a7abe981d590c1b7db162f3b2250460adaea.zip | |
Wrapped direct use of C++20 library concepts
Some C++ libraries do not fully support concepts in the versions that
are available by default on Linux and MacOS. The compilers do support
them though so we can still apply concepts/requires to templates
Diffstat (limited to 'zencore/include')
| -rw-r--r-- | zencore/include/zencore/intmath.h | 4 | ||||
| -rw-r--r-- | zencore/include/zencore/refcount.h | 2 | ||||
| -rw-r--r-- | zencore/include/zencore/sharedbuffer.h | 2 | ||||
| -rw-r--r-- | zencore/include/zencore/stats.h | 2 | ||||
| -rw-r--r-- | zencore/include/zencore/string.h | 6 | ||||
| -rw-r--r-- | zencore/include/zencore/zencore.h | 23 |
6 files changed, 31 insertions, 8 deletions
diff --git a/zencore/include/zencore/intmath.h b/zencore/include/zencore/intmath.h index 0d0ceff16..b4a0b6796 100644 --- a/zencore/include/zencore/intmath.h +++ b/zencore/include/zencore/intmath.h @@ -59,7 +59,7 @@ IsPow2(uint64_t n) } /// Round an integer up to the closest integer multiplier of 'base' ('base' must be a power of two) -template<std::integral T> +template<Integral T> T RoundUp(T Value, auto Base) { @@ -68,7 +68,7 @@ RoundUp(T Value, auto Base) } bool -IsMultipleOf(std::integral auto Value, auto MultiplierPow2) +IsMultipleOf(Integral auto Value, auto MultiplierPow2) { ZEN_ASSERT_SLOW(IsPow2(MultiplierPow2)); return (Value & (MultiplierPow2 - 1)) == 0; diff --git a/zencore/include/zencore/refcount.h b/zencore/include/zencore/refcount.h index e8a3095f9..5c8b70ef6 100644 --- a/zencore/include/zencore/refcount.h +++ b/zencore/include/zencore/refcount.h @@ -118,7 +118,7 @@ public: inline ~Ref() { m_Ref && m_Ref->Release(); } template<typename DerivedType> - requires std::derived_from<DerivedType, T> + requires DerivedFrom<DerivedType, T> inline Ref(const Ref<DerivedType>& Rhs) : Ref(Rhs.m_Ref) {} [[nodiscard]] inline bool IsNull() const { return m_Ref == nullptr; } diff --git a/zencore/include/zencore/sharedbuffer.h b/zencore/include/zencore/sharedbuffer.h index 1f87dc639..97c5a9d21 100644 --- a/zencore/include/zencore/sharedbuffer.h +++ b/zencore/include/zencore/sharedbuffer.h @@ -144,7 +144,7 @@ public: /** Make a non-owned view of the input */ [[nodiscard]] inline static SharedBuffer MakeView(MemoryView View) { return MakeView(View.GetData(), View.GetSize()); } /** Make a non-owning view of the memory of the contiguous container. */ - [[nodiscard]] inline static SharedBuffer MakeView(const std::ranges::contiguous_range auto& Container) + [[nodiscard]] inline static SharedBuffer MakeView(const ContiguousRange auto& Container) { std::span Span = Container; return MakeView(Span.data(), Span.size() * sizeof(typename decltype(Span)::element_type)); diff --git a/zencore/include/zencore/stats.h b/zencore/include/zencore/stats.h index d58435a20..f0efc36d8 100644 --- a/zencore/include/zencore/stats.h +++ b/zencore/include/zencore/stats.h @@ -142,7 +142,7 @@ public: void Update(int64_t Value); SampleSnapshot Snapshot() const; - template<std::invocable<int64_t> T> + template<Invocable<int64_t> T> void IterateValues(T Callback) const { for (const auto& Value : m_Values) diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h index 848310aa3..df78b2757 100644 --- a/zencore/include/zencore/string.h +++ b/zencore/include/zencore/string.h @@ -74,8 +74,8 @@ protected: struct IntNum : public TextNumBase { - inline IntNum(std::unsigned_integral auto Number) { ToString(m_Buffer, uint64_t(Number)); } - inline IntNum(std::signed_integral auto Number) { ToString(m_Buffer, int64_t(Number)); } + inline IntNum(UnsignedIntegral auto Number) { ToString(m_Buffer, uint64_t(Number)); } + inline IntNum(SignedIntegral auto Number) { ToString(m_Buffer, int64_t(Number)); } }; ////////////////////////////////////////////////////////////////////////// @@ -602,7 +602,7 @@ NiceRate(uint64_t Num, uint32_t DurationMilliseconds, const char* Unit = "B") ////////////////////////////////////////////////////////////////////////// -template<std::integral T> +template<Integral T> std::optional<T> ParseInt(const std::string_view& Input) { diff --git a/zencore/include/zencore/zencore.h b/zencore/include/zencore/zencore.h index 3352af5d2..8688ddc13 100644 --- a/zencore/include/zencore/zencore.h +++ b/zencore/include/zencore/zencore.h @@ -5,6 +5,7 @@ #include <cinttypes> #include <stdexcept> #include <string> +#include <version> #ifndef ZEN_WITH_TESTS # define ZEN_WITH_TESTS 1 @@ -119,6 +120,28 @@ } while (0) #endif +// At the time of writing only ver >= 13 of LLVM's libc++ has an implementation +// of std::integral. Some platforms like Ubuntu and Mac OS are still on 12. +#if defined(__cpp_lib_concepts) + template <class T> concept Integral = std::integral<T>; + template <class T> concept SignedIntegral = std::signed_integral<T>; + template <class T> concept UnsignedIntegral = std::unsigned_integral<T>; + template <class F, class... A> concept Invocable = std::invocable<F, A...>; + template <class D, class B> concept DerivedFrom = std::derived_from<D, B>; +#else + template <class T> concept Integral = std::is_integral_v<T>; + template <class T> concept SignedIntegral = Integral<T> && std::is_signed_v<T>; + template <class T> concept UnsignedIntegral = Integral<T> && !std::is_signed_v<T>; + template <class F, class... A> concept Invocable = true; + template <class D, class B> concept DerivedFrom = std::is_base_of_v<B, D> && std::is_convertible_v<const volatile D*, const volatile B*>; +#endif + +#if defined(__cpp_lib_ranges) + template <typename T> concept ContiguousRange = std::ranges::contiguous_range<T>; +#else + template <typename T> concept ContiguousRange = true; +#endif + ////////////////////////////////////////////////////////////////////////// // Architecture // |