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