packio
arg.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_ARG_H
6 #define PACKIO_ARG_H
7 
10 
11 #include <string>
12 #include <string_view>
13 
14 namespace packio {
15 
17 class arg {
18 public:
20  template <typename T>
21  struct with_value {
22  std::string name;
23  T value;
24  };
25 
26  explicit constexpr arg(std::string_view name) : name_{name} {}
27  std::string_view name() const { return name_; }
28 
29  template <typename T>
30  constexpr with_value<T> operator=(T&& value)
31  {
32  return {std::string{name_}, std::forward<T>(value)};
33  }
34 
35 private:
36  std::string_view name_;
37 };
38 
39 template <typename T>
40 struct is_arg_impl : std::false_type {
41 };
42 
43 template <typename T>
44 struct is_arg_impl<arg::with_value<T>> : std::true_type {
45 };
46 
47 template <typename T>
48 struct is_arg : is_arg_impl<std::decay_t<T>> {
49 };
50 
51 template <typename T>
52 constexpr bool is_arg_v = is_arg<T>::value;
53 
56 namespace arg_literals {
57 
58 constexpr arg operator"" _arg(const char* str, std::size_t)
59 {
60  return arg{str};
61 }
62 
63 } // arg_literals
64 
65 } // packio
66 
67 #endif // PACKIO_INTERNAL_ARG_H
A named argument.
Definition: arg.h:17
Namespace containing string literals to define arguments.
The packio namespace.
Definition: arg.h:14
A named argument with a value.
Definition: arg.h:21