packio
traits.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4 
5 #ifndef PACKIO_TRAITS_H
6 #define PACKIO_TRAITS_H
7 
10 
11 #include <type_traits>
12 #include <utility>
13 
14 #include "internal/config.h"
15 #include "internal/rpc.h"
16 #include "internal/utils.h"
17 
18 #if defined(PACKIO_TRAITS_CHECK_DISABLE)
19 #define PACKIO_STATIC_ASSERT_TRAIT(Trait) (void)0
20 #define PACKIO_STATIC_ASSERT_TTRAIT(Trait, ...) (void)0
21 #else
22 #define PACKIO_STATIC_ASSERT_TRAIT(Trait) \
23  static_assert( \
24  ::packio::traits::Trait<Trait>::value, "Trait " #Trait " not satisfied")
25 #define PACKIO_STATIC_ASSERT_TTRAIT(Trait, ...) \
26  static_assert( \
27  ::packio::traits::Trait<Trait, __VA_ARGS__>::value, \
28  "Trait " #Trait " not satisfied")
29 #endif
30 
31 namespace packio {
32 
33 template <typename Rpc>
34 class completion_handler;
35 
36 namespace traits {
37 namespace details {
38 
39 template <typename, typename, typename = void>
40 struct AsyncProcedureImpl : std::false_type {
41 };
42 
43 template <typename T, typename Rpc>
44 struct AsyncProcedureImpl<T, Rpc, std::enable_if_t<internal::func_traits_v<T>>> {
45  using TArgs = typename internal::func_traits<T>::args_type;
46  static constexpr bool value = [] {
47  if constexpr (std::tuple_size_v<TArgs> == 0) {
48  return false;
49  }
50  else {
51  return std::is_same_v<
52  std::decay_t<std::tuple_element_t<0, TArgs>>,
53  completion_handler<Rpc>>;
54  }
55  }();
56 };
57 
58 template <typename T, typename Rpc>
59 constexpr bool is_async_procedure_v = AsyncProcedureImpl<T, Rpc>::value;
60 
61 } // details
62 
63 template <bool condition>
64 struct Trait : std::integral_constant<bool, condition> {
65 };
66 
71 template <typename T>
72 struct NotifyHandler : Trait<std::is_invocable_v<T, error_code>> {
73 };
74 
79 template <typename T, typename Rpc>
81  : Trait<std::is_invocable_v<T, error_code, typename Rpc::response_type>> {
82 };
83 
89 template <typename T, typename Session>
91  : Trait<std::is_invocable_v<T, error_code, std::shared_ptr<Session>>> {
92 };
93 
100 template <typename T, typename Rpc>
101 struct AsyncProcedure : Trait<details::is_async_procedure_v<T, Rpc>> {
102 };
103 
110 template <typename T>
111 struct SyncProcedure : Trait<internal::func_traits_v<T>> {
112 };
113 
114 #if defined(PACKIO_HAS_CO_AWAIT)
121 template <typename T>
123  : Trait<internal::is_coroutine_v<T> && internal::func_traits_v<T>> {
124 };
125 #endif // defined(PACKIO_HAS_CO_AWAIT)
126 
127 } // traits
128 } // packio
129 
130 #endif // PACKIO_TRAITS_H
completion_handler< rpc > completion_handler
The completion_handler for JSON-RPC.
Definition: json_rpc.h:23
The packio namespace.
Definition: arg.h:14
AsyncProcedure trait.
Definition: traits.h:101
CallHandler trait.
Definition: traits.h:81
CoroProcedure trait.
Definition: traits.h:123
NotifyHandler trait.
Definition: traits.h:72
ServeHandler trait.
Definition: traits.h:91
SyncProcedure trait.
Definition: traits.h:111