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