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