CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
TypeTools.hpp
1// Copyright (c) 2017-2025, 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// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// [CLI11:public_includes:set]
12#include <algorithm>
13#include <cmath>
14#include <cstdint>
15#include <exception>
16#include <limits>
17#include <memory>
18#include <string>
19#include <type_traits>
20#include <utility>
21#include <vector>
22// [CLI11:public_includes:end]
23
24#include "Encoding.hpp"
25#include "StringTools.hpp"
26
27namespace CLI {
28// [CLI11:type_tools_hpp:verbatim]
29
30// Type tools
31
32// Utilities for type enabling
33namespace detail {
34// Based generally on https://rmf.io/cxx11/almost-static-if
36enum class enabler : std::uint8_t {};
37
39constexpr enabler dummy = {};
40} // namespace detail
41
47template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
48
50template <typename... Ts> struct make_void {
51 using type = void;
52};
53
55template <typename... Ts> using void_t = typename make_void<Ts...>::type;
56
58template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
59
61template <typename T> struct is_bool : std::false_type {};
62
64template <> struct is_bool<bool> : std::true_type {};
65
67template <typename T> struct is_shared_ptr : std::false_type {};
68
70template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
71
73template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
74
76template <typename T> struct is_copyable_ptr {
77 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
78};
79
81template <typename T> struct IsMemberType {
82 using type = T;
83};
84
86template <> struct IsMemberType<const char *> {
87 using type = std::string;
88};
89
90namespace adl_detail {
96template <typename T, typename S = std::string> class is_lexical_castable {
97 template <typename TT, typename SS>
98 static auto test(int) -> decltype(lexical_cast(std::declval<const SS &>(), std::declval<TT &>()), std::true_type());
99
100 template <typename, typename> static auto test(...) -> std::false_type;
101
102 public:
103 static constexpr bool value = decltype(test<T, S>(0))::value;
104};
105} // namespace adl_detail
106
107namespace detail {
108
109// These are utilities for IsMember and other transforming objects
110
113
115template <typename T, typename Enable = void> struct element_type {
116 using type = T;
117};
118
119template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
120 using type = typename std::pointer_traits<T>::element_type;
121};
122
125template <typename T> struct element_value_type {
126 using type = typename element_type<T>::type::value_type;
127};
128
130template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
131 using value_type = typename T::value_type;
132 using first_type = typename std::remove_const<value_type>::type;
133 using second_type = typename std::remove_const<value_type>::type;
134
136 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
137 return std::forward<Q>(pair_value);
138 }
140 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
141 return std::forward<Q>(pair_value);
142 }
143};
144
147template <typename T>
149 T,
150 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
151 : std::true_type {
152 using value_type = typename T::value_type;
153 using first_type = typename std::remove_const<typename value_type::first_type>::type;
154 using second_type = typename std::remove_const<typename value_type::second_type>::type;
155
157 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
158 return std::get<0>(std::forward<Q>(pair_value));
159 }
161 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
162 return std::get<1>(std::forward<Q>(pair_value));
163 }
164};
165
166// Warning is suppressed due to "bug" in gcc<5.0 and gcc 7.0 with c++17 enabled that generates a -Wnarrowing warning
167// in the unevaluated context even if the function that was using this wasn't used. The standard says narrowing in
168// brace initialization shouldn't be allowed but for backwards compatibility gcc allows it in some contexts. It is a
169// little fuzzy what happens in template constructs and I think that was something GCC took a little while to work out.
170// But regardless some versions of gcc generate a warning when they shouldn't from the following code so that should be
171// suppressed
172#ifdef __GNUC__
173#pragma GCC diagnostic push
174#pragma GCC diagnostic ignored "-Wnarrowing"
175#endif
176// check for constructibility from a specific type and copy assignable used in the parse detection
177template <typename T, typename C> class is_direct_constructible {
178 template <typename TT, typename CC>
179 static auto test(int, std::true_type) -> decltype(
180// NVCC warns about narrowing conversions here
181#ifdef __CUDACC__
182#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
183#pragma nv_diag_suppress 2361
184#else
185#pragma diag_suppress 2361
186#endif
187#endif
188 TT{std::declval<CC>()}
189#ifdef __CUDACC__
190#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
191#pragma nv_diag_default 2361
192#else
193#pragma diag_default 2361
194#endif
195#endif
196 ,
197 std::is_move_assignable<TT>());
198
199 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
200
201 template <typename, typename> static auto test(...) -> std::false_type;
202
203 public:
204 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
205};
206#ifdef __GNUC__
207#pragma GCC diagnostic pop
208#endif
209
210// Check for output streamability
211// Based on https://stackoverflow.com/questions/22758291/how-can-i-detect-if-a-type-can-be-streamed-to-an-stdostream
212
213template <typename T, typename S = std::ostringstream> class is_ostreamable {
214 template <typename TT, typename SS>
215 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
216
217 template <typename, typename> static auto test(...) -> std::false_type;
218
219 public:
220 static constexpr bool value = decltype(test<T, S>(0))::value;
221};
222
224template <typename T, typename S = std::istringstream> class is_istreamable {
225 template <typename TT, typename SS>
226 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
227
228 template <typename, typename> static auto test(...) -> std::false_type;
229
230 public:
231 static constexpr bool value = decltype(test<T, S>(0))::value;
232};
233
235template <typename T> class is_complex {
236 template <typename TT>
237 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
238
239 template <typename> static auto test(...) -> std::false_type;
240
241 public:
242 static constexpr bool value = decltype(test<T>(0))::value;
243};
244
246template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
247bool from_stream(const std::string &istring, T &obj) {
248 std::istringstream is;
249 is.str(istring);
250 is >> obj;
251 return !is.fail() && !is.rdbuf()->in_avail();
252}
253
254template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
255bool from_stream(const std::string & /*istring*/, T & /*obj*/) {
256 return false;
257}
258
259// check to see if an object is a mutable container (fail by default)
260template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
261
265template <typename T>
267 T,
268 conditional_t<false,
269 void_t<typename T::value_type,
270 decltype(std::declval<T>().end()),
271 decltype(std::declval<T>().clear()),
272 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
273 std::declval<const typename T::value_type &>()))>,
274 void>> : public conditional_t<std::is_constructible<T, std::string>::value ||
275 std::is_constructible<T, std::wstring>::value,
276 std::false_type,
277 std::true_type> {};
278
279// check to see if an object is a mutable container (fail by default)
280template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
281
284template <typename T>
286 T,
287 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
288 : public std::true_type {};
289
290// check to see if an object is a wrapper (fail by default)
291template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
292
293// check if an object is a wrapper (it has a value_type defined)
294template <typename T>
295struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
296
297// Check for tuple like types, as in classes with a tuple_size type trait
298// Even though in C++26 std::complex gains a std::tuple interface, for our purposes we treat is as NOT a tuple
299template <typename S> class is_tuple_like {
300 template <typename SS, enable_if_t<!is_complex<SS>::value, detail::enabler> = detail::dummy>
301 // static auto test(int)
302 // -> decltype(std::conditional<(std::tuple_size<SS>::value > 0), std::true_type, std::false_type>::type());
303 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
304 template <typename> static auto test(...) -> std::false_type;
305
306 public:
307 static constexpr bool value = decltype(test<S>(0))::value;
308};
309
311template <typename T, typename Enable = void> struct type_count_base {
312 static const int value{0};
313};
314
316template <typename T>
318 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
319 !std::is_void<T>::value>::type> {
320 static constexpr int value{1};
321};
322
324template <typename T>
325struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
326 static constexpr int value{// cppcheck-suppress unusedStructMember
327 std::tuple_size<typename std::decay<T>::type>::value};
328};
329
331template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
332 static constexpr int value{type_count_base<typename T::value_type>::value};
333};
334
336template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
337auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
338 return std::forward<T>(value);
339}
340
342template <typename T,
343 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
344 detail::enabler> = detail::dummy>
345std::string to_string(T &&value) {
346 return std::string(value); // NOLINT(google-readability-casting)
347}
348
350template <typename T,
351 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
352 is_ostreamable<T>::value,
353 detail::enabler> = detail::dummy>
354std::string to_string(T &&value) {
355 std::stringstream stream;
356 stream << value;
357 return stream.str();
358}
359
360// additional forward declarations
361
363template <typename T,
364 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
365 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
366 detail::enabler> = detail::dummy>
367inline std::string to_string(T &&value);
368
370template <typename T,
371 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
372 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
373 detail::enabler> = detail::dummy>
374inline std::string to_string(T &&value);
375
377template <
378 typename T,
379 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
380 !is_ostreamable<T>::value && !is_readable_container<typename std::remove_const<T>::type>::value &&
381 !is_tuple_like<T>::value,
382 detail::enabler> = detail::dummy>
383inline std::string to_string(T &&) {
384 return {};
385}
386
388template <typename T,
389 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
390 !is_ostreamable<T>::value && is_readable_container<T>::value && !is_tuple_like<T>::value,
391 detail::enabler> = detail::dummy>
392inline std::string to_string(T &&variable) {
393 auto cval = variable.begin();
394 auto end = variable.end();
395 if(cval == end) {
396 return {"{}"};
397 }
398 std::vector<std::string> defaults;
399 while(cval != end) {
400 defaults.emplace_back(CLI::detail::to_string(*cval));
401 ++cval;
402 }
403 return {"[" + detail::join(defaults) + "]"};
404}
405
407
409template <typename T, std::size_t I>
410inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && /*value*/);
411
413template <typename T, std::size_t I>
414inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value);
415
417template <typename T,
418 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
419 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
420 detail::enabler>>
421inline std::string to_string(T &&value) {
422 return to_string(std::get<0>(value));
423}
424
426template <typename T,
427 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
428 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
429 detail::enabler>>
430inline std::string to_string(T &&value) {
431 auto tname = std::string(1, '[') + tuple_value_string<T, 0>(value);
432 tname.push_back(']');
433 return tname;
434}
435
437template <typename T, std::size_t I>
438inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && /*value*/) {
439 return std::string{};
440}
441
443template <typename T, std::size_t I>
444inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value) {
445 auto str = std::string{to_string(std::get<I>(value))} + ',' + tuple_value_string<T, I + 1>(value);
446 if(str.back() == ',')
447 str.pop_back();
448 return str;
449}
450
452template <typename T1,
453 typename T2,
454 typename T,
455 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
456auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
457 return to_string(std::forward<T>(value));
458}
459
461template <typename T1,
462 typename T2,
463 typename T,
464 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
465std::string checked_to_string(T &&) {
466 return std::string{};
467}
469template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
470std::string value_string(const T &value) {
471 return std::to_string(value);
472}
474template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
475std::string value_string(const T &value) {
476 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
477}
479template <typename T,
480 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
481auto value_string(const T &value) -> decltype(to_string(value)) {
482 return to_string(value);
483}
484
486template <typename T, typename def, typename Enable = void> struct wrapped_type {
487 using type = def;
488};
489
491template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
492 using type = typename T::value_type;
493};
494
496
498template <typename T> struct subtype_count;
499
501template <typename T> struct subtype_count_min;
502
504template <typename T, typename Enable = void> struct type_count {
505 static const int value{0};
506};
507
509template <typename T>
510struct type_count<T,
511 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
512 !std::is_void<T>::value>::type> {
513 static constexpr int value{1};
514};
515
517template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
518 static constexpr int value{2};
519};
520
522template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
523 static constexpr int value{subtype_count<typename T::value_type>::value};
524};
525
527template <typename T>
528struct type_count<T,
529 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
530 !is_mutable_container<T>::value>::type> {
531 static constexpr int value{type_count<typename T::value_type>::value};
532};
533
535template <typename T, std::size_t I>
536constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
537 return 0;
538}
539
541template <typename T, std::size_t I>
542 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
543 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
544}
545
547template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
548 static constexpr int value{tuple_type_size<T, 0>()};
549};
550
552template <typename T> struct subtype_count {
553 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
554};
555
557template <typename T, typename Enable = void> struct type_count_min {
558 static const int value{0};
559};
560
562template <typename T>
563struct type_count_min<
564 T,
565 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
566 !is_complex<T>::value && !std::is_void<T>::value>::type> {
567 static constexpr int value{type_count<T>::value};
568};
569
571template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
572 static constexpr int value{1};
573};
574
576template <typename T>
577struct type_count_min<
578 T,
579 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
580 static constexpr int value{subtype_count_min<typename T::value_type>::value};
581};
582
584template <typename T, std::size_t I>
585constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
586 return 0;
587}
588
590template <typename T, std::size_t I>
591 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
592 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
593}
594
596template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
597 static constexpr int value{tuple_type_size_min<T, 0>()};
598};
599
601template <typename T> struct subtype_count_min {
602 static constexpr int value{is_mutable_container<T>::value
603 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
604 : type_count_min<T>::value};
605};
606
608template <typename T, typename Enable = void> struct expected_count {
609 static const int value{0};
610};
611
613template <typename T>
614struct expected_count<T,
615 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
616 !std::is_void<T>::value>::type> {
617 static constexpr int value{1};
618};
620template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
621 static constexpr int value{expected_max_vector_size};
622};
623
625template <typename T>
626struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
627 static constexpr int value{expected_count<typename T::value_type>::value};
628};
629
630// Enumeration of the different supported categorizations of objects
631enum class object_category : std::uint8_t {
632 char_value = 1,
633 integral_value = 2,
634 unsigned_integral = 4,
635 enumeration = 6,
636 boolean_value = 8,
637 floating_point = 10,
638 number_constructible = 12,
639 double_constructible = 14,
640 integer_constructible = 16,
641 // string like types
642 string_assignable = 23,
643 string_constructible = 24,
644 wstring_assignable = 25,
645 wstring_constructible = 26,
646 other = 45,
647 // special wrapper or container types
648 wrapper_value = 50,
649 complex_number = 60,
650 tuple_value = 70,
651 container_value = 80,
652
653};
654
656
658template <typename T, typename Enable = void> struct classify_object {
659 static constexpr object_category value{object_category::other};
660};
661
663template <typename T>
664struct classify_object<
665 T,
666 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
667 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
668 static constexpr object_category value{object_category::integral_value};
669};
670
672template <typename T>
673struct classify_object<T,
674 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
675 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
676 static constexpr object_category value{object_category::unsigned_integral};
677};
678
680template <typename T>
681struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
682 static constexpr object_category value{object_category::char_value};
683};
684
686template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
687 static constexpr object_category value{object_category::boolean_value};
688};
689
691template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
692 static constexpr object_category value{object_category::floating_point};
693};
694#if defined _MSC_VER
695// in MSVC wstring should take precedence if available this isn't as useful on other compilers due to the broader use of
696// utf-8 encoding
697#define WIDE_STRING_CHECK \
698 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
699#define STRING_CHECK true
700#else
701#define WIDE_STRING_CHECK true
702#define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
703#endif
704
706template <typename T>
707struct classify_object<
708 T,
709 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
710 std::is_assignable<T &, std::string>::value>::type> {
711 static constexpr object_category value{object_category::string_assignable};
712};
713
715template <typename T>
716struct classify_object<
717 T,
718 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
719 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
720 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
721 static constexpr object_category value{object_category::string_constructible};
722};
723
725template <typename T>
726struct classify_object<T,
727 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
728 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
729 static constexpr object_category value{object_category::wstring_assignable};
730};
731
732template <typename T>
733struct classify_object<
734 T,
735 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
736 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
737 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
738 static constexpr object_category value{object_category::wstring_constructible};
739};
740
742template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
743 static constexpr object_category value{object_category::enumeration};
744};
745
746template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
747 static constexpr object_category value{object_category::complex_number};
748};
749
752template <typename T> struct uncommon_type {
753 using type = typename std::conditional<
754 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
755 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
756 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
757 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
758 std::true_type,
759 std::false_type>::type;
760 static constexpr bool value = type::value;
761};
762
764template <typename T>
765struct classify_object<T,
766 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
767 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
768 static constexpr object_category value{object_category::wrapper_value};
769};
770
772template <typename T>
773struct classify_object<T,
774 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
775 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
776 is_direct_constructible<T, int>::value>::type> {
777 static constexpr object_category value{object_category::number_constructible};
778};
779
781template <typename T>
782struct classify_object<T,
783 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
784 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
785 is_direct_constructible<T, int>::value>::type> {
786 static constexpr object_category value{object_category::integer_constructible};
787};
788
790template <typename T>
791struct classify_object<T,
792 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
793 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
794 !is_direct_constructible<T, int>::value>::type> {
795 static constexpr object_category value{object_category::double_constructible};
796};
797
799template <typename T>
800struct classify_object<
801 T,
802 typename std::enable_if<is_tuple_like<T>::value &&
803 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
804 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
805 !is_direct_constructible<T, int>::value) ||
806 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
807 static constexpr object_category value{object_category::tuple_value};
808 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
809 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
810 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
811 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
812 // those cases that are caught by other object classifications
813};
814
816template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
817 static constexpr object_category value{object_category::container_value};
818};
819
820// Type name print
821
825
826template <typename T,
827 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
828constexpr const char *type_name() {
829 return "CHAR";
830}
831
832template <typename T,
833 enable_if_t<classify_object<T>::value == object_category::integral_value ||
834 classify_object<T>::value == object_category::integer_constructible,
835 detail::enabler> = detail::dummy>
836constexpr const char *type_name() {
837 return "INT";
838}
839
840template <typename T,
841 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
842constexpr const char *type_name() {
843 return "UINT";
844}
845
846template <typename T,
847 enable_if_t<classify_object<T>::value == object_category::floating_point ||
848 classify_object<T>::value == object_category::number_constructible ||
849 classify_object<T>::value == object_category::double_constructible,
850 detail::enabler> = detail::dummy>
851constexpr const char *type_name() {
852 return "FLOAT";
853}
854
856template <typename T,
857 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
858constexpr const char *type_name() {
859 return "ENUM";
860}
861
863template <typename T,
864 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
865constexpr const char *type_name() {
866 return "BOOLEAN";
867}
868
870template <typename T,
871 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
872constexpr const char *type_name() {
873 return "COMPLEX";
874}
875
877template <typename T,
878 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
879 classify_object<T>::value <= object_category::other,
880 detail::enabler> = detail::dummy>
881constexpr const char *type_name() {
882 return "TEXT";
883}
885template <typename T,
886 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
887 detail::enabler> = detail::dummy>
888std::string type_name(); // forward declaration
889
891template <typename T,
892 enable_if_t<classify_object<T>::value == object_category::container_value ||
893 classify_object<T>::value == object_category::wrapper_value,
894 detail::enabler> = detail::dummy>
895std::string type_name(); // forward declaration
896
898template <typename T,
899 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
900 detail::enabler> = detail::dummy>
901inline std::string type_name() {
902 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
903}
904
906template <typename T, std::size_t I>
907inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
908 return std::string{};
909}
910
912template <typename T, std::size_t I>
913inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
914 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
915 tuple_name<T, I + 1>();
916 if(str.back() == ',')
917 str.pop_back();
918 return str;
919}
920
922template <typename T,
923 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
924 detail::enabler>>
925inline std::string type_name() {
926 auto tname = std::string(1, '[') + tuple_name<T, 0>();
927 tname.push_back(']');
928 return tname;
929}
930
932template <typename T,
933 enable_if_t<classify_object<T>::value == object_category::container_value ||
934 classify_object<T>::value == object_category::wrapper_value,
935 detail::enabler>>
936inline std::string type_name() {
937 return type_name<typename T::value_type>();
938}
939
940// Lexical cast
941
943template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
944bool integral_conversion(const std::string &input, T &output) noexcept {
945 if(input.empty() || input.front() == '-') {
946 return false;
947 }
948 char *val{nullptr};
949 errno = 0;
950 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
951 if(errno == ERANGE) {
952 return false;
953 }
954 output = static_cast<T>(output_ll);
955 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
956 return true;
957 }
958 val = nullptr;
959 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
960 if(val == (input.c_str() + input.size())) {
961 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
962 return (static_cast<std::int64_t>(output) == output_sll);
963 }
964 // remove separators if present
965 auto group_separators = get_group_separators();
966 if(input.find_first_of(group_separators) != std::string::npos) {
967 std::string nstring = input;
968 for(auto &separator : group_separators) {
969 if(input.find_first_of(separator) != std::string::npos) {
970 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
971 }
972 }
973 return integral_conversion(nstring, output);
974 }
975
976 if(std::isspace(static_cast<unsigned char>(input.back()))) {
977 return integral_conversion(trim_copy(input), output);
978 }
979 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
980 val = nullptr;
981 errno = 0;
982 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
983 if(errno == ERANGE) {
984 return false;
985 }
986 output = static_cast<T>(output_ll);
987 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
988 }
989 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
990 // LCOV_EXCL_START
991 // In some new compilers including the coverage testing one binary strings are handled properly in strtoull
992 // automatically so this coverage is missing but is well tested in other compilers
993 val = nullptr;
994 errno = 0;
995 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
996 if(errno == ERANGE) {
997 return false;
998 }
999 output = static_cast<T>(output_ll);
1000 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1001 // LCOV_EXCL_STOP
1002 }
1003 return false;
1004}
1005
1007template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1008bool integral_conversion(const std::string &input, T &output) noexcept {
1009 if(input.empty()) {
1010 return false;
1011 }
1012 char *val = nullptr;
1013 errno = 0;
1014 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1015 if(errno == ERANGE) {
1016 return false;
1017 }
1018 output = static_cast<T>(output_ll);
1019 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1020 return true;
1021 }
1022 if(input == "true") {
1023 // this is to deal with a few oddities with flags and wrapper int types
1024 output = static_cast<T>(1);
1025 return true;
1026 }
1027 // remove separators if present
1028 auto group_separators = get_group_separators();
1029 if(input.find_first_of(group_separators) != std::string::npos) {
1030 for(auto &separator : group_separators) {
1031 if(input.find_first_of(separator) != std::string::npos) {
1032 std::string nstring = input;
1033 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1034 return integral_conversion(nstring, output);
1035 }
1036 }
1037 }
1038 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1039 return integral_conversion(trim_copy(input), output);
1040 }
1041 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1042 val = nullptr;
1043 errno = 0;
1044 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1045 if(errno == ERANGE) {
1046 return false;
1047 }
1048 output = static_cast<T>(output_ll);
1049 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1050 }
1051 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1052 // LCOV_EXCL_START
1053 // In some new compilers including the coverage testing one binary strings are handled properly in strtoll
1054 // automatically so this coverage is missing but is well tested in other compilers
1055 val = nullptr;
1056 errno = 0;
1057 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1058 if(errno == ERANGE) {
1059 return false;
1060 }
1061 output = static_cast<T>(output_ll);
1062 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1063 // LCOV_EXCL_STOP
1064 }
1065 return false;
1066}
1067
1069inline std::int64_t to_flag_value(std::string val) noexcept {
1070 static const std::string trueString("true");
1071 static const std::string falseString("false");
1072 if(val == trueString) {
1073 return 1;
1074 }
1075 if(val == falseString) {
1076 return -1;
1077 }
1078 val = detail::to_lower(val);
1079 std::int64_t ret = 0;
1080 if(val.size() == 1) {
1081 if(val[0] >= '1' && val[0] <= '9') {
1082 return (static_cast<std::int64_t>(val[0]) - '0');
1083 }
1084 switch(val[0]) {
1085 case '0':
1086 case 'f':
1087 case 'n':
1088 case '-':
1089 ret = -1;
1090 break;
1091 case 't':
1092 case 'y':
1093 case '+':
1094 ret = 1;
1095 break;
1096 default:
1097 errno = EINVAL;
1098 return -1;
1099 }
1100 return ret;
1101 }
1102 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1103 ret = 1;
1104 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1105 ret = -1;
1106 } else {
1107 char *loc_ptr{nullptr};
1108 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1109 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1110 errno = EINVAL;
1111 }
1112 }
1113 return ret;
1114}
1115
1117template <typename T,
1118 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1119 classify_object<T>::value == object_category::unsigned_integral,
1120 detail::enabler> = detail::dummy>
1121bool lexical_cast(const std::string &input, T &output) {
1122 return integral_conversion(input, output);
1123}
1124
1126template <typename T,
1127 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1128bool lexical_cast(const std::string &input, T &output) {
1129 if(input.size() == 1) {
1130 output = static_cast<T>(input[0]);
1131 return true;
1132 }
1133 return integral_conversion(input, output);
1134}
1135
1137template <typename T,
1138 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1139bool lexical_cast(const std::string &input, T &output) {
1140 errno = 0;
1141 auto out = to_flag_value(input);
1142 if(errno == 0) {
1143 output = (out > 0);
1144 } else if(errno == ERANGE) {
1145 output = (input[0] != '-');
1146 } else {
1147 return false;
1148 }
1149 return true;
1150}
1151
1153template <typename T,
1154 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1155bool lexical_cast(const std::string &input, T &output) {
1156 if(input.empty()) {
1157 return false;
1158 }
1159 char *val = nullptr;
1160 auto output_ld = std::strtold(input.c_str(), &val);
1161 output = static_cast<T>(output_ld);
1162 if(val == (input.c_str() + input.size())) {
1163 return true;
1164 }
1165 while(std::isspace(static_cast<unsigned char>(*val))) {
1166 ++val;
1167 if(val == (input.c_str() + input.size())) {
1168 return true;
1169 }
1170 }
1171
1172 // remove separators if present
1173 auto group_separators = get_group_separators();
1174 if(input.find_first_of(group_separators) != std::string::npos) {
1175 for(auto &separator : group_separators) {
1176 if(input.find_first_of(separator) != std::string::npos) {
1177 std::string nstring = input;
1178 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1179 return lexical_cast(nstring, output);
1180 }
1181 }
1182 }
1183 return false;
1184}
1185
1187template <typename T,
1188 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1189bool lexical_cast(const std::string &input, T &output) {
1190 using XC = typename wrapped_type<T, double>::type;
1191 XC x{0.0}, y{0.0};
1192 auto str1 = input;
1193 bool worked = false;
1194 auto nloc = str1.find_last_of("+-");
1195 if(nloc != std::string::npos && nloc > 0) {
1196 worked = lexical_cast(str1.substr(0, nloc), x);
1197 str1 = str1.substr(nloc);
1198 if(str1.back() == 'i' || str1.back() == 'j')
1199 str1.pop_back();
1200 worked = worked && lexical_cast(str1, y);
1201 } else {
1202 if(str1.back() == 'i' || str1.back() == 'j') {
1203 str1.pop_back();
1204 worked = lexical_cast(str1, y);
1205 x = XC{0};
1206 } else {
1207 worked = lexical_cast(str1, x);
1208 y = XC{0};
1209 }
1210 }
1211 if(worked) {
1212 output = T{x, y};
1213 return worked;
1214 }
1215 return from_stream(input, output);
1216}
1217
1219template <typename T,
1220 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1221bool lexical_cast(const std::string &input, T &output) {
1222 output = input;
1223 return true;
1224}
1225
1227template <
1228 typename T,
1229 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1230bool lexical_cast(const std::string &input, T &output) {
1231 output = T(input);
1232 return true;
1233}
1234
1236template <
1237 typename T,
1238 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1239bool lexical_cast(const std::string &input, T &output) {
1240 output = widen(input);
1241 return true;
1242}
1243
1244template <
1245 typename T,
1246 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1247bool lexical_cast(const std::string &input, T &output) {
1248 output = T{widen(input)};
1249 return true;
1250}
1251
1253template <typename T,
1254 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1255bool lexical_cast(const std::string &input, T &output) {
1256 typename std::underlying_type<T>::type val;
1257 if(!integral_conversion(input, val)) {
1258 return false;
1259 }
1260 output = static_cast<T>(val);
1261 return true;
1262}
1263
1265template <typename T,
1266 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1267 std::is_assignable<T &, typename T::value_type>::value,
1268 detail::enabler> = detail::dummy>
1269bool lexical_cast(const std::string &input, T &output) {
1270 typename T::value_type val;
1271 if(lexical_cast(input, val)) {
1272 output = val;
1273 return true;
1274 }
1275 return from_stream(input, output);
1276}
1277
1278template <typename T,
1279 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1280 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1281 detail::enabler> = detail::dummy>
1282bool lexical_cast(const std::string &input, T &output) {
1283 typename T::value_type val;
1284 if(lexical_cast(input, val)) {
1285 output = T{val};
1286 return true;
1287 }
1288 return from_stream(input, output);
1289}
1290
1292template <
1293 typename T,
1294 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1295bool lexical_cast(const std::string &input, T &output) {
1296 int val = 0;
1297 if(integral_conversion(input, val)) {
1298 output = T(val);
1299 return true;
1300 }
1301
1302 double dval = 0.0;
1303 if(lexical_cast(input, dval)) {
1304 output = T{dval};
1305 return true;
1306 }
1307
1308 return from_stream(input, output);
1309}
1310
1312template <
1313 typename T,
1314 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1315bool lexical_cast(const std::string &input, T &output) {
1316 int val = 0;
1317 if(integral_conversion(input, val)) {
1318 output = T(val);
1319 return true;
1320 }
1321 return from_stream(input, output);
1322}
1323
1325template <
1326 typename T,
1327 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1328bool lexical_cast(const std::string &input, T &output) {
1329 double val = 0.0;
1330 if(lexical_cast(input, val)) {
1331 output = T{val};
1332 return true;
1333 }
1334 return from_stream(input, output);
1335}
1336
1338template <typename T,
1339 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1340 detail::enabler> = detail::dummy>
1341bool lexical_cast(const std::string &input, T &output) {
1342 int val = 0;
1343 if(integral_conversion(input, val)) {
1344#ifdef _MSC_VER
1345#pragma warning(push)
1346#pragma warning(disable : 4800)
1347#endif
1348 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1349 // so will most likely still work
1350 output = val;
1351#ifdef _MSC_VER
1352#pragma warning(pop)
1353#endif
1354 return true;
1355 }
1356 // LCOV_EXCL_START
1357 // This version of cast is only used for odd cases in an older compilers the fail over
1358 // from_stream is tested elsewhere an not relevant for coverage here
1359 return from_stream(input, output);
1360 // LCOV_EXCL_STOP
1361}
1362
1364template <typename T,
1365 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1366 is_istreamable<T>::value,
1367 detail::enabler> = detail::dummy>
1368bool lexical_cast(const std::string &input, T &output) {
1369 return from_stream(input, output);
1370}
1371
1374template <typename T,
1375 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1376 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1377 detail::enabler> = detail::dummy>
1378bool lexical_cast(const std::string & /*input*/, T & /*output*/) {
1379 static_assert(!std::is_same<T, T>::value, // Can't just write false here.
1380 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1381 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1382 return false;
1383}
1384
1387template <typename AssignTo,
1388 typename ConvertTo,
1389 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1390 (classify_object<AssignTo>::value == object_category::string_assignable ||
1391 classify_object<AssignTo>::value == object_category::string_constructible ||
1392 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1393 classify_object<AssignTo>::value == object_category::wstring_constructible),
1394 detail::enabler> = detail::dummy>
1395bool lexical_assign(const std::string &input, AssignTo &output) {
1396 return lexical_cast(input, output);
1397}
1398
1400template <typename AssignTo,
1401 typename ConvertTo,
1402 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1403 classify_object<AssignTo>::value != object_category::string_assignable &&
1404 classify_object<AssignTo>::value != object_category::string_constructible &&
1405 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1406 classify_object<AssignTo>::value != object_category::wstring_constructible,
1407 detail::enabler> = detail::dummy>
1408bool lexical_assign(const std::string &input, AssignTo &output) {
1409 if(input.empty()) {
1410 output = AssignTo{};
1411 return true;
1412 }
1413
1414 return lexical_cast(input, output);
1415} // LCOV_EXCL_LINE
1416
1418template <typename AssignTo,
1419 typename ConvertTo,
1420 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1421 classify_object<AssignTo>::value == object_category::wrapper_value,
1422 detail::enabler> = detail::dummy>
1423bool lexical_assign(const std::string &input, AssignTo &output) {
1424 if(input.empty()) {
1425 typename AssignTo::value_type emptyVal{};
1426 output = emptyVal;
1427 return true;
1428 }
1429 return lexical_cast(input, output);
1430}
1431
1434template <typename AssignTo,
1435 typename ConvertTo,
1436 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1437 classify_object<AssignTo>::value != object_category::wrapper_value &&
1438 std::is_assignable<AssignTo &, int>::value,
1439 detail::enabler> = detail::dummy>
1440bool lexical_assign(const std::string &input, AssignTo &output) {
1441 if(input.empty()) {
1442 output = 0;
1443 return true;
1444 }
1445 int val{0};
1446 if(lexical_cast(input, val)) {
1447#if defined(__clang__)
1448/* on some older clang compilers */
1449#pragma clang diagnostic push
1450#pragma clang diagnostic ignored "-Wsign-conversion"
1451#endif
1452 output = val;
1453#if defined(__clang__)
1454#pragma clang diagnostic pop
1455#endif
1456 return true;
1457 }
1458 return false;
1459}
1460
1462template <typename AssignTo,
1463 typename ConvertTo,
1464 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1465 detail::enabler> = detail::dummy>
1466bool lexical_assign(const std::string &input, AssignTo &output) {
1467 ConvertTo val{};
1468 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1469 if(parse_result) {
1470 output = val;
1471 }
1472 return parse_result;
1473}
1474
1476template <
1477 typename AssignTo,
1478 typename ConvertTo,
1479 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1480 std::is_move_assignable<AssignTo>::value,
1481 detail::enabler> = detail::dummy>
1482bool lexical_assign(const std::string &input, AssignTo &output) {
1483 ConvertTo val{};
1484 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1485 if(parse_result) {
1486 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1487 }
1488 return parse_result;
1489}
1490
1492template <typename AssignTo,
1493 typename ConvertTo,
1494 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1495 classify_object<AssignTo>::value <= object_category::wrapper_value,
1496 detail::enabler> = detail::dummy>
1497bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1498 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1499}
1500
1503template <typename AssignTo,
1504 typename ConvertTo,
1505 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1506 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1507 detail::enabler> = detail::dummy>
1508bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1509 // the remove const is to handle pair types coming from a container
1510 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1511 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1512 FirstType v1;
1513 SecondType v2{};
1514 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1515 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1516 if(retval) {
1517 output = AssignTo{v1, v2};
1518 }
1519 return retval;
1520}
1521
1523template <class AssignTo,
1524 class ConvertTo,
1525 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1526 type_count<ConvertTo>::value == 1,
1527 detail::enabler> = detail::dummy>
1528bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1529 output.erase(output.begin(), output.end());
1530 if(strings.empty()) {
1531 return true;
1532 }
1533 if(strings.size() == 1 && strings[0] == "{}") {
1534 return true;
1535 }
1536 bool skip_remaining = false;
1537 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1538 skip_remaining = true;
1539 }
1540 for(const auto &elem : strings) {
1541 typename AssignTo::value_type out;
1542 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1543 if(!retval) {
1544 return false;
1545 }
1546 output.insert(output.end(), std::move(out));
1547 if(skip_remaining) {
1548 break;
1549 }
1550 }
1551 return (!output.empty());
1552}
1553
1555template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1556bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1557
1558 if(strings.size() >= 2 && !strings[1].empty()) {
1559 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1560 XC2 x{0.0}, y{0.0};
1561 auto str1 = strings[1];
1562 if(str1.back() == 'i' || str1.back() == 'j') {
1563 str1.pop_back();
1564 }
1565 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1566 if(worked) {
1567 output = ConvertTo{x, y};
1568 }
1569 return worked;
1570 }
1571 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1572}
1573
1575template <class AssignTo,
1576 class ConvertTo,
1577 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1578 (type_count<ConvertTo>::value == 1),
1579 detail::enabler> = detail::dummy>
1580bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1581 bool retval = true;
1582 output.clear();
1583 output.reserve(strings.size());
1584 for(const auto &elem : strings) {
1585
1586 output.emplace_back();
1587 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1588 }
1589 return (!output.empty()) && retval;
1590}
1591
1592// forward declaration
1593
1595template <class AssignTo,
1596 class ConvertTo,
1597 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1598 type_count_base<ConvertTo>::value == 2,
1599 detail::enabler> = detail::dummy>
1600bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1601
1603template <class AssignTo,
1604 class ConvertTo,
1605 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1606 type_count_base<ConvertTo>::value != 2 &&
1607 ((type_count<ConvertTo>::value > 2) ||
1608 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1609 detail::enabler> = detail::dummy>
1610bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1611
1613template <class AssignTo,
1614 class ConvertTo,
1615 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1616 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1617 type_count<ConvertTo>::value > 2),
1618 detail::enabler> = detail::dummy>
1619bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1620
1623template <typename AssignTo,
1624 typename ConvertTo,
1625 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1626 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1627 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1628 detail::enabler> = detail::dummy>
1629bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1630
1631 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1632 ConvertTo val;
1633 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1634 output = AssignTo{val};
1635 return retval;
1636 }
1637 output = AssignTo{};
1638 return true;
1639}
1640
1642template <class AssignTo, class ConvertTo, std::size_t I>
1643inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1644tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1645 return true;
1646}
1647
1649template <class AssignTo, class ConvertTo>
1650inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1651tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1652 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1653 strings.erase(strings.begin());
1654 return retval;
1655}
1656
1658template <class AssignTo, class ConvertTo>
1659inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1660 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1661 bool>::type
1662tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1663 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1664 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1665 return retval;
1666}
1667
1669template <class AssignTo, class ConvertTo>
1670inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1671 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1672 bool>::type
1673tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1674
1675 std::size_t index{subtype_count_min<ConvertTo>::value};
1676 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1677 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1678
1679 while(index < mx) {
1680 if(is_separator(strings[index])) {
1681 break;
1682 }
1683 ++index;
1684 }
1685 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1686 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1687 if(strings.size() > index) {
1688 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1689 } else {
1690 strings.clear();
1691 }
1692 return retval;
1693}
1694
1696template <class AssignTo, class ConvertTo, std::size_t I>
1697inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1698tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1699 bool retval = true;
1700 using ConvertToElement = typename std::
1701 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1702 if(!strings.empty()) {
1703 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1704 strings, std::get<I>(output));
1705 }
1706 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1707 return retval;
1708}
1709
1711template <class AssignTo,
1712 class ConvertTo,
1713 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1714 type_count_base<ConvertTo>::value == 2,
1715 detail::enabler>>
1716bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1717 output.clear();
1718 while(!strings.empty()) {
1719
1720 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1721 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1722 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1723 if(!strings.empty()) {
1724 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1725 }
1726 if(retval) {
1727 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1728 } else {
1729 return false;
1730 }
1731 }
1732 return (!output.empty());
1733}
1734
1736template <class AssignTo,
1737 class ConvertTo,
1738 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1739 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1740 type_count<ConvertTo>::value > 2),
1741 detail::enabler>>
1742bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1743 static_assert(
1744 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1745 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1746 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1747}
1748
1750template <class AssignTo,
1751 class ConvertTo,
1752 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1753 type_count_base<ConvertTo>::value != 2 &&
1754 ((type_count<ConvertTo>::value > 2) ||
1755 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1756 detail::enabler>>
1757bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1758 bool retval = true;
1759 output.clear();
1760 std::vector<std::string> temp;
1761 std::size_t ii{0};
1762 std::size_t icount{0};
1763 std::size_t xcm{type_count<ConvertTo>::value};
1764 auto ii_max = strings.size();
1765 while(ii < ii_max) {
1766 temp.push_back(strings[ii]);
1767 ++ii;
1768 ++icount;
1769 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1770 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1771 temp.pop_back();
1772 }
1773 typename AssignTo::value_type temp_out;
1774 retval = retval &&
1775 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1776 temp.clear();
1777 if(!retval) {
1778 return false;
1779 }
1780 output.insert(output.end(), std::move(temp_out));
1781 icount = 0;
1782 }
1783 }
1784 return retval;
1785}
1786
1788template <typename AssignTo,
1789 class ConvertTo,
1790 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1791 std::is_assignable<ConvertTo &, ConvertTo>::value,
1792 detail::enabler> = detail::dummy>
1793bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1794 if(strings.empty() || strings.front().empty()) {
1795 output = ConvertTo{};
1796 return true;
1797 }
1798 typename ConvertTo::value_type val;
1799 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1800 output = ConvertTo{val};
1801 return true;
1802 }
1803 return false;
1804}
1805
1807template <typename AssignTo,
1808 class ConvertTo,
1809 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1810 !std::is_assignable<AssignTo &, ConvertTo>::value,
1811 detail::enabler> = detail::dummy>
1812bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1813 using ConvertType = typename ConvertTo::value_type;
1814 if(strings.empty() || strings.front().empty()) {
1815 output = ConvertType{};
1816 return true;
1817 }
1818 ConvertType val;
1819 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1820 output = val;
1821 return true;
1822 }
1823 return false;
1824}
1825
1827inline std::string sum_string_vector(const std::vector<std::string> &values) {
1828 double val{0.0};
1829 bool fail{false};
1830 std::string output;
1831 for(const auto &arg : values) {
1832 double tv{0.0};
1833 auto comp = lexical_cast(arg, tv);
1834 if(!comp) {
1835 errno = 0;
1836 auto fv = detail::to_flag_value(arg);
1837 fail = (errno != 0);
1838 if(fail) {
1839 break;
1840 }
1841 tv = static_cast<double>(fv);
1842 }
1843 val += tv;
1844 }
1845 if(fail) {
1846 for(const auto &arg : values) {
1847 output.append(arg);
1848 }
1849 } else {
1850 std::ostringstream out;
1851 out.precision(16);
1852 out << val;
1853 output = out.str();
1854 }
1855 return output;
1856}
1857
1858} // namespace detail
1859// [CLI11:type_tools_hpp:end]
1860} // namespace CLI
Definition TypeTools.hpp:96
Check for complex.
Definition TypeTools.hpp:235
Definition TypeTools.hpp:177
Check for input streamability.
Definition TypeTools.hpp:224
Definition TypeTools.hpp:213
Definition TypeTools.hpp:299
This can be specialized to override the type deduction for IsMember.
Definition TypeTools.hpp:81
not a pointer
Definition TypeTools.hpp:115
Definition TypeTools.hpp:125
Definition TypeTools.hpp:260
Definition TypeTools.hpp:280
Definition TypeTools.hpp:291
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:157
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:161
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition TypeTools.hpp:130
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition TypeTools.hpp:140
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition TypeTools.hpp:136
forward declare the subtype_count_min structure
Definition TypeTools.hpp:501
Set of overloads to get the type size of an object.
Definition TypeTools.hpp:498
This will only trigger for actual void type.
Definition TypeTools.hpp:311
This will only trigger for actual void type.
Definition TypeTools.hpp:504
template to get the underlying value type if it exists or use a default
Definition TypeTools.hpp:486
Check to see if something is bool (fail check by default)
Definition TypeTools.hpp:61
Check to see if something is copyable pointer.
Definition TypeTools.hpp:76
Check to see if something is a shared pointer.
Definition TypeTools.hpp:67
A copy of std::void_t from C++17 (helper for C++11 and C++14)
Definition TypeTools.hpp:50