CLI11 2.3.1
Loading...
Searching...
No Matches
TypeTools.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2022, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// [CLI11:public_includes:set]
10#include <cstdint>
11#include <exception>
12#include <limits>
13#include <memory>
14#include <string>
15#include <type_traits>
16#include <utility>
17#include <vector>
18// [CLI11:public_includes:end]
19
20#include "StringTools.hpp"
21
22namespace CLI {
23// [CLI11:type_tools_hpp:verbatim]
24
25// Type tools
26
27// Utilities for type enabling
28namespace detail {
29// Based generally on https://rmf.io/cxx11/almost-static-if
31enum class enabler {};
32
34constexpr enabler dummy = {};
35} // namespace detail
36
42template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
43
45template <typename... Ts> struct make_void { using type = void; };
46
48template <typename... Ts> using void_t = typename make_void<Ts...>::type;
49
51template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
52
54template <typename T> struct is_bool : std::false_type {};
55
57template <> struct is_bool<bool> : std::true_type {};
58
60template <typename T> struct is_shared_ptr : std::false_type {};
61
63template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
64
66template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
67
69template <typename T> struct is_copyable_ptr {
70 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
71};
72
74template <typename T> struct IsMemberType { using type = T; };
75
77template <> struct IsMemberType<const char *> { using type = std::string; };
78
79namespace detail {
80
81// These are utilities for IsMember and other transforming objects
82
85
87template <typename T, typename Enable = void> struct element_type { using type = T; };
88
89template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
90 using type = typename std::pointer_traits<T>::element_type;
91};
92
95template <typename T> struct element_value_type { using type = typename element_type<T>::type::value_type; };
96
98template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
99 using value_type = typename T::value_type;
100 using first_type = typename std::remove_const<value_type>::type;
101 using second_type = typename std::remove_const<value_type>::type;
102
104 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
105 return std::forward<Q>(pair_value);
106 }
108 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
109 return std::forward<Q>(pair_value);
110 }
111};
112
115template <typename T>
117 T,
118 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
119 : std::true_type {
120 using value_type = typename T::value_type;
121 using first_type = typename std::remove_const<typename value_type::first_type>::type;
122 using second_type = typename std::remove_const<typename value_type::second_type>::type;
123
125 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
126 return std::get<0>(std::forward<Q>(pair_value));
127 }
129 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
130 return std::get<1>(std::forward<Q>(pair_value));
131 }
132};
133
134// Warning is suppressed due to "bug" in gcc<5.0 and gcc 7.0 with c++17 enabled that generates a Wnarrowing warning
135// in the unevaluated context even if the function that was using this wasn't used. The standard says narrowing in
136// brace initialization shouldn't be allowed but for backwards compatibility gcc allows it in some contexts. It is a
137// little fuzzy what happens in template constructs and I think that was something GCC took a little while to work out.
138// But regardless some versions of gcc generate a warning when they shouldn't from the following code so that should be
139// suppressed
140#ifdef __GNUC__
141#pragma GCC diagnostic push
142#pragma GCC diagnostic ignored "-Wnarrowing"
143#endif
144// check for constructibility from a specific type and copy assignable used in the parse detection
145template <typename T, typename C> class is_direct_constructible {
146 template <typename TT, typename CC>
147 static auto test(int, std::true_type) -> decltype(
148// NVCC warns about narrowing conversions here
149#ifdef __CUDACC__
150#pragma diag_suppress 2361
151#endif
152 TT{std::declval<CC>()}
153#ifdef __CUDACC__
154#pragma diag_default 2361
155#endif
156 ,
157 std::is_move_assignable<TT>());
158
159 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
160
161 template <typename, typename> static auto test(...) -> std::false_type;
162
163 public:
164 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
165};
166#ifdef __GNUC__
167#pragma GCC diagnostic pop
168#endif
169
170// Check for output streamability
171// Based on https://stackoverflow.com/questions/22758291/how-can-i-detect-if-a-type-can-be-streamed-to-an-stdostream
172
173template <typename T, typename S = std::ostringstream> class is_ostreamable {
174 template <typename TT, typename SS>
175 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
176
177 template <typename, typename> static auto test(...) -> std::false_type;
178
179 public:
180 static constexpr bool value = decltype(test<T, S>(0))::value;
181};
182
184template <typename T, typename S = std::istringstream> class is_istreamable {
185 template <typename TT, typename SS>
186 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
187
188 template <typename, typename> static auto test(...) -> std::false_type;
189
190 public:
191 static constexpr bool value = decltype(test<T, S>(0))::value;
192};
193
195template <typename T> class is_complex {
196 template <typename TT>
197 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
198
199 template <typename> static auto test(...) -> std::false_type;
200
201 public:
202 static constexpr bool value = decltype(test<T>(0))::value;
203};
204
206template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
207bool from_stream(const std::string &istring, T &obj) {
208 std::istringstream is;
209 is.str(istring);
210 is >> obj;
211 return !is.fail() && !is.rdbuf()->in_avail();
212}
213
214template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
215bool from_stream(const std::string & /*istring*/, T & /*obj*/) {
216 return false;
217}
218
219// check to see if an object is a mutable container (fail by default)
220template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
221
225template <typename T>
227 T,
228 conditional_t<false,
229 void_t<typename T::value_type,
230 decltype(std::declval<T>().end()),
231 decltype(std::declval<T>().clear()),
232 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
233 std::declval<const typename T::value_type &>()))>,
234 void>>
235 : public conditional_t<std::is_constructible<T, std::string>::value, std::false_type, std::true_type> {};
236
237// check to see if an object is a mutable container (fail by default)
238template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
239
243template <typename T>
245 T,
246 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
247 : public std::true_type {};
248
249// check to see if an object is a wrapper (fail by default)
250template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
251
252// check if an object is a wrapper (it has a value_type defined)
253template <typename T>
254struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
255
256// Check for tuple like types, as in classes with a tuple_size type trait
257template <typename S> class is_tuple_like {
258 template <typename SS>
259 // static auto test(int)
260 // -> decltype(std::conditional<(std::tuple_size<SS>::value > 0), std::true_type, std::false_type>::type());
261 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
262 template <typename> static auto test(...) -> std::false_type;
263
264 public:
265 static constexpr bool value = decltype(test<S>(0))::value;
266};
267
269template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
270auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
271 return std::forward<T>(value);
272}
273
275template <typename T,
276 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
278std::string to_string(const T &value) {
279 return std::string(value); // NOLINT(google-readability-casting)
280}
281
283template <typename T,
284 enable_if_t<!std::is_convertible<std::string, T>::value && !std::is_constructible<std::string, T>::value &&
287std::string to_string(T &&value) {
288 std::stringstream stream;
289 stream << value;
290 return stream.str();
291}
292
294template <typename T,
296 !is_readable_container<typename std::remove_const<T>::type>::value,
298std::string to_string(T &&) {
299 return {};
300}
301
303template <typename T,
304 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
305 is_readable_container<T>::value,
307std::string to_string(T &&variable) {
308 auto cval = variable.begin();
309 auto end = variable.end();
310 if(cval == end) {
311 return {"{}"};
312 }
313 std::vector<std::string> defaults;
314 while(cval != end) {
315 defaults.emplace_back(CLI::detail::to_string(*cval));
316 ++cval;
317 }
318 return {"[" + detail::join(defaults) + "]"};
319}
320
322template <typename T1,
323 typename T2,
324 typename T,
325 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
326auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
327 return to_string(std::forward<T>(value));
328}
329
331template <typename T1,
332 typename T2,
333 typename T,
335std::string checked_to_string(T &&) {
336 return std::string{};
337}
339template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
340std::string value_string(const T &value) {
341 return std::to_string(value);
342}
344template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
345std::string value_string(const T &value) {
346 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
347}
349template <typename T,
350 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
351auto value_string(const T &value) -> decltype(to_string(value)) {
352 return to_string(value);
353}
354
356template <typename T, typename def, typename Enable = void> struct wrapped_type { using type = def; };
357
359template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
360 using type = typename T::value_type;
361};
362
364template <typename T, typename Enable = void> struct type_count_base { static const int value{0}; };
365
367template <typename T>
369 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
370 !std::is_void<T>::value>::type> {
371 static constexpr int value{1};
372};
373
375template <typename T>
376struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
377 static constexpr int value{std::tuple_size<T>::value};
378};
379
381template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
383};
384
386
388template <typename T> struct subtype_count;
389
391template <typename T> struct subtype_count_min;
392
394template <typename T, typename Enable = void> struct type_count { static const int value{0}; };
395
397template <typename T>
398struct type_count<T,
399 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
400 !std::is_void<T>::value>::type> {
401 static constexpr int value{1};
402};
403
405template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
406 static constexpr int value{2};
407};
408
410template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
412};
413
415template <typename T>
416struct type_count<T,
417 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
418 !is_mutable_container<T>::value>::type> {
420};
421
423template <typename T, std::size_t I>
424constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
425 return 0;
426}
427
429template <typename T, std::size_t I>
430 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
431 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
432}
433
435template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
436 static constexpr int value{tuple_type_size<T, 0>()};
437};
438
440template <typename T> struct subtype_count {
441 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
442};
443
445template <typename T, typename Enable = void> struct type_count_min { static const int value{0}; };
446
448template <typename T>
449struct type_count_min<
450 T,
451 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
452 !is_complex<T>::value && !std::is_void<T>::value>::type> {
453 static constexpr int value{type_count<T>::value};
454};
455
457template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
458 static constexpr int value{1};
459};
460
462template <typename T>
463struct type_count_min<
464 T,
465 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
466 static constexpr int value{subtype_count_min<typename T::value_type>::value};
467};
468
470template <typename T, std::size_t I>
471constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
472 return 0;
473}
474
476template <typename T, std::size_t I>
477 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
478 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
479}
480
482template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
483 static constexpr int value{tuple_type_size_min<T, 0>()};
484};
485
487template <typename T> struct subtype_count_min {
488 static constexpr int value{is_mutable_container<T>::value
490 : type_count_min<T>::value};
491};
492
494template <typename T, typename Enable = void> struct expected_count { static const int value{0}; };
495
497template <typename T>
498struct expected_count<T,
499 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
500 !std::is_void<T>::value>::type> {
501 static constexpr int value{1};
502};
504template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
505 static constexpr int value{expected_max_vector_size};
506};
507
509template <typename T>
510struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
511 static constexpr int value{expected_count<typename T::value_type>::value};
512};
513
514// Enumeration of the different supported categorizations of objects
515enum class object_category : int {
516 char_value = 1,
517 integral_value = 2,
518 unsigned_integral = 4,
519 enumeration = 6,
520 boolean_value = 8,
521 floating_point = 10,
522 number_constructible = 12,
523 double_constructible = 14,
524 integer_constructible = 16,
525 // string like types
526 string_assignable = 23,
527 string_constructible = 24,
528 other = 45,
529 // special wrapper or container types
530 wrapper_value = 50,
531 complex_number = 60,
532 tuple_value = 70,
533 container_value = 80,
534
535};
536
538
540template <typename T, typename Enable = void> struct classify_object {
541 static constexpr object_category value{object_category::other};
542};
543
545template <typename T>
546struct classify_object<
547 T,
548 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
549 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
550 static constexpr object_category value{object_category::integral_value};
551};
552
554template <typename T>
555struct classify_object<T,
556 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
557 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
558 static constexpr object_category value{object_category::unsigned_integral};
559};
560
562template <typename T>
563struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
564 static constexpr object_category value{object_category::char_value};
565};
566
568template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
569 static constexpr object_category value{object_category::boolean_value};
570};
571
573template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
574 static constexpr object_category value{object_category::floating_point};
575};
576
578template <typename T>
579struct classify_object<T,
580 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
581 std::is_assignable<T &, std::string>::value>::type> {
582 static constexpr object_category value{object_category::string_assignable};
583};
584
586template <typename T>
587struct classify_object<
588 T,
589 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
590 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
591 std::is_constructible<T, std::string>::value>::type> {
592 static constexpr object_category value{object_category::string_constructible};
593};
594
596template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
597 static constexpr object_category value{object_category::enumeration};
598};
599
600template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
601 static constexpr object_category value{object_category::complex_number};
602};
603
606template <typename T> struct uncommon_type {
607 using type = typename std::conditional<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
608 !std::is_assignable<T &, std::string>::value &&
609 !std::is_constructible<T, std::string>::value && !is_complex<T>::value &&
610 !is_mutable_container<T>::value && !std::is_enum<T>::value,
611 std::true_type,
612 std::false_type>::type;
613 static constexpr bool value = type::value;
614};
615
617template <typename T>
618struct classify_object<T,
619 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
620 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
621 static constexpr object_category value{object_category::wrapper_value};
622};
623
625template <typename T>
626struct classify_object<T,
627 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
628 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
629 is_direct_constructible<T, int>::value>::type> {
630 static constexpr object_category value{object_category::number_constructible};
631};
632
634template <typename T>
635struct classify_object<T,
636 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
637 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
638 is_direct_constructible<T, int>::value>::type> {
639 static constexpr object_category value{object_category::integer_constructible};
640};
641
643template <typename T>
644struct classify_object<T,
645 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
646 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
647 !is_direct_constructible<T, int>::value>::type> {
648 static constexpr object_category value{object_category::double_constructible};
649};
650
652template <typename T>
653struct classify_object<
654 T,
655 typename std::enable_if<is_tuple_like<T>::value &&
656 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
657 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
658 !is_direct_constructible<T, int>::value) ||
659 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
660 static constexpr object_category value{object_category::tuple_value};
661 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
662 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
663 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
664 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
665 // those cases that are caught by other object classifications
666};
667
669template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
670 static constexpr object_category value{object_category::container_value};
671};
672
673// Type name print
674
678
679template <typename T,
680 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
681constexpr const char *type_name() {
682 return "CHAR";
683}
684
685template <typename T,
686 enable_if_t<classify_object<T>::value == object_category::integral_value ||
687 classify_object<T>::value == object_category::integer_constructible,
689constexpr const char *type_name() {
690 return "INT";
691}
692
693template <typename T,
694 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
695constexpr const char *type_name() {
696 return "UINT";
697}
698
699template <typename T,
700 enable_if_t<classify_object<T>::value == object_category::floating_point ||
701 classify_object<T>::value == object_category::number_constructible ||
702 classify_object<T>::value == object_category::double_constructible,
704constexpr const char *type_name() {
705 return "FLOAT";
706}
707
709template <typename T,
710 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
711constexpr const char *type_name() {
712 return "ENUM";
713}
714
716template <typename T,
717 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
718constexpr const char *type_name() {
719 return "BOOLEAN";
720}
721
723template <typename T,
724 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
725constexpr const char *type_name() {
726 return "COMPLEX";
727}
728
730template <typename T,
731 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
732 classify_object<T>::value <= object_category::other,
734constexpr const char *type_name() {
735 return "TEXT";
736}
738template <typename T,
739 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
741std::string type_name(); // forward declaration
742
744template <typename T,
745 enable_if_t<classify_object<T>::value == object_category::container_value ||
746 classify_object<T>::value == object_category::wrapper_value,
748std::string type_name(); // forward declaration
749
751template <typename T,
752 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
754inline std::string type_name() {
755 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
756}
757
759template <typename T, std::size_t I>
760inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
761 return std::string{};
762}
763
765template <typename T, std::size_t I>
766inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
767 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
768 tuple_name<T, I + 1>();
769 if(str.back() == ',')
770 str.pop_back();
771 return str;
772}
773
775template <typename T,
776 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
778inline std::string type_name() {
779 auto tname = std::string(1, '[') + tuple_name<T, 0>();
780 tname.push_back(']');
781 return tname;
782}
783
785template <typename T,
786 enable_if_t<classify_object<T>::value == object_category::container_value ||
787 classify_object<T>::value == object_category::wrapper_value,
789inline std::string type_name() {
790 return type_name<typename T::value_type>();
791}
792
793// Lexical cast
794
796template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
797bool integral_conversion(const std::string &input, T &output) noexcept {
798 if(input.empty()) {
799 return false;
800 }
801 char *val = nullptr;
802 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
803 output = static_cast<T>(output_ll);
804 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
805 return true;
806 }
807 val = nullptr;
808 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
809 if(val == (input.c_str() + input.size())) {
810 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
811 return (static_cast<std::int64_t>(output) == output_sll);
812 }
813 return false;
814}
815
817template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
818bool integral_conversion(const std::string &input, T &output) noexcept {
819 if(input.empty()) {
820 return false;
821 }
822 char *val = nullptr;
823 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
824 output = static_cast<T>(output_ll);
825 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
826 return true;
827 }
828 if(input == "true") {
829 // this is to deal with a few oddities with flags and wrapper int types
830 output = static_cast<T>(1);
831 return true;
832 }
833 return false;
834}
835
837inline std::int64_t to_flag_value(std::string val) {
838 static const std::string trueString("true");
839 static const std::string falseString("false");
840 if(val == trueString) {
841 return 1;
842 }
843 if(val == falseString) {
844 return -1;
845 }
846 val = detail::to_lower(val);
847 std::int64_t ret = 0;
848 if(val.size() == 1) {
849 if(val[0] >= '1' && val[0] <= '9') {
850 return (static_cast<std::int64_t>(val[0]) - '0');
851 }
852 switch(val[0]) {
853 case '0':
854 case 'f':
855 case 'n':
856 case '-':
857 ret = -1;
858 break;
859 case 't':
860 case 'y':
861 case '+':
862 ret = 1;
863 break;
864 default:
865 throw std::invalid_argument("unrecognized character");
866 }
867 return ret;
868 }
869 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
870 ret = 1;
871 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
872 ret = -1;
873 } else {
874 ret = std::stoll(val);
875 }
876 return ret;
877}
878
880template <typename T,
881 enable_if_t<classify_object<T>::value == object_category::integral_value ||
882 classify_object<T>::value == object_category::unsigned_integral,
884bool lexical_cast(const std::string &input, T &output) {
885 return integral_conversion(input, output);
886}
887
889template <typename T,
891bool lexical_cast(const std::string &input, T &output) {
892 if(input.size() == 1) {
893 output = static_cast<T>(input[0]);
894 return true;
895 }
896 return integral_conversion(input, output);
897}
898
900template <typename T,
901 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
902bool lexical_cast(const std::string &input, T &output) {
903 try {
904 auto out = to_flag_value(input);
905 output = (out > 0);
906 return true;
907 } catch(const std::invalid_argument &) {
908 return false;
909 } catch(const std::out_of_range &) {
910 // if the number is out of the range of a 64 bit value then it is still a number and for this purpose is still
911 // valid all we care about the sign
912 output = (input[0] != '-');
913 return true;
914 }
915}
916
918template <typename T,
919 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
920bool lexical_cast(const std::string &input, T &output) {
921 if(input.empty()) {
922 return false;
923 }
924 char *val = nullptr;
925 auto output_ld = std::strtold(input.c_str(), &val);
926 output = static_cast<T>(output_ld);
927 return val == (input.c_str() + input.size());
928}
929
931template <typename T,
932 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
933bool lexical_cast(const std::string &input, T &output) {
934 using XC = typename wrapped_type<T, double>::type;
935 XC x{0.0}, y{0.0};
936 auto str1 = input;
937 bool worked = false;
938 auto nloc = str1.find_last_of("+-");
939 if(nloc != std::string::npos && nloc > 0) {
940 worked = detail::lexical_cast(str1.substr(0, nloc), x);
941 str1 = str1.substr(nloc);
942 if(str1.back() == 'i' || str1.back() == 'j')
943 str1.pop_back();
944 worked = worked && detail::lexical_cast(str1, y);
945 } else {
946 if(str1.back() == 'i' || str1.back() == 'j') {
947 str1.pop_back();
948 worked = detail::lexical_cast(str1, y);
949 x = XC{0};
950 } else {
951 worked = detail::lexical_cast(str1, x);
952 y = XC{0};
953 }
954 }
955 if(worked) {
956 output = T{x, y};
957 return worked;
958 }
959 return from_stream(input, output);
960}
961
963template <typename T,
964 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
965bool lexical_cast(const std::string &input, T &output) {
966 output = input;
967 return true;
968}
969
971template <
972 typename T,
973 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
974bool lexical_cast(const std::string &input, T &output) {
975 output = T(input);
976 return true;
977}
978
980template <typename T,
981 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
982bool lexical_cast(const std::string &input, T &output) {
983 typename std::underlying_type<T>::type val;
984 if(!integral_conversion(input, val)) {
985 return false;
986 }
987 output = static_cast<T>(val);
988 return true;
989}
990
992template <typename T,
993 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
994 std::is_assignable<T &, typename T::value_type>::value,
996bool lexical_cast(const std::string &input, T &output) {
997 typename T::value_type val;
998 if(lexical_cast(input, val)) {
999 output = val;
1000 return true;
1001 }
1002 return from_stream(input, output);
1003}
1004
1005template <typename T,
1006 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1007 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1009bool lexical_cast(const std::string &input, T &output) {
1010 typename T::value_type val;
1011 if(lexical_cast(input, val)) {
1012 output = T{val};
1013 return true;
1014 }
1015 return from_stream(input, output);
1016}
1017
1019template <
1020 typename T,
1021 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1022bool lexical_cast(const std::string &input, T &output) {
1023 int val = 0;
1024 if(integral_conversion(input, val)) {
1025 output = T(val);
1026 return true;
1027 }
1028
1029 double dval = 0.0;
1030 if(lexical_cast(input, dval)) {
1031 output = T{dval};
1032 return true;
1033 }
1034
1035 return from_stream(input, output);
1036}
1037
1039template <
1040 typename T,
1041 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1042bool lexical_cast(const std::string &input, T &output) {
1043 int val = 0;
1044 if(integral_conversion(input, val)) {
1045 output = T(val);
1046 return true;
1047 }
1048 return from_stream(input, output);
1049}
1050
1052template <
1053 typename T,
1054 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1055bool lexical_cast(const std::string &input, T &output) {
1056 double val = 0.0;
1057 if(lexical_cast(input, val)) {
1058 output = T{val};
1059 return true;
1060 }
1061 return from_stream(input, output);
1062}
1063
1065template <typename T,
1066 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1068bool lexical_cast(const std::string &input, T &output) {
1069 int val = 0;
1070 if(integral_conversion(input, val)) {
1071#ifdef _MSC_VER
1072#pragma warning(push)
1073#pragma warning(disable : 4800)
1074#endif
1075 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1076 // so will most likely still work
1077 output = val;
1078#ifdef _MSC_VER
1079#pragma warning(pop)
1080#endif
1081 return true;
1082 }
1083 // LCOV_EXCL_START
1084 // This version of cast is only used for odd cases in an older compilers the fail over
1085 // from_stream is tested elsewhere an not relevant for coverage here
1086 return from_stream(input, output);
1087 // LCOV_EXCL_STOP
1088}
1089
1091template <typename T,
1092 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value,
1094bool lexical_cast(const std::string &input, T &output) {
1095 static_assert(is_istreamable<T>::value,
1096 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1097 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1098 return from_stream(input, output);
1099}
1100
1103template <typename AssignTo,
1104 typename ConvertTo,
1105 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1106 (classify_object<AssignTo>::value == object_category::string_assignable ||
1107 classify_object<AssignTo>::value == object_category::string_constructible),
1109bool lexical_assign(const std::string &input, AssignTo &output) {
1110 return lexical_cast(input, output);
1111}
1112
1114template <typename AssignTo,
1115 typename ConvertTo,
1116 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1117 classify_object<AssignTo>::value != object_category::string_assignable &&
1118 classify_object<AssignTo>::value != object_category::string_constructible,
1120bool lexical_assign(const std::string &input, AssignTo &output) {
1121 if(input.empty()) {
1122 output = AssignTo{};
1123 return true;
1124 }
1125
1126 return lexical_cast(input, output);
1127}
1128
1130template <typename AssignTo,
1131 typename ConvertTo,
1132 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1133 classify_object<AssignTo>::value == object_category::wrapper_value,
1135bool lexical_assign(const std::string &input, AssignTo &output) {
1136 if(input.empty()) {
1137 typename AssignTo::value_type emptyVal{};
1138 output = emptyVal;
1139 return true;
1140 }
1141 return lexical_cast(input, output);
1142}
1143
1146template <typename AssignTo,
1147 typename ConvertTo,
1148 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1149 classify_object<AssignTo>::value != object_category::wrapper_value &&
1150 std::is_assignable<AssignTo &, int>::value,
1152bool lexical_assign(const std::string &input, AssignTo &output) {
1153 if(input.empty()) {
1154 output = 0;
1155 return true;
1156 }
1157 int val = 0;
1158 if(lexical_cast(input, val)) {
1159 output = val;
1160 return true;
1161 }
1162 return false;
1163}
1164
1166template <typename AssignTo,
1167 typename ConvertTo,
1168 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1170bool lexical_assign(const std::string &input, AssignTo &output) {
1171 ConvertTo val{};
1172 bool parse_result = (!input.empty()) ? lexical_cast<ConvertTo>(input, val) : true;
1173 if(parse_result) {
1174 output = val;
1175 }
1176 return parse_result;
1177}
1178
1180template <
1181 typename AssignTo,
1182 typename ConvertTo,
1183 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1184 std::is_move_assignable<AssignTo>::value,
1186bool lexical_assign(const std::string &input, AssignTo &output) {
1187 ConvertTo val{};
1188 bool parse_result = input.empty() ? true : lexical_cast<ConvertTo>(input, val);
1189 if(parse_result) {
1190 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1191 }
1192 return parse_result;
1193}
1194
1196template <typename AssignTo,
1197 typename ConvertTo,
1198 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1199 classify_object<AssignTo>::value <= object_category::wrapper_value,
1201bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1202 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1203}
1204
1207template <typename AssignTo,
1208 typename ConvertTo,
1209 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1212bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1213 // the remove const is to handle pair types coming from a container
1214 typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type v1;
1215 typename std::tuple_element<1, ConvertTo>::type v2;
1216 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[0], v1);
1217 if(strings.size() > 1) {
1218 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[1], v2);
1219 }
1220 if(retval) {
1221 output = AssignTo{v1, v2};
1222 }
1223 return retval;
1224}
1225
1227template <class AssignTo,
1228 class ConvertTo,
1229 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1232bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1233 output.erase(output.begin(), output.end());
1234 if(strings.size() == 1 && strings[0] == "{}") {
1235 return true;
1236 }
1237 bool skip_remaining = false;
1238 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1239 skip_remaining = true;
1240 }
1241 for(const auto &elem : strings) {
1242 typename AssignTo::value_type out;
1243 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1244 if(!retval) {
1245 return false;
1246 }
1247 output.insert(output.end(), std::move(out));
1248 if(skip_remaining) {
1249 break;
1250 }
1251 }
1252 return (!output.empty());
1253}
1254
1256template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1257bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1258
1259 if(strings.size() >= 2 && !strings[1].empty()) {
1260 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1261 XC2 x{0.0}, y{0.0};
1262 auto str1 = strings[1];
1263 if(str1.back() == 'i' || str1.back() == 'j') {
1264 str1.pop_back();
1265 }
1266 auto worked = detail::lexical_cast(strings[0], x) && detail::lexical_cast(str1, y);
1267 if(worked) {
1268 output = ConvertTo{x, y};
1269 }
1270 return worked;
1271 }
1272 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1273}
1274
1276template <class AssignTo,
1277 class ConvertTo,
1278 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1281bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1282 bool retval = true;
1283 output.clear();
1284 output.reserve(strings.size());
1285 for(const auto &elem : strings) {
1286
1287 output.emplace_back();
1288 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1289 }
1290 return (!output.empty()) && retval;
1291}
1292
1293// forward declaration
1294
1296template <class AssignTo,
1297 class ConvertTo,
1298 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1301bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1302
1304template <class AssignTo,
1305 class ConvertTo,
1306 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1311bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1312
1314template <class AssignTo,
1315 class ConvertTo,
1316 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1320bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1321
1324template <typename AssignTo,
1325 typename ConvertTo,
1326 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1327 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1328 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1330bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1331
1332 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1333 ConvertTo val;
1334 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1335 output = AssignTo{val};
1336 return retval;
1337 }
1338 output = AssignTo{};
1339 return true;
1340}
1341
1343template <class AssignTo, class ConvertTo, std::size_t I>
1344inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1345tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1346 return true;
1347}
1348
1350template <class AssignTo, class ConvertTo>
1351inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1352tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1353 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1354 strings.erase(strings.begin());
1355 return retval;
1356}
1357
1359template <class AssignTo, class ConvertTo>
1360inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1361 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1362 bool>::type
1363tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1364 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1365 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1366 return retval;
1367}
1368
1370template <class AssignTo, class ConvertTo>
1371inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1372 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1373 bool>::type
1374tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1375
1376 std::size_t index{subtype_count_min<ConvertTo>::value};
1377 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1378 const std::size_t mx{(std::max)(mx_count, strings.size())};
1379
1380 while(index < mx) {
1381 if(is_separator(strings[index])) {
1382 break;
1383 }
1384 ++index;
1385 }
1386 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1387 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1388 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1389 return retval;
1390}
1391
1393template <class AssignTo, class ConvertTo, std::size_t I>
1394inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1395tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1396 bool retval = true;
1397 using ConvertToElement = typename std::
1398 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1399 if(!strings.empty()) {
1400 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1401 strings, std::get<I>(output));
1402 }
1403 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1404 return retval;
1405}
1406
1408template <class AssignTo,
1409 class ConvertTo,
1410 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1413bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1414 output.clear();
1415 while(!strings.empty()) {
1416
1417 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1418 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1419 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1420 if(!strings.empty()) {
1421 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1422 }
1423 if(retval) {
1424 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1425 } else {
1426 return false;
1427 }
1428 }
1429 return (!output.empty());
1430}
1431
1433template <class AssignTo,
1434 class ConvertTo,
1435 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1439bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1440 static_assert(
1442 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1443 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1444}
1445
1447template <class AssignTo,
1448 class ConvertTo,
1449 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1454bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1455 bool retval = true;
1456 output.clear();
1457 std::vector<std::string> temp;
1458 std::size_t ii{0};
1459 std::size_t icount{0};
1460 std::size_t xcm{type_count<ConvertTo>::value};
1461 auto ii_max = strings.size();
1462 while(ii < ii_max) {
1463 temp.push_back(strings[ii]);
1464 ++ii;
1465 ++icount;
1466 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1467 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1468 temp.pop_back();
1469 }
1470 typename AssignTo::value_type temp_out;
1471 retval = retval &&
1472 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1473 temp.clear();
1474 if(!retval) {
1475 return false;
1476 }
1477 output.insert(output.end(), std::move(temp_out));
1478 icount = 0;
1479 }
1480 }
1481 return retval;
1482}
1483
1485template <typename AssignTo,
1486 class ConvertTo,
1487 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1488 std::is_assignable<ConvertTo &, ConvertTo>::value,
1490bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1491 if(strings.empty() || strings.front().empty()) {
1492 output = ConvertTo{};
1493 return true;
1494 }
1495 typename ConvertTo::value_type val;
1496 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1497 output = ConvertTo{val};
1498 return true;
1499 }
1500 return false;
1501}
1502
1504template <typename AssignTo,
1505 class ConvertTo,
1506 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1507 !std::is_assignable<AssignTo &, ConvertTo>::value,
1509bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1510 using ConvertType = typename ConvertTo::value_type;
1511 if(strings.empty() || strings.front().empty()) {
1512 output = ConvertType{};
1513 return true;
1514 }
1515 ConvertType val;
1516 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1517 output = val;
1518 return true;
1519 }
1520 return false;
1521}
1522
1524inline std::string sum_string_vector(const std::vector<std::string> &values) {
1525 double val{0.0};
1526 bool fail{false};
1527 std::string output;
1528 for(const auto &arg : values) {
1529 double tv{0.0};
1530 auto comp = detail::lexical_cast<double>(arg, tv);
1531 if(!comp) {
1532 try {
1533 tv = static_cast<double>(detail::to_flag_value(arg));
1534 } catch(const std::exception &) {
1535 fail = true;
1536 break;
1537 }
1538 }
1539 val += tv;
1540 }
1541 if(fail) {
1542 for(const auto &arg : values) {
1543 output.append(arg);
1544 }
1545 } else {
1546 if(val <= static_cast<double>((std::numeric_limits<std::int64_t>::min)()) ||
1547 val >= static_cast<double>((std::numeric_limits<std::int64_t>::max)()) ||
1548 // NOLINTNEXTLINE(clang-diagnostic-float-equal,bugprone-narrowing-conversions)
1549 val == static_cast<std::int64_t>(val)) {
1550 output = detail::value_string(static_cast<int64_t>(val));
1551 } else {
1552 output = detail::value_string(val);
1553 }
1554 }
1555 return output;
1556}
1557
1558} // namespace detail
1559// [CLI11:type_tools_hpp:end]
1560} // namespace CLI
Check for complex.
Definition: TypeTools.hpp:195
static constexpr bool value
Definition: TypeTools.hpp:202
Definition: TypeTools.hpp:145
static constexpr bool value
Definition: TypeTools.hpp:164
Check for input streamability.
Definition: TypeTools.hpp:184
static constexpr bool value
Definition: TypeTools.hpp:191
Definition: TypeTools.hpp:173
static constexpr bool value
Definition: TypeTools.hpp:180
Definition: TypeTools.hpp:257
static constexpr bool value
Definition: TypeTools.hpp:265
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:34
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:270
auto checked_to_string(T &&value) -> decltype(to_string(std::forward< T >(value)))
special template overload
Definition: TypeTools.hpp:326
bool is_separator(const std::string &str)
check if a string is a container segment separator (empty or "%%")
Definition: StringTools.hpp:163
constexpr std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size() { return subtype_count< typename std::tuple_element< I, T >::type >::value+tuple_type_size< T, I+1 >();}template< typename T > struct type_count< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size< T, 0 >()};};template< typename T > struct subtype_count { static constexpr int value{is_mutable_container< T >::value ? expected_max_vector_size :type_count< T >::value};};template< typename T, typename Enable=void > struct type_count_min { static const int value{0};};template< typename T >struct type_count_min< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_tuple_like< T >::value &&!is_wrapper< T >::value &&!is_complex< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{type_count< T >::value};};template< typename T > struct type_count_min< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr int value{1};};template< typename T >struct type_count_min< T, typename std::enable_if< is_wrapper< T >::value &&!is_complex< T >::value &&!is_tuple_like< T >::value >::type > { static constexpr int value{subtype_count_min< typename T::value_type >::value};};template< typename T, std::size_t I >constexpr typename std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size_min() { return 0;}template< typename T, std::size_t I > constexpr typename std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size_min() { return subtype_count_min< typename std::tuple_element< I, T >::type >::value+tuple_type_size_min< T, I+1 >();}template< typename T > struct type_count_min< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size_min< T, 0 >()};};template< typename T > struct subtype_count_min { static constexpr int value{is_mutable_container< T >::value ?((type_count< T >::value< expected_max_vector_size) ? type_count< T >::value :0) :type_count_min< T >::value};};template< typename T, typename Enable=void > struct expected_count { static const int value{0};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_wrapper< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{1};};template< typename T > struct expected_count< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr int value{expected_max_vector_size};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&is_wrapper< T >::value >::type > { static constexpr int value{expected_count< typename T::value_type >::value};};enum class object_category :int { char_value=1, integral_value=2, unsigned_integral=4, enumeration=6, boolean_value=8, floating_point=10, number_constructible=12, double_constructible=14, integer_constructible=16, string_assignable=23, string_constructible=24, other=45, wrapper_value=50, complex_number=60, tuple_value=70, container_value=80,};template< typename T, typename Enable=void > struct classify_object { static constexpr object_category value{object_category::other};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, char >::value &&std::is_signed< T >::value &&!is_bool< T >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::integral_value};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&!std::is_same< T, char >::value &&!is_bool< T >::value >::type > { static constexpr object_category value{object_category::unsigned_integral};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_same< T, char >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::char_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_bool< T >::value >::type > { static constexpr object_category value{object_category::boolean_value};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_floating_point< T >::value >::type > { static constexpr object_category value{object_category::floating_point};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&std::is_assignable< T &, std::string >::value >::type > { static constexpr object_category value{object_category::string_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&(type_count< T >::value==1) &&std::is_constructible< T, std::string >::value >::type > { static constexpr object_category value{object_category::string_constructible};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::enumeration};};template< typename T > struct classify_object< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr object_category value{object_category::complex_number};};template< typename T > struct uncommon_type { using type=typename std::conditional<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&!std::is_constructible< T, std::string >::value &&!is_complex< T >::value &&!is_mutable_container< T >::value &&!std::is_enum< T >::value, std::true_type, std::false_type >::type;static constexpr bool value=type::value;};template< typename T >struct classify_object< T, typename std::enable_if<(!is_mutable_container< T >::value &&is_wrapper< T >::value &&!is_tuple_like< T >::value &&uncommon_type< T >::value)>::type > { static constexpr object_category value{object_category::wrapper_value};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::number_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&!is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::integer_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::double_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< is_tuple_like< T >::value &&((type_count< T >::value >=2 &&!is_wrapper< T >::value)||(uncommon_type< T >::value &&!is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value)||(uncommon_type< T >::value &&type_count< T >::value >=2))>::type > { static constexpr object_category value{object_category::tuple_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr object_category value{object_category::container_value};};template< typename T, enable_if_t< classify_object< T >::value==object_category::char_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "CHAR";}template< typename T, enable_if_t< classify_object< T >::value==object_category::integral_value||classify_object< T >::value==object_category::integer_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "INT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::unsigned_integral, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "UINT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::floating_point||classify_object< T >::value==object_category::number_constructible||classify_object< T >::value==object_category::double_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "FLOAT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::enumeration, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "ENUM";}template< typename T, enable_if_t< classify_object< T >::value==object_category::boolean_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "BOOLEAN";}template< typename T, enable_if_t< classify_object< T >::value==object_category::complex_number, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "COMPLEX";}template< typename T, enable_if_t< classify_object< T >::value >=object_category::string_assignable &&classify_object< T >::value<=object_category::other, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "TEXT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::container_value||classify_object< T >::value==object_category::wrapper_value, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value==1, detail::enabler >=detail::dummy >inline std::string type_name() { return type_name< typename std::decay< typename std::tuple_element< 0, T >::type >::type >();}template< typename T, std::size_t I >inline typename std::enable_if< I==type_count_base< T >::value, std::string >::type tuple_name() { return std::string{};}template< typename T, std::size_t I >inline typename std::enable_if<(I< type_count_base< T >::value), std::string >::type tuple_name() { auto str=std::string{type_name< typename std::decay< typename std::tuple_element< I, T >::type >::type >()}+','+tuple_name< T, I+1 >();if(str.back()==',') str.pop_back();return str;}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler > > std::string type_name()
Recursively generate the tuple type name.
Definition: TypeTools.hpp:778
bool from_stream(const std::string &istring, T &obj)
Templated operation to get a value from a stream.
Definition: TypeTools.hpp:207
constexpr int expected_max_vector_size
Definition: StringTools.hpp:45
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:340
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:51
bool lexical_assign(const std::string &input, AssignTo &output)
Assign a value through lexical cast operations.
Definition: TypeTools.hpp:1109
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:174
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:31
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:884
std::int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition: TypeTools.hpp:837
bool integral_conversion(const std::string &input, T &output) noexcept
Convert to an unsigned integral.
Definition: TypeTools.hpp:797
constexpr std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size()
0 if the index > tuple size
Definition: TypeTools.hpp:424
Definition: App.hpp:34
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:42
typename std::conditional< B, T, F >::type conditional_t
A copy of std::conditional_t from C++14 - same reasoning as enable_if_t, it does not hurt to redefine...
Definition: TypeTools.hpp:51
typename make_void< Ts... >::type void_t
A copy of std::void_t from C++17 - same reasoning as enable_if_t, it does not hurt to redefine.
Definition: TypeTools.hpp:48
std::string type
Definition: TypeTools.hpp:77
This can be specialized to override the type deduction for IsMember.
Definition: TypeTools.hpp:74
T type
Definition: TypeTools.hpp:74
typename std::pointer_traits< T >::element_type type
Definition: TypeTools.hpp:90
not a pointer
Definition: TypeTools.hpp:87
T type
Definition: TypeTools.hpp:87
Definition: TypeTools.hpp:95
typename element_type< T >::type::value_type type
Definition: TypeTools.hpp:95
Definition: TypeTools.hpp:220
Definition: TypeTools.hpp:238
Definition: TypeTools.hpp:250
typename std::remove_const< typename value_type::first_type >::type first_type
Definition: TypeTools.hpp:121
static auto first(Q &&pair_value) -> decltype(std::get< 0 >(std::forward< Q >(pair_value)))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:125
static auto second(Q &&pair_value) -> decltype(std::get< 1 >(std::forward< Q >(pair_value)))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:129
typename std::remove_const< typename value_type::second_type >::type second_type
Definition: TypeTools.hpp:122
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition: TypeTools.hpp:98
typename T::value_type value_type
Definition: TypeTools.hpp:99
typename std::remove_const< value_type >::type second_type
Definition: TypeTools.hpp:101
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:108
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:100
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:104
forward declare the subtype_count_min structure
Definition: TypeTools.hpp:391
Set of overloads to get the type size of an object.
Definition: TypeTools.hpp:388
This will only trigger for actual void type.
Definition: TypeTools.hpp:364
static const int value
Definition: TypeTools.hpp:364
This will only trigger for actual void type.
Definition: TypeTools.hpp:394
static const int value
Definition: TypeTools.hpp:394
template to get the underlying value type if it exists or use a default
Definition: TypeTools.hpp:356
def type
Definition: TypeTools.hpp:356
Check to see if something is bool (fail check by default)
Definition: TypeTools.hpp:54
Check to see if something is copyable pointer.
Definition: TypeTools.hpp:69
static bool const value
Definition: TypeTools.hpp:70
Check to see if something is a shared pointer.
Definition: TypeTools.hpp:60
A copy of std::void_t from C++17 (helper for C++11 and C++14)
Definition: TypeTools.hpp:45
void type
Definition: TypeTools.hpp:45