diff options
| author | arkady-e1ppa <[email protected]> | 2024-04-21 17:59:07 +0300 | 
|---|---|---|
| committer | arkady-e1ppa <[email protected]> | 2024-04-21 18:10:02 +0300 | 
| commit | c91bd7c47de002cd40e5c150cea52dbfe88d5514 (patch) | |
| tree | 63ae2ab219f8b42946e55ad277080dc427ead012 /library/cpp | |
| parent | f66bba5f5878c08d7dde25e3d1a44d6972684f88 (diff) | |
Move concepts to library/cpp/yt/misc/concepts.h
Done
b2c0a25fcacbb46fcf6294ce86b1a27ad2adac50
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/yt/memory/function_view.h | 32 | ||||
| -rw-r--r-- | library/cpp/yt/misc/concepts.h | 49 | 
2 files changed, 53 insertions, 28 deletions
diff --git a/library/cpp/yt/memory/function_view.h b/library/cpp/yt/memory/function_view.h index 259238521ff..108fd076ce3 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 00000000000..976c707ffec --- /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  | 
