CLI11 2.2.0
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,
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);
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 std::string{};
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 std::string("{}");
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 std::string("[" + 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))>::type> {
659 static constexpr object_category value{object_category::tuple_value};
660 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
661 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
662 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
663 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
664 // those cases that are caught by other object classifications
665};
666
668template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
669 static constexpr object_category value{object_category::container_value};
670};
671
672// Type name print
673
677
678template <typename T,
679 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
680constexpr const char *type_name() {
681 return "CHAR";
682}
683
684template <typename T,
685 enable_if_t<classify_object<T>::value == object_category::integral_value ||
686 classify_object<T>::value == object_category::integer_constructible,
688constexpr const char *type_name() {
689 return "INT";
690}
691
692template <typename T,
693 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
694constexpr const char *type_name() {
695 return "UINT";
696}
697
698template <typename T,
699 enable_if_t<classify_object<T>::value == object_category::floating_point ||
700 classify_object<T>::value == object_category::number_constructible ||
701 classify_object<T>::value == object_category::double_constructible,
703constexpr const char *type_name() {
704 return "FLOAT";
705}
706
708template <typename T,
709 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
710constexpr const char *type_name() {
711 return "ENUM";
712}
713
715template <typename T,
716 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
717constexpr const char *type_name() {
718 return "BOOLEAN";
719}
720
722template <typename T,
723 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
724constexpr const char *type_name() {
725 return "COMPLEX";
726}
727
729template <typename T,
730 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
731 classify_object<T>::value <= object_category::other,
733constexpr const char *type_name() {
734 return "TEXT";
735}
737template <typename T,
738 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
740std::string type_name(); // forward declaration
741
743template <typename T,
744 enable_if_t<classify_object<T>::value == object_category::container_value ||
745 classify_object<T>::value == object_category::wrapper_value,
747std::string type_name(); // forward declaration
748
750template <typename T,
751 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
753inline std::string type_name() {
754 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
755}
756
758template <typename T, std::size_t I>
759inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
760 return std::string{};
761}
762
764template <typename T, std::size_t I>
765inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
766 std::string str = std::string(type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()) +
767 ',' + tuple_name<T, I + 1>();
768 if(str.back() == ',')
769 str.pop_back();
770 return str;
771}
772
774template <typename T,
775 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
777inline std::string type_name() {
778 auto tname = std::string(1, '[') + tuple_name<T, 0>();
779 tname.push_back(']');
780 return tname;
781}
782
784template <typename T,
785 enable_if_t<classify_object<T>::value == object_category::container_value ||
786 classify_object<T>::value == object_category::wrapper_value,
788inline std::string type_name() {
789 return type_name<typename T::value_type>();
790}
791
792// Lexical cast
793
795template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
796bool integral_conversion(const std::string &input, T &output) noexcept {
797 if(input.empty()) {
798 return false;
799 }
800 char *val = nullptr;
801 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
802 output = static_cast<T>(output_ll);
803 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
804 return true;
805 }
806 val = nullptr;
807 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
808 if(val == (input.c_str() + input.size())) {
809 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
810 return (static_cast<std::int64_t>(output) == output_sll);
811 }
812 return false;
813}
814
816template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
817bool integral_conversion(const std::string &input, T &output) noexcept {
818 if(input.empty()) {
819 return false;
820 }
821 char *val = nullptr;
822 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
823 output = static_cast<T>(output_ll);
824 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
825 return true;
826 }
827 if(input == "true") {
828 // this is to deal with a few oddities with flags and wrapper int types
829 output = static_cast<T>(1);
830 return true;
831 }
832 return false;
833}
834
836inline std::int64_t to_flag_value(std::string val) {
837 static const std::string trueString("true");
838 static const std::string falseString("false");
839 if(val == trueString) {
840 return 1;
841 }
842 if(val == falseString) {
843 return -1;
844 }
845 val = detail::to_lower(val);
846 std::int64_t ret;
847 if(val.size() == 1) {
848 if(val[0] >= '1' && val[0] <= '9') {
849 return (static_cast<std::int64_t>(val[0]) - '0');
850 }
851 switch(val[0]) {
852 case '0':
853 case 'f':
854 case 'n':
855 case '-':
856 ret = -1;
857 break;
858 case 't':
859 case 'y':
860 case '+':
861 ret = 1;
862 break;
863 default:
864 throw std::invalid_argument("unrecognized character");
865 }
866 return ret;
867 }
868 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
869 ret = 1;
870 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
871 ret = -1;
872 } else {
873 ret = std::stoll(val);
874 }
875 return ret;
876}
877
879template <typename T,
880 enable_if_t<classify_object<T>::value == object_category::integral_value ||
881 classify_object<T>::value == object_category::unsigned_integral,
883bool lexical_cast(const std::string &input, T &output) {
884 return integral_conversion(input, output);
885}
886
888template <typename T,
890bool lexical_cast(const std::string &input, T &output) {
891 if(input.size() == 1) {
892 output = static_cast<T>(input[0]);
893 return true;
894 }
895 return integral_conversion(input, output);
896}
897
899template <typename T,
900 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
901bool lexical_cast(const std::string &input, T &output) {
902 try {
903 auto out = to_flag_value(input);
904 output = (out > 0);
905 return true;
906 } catch(const std::invalid_argument &) {
907 return false;
908 } catch(const std::out_of_range &) {
909 // 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
910 // valid all we care about the sign
911 output = (input[0] != '-');
912 return true;
913 }
914}
915
917template <typename T,
918 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
919bool lexical_cast(const std::string &input, T &output) {
920 if(input.empty()) {
921 return false;
922 }
923 char *val = nullptr;
924 auto output_ld = std::strtold(input.c_str(), &val);
925 output = static_cast<T>(output_ld);
926 return val == (input.c_str() + input.size());
927}
928
930template <typename T,
931 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
932bool lexical_cast(const std::string &input, T &output) {
933 using XC = typename wrapped_type<T, double>::type;
934 XC x{0.0}, y{0.0};
935 auto str1 = input;
936 bool worked = false;
937 auto nloc = str1.find_last_of("+-");
938 if(nloc != std::string::npos && nloc > 0) {
939 worked = detail::lexical_cast(str1.substr(0, nloc), x);
940 str1 = str1.substr(nloc);
941 if(str1.back() == 'i' || str1.back() == 'j')
942 str1.pop_back();
943 worked = worked && detail::lexical_cast(str1, y);
944 } else {
945 if(str1.back() == 'i' || str1.back() == 'j') {
946 str1.pop_back();
947 worked = detail::lexical_cast(str1, y);
948 x = XC{0};
949 } else {
950 worked = detail::lexical_cast(str1, x);
951 y = XC{0};
952 }
953 }
954 if(worked) {
955 output = T{x, y};
956 return worked;
957 }
958 return from_stream(input, output);
959}
960
962template <typename T,
963 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
964bool lexical_cast(const std::string &input, T &output) {
965 output = input;
966 return true;
967}
968
970template <
971 typename T,
972 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
973bool lexical_cast(const std::string &input, T &output) {
974 output = T(input);
975 return true;
976}
977
979template <typename T,
980 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
981bool lexical_cast(const std::string &input, T &output) {
982 typename std::underlying_type<T>::type val;
983 if(!integral_conversion(input, val)) {
984 return false;
985 }
986 output = static_cast<T>(val);
987 return true;
988}
989
991template <typename T,
992 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
993 std::is_assignable<T &, typename T::value_type>::value,
995bool lexical_cast(const std::string &input, T &output) {
996 typename T::value_type val;
997 if(lexical_cast(input, val)) {
998 output = val;
999 return true;
1000 }
1001 return from_stream(input, output);
1002}
1003
1004template <typename T,
1005 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1006 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1008bool lexical_cast(const std::string &input, T &output) {
1009 typename T::value_type val;
1010 if(lexical_cast(input, val)) {
1011 output = T{val};
1012 return true;
1013 }
1014 return from_stream(input, output);
1015}
1016
1018template <
1019 typename T,
1020 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1021bool lexical_cast(const std::string &input, T &output) {
1022 int val;
1023 if(integral_conversion(input, val)) {
1024 output = T(val);
1025 return true;
1026 } else {
1027 double dval;
1028 if(lexical_cast(input, dval)) {
1029 output = T{dval};
1030 return true;
1031 }
1032 }
1033 return from_stream(input, output);
1034}
1035
1037template <
1038 typename T,
1039 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1040bool lexical_cast(const std::string &input, T &output) {
1041 int val;
1042 if(integral_conversion(input, val)) {
1043 output = T(val);
1044 return true;
1045 }
1046 return from_stream(input, output);
1047}
1048
1050template <
1051 typename T,
1052 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1053bool lexical_cast(const std::string &input, T &output) {
1054 double val;
1055 if(lexical_cast(input, val)) {
1056 output = T{val};
1057 return true;
1058 }
1059 return from_stream(input, output);
1060}
1061
1063template <typename T,
1064 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1066bool lexical_cast(const std::string &input, T &output) {
1067 int val;
1068 if(integral_conversion(input, val)) {
1069#ifdef _MSC_VER
1070#pragma warning(push)
1071#pragma warning(disable : 4800)
1072#endif
1073 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1074 // so will most likely still work
1075 output = val;
1076#ifdef _MSC_VER
1077#pragma warning(pop)
1078#endif
1079 return true;
1080 }
1081 // LCOV_EXCL_START
1082 // This version of cast is only used for odd cases in an older compilers the fail over
1083 // from_stream is tested elsewhere an not relevant for coverage here
1084 return from_stream(input, output);
1085 // LCOV_EXCL_STOP
1086}
1087
1089template <typename T,
1090 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value,
1092bool lexical_cast(const std::string &input, T &output) {
1093 static_assert(is_istreamable<T>::value,
1094 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1095 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1096 return from_stream(input, output);
1097}
1098
1101template <typename AssignTo,
1102 typename ConvertTo,
1103 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1104 (classify_object<AssignTo>::value == object_category::string_assignable ||
1105 classify_object<AssignTo>::value == object_category::string_constructible),
1107bool lexical_assign(const std::string &input, AssignTo &output) {
1108 return lexical_cast(input, output);
1109}
1110
1112template <typename AssignTo,
1113 typename ConvertTo,
1114 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1115 classify_object<AssignTo>::value != object_category::string_assignable &&
1116 classify_object<AssignTo>::value != object_category::string_constructible,
1118bool lexical_assign(const std::string &input, AssignTo &output) {
1119 if(input.empty()) {
1120 output = AssignTo{};
1121 return true;
1122 }
1123
1124 return lexical_cast(input, output);
1125}
1126
1128template <typename AssignTo,
1129 typename ConvertTo,
1130 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1131 classify_object<AssignTo>::value == object_category::wrapper_value,
1133bool lexical_assign(const std::string &input, AssignTo &output) {
1134 if(input.empty()) {
1135 typename AssignTo::value_type emptyVal{};
1136 output = emptyVal;
1137 return true;
1138 }
1139 return lexical_cast(input, output);
1140}
1141
1144template <typename AssignTo,
1145 typename ConvertTo,
1146 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1147 classify_object<AssignTo>::value != object_category::wrapper_value &&
1148 std::is_assignable<AssignTo &, int>::value,
1150bool lexical_assign(const std::string &input, AssignTo &output) {
1151 if(input.empty()) {
1152 output = 0;
1153 return true;
1154 }
1155 int val;
1156 if(lexical_cast(input, val)) {
1157 output = val;
1158 return true;
1159 }
1160 return false;
1161}
1162
1164template <typename AssignTo,
1165 typename ConvertTo,
1166 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1168bool lexical_assign(const std::string &input, AssignTo &output) {
1169 ConvertTo val{};
1170 bool parse_result = (!input.empty()) ? lexical_cast<ConvertTo>(input, val) : true;
1171 if(parse_result) {
1172 output = val;
1173 }
1174 return parse_result;
1175}
1176
1178template <
1179 typename AssignTo,
1180 typename ConvertTo,
1181 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1182 std::is_move_assignable<AssignTo>::value,
1184bool lexical_assign(const std::string &input, AssignTo &output) {
1185 ConvertTo val{};
1186 bool parse_result = input.empty() ? true : lexical_cast<ConvertTo>(input, val);
1187 if(parse_result) {
1188 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1189 }
1190 return parse_result;
1191}
1192
1194template <typename AssignTo,
1195 typename ConvertTo,
1196 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1197 classify_object<AssignTo>::value <= object_category::wrapper_value,
1199bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1200 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1201}
1202
1205template <typename AssignTo,
1206 typename ConvertTo,
1207 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1210bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1211 // the remove const is to handle pair types coming from a container
1212 typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type v1;
1213 typename std::tuple_element<1, ConvertTo>::type v2;
1214 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[0], v1);
1215 if(strings.size() > 1) {
1216 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[1], v2);
1217 }
1218 if(retval) {
1219 output = AssignTo{v1, v2};
1220 }
1221 return retval;
1222}
1223
1225template <class AssignTo,
1226 class ConvertTo,
1227 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1230bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1231 output.erase(output.begin(), output.end());
1232 if(strings.size() == 1 && strings[0] == "{}") {
1233 return true;
1234 }
1235 bool skip_remaining = false;
1236 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1237 skip_remaining = true;
1238 }
1239 for(const auto &elem : strings) {
1240 typename AssignTo::value_type out;
1242 if(!retval) {
1243 return false;
1244 }
1245 output.insert(output.end(), std::move(out));
1246 if(skip_remaining) {
1247 break;
1248 }
1249 }
1250 return (!output.empty());
1251}
1252
1254template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1255bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1256
1257 if(strings.size() >= 2 && !strings[1].empty()) {
1258 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1259 XC2 x{0.0}, y{0.0};
1260 auto str1 = strings[1];
1261 if(str1.back() == 'i' || str1.back() == 'j') {
1262 str1.pop_back();
1263 }
1264 auto worked = detail::lexical_cast(strings[0], x) && detail::lexical_cast(str1, y);
1265 if(worked) {
1266 output = ConvertTo{x, y};
1267 }
1268 return worked;
1269 } else {
1270 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1271 }
1272}
1273
1275template <class AssignTo,
1276 class ConvertTo,
1277 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1280bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1281 bool retval = true;
1282 output.clear();
1283 output.reserve(strings.size());
1284 for(const auto &elem : strings) {
1285
1286 output.emplace_back();
1288 }
1289 return (!output.empty()) && retval;
1290}
1291
1292// forward declaration
1293
1295template <class AssignTo,
1296 class ConvertTo,
1297 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1300bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1301
1303template <class AssignTo,
1304 class ConvertTo,
1305 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1310bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1311
1313template <class AssignTo,
1314 class ConvertTo,
1315 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1319bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1320
1323template <typename AssignTo,
1324 typename ConvertTo,
1325 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1326 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1327 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1329bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1330
1331 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1332 ConvertTo val;
1333 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1334 output = AssignTo{val};
1335 return retval;
1336 }
1337 output = AssignTo{};
1338 return true;
1339}
1340
1342template <class AssignTo, class ConvertTo, std::size_t I>
1343inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1344tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1345 return true;
1346}
1347
1349template <class AssignTo, class ConvertTo>
1350inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1351tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1352 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1353 strings.erase(strings.begin());
1354 return retval;
1355}
1356
1358template <class AssignTo, class ConvertTo>
1359inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1360 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1361 bool>::type
1362tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1363 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1364 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1365 return retval;
1366}
1367
1369template <class AssignTo, class ConvertTo>
1370inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1371 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1372 bool>::type
1373tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1374
1375 std::size_t index{subtype_count_min<ConvertTo>::value};
1376 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1377 const std::size_t mx{(std::max)(mx_count, strings.size())};
1378
1379 while(index < mx) {
1380 if(is_separator(strings[index])) {
1381 break;
1382 }
1383 ++index;
1384 }
1385 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1386 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1387 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1388 return retval;
1389}
1390
1392template <class AssignTo, class ConvertTo, std::size_t I>
1393inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1394tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1395 bool retval = true;
1396 using ConvertToElement = typename std::
1397 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1398 if(!strings.empty()) {
1399 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1400 strings, std::get<I>(output));
1401 }
1402 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1403 return retval;
1404}
1405
1407template <class AssignTo,
1408 class ConvertTo,
1409 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1412bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1413 output.clear();
1414 while(!strings.empty()) {
1415
1416 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1417 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1418 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1419 if(!strings.empty()) {
1420 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1421 }
1422 if(retval) {
1423 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1424 } else {
1425 return false;
1426 }
1427 }
1428 return (!output.empty());
1429}
1430
1432template <class AssignTo,
1433 class ConvertTo,
1434 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1438bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1439 static_assert(
1441 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1442 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1443}
1444
1446template <class AssignTo,
1447 class ConvertTo,
1448 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1453bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1454 bool retval = true;
1455 output.clear();
1456 std::vector<std::string> temp;
1457 std::size_t ii{0};
1458 std::size_t icount{0};
1459 std::size_t xcm{type_count<ConvertTo>::value};
1460 auto ii_max = strings.size();
1461 while(ii < ii_max) {
1462 temp.push_back(strings[ii]);
1463 ++ii;
1464 ++icount;
1465 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1466 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1467 temp.pop_back();
1468 }
1469 typename AssignTo::value_type temp_out;
1470 retval = retval &&
1471 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1472 temp.clear();
1473 if(!retval) {
1474 return false;
1475 }
1476 output.insert(output.end(), std::move(temp_out));
1477 icount = 0;
1478 }
1479 }
1480 return retval;
1481}
1482
1484template <typename AssignTo,
1485 class ConvertTo,
1486 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1487 std::is_assignable<ConvertTo &, ConvertTo>::value,
1489bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1490 if(strings.empty() || strings.front().empty()) {
1491 output = ConvertTo{};
1492 return true;
1493 }
1494 typename ConvertTo::value_type val;
1495 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1496 output = ConvertTo{val};
1497 return true;
1498 }
1499 return false;
1500}
1501
1503template <typename AssignTo,
1504 class ConvertTo,
1505 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1506 !std::is_assignable<AssignTo &, ConvertTo>::value,
1508bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1509 using ConvertType = typename ConvertTo::value_type;
1510 if(strings.empty() || strings.front().empty()) {
1511 output = ConvertType{};
1512 return true;
1513 }
1514 ConvertType val;
1515 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1516 output = val;
1517 return true;
1518 }
1519 return false;
1520}
1521
1523inline std::string sum_string_vector(const std::vector<std::string> &values) {
1524 double val{0.0};
1525 bool fail{false};
1526 std::string output;
1527 for(const auto &arg : values) {
1528 double tv{0.0};
1529 auto comp = detail::lexical_cast<double>(arg, tv);
1530 if(!comp) {
1531 try {
1532 tv = static_cast<double>(detail::to_flag_value(arg));
1533 } catch(const std::exception &) {
1534 fail = true;
1535 break;
1536 }
1537 }
1538 val += tv;
1539 }
1540 if(fail) {
1541 for(const auto &arg : values) {
1542 output.append(arg);
1543 }
1544 } else {
1545 if(val <= static_cast<double>(std::numeric_limits<std::int64_t>::min()) ||
1546 val >= static_cast<double>(std::numeric_limits<std::int64_t>::max()) ||
1547 val == static_cast<std::int64_t>(val)) {
1548 output = detail::value_string(static_cast<int64_t>(val));
1549 } else {
1551 }
1552 }
1553 return output;
1554}
1555
1556} // namespace detail
1557// [CLI11:type_tools_hpp:end]
1558} // 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
std::vector< std::string > output
Definition StringTools.hpp:354
return false
Definition TypeTools.hpp:812
bool is_separator(const std::string &str)
check if a string is a container segment separator (empty or "%%")
Definition StringTools.hpp:248
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:43
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:63
bool lexical_assign(const std::string &input, AssignTo &output)
Assign a value through lexical cast operations.
Definition TypeTools.hpp:1107
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition StringTools.hpp:259
enabler
Simple empty scoped class.
Definition TypeTools.hpp:31
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition TypeTools.hpp:883
std::int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition TypeTools.hpp:836
bool integral_conversion(const std::string &input, T &output) noexcept
Convert to a signed integral.
Definition TypeTools.hpp:817
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 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
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
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