diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-04-21 17:59:07 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-04-21 18:10:02 +0300 |
commit | c91bd7c47de002cd40e5c150cea52dbfe88d5514 (patch) | |
tree | 63ae2ab219f8b42946e55ad277080dc427ead012 | |
parent | f66bba5f5878c08d7dde25e3d1a44d6972684f88 (diff) | |
download | ydb-c91bd7c47de002cd40e5c150cea52dbfe88d5514.tar.gz |
Move concepts to library/cpp/yt/misc/concepts.h
Done
b2c0a25fcacbb46fcf6294ce86b1a27ad2adac50
-rw-r--r-- | library/cpp/yt/memory/function_view.h | 32 | ||||
-rw-r--r-- | library/cpp/yt/misc/concepts.h | 49 | ||||
-rw-r--r-- | yt/yt/core/misc/public.h | 35 |
3 files changed, 55 insertions, 61 deletions
diff --git a/library/cpp/yt/memory/function_view.h b/library/cpp/yt/memory/function_view.h index 259238521f..108fd076ce 100644 --- a/library/cpp/yt/memory/function_view.h +++ b/library/cpp/yt/memory/function_view.h @@ -1,36 +1,11 @@ #pragma once -#include <concepts> +#include <library/cpp/yt/misc/concepts.h> namespace NYT { //////////////////////////////////////////////////////////////////////////////// -namespace NDetail { - -template <class TSignature> -struct TTypeErasureTraits; - -template <class TResult, bool NoExcept, class... TArgs> -struct TTypeErasureTraits<TResult(TArgs...) noexcept(NoExcept)> -{ - using TSignature = TResult(TArgs...) noexcept(NoExcept); - - // TODO(arkady-e1ppa): Support pointer-to-member-function? - template <class T> - static constexpr bool IsInvocable = NoExcept - ? requires (T obj, TArgs... args) { - { obj(std::forward<TArgs>(args)...) } noexcept -> std::same_as<TResult>; - } - : requires (T obj, TArgs... args) { - { obj(std::forward<TArgs>(args)...) } -> std::same_as<TResult>; - }; -}; - -} // namespace NDetail - -//////////////////////////////////////////////////////////////////////////////// - // Non-owning type-erasure container. /* Example: @@ -75,9 +50,10 @@ class TFunctionView; //////////////////////////////////////////////////////////////////////////////// +// TODO(arkady-e1ppa): Support pointer-to-member-function? template <class T, class TSignature> concept CTypeErasable = - NDetail::TTypeErasureTraits<TSignature>::template IsInvocable<T> && + CInvocable<T, TSignature> && (!std::same_as<T, TFunctionView<TSignature>>); //////////////////////////////////////////////////////////////////////////////// @@ -105,7 +81,7 @@ public: bool IsValid() const noexcept; void Reset() noexcept; - // bool operator==(const TFunctionView& other) const & = default; + bool operator==(const TFunctionView& other) const & = default; private: // NB: Technically, this is UB according to C standard, which diff --git a/library/cpp/yt/misc/concepts.h b/library/cpp/yt/misc/concepts.h new file mode 100644 index 0000000000..976c707ffe --- /dev/null +++ b/library/cpp/yt/misc/concepts.h @@ -0,0 +1,49 @@ +#pragma once + +#include <concepts> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +namespace NDetail { + +template <class T, class TSignature> +struct TIsInvocable; + +template <class T, class TResult, bool NoExcept, class... TArgs> +struct TIsInvocable<T, TResult(TArgs...) noexcept(NoExcept)> +{ +private: + static constexpr bool IsInvocable_ = requires (T&& t, TArgs&&... args) { + { std::forward<T>(t)(std::forward<TArgs>(args)...) } -> std::same_as<TResult>; + }; + + static constexpr bool IsNoThrowInvocable_ = requires (T&& t, TArgs&&... args) { + { std::forward<T>(t)(std::forward<TArgs>(args)...) } noexcept -> std::same_as<TResult>; + }; + +public: + static constexpr bool Value = + IsInvocable_ && + (!NoExcept || IsNoThrowInvocable_); +}; + +} // namespace NDetail + +//////////////////////////////////////////////////////////////////////////////// + +template <class TObject, class TScalar> +concept CScalable = requires (TObject object, TScalar scalar) +{ + { object * scalar } -> std::same_as<TObject>; +}; + +//////////////////////////////////////////////////////////////////////////////// + +template <class T, class TSignature> +concept CInvocable = NDetail::TIsInvocable<T, TSignature>::Value; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/yt/yt/core/misc/public.h b/yt/yt/core/misc/public.h index 7d002624e2..b43ad3d9f6 100644 --- a/yt/yt/core/misc/public.h +++ b/yt/yt/core/misc/public.h @@ -3,6 +3,8 @@ #include "common.h" #include "error_code.h" +#include <library/cpp/yt/misc/concepts.h> + // Google Protobuf forward declarations. namespace google::protobuf { @@ -173,37 +175,4 @@ DECLARE_REFCOUNTED_STRUCT(IMemoryUsageTracker) //////////////////////////////////////////////////////////////////////////////// -template <class TObject, class TScalar> -concept CScalable = requires (TObject object, TScalar scalar) -{ - { object * scalar } -> std::same_as<TObject>; -}; - -//////////////////////////////////////////////////////////////////////////////// - -template <class T, class Sig> -struct TIsInvocable; - -template <class T, class TRet, bool NoExcept, class... TArgs> -struct TIsInvocable<T, TRet(TArgs...) noexcept(NoExcept)> -{ -private: - static constexpr bool IsInvocable_ = requires (T&& t, TArgs&&... args) { - { std::forward<T>(t)(std::forward<TArgs>(args)...) } -> std::same_as<TRet>; - }; - - static constexpr bool IsNoThrowInvocable_ = requires (T&& t, TArgs&&... args) { - { std::forward<T>(t)(std::forward<TArgs>(args)...) } noexcept; - }; -public: - static constexpr bool Value = - IsInvocable_ && - (!NoExcept || IsNoThrowInvocable_); -}; - -template <class T, class Sig> -concept CInvocable = TIsInvocable<T, Sig>::Value; - -//////////////////////////////////////////////////////////////////////////////// - } // namespace NYT |