aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/concepts.h
blob: aecad4fe60bcfe6e11f411037cb0303c10844ee4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

#include <concepts>
#include <vector>

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;

////////////////////////////////////////////////////////////////////////////////

template <class V>
concept CStdVector = requires (V& vec) {
    [] <class... T> (std::vector<T...>&) { } (vec);
};

////////////////////////////////////////////////////////////////////////////////

template <class M>
concept CAnyMap = requires {
    typename M::mapped_type;
    typename M::key_type;
};

////////////////////////////////////////////////////////////////////////////////

template <class T>
concept CConst = std::is_const_v<T>;

template <class T>
concept CNonConst = !CConst<T>;

////////////////////////////////////////////////////////////////////////////////

template <class T>
concept CRawPtr = std::is_pointer_v<T>;

template <class T>
concept CConstRawPtr = CRawPtr<T> && CConst<decltype(*std::declval<T>())>;

template <class T>
concept CMutRawPtr = CRawPtr<T> && !CConstRawPtr<T>;

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT