arg_router  1.4.0
C++ command line argument parsing and routing
policy.hpp
1 // Copyright (C) 2022 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include "arg_router/parsing/dynamic_token_adapter.hpp"
8 #include "arg_router/parsing/parse_target.hpp"
9 #include "arg_router/utility/compile_time_optional.hpp"
10 
11 #include <boost/mp11/algorithm.hpp>
12 #include <boost/mp11/bind.hpp>
13 
14 #include <type_traits>
15 
20 namespace arg_router::policy
21 {
29 template <typename T, typename... Args>
30 struct is_policy : std::false_type {
31 };
32 
37 template <typename T>
39 
44 template <typename Tuple>
45 struct is_all_policies : boost::mp11::mp_all_of<Tuple, is_policy> {
46 };
47 
52 template <typename Tuple>
54 
59 template <typename T>
61  static_assert(policy::is_policy_v<T>, "T must be a policy");
62 
63  template <typename U>
64  using type = decltype( //
65  std::declval<const U&>().template pre_parse_phase<>(
66  std::declval<parsing::dynamic_token_adapter&>(),
68  std::declval<parsing::parse_target&>()));
69 
70  constexpr static bool value = boost::mp11::mp_valid<type, T>::value;
71 };
72 
77 template <typename T>
78 constexpr static bool has_pre_parse_phase_method_v = has_pre_parse_phase_method<T>::value;
79 
85 template <typename T, typename ValueType>
87  static_assert(policy::is_policy_v<T>, "T must be a policy");
88 
89  template <typename U>
90  using type = decltype( //
91  std::declval<const U&>().template parse_phase<ValueType>(std::declval<std::string_view>()));
92 
93  constexpr static bool value = boost::mp11::mp_valid<type, T>::value;
94 };
95 
101 template <typename T, typename ValueType>
102 constexpr static bool has_parse_phase_method_v = has_parse_phase_method<T, ValueType>::value;
103 
109 template <typename T, typename ValueType>
111  static_assert(policy::is_policy_v<T>, "T must be a policy");
112 
113  template <typename U>
114  using type1 = decltype( //
115  std::declval<const U&>().template validation_phase<ValueType>(
116  std::declval<const ValueType&>()));
117 
118  template <typename U>
119  using type2 = decltype( //
120  std::declval<const U&>().validation_phase(std::declval<const ValueType&>()));
121 
122  constexpr static bool value =
123  boost::mp11::mp_valid<type1, T>::value || boost::mp11::mp_valid<type2, T>::value;
124 };
125 
131 template <typename T, typename ValueType>
132 constexpr static bool has_validation_phase_method_v =
134 
139 template <typename T>
141  static_assert(policy::is_policy_v<T>, "T must be a policy");
142 
143  template <typename U>
144  using type = decltype(std::declval<const U&>().template routing_phase<>());
145 
146  constexpr static bool value = boost::mp11::mp_valid<type, T>::value;
147 };
148 
153 template <typename T>
154 constexpr static bool has_routing_phase_method_v = has_routing_phase_method<T>::value;
155 
161 template <typename T, typename ValueType>
163  static_assert(policy::is_policy_v<T>, "T must be a policy");
164 
165  template <typename U>
166  using type = decltype( //
167  std::declval<const U&>().template missing_phase<ValueType>());
168 
169  constexpr static bool value = boost::mp11::mp_valid<type, T>::value;
170 };
171 
177 template <typename T, typename ValueType>
178 constexpr static bool has_missing_phase_method_v = has_missing_phase_method<T, ValueType>::value;
179 
184 template <typename T>
185 struct has_priority {
186  static_assert(policy::is_policy_v<T>, "T must be a policy");
187 
188  template <typename U>
189  using type = decltype(U::priority);
190 
191  constexpr static bool value = boost::mp11::mp_valid<type, T>::value;
192 };
193 
198 template <typename T>
199 constexpr static bool has_priority_v = has_priority<T>::value;
200 
206 template <typename ParentsTuple>
208 {
209  template <typename Parent>
210  struct policy_finder {
211  constexpr static bool value =
212  boost::mp11::mp_find_if_q<
213  typename Parent::policies_type,
214  boost::mp11::mp_bind<has_routing_phase_method, boost::mp11::_1>>::value !=
215  std::tuple_size_v<typename Parent::policies_type>;
216  };
217 
218 public:
221  using index = boost::mp11::mp_find_if<ParentsTuple, policy_finder>;
222 
224  using type = boost::mp11::mp_eval_if_c<index::value == std::tuple_size_v<ParentsTuple>,
225  void,
226  boost::mp11::mp_at,
227  ParentsTuple,
228  index>;
229 };
230 } // namespace arg_router::policy
boost::mp11::mp_find_if< ParentsTuple, policy_finder > index
Definition: policy.hpp:221
boost::mp11::mp_eval_if_c< index::value==std::tuple_size_v< ParentsTuple >, void, boost::mp11::mp_at, ParentsTuple, index > type
Definition: policy.hpp:228
constexpr auto is_policy_v
Definition: policy.hpp:38
constexpr auto is_all_policies_v
Definition: policy.hpp:53