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