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 {};
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,
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 : int {
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
965 if(input.find_first_of("_'") != std::string::npos) {
966 std::string nstring = input;
967 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
968 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
969 return integral_conversion(nstring, output);
970 }
971 if(std::isspace(static_cast<unsigned char>(input.back()))) {
972 return integral_conversion(trim_copy(input), output);
973 }
974 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
975 val = nullptr;
976 errno = 0;
977 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
978 if(errno == ERANGE) {
979 return false;
980 }
981 output = static_cast<T>(output_ll);
982 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
983 }
984 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
985 // LCOV_EXCL_START
986 // In some new compilers including the coverage testing one binary strings are handled properly in strtoull
987 // automatically so this coverage is missing but is well tested in other compilers
988 val = nullptr;
989 errno = 0;
990 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
991 if(errno == ERANGE) {
992 return false;
993 }
994 output = static_cast<T>(output_ll);
995 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
996 // LCOV_EXCL_STOP
997 }
998 return false;
999}
1000
1002template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1003bool integral_conversion(const std::string &input, T &output) noexcept {
1004 if(input.empty()) {
1005 return false;
1006 }
1007 char *val = nullptr;
1008 errno = 0;
1009 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1010 if(errno == ERANGE) {
1011 return false;
1012 }
1013 output = static_cast<T>(output_ll);
1014 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1015 return true;
1016 }
1017 if(input == "true") {
1018 // this is to deal with a few oddities with flags and wrapper int types
1019 output = static_cast<T>(1);
1020 return true;
1021 }
1022 // remove separators and trailing spaces
1023 if(input.find_first_of("_'") != std::string::npos) {
1024 std::string nstring = input;
1025 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1026 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1027 return integral_conversion(nstring, output);
1028 }
1029 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1030 return integral_conversion(trim_copy(input), output);
1031 }
1032 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1033 val = nullptr;
1034 errno = 0;
1035 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1036 if(errno == ERANGE) {
1037 return false;
1038 }
1039 output = static_cast<T>(output_ll);
1040 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1041 }
1042 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1043 // LCOV_EXCL_START
1044 // In some new compilers including the coverage testing one binary strings are handled properly in strtoll
1045 // automatically so this coverage is missing but is well tested in other compilers
1046 val = nullptr;
1047 errno = 0;
1048 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1049 if(errno == ERANGE) {
1050 return false;
1051 }
1052 output = static_cast<T>(output_ll);
1053 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1054 // LCOV_EXCL_STOP
1055 }
1056 return false;
1057}
1058
1060inline std::int64_t to_flag_value(std::string val) noexcept {
1061 static const std::string trueString("true");
1062 static const std::string falseString("false");
1063 if(val == trueString) {
1064 return 1;
1065 }
1066 if(val == falseString) {
1067 return -1;
1068 }
1069 val = detail::to_lower(val);
1070 std::int64_t ret = 0;
1071 if(val.size() == 1) {
1072 if(val[0] >= '1' && val[0] <= '9') {
1073 return (static_cast<std::int64_t>(val[0]) - '0');
1074 }
1075 switch(val[0]) {
1076 case '0':
1077 case 'f':
1078 case 'n':
1079 case '-':
1080 ret = -1;
1081 break;
1082 case 't':
1083 case 'y':
1084 case '+':
1085 ret = 1;
1086 break;
1087 default:
1088 errno = EINVAL;
1089 return -1;
1090 }
1091 return ret;
1092 }
1093 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1094 ret = 1;
1095 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1096 ret = -1;
1097 } else {
1098 char *loc_ptr{nullptr};
1099 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1100 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1101 errno = EINVAL;
1102 }
1103 }
1104 return ret;
1105}
1106
1108template <typename T,
1109 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1110 classify_object<T>::value == object_category::unsigned_integral,
1111 detail::enabler> = detail::dummy>
1112bool lexical_cast(const std::string &input, T &output) {
1113 return integral_conversion(input, output);
1114}
1115
1117template <typename T,
1118 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1119bool lexical_cast(const std::string &input, T &output) {
1120 if(input.size() == 1) {
1121 output = static_cast<T>(input[0]);
1122 return true;
1123 }
1124 return integral_conversion(input, output);
1125}
1126
1128template <typename T,
1129 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1130bool lexical_cast(const std::string &input, T &output) {
1131 errno = 0;
1132 auto out = to_flag_value(input);
1133 if(errno == 0) {
1134 output = (out > 0);
1135 } else if(errno == ERANGE) {
1136 output = (input[0] != '-');
1137 } else {
1138 return false;
1139 }
1140 return true;
1141}
1142
1144template <typename T,
1145 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1146bool lexical_cast(const std::string &input, T &output) {
1147 if(input.empty()) {
1148 return false;
1149 }
1150 char *val = nullptr;
1151 auto output_ld = std::strtold(input.c_str(), &val);
1152 output = static_cast<T>(output_ld);
1153 if(val == (input.c_str() + input.size())) {
1154 return true;
1155 }
1156 while(std::isspace(static_cast<unsigned char>(*val))) {
1157 ++val;
1158 if(val == (input.c_str() + input.size())) {
1159 return true;
1160 }
1161 }
1162
1163 // remove separators
1164 if(input.find_first_of("_'") != std::string::npos) {
1165 std::string nstring = input;
1166 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1167 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1168 return lexical_cast(nstring, output);
1169 }
1170 return false;
1171}
1172
1174template <typename T,
1175 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1176bool lexical_cast(const std::string &input, T &output) {
1177 using XC = typename wrapped_type<T, double>::type;
1178 XC x{0.0}, y{0.0};
1179 auto str1 = input;
1180 bool worked = false;
1181 auto nloc = str1.find_last_of("+-");
1182 if(nloc != std::string::npos && nloc > 0) {
1183 worked = lexical_cast(str1.substr(0, nloc), x);
1184 str1 = str1.substr(nloc);
1185 if(str1.back() == 'i' || str1.back() == 'j')
1186 str1.pop_back();
1187 worked = worked && lexical_cast(str1, y);
1188 } else {
1189 if(str1.back() == 'i' || str1.back() == 'j') {
1190 str1.pop_back();
1191 worked = lexical_cast(str1, y);
1192 x = XC{0};
1193 } else {
1194 worked = lexical_cast(str1, x);
1195 y = XC{0};
1196 }
1197 }
1198 if(worked) {
1199 output = T{x, y};
1200 return worked;
1201 }
1202 return from_stream(input, output);
1203}
1204
1206template <typename T,
1207 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1208bool lexical_cast(const std::string &input, T &output) {
1209 output = input;
1210 return true;
1211}
1212
1214template <
1215 typename T,
1216 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1217bool lexical_cast(const std::string &input, T &output) {
1218 output = T(input);
1219 return true;
1220}
1221
1223template <
1224 typename T,
1225 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1226bool lexical_cast(const std::string &input, T &output) {
1227 output = widen(input);
1228 return true;
1229}
1230
1231template <
1232 typename T,
1233 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1234bool lexical_cast(const std::string &input, T &output) {
1235 output = T{widen(input)};
1236 return true;
1237}
1238
1240template <typename T,
1241 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1242bool lexical_cast(const std::string &input, T &output) {
1243 typename std::underlying_type<T>::type val;
1244 if(!integral_conversion(input, val)) {
1245 return false;
1246 }
1247 output = static_cast<T>(val);
1248 return true;
1249}
1250
1252template <typename T,
1253 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1254 std::is_assignable<T &, typename T::value_type>::value,
1255 detail::enabler> = detail::dummy>
1256bool lexical_cast(const std::string &input, T &output) {
1257 typename T::value_type val;
1258 if(lexical_cast(input, val)) {
1259 output = val;
1260 return true;
1261 }
1262 return from_stream(input, output);
1263}
1264
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 && std::is_assignable<T &, T>::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 = T{val};
1273 return true;
1274 }
1275 return from_stream(input, output);
1276}
1277
1279template <
1280 typename T,
1281 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1282bool lexical_cast(const std::string &input, T &output) {
1283 int val = 0;
1284 if(integral_conversion(input, val)) {
1285 output = T(val);
1286 return true;
1287 }
1288
1289 double dval = 0.0;
1290 if(lexical_cast(input, dval)) {
1291 output = T{dval};
1292 return true;
1293 }
1294
1295 return from_stream(input, output);
1296}
1297
1299template <
1300 typename T,
1301 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1302bool lexical_cast(const std::string &input, T &output) {
1303 int val = 0;
1304 if(integral_conversion(input, val)) {
1305 output = T(val);
1306 return true;
1307 }
1308 return from_stream(input, output);
1309}
1310
1312template <
1313 typename T,
1314 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1315bool lexical_cast(const std::string &input, T &output) {
1316 double val = 0.0;
1317 if(lexical_cast(input, val)) {
1318 output = T{val};
1319 return true;
1320 }
1321 return from_stream(input, output);
1322}
1323
1325template <typename T,
1326 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1327 detail::enabler> = detail::dummy>
1328bool lexical_cast(const std::string &input, T &output) {
1329 int val = 0;
1330 if(integral_conversion(input, val)) {
1331#ifdef _MSC_VER
1332#pragma warning(push)
1333#pragma warning(disable : 4800)
1334#endif
1335 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1336 // so will most likely still work
1337 output = val;
1338#ifdef _MSC_VER
1339#pragma warning(pop)
1340#endif
1341 return true;
1342 }
1343 // LCOV_EXCL_START
1344 // This version of cast is only used for odd cases in an older compilers the fail over
1345 // from_stream is tested elsewhere an not relevant for coverage here
1346 return from_stream(input, output);
1347 // LCOV_EXCL_STOP
1348}
1349
1351template <typename T,
1352 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1353 is_istreamable<T>::value,
1354 detail::enabler> = detail::dummy>
1355bool lexical_cast(const std::string &input, T &output) {
1356 return from_stream(input, output);
1357}
1358
1361template <typename T,
1362 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1363 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1364 detail::enabler> = detail::dummy>
1365bool lexical_cast(const std::string & /*input*/, T & /*output*/) {
1366 static_assert(!std::is_same<T, T>::value, // Can't just write false here.
1367 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1368 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1369 return false;
1370}
1371
1374template <typename AssignTo,
1375 typename ConvertTo,
1376 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1377 (classify_object<AssignTo>::value == object_category::string_assignable ||
1378 classify_object<AssignTo>::value == object_category::string_constructible ||
1379 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1380 classify_object<AssignTo>::value == object_category::wstring_constructible),
1381 detail::enabler> = detail::dummy>
1382bool lexical_assign(const std::string &input, AssignTo &output) {
1383 return lexical_cast(input, output);
1384}
1385
1387template <typename AssignTo,
1388 typename ConvertTo,
1389 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::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 if(input.empty()) {
1397 output = AssignTo{};
1398 return true;
1399 }
1400
1401 return lexical_cast(input, output);
1402} // LCOV_EXCL_LINE
1403
1405template <typename AssignTo,
1406 typename ConvertTo,
1407 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1408 classify_object<AssignTo>::value == object_category::wrapper_value,
1409 detail::enabler> = detail::dummy>
1410bool lexical_assign(const std::string &input, AssignTo &output) {
1411 if(input.empty()) {
1412 typename AssignTo::value_type emptyVal{};
1413 output = emptyVal;
1414 return true;
1415 }
1416 return lexical_cast(input, output);
1417}
1418
1421template <typename AssignTo,
1422 typename ConvertTo,
1423 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1424 classify_object<AssignTo>::value != object_category::wrapper_value &&
1425 std::is_assignable<AssignTo &, int>::value,
1426 detail::enabler> = detail::dummy>
1427bool lexical_assign(const std::string &input, AssignTo &output) {
1428 if(input.empty()) {
1429 output = 0;
1430 return true;
1431 }
1432 int val{0};
1433 if(lexical_cast(input, val)) {
1434#if defined(__clang__)
1435/* on some older clang compilers */
1436#pragma clang diagnostic push
1437#pragma clang diagnostic ignored "-Wsign-conversion"
1438#endif
1439 output = val;
1440#if defined(__clang__)
1441#pragma clang diagnostic pop
1442#endif
1443 return true;
1444 }
1445 return false;
1446}
1447
1449template <typename AssignTo,
1450 typename ConvertTo,
1451 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1452 detail::enabler> = detail::dummy>
1453bool lexical_assign(const std::string &input, AssignTo &output) {
1454 ConvertTo val{};
1455 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1456 if(parse_result) {
1457 output = val;
1458 }
1459 return parse_result;
1460}
1461
1463template <
1464 typename AssignTo,
1465 typename ConvertTo,
1466 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1467 std::is_move_assignable<AssignTo>::value,
1468 detail::enabler> = detail::dummy>
1469bool lexical_assign(const std::string &input, AssignTo &output) {
1470 ConvertTo val{};
1471 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1472 if(parse_result) {
1473 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1474 }
1475 return parse_result;
1476}
1477
1479template <typename AssignTo,
1480 typename ConvertTo,
1481 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1482 classify_object<AssignTo>::value <= object_category::wrapper_value,
1483 detail::enabler> = detail::dummy>
1484bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1485 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1486}
1487
1490template <typename AssignTo,
1491 typename ConvertTo,
1492 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1493 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1494 detail::enabler> = detail::dummy>
1495bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1496 // the remove const is to handle pair types coming from a container
1497 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1498 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1499 FirstType v1;
1500 SecondType v2;
1501 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1502 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1503 if(retval) {
1504 output = AssignTo{v1, v2};
1505 }
1506 return retval;
1507}
1508
1510template <class AssignTo,
1511 class ConvertTo,
1512 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1513 type_count<ConvertTo>::value == 1,
1514 detail::enabler> = detail::dummy>
1515bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1516 output.erase(output.begin(), output.end());
1517 if(strings.empty()) {
1518 return true;
1519 }
1520 if(strings.size() == 1 && strings[0] == "{}") {
1521 return true;
1522 }
1523 bool skip_remaining = false;
1524 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1525 skip_remaining = true;
1526 }
1527 for(const auto &elem : strings) {
1528 typename AssignTo::value_type out;
1529 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1530 if(!retval) {
1531 return false;
1532 }
1533 output.insert(output.end(), std::move(out));
1534 if(skip_remaining) {
1535 break;
1536 }
1537 }
1538 return (!output.empty());
1539}
1540
1542template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1543bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1544
1545 if(strings.size() >= 2 && !strings[1].empty()) {
1546 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1547 XC2 x{0.0}, y{0.0};
1548 auto str1 = strings[1];
1549 if(str1.back() == 'i' || str1.back() == 'j') {
1550 str1.pop_back();
1551 }
1552 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1553 if(worked) {
1554 output = ConvertTo{x, y};
1555 }
1556 return worked;
1557 }
1558 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1559}
1560
1562template <class AssignTo,
1563 class ConvertTo,
1564 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1565 (type_count<ConvertTo>::value == 1),
1566 detail::enabler> = detail::dummy>
1567bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1568 bool retval = true;
1569 output.clear();
1570 output.reserve(strings.size());
1571 for(const auto &elem : strings) {
1572
1573 output.emplace_back();
1574 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1575 }
1576 return (!output.empty()) && retval;
1577}
1578
1579// forward declaration
1580
1582template <class AssignTo,
1583 class ConvertTo,
1584 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1585 type_count_base<ConvertTo>::value == 2,
1586 detail::enabler> = detail::dummy>
1587bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1588
1590template <class AssignTo,
1591 class ConvertTo,
1592 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1593 type_count_base<ConvertTo>::value != 2 &&
1594 ((type_count<ConvertTo>::value > 2) ||
1595 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1596 detail::enabler> = detail::dummy>
1597bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1598
1600template <class AssignTo,
1601 class ConvertTo,
1602 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1603 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1604 type_count<ConvertTo>::value > 2),
1605 detail::enabler> = detail::dummy>
1606bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1607
1610template <typename AssignTo,
1611 typename ConvertTo,
1612 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1613 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1614 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1615 detail::enabler> = detail::dummy>
1616bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1617
1618 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1619 ConvertTo val;
1620 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1621 output = AssignTo{val};
1622 return retval;
1623 }
1624 output = AssignTo{};
1625 return true;
1626}
1627
1629template <class AssignTo, class ConvertTo, std::size_t I>
1630inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1631tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1632 return true;
1633}
1634
1636template <class AssignTo, class ConvertTo>
1637inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1638tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1639 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1640 strings.erase(strings.begin());
1641 return retval;
1642}
1643
1645template <class AssignTo, class ConvertTo>
1646inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1647 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1648 bool>::type
1649tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1650 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1651 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1652 return retval;
1653}
1654
1656template <class AssignTo, class ConvertTo>
1657inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1658 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1659 bool>::type
1660tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1661
1662 std::size_t index{subtype_count_min<ConvertTo>::value};
1663 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1664 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1665
1666 while(index < mx) {
1667 if(is_separator(strings[index])) {
1668 break;
1669 }
1670 ++index;
1671 }
1672 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1673 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1674 if(strings.size() > index) {
1675 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1676 } else {
1677 strings.clear();
1678 }
1679 return retval;
1680}
1681
1683template <class AssignTo, class ConvertTo, std::size_t I>
1684inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1685tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1686 bool retval = true;
1687 using ConvertToElement = typename std::
1688 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1689 if(!strings.empty()) {
1690 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1691 strings, std::get<I>(output));
1692 }
1693 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1694 return retval;
1695}
1696
1698template <class AssignTo,
1699 class ConvertTo,
1700 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1701 type_count_base<ConvertTo>::value == 2,
1702 detail::enabler>>
1703bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1704 output.clear();
1705 while(!strings.empty()) {
1706
1707 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1708 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1709 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1710 if(!strings.empty()) {
1711 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1712 }
1713 if(retval) {
1714 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1715 } else {
1716 return false;
1717 }
1718 }
1719 return (!output.empty());
1720}
1721
1723template <class AssignTo,
1724 class ConvertTo,
1725 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1726 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1727 type_count<ConvertTo>::value > 2),
1728 detail::enabler>>
1729bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1730 static_assert(
1731 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1732 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1733 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1734}
1735
1737template <class AssignTo,
1738 class ConvertTo,
1739 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1740 type_count_base<ConvertTo>::value != 2 &&
1741 ((type_count<ConvertTo>::value > 2) ||
1742 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1743 detail::enabler>>
1744bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1745 bool retval = true;
1746 output.clear();
1747 std::vector<std::string> temp;
1748 std::size_t ii{0};
1749 std::size_t icount{0};
1750 std::size_t xcm{type_count<ConvertTo>::value};
1751 auto ii_max = strings.size();
1752 while(ii < ii_max) {
1753 temp.push_back(strings[ii]);
1754 ++ii;
1755 ++icount;
1756 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1757 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1758 temp.pop_back();
1759 }
1760 typename AssignTo::value_type temp_out;
1761 retval = retval &&
1762 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1763 temp.clear();
1764 if(!retval) {
1765 return false;
1766 }
1767 output.insert(output.end(), std::move(temp_out));
1768 icount = 0;
1769 }
1770 }
1771 return retval;
1772}
1773
1775template <typename AssignTo,
1776 class ConvertTo,
1777 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1778 std::is_assignable<ConvertTo &, ConvertTo>::value,
1779 detail::enabler> = detail::dummy>
1780bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1781 if(strings.empty() || strings.front().empty()) {
1782 output = ConvertTo{};
1783 return true;
1784 }
1785 typename ConvertTo::value_type val;
1786 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1787 output = ConvertTo{val};
1788 return true;
1789 }
1790 return false;
1791}
1792
1794template <typename AssignTo,
1795 class ConvertTo,
1796 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1797 !std::is_assignable<AssignTo &, ConvertTo>::value,
1798 detail::enabler> = detail::dummy>
1799bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1800 using ConvertType = typename ConvertTo::value_type;
1801 if(strings.empty() || strings.front().empty()) {
1802 output = ConvertType{};
1803 return true;
1804 }
1805 ConvertType val;
1806 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1807 output = val;
1808 return true;
1809 }
1810 return false;
1811}
1812
1814inline std::string sum_string_vector(const std::vector<std::string> &values) {
1815 double val{0.0};
1816 bool fail{false};
1817 std::string output;
1818 for(const auto &arg : values) {
1819 double tv{0.0};
1820 auto comp = lexical_cast(arg, tv);
1821 if(!comp) {
1822 errno = 0;
1823 auto fv = detail::to_flag_value(arg);
1824 fail = (errno != 0);
1825 if(fail) {
1826 break;
1827 }
1828 tv = static_cast<double>(fv);
1829 }
1830 val += tv;
1831 }
1832 if(fail) {
1833 for(const auto &arg : values) {
1834 output.append(arg);
1835 }
1836 } else {
1837 std::ostringstream out;
1838 out.precision(16);
1839 out << val;
1840 output = out.str();
1841 }
1842 return output;
1843}
1844
1845} // namespace detail
1846// [CLI11:type_tools_hpp:end]
1847} // 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