arg_router  1.4.0
C++ command line argument parsing and routing
runtime_enable.hpp
1 // Copyright (C) 2023 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/parsing.hpp"
8 #include "arg_router/policy/required.hpp"
9 
10 namespace arg_router::policy
11 {
23 template <typename T = void>
25 {
26 public:
28  constexpr static auto priority = std::size_t{800};
29 
34  explicit runtime_enable(bool enable) noexcept : enabled_{enable} {}
35 
37  [[nodiscard]] bool runtime_enabled() const noexcept { return enabled_; }
38 
52  template <typename ProcessedTarget, typename... Parents>
54  [[maybe_unused]] parsing::dynamic_token_adapter& tokens,
55  [[maybe_unused]] utility::compile_time_optional<ProcessedTarget> processed_target,
56  [[maybe_unused]] parsing::parse_target& target,
57  [[maybe_unused]] const Parents&... parents) const
58  {
59  static_assert(sizeof...(Parents) >= 1, "Runtime enable requires at least 1 parent");
60  using node_type = boost::mp11::mp_first<std::tuple<Parents...>>;
61  static_assert(!policy::is_required_v<node_type>,
62  "Runtime enable must not be used with policy::required");
63 
66  }
67 
68 protected:
69  bool enabled_;
70 };
71 
72 template <typename T>
73 struct is_policy<runtime_enable<T>> : std::true_type {
74 };
75 
76 template <typename T>
77 class runtime_enable_required : public runtime_enable<T>
78 {
79 public:
81 
83  using value_type = T;
84 
93  explicit runtime_enable_required(bool enable, T default_value = {}) noexcept :
94  runtime_enable<T>{enable}, default_value_{std::move(default_value)}
95  {
96  }
97 
106  template <typename ValueType, typename... Parents>
107  [[nodiscard]] ValueType missing_phase([[maybe_unused]] const Parents&... parents) const
108  {
109  if (this->enabled_) {
110  static_assert(sizeof...(Parents) >= 1, "Runtime enable requires at least 1 parent");
111  using node_type = boost::mp11::mp_first<std::tuple<Parents...>>;
112 
113  throw multi_lang_exception{error_code::missing_required_argument,
114  parsing::node_token_type<node_type>()};
115  }
116 
117  return default_value_;
118  }
119 
120 private:
121  value_type default_value_;
122 };
123 
124 template <typename T>
125 struct is_policy<runtime_enable_required<T>> : std::true_type {
126 };
127 } // namespace arg_router::policy
parsing::pre_parse_result pre_parse_phase([[maybe_unused]] parsing::dynamic_token_adapter &tokens, [[maybe_unused]] utility::compile_time_optional< ProcessedTarget > processed_target, [[maybe_unused]] parsing::parse_target &target, [[maybe_unused]] const Parents &... parents) const
bool runtime_enabled() const noexcept
runtime_enable(bool enable) noexcept
@ missing_required_argument
A node has been marked as required but no token matches it.