arg_router  1.4.0
C++ command line argument parsing and routing
pre_parse_data.hpp
1 // Copyright (C) 2022-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/parse_target.hpp"
8 
9 namespace arg_router::parsing
10 {
11 namespace detail
12 {
13 struct always_returns_true {
14  template <typename... Args>
15  constexpr bool operator()([[maybe_unused]] Args&&... args) const noexcept
16  {
17  return true;
18  }
19 };
20 } // namespace detail
21 
28 template <typename Validator, bool HasTarget>
30 {
31 public:
33  constexpr static auto has_target = HasTarget;
34 
39  [[nodiscard]] vector<parsing::token_type>& args() noexcept { return args_; }
40 
45  [[nodiscard]] const vector<parsing::token_type>& args() const noexcept { return args_; }
46 
51  [[nodiscard]] const Validator& validator() const noexcept { return validator_; }
52 
53 protected:
54  explicit pre_parse_data_base(
56  const Validator& validator = [](const auto&...) { return true; }) noexcept :
57  args_{args},
58  validator_{validator}
59  {
60  }
61 
62 private:
63  std::reference_wrapper<vector<parsing::token_type>> args_;
64  std::reference_wrapper<const Validator> validator_;
65 };
66 
89 template <typename Validator, bool>
91 
107 template <typename Validator>
108 class pre_parse_data<Validator, false> : public pre_parse_data_base<Validator, false>
109 {
110 public:
117  const Validator& validator = detail::always_returns_true{}) noexcept :
119  {
120  }
121 };
122 
138 template <typename Validator>
139 class pre_parse_data<Validator, true> : public pre_parse_data_base<Validator, true>
140 {
141 public:
149  const parse_target& target,
150  const Validator& validator = detail::always_returns_true{}) noexcept :
151  pre_parse_data_base<Validator, true>{args, validator},
152  target_{target}
153  {
154  }
155 
160  [[nodiscard]] const parse_target& target() const noexcept { return target_; }
161 
162 private:
163  std::reference_wrapper<const parse_target> target_;
164 };
165 
166 // Deduction guides
167 template <typename... T>
168 pre_parse_data(vector<parsing::token_type>&) -> pre_parse_data<detail::always_returns_true, false>;
169 
170 template <typename T>
171 pre_parse_data(vector<parsing::token_type>&, const T&) -> pre_parse_data<T, false>;
172 
173 template <typename... T>
174 pre_parse_data(vector<parsing::token_type>&, const parse_target&)
175  -> pre_parse_data<detail::always_returns_true, true>;
176 
177 template <typename T>
178 pre_parse_data(vector<parsing::token_type>&, const parse_target&, const T&)
179  -> pre_parse_data<T, true>;
180 } // namespace arg_router::parsing
pre_parse_data(vector< parsing::token_type > &args, const Validator &validator=detail::always_returns_true{}) noexcept
const parse_target & target() const noexcept
pre_parse_data(vector< parsing::token_type > &args, const parse_target &target, const Validator &validator=detail::always_returns_true{}) noexcept
const vector< parsing::token_type > & args() const noexcept
const Validator & validator() const noexcept
vector< parsing::token_type > & args() noexcept
std::vector< T, config::allocator< T > > vector
Definition: basic_types.hpp:39