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