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
285template <typename T>
287 T,
288 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
289 : public std::true_type {};
290
291// check to see if an object is a wrapper (fail by default)
292template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
293
294// check if an object is a wrapper (it has a value_type defined)
295template <typename T>
296struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
297
298// Check for tuple like types, as in classes with a tuple_size type trait
299template <typename S> class is_tuple_like {
300 template <typename SS>
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, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
312auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
313 return std::forward<T>(value);
314}
315
317template <typename T,
318 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
319 detail::enabler> = detail::dummy>
320std::string to_string(const T &value) {
321 return std::string(value); // NOLINT(google-readability-casting)
322}
323
325template <typename T,
326 enable_if_t<!std::is_convertible<std::string, T>::value && !std::is_constructible<std::string, T>::value &&
327 is_ostreamable<T>::value,
328 detail::enabler> = detail::dummy>
329std::string to_string(T &&value) {
330 std::stringstream stream;
331 stream << value;
332 return stream.str();
333}
334
336template <typename T,
337 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
338 !is_readable_container<typename std::remove_const<T>::type>::value,
339 detail::enabler> = detail::dummy>
340std::string to_string(T &&) {
341 return {};
342}
343
345template <typename T,
346 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
347 is_readable_container<T>::value,
348 detail::enabler> = detail::dummy>
349std::string to_string(T &&variable) {
350 auto cval = variable.begin();
351 auto end = variable.end();
352 if(cval == end) {
353 return {"{}"};
354 }
355 std::vector<std::string> defaults;
356 while(cval != end) {
357 defaults.emplace_back(CLI::detail::to_string(*cval));
358 ++cval;
359 }
360 return {"[" + detail::join(defaults) + "]"};
361}
362
364template <typename T1,
365 typename T2,
366 typename T,
367 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
368auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
369 return to_string(std::forward<T>(value));
370}
371
373template <typename T1,
374 typename T2,
375 typename T,
376 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
377std::string checked_to_string(T &&) {
378 return std::string{};
379}
381template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
382std::string value_string(const T &value) {
383 return std::to_string(value);
384}
386template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
387std::string value_string(const T &value) {
388 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
389}
391template <typename T,
392 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
393auto value_string(const T &value) -> decltype(to_string(value)) {
394 return to_string(value);
395}
396
398template <typename T, typename def, typename Enable = void> struct wrapped_type {
399 using type = def;
400};
401
403template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
404 using type = typename T::value_type;
405};
406
408template <typename T, typename Enable = void> struct type_count_base {
409 static const int value{0};
410};
411
413template <typename T>
415 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
416 !std::is_void<T>::value>::type> {
417 static constexpr int value{1};
418};
419
421template <typename T>
422struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
423 static constexpr int value{std::tuple_size<T>::value};
424};
425
427template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
428 static constexpr int value{type_count_base<typename T::value_type>::value};
429};
430
432
434template <typename T> struct subtype_count;
435
437template <typename T> struct subtype_count_min;
438
440template <typename T, typename Enable = void> struct type_count {
441 static const int value{0};
442};
443
445template <typename T>
446struct type_count<T,
447 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
448 !std::is_void<T>::value>::type> {
449 static constexpr int value{1};
450};
451
453template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
454 static constexpr int value{2};
455};
456
458template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
459 static constexpr int value{subtype_count<typename T::value_type>::value};
460};
461
463template <typename T>
464struct type_count<T,
465 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
466 !is_mutable_container<T>::value>::type> {
467 static constexpr int value{type_count<typename T::value_type>::value};
468};
469
471template <typename T, std::size_t I>
472constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
473 return 0;
474}
475
477template <typename T, std::size_t I>
478 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
479 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
480}
481
483template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
484 static constexpr int value{tuple_type_size<T, 0>()};
485};
486
488template <typename T> struct subtype_count {
489 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
490};
491
493template <typename T, typename Enable = void> struct type_count_min {
494 static const int value{0};
495};
496
498template <typename T>
499struct type_count_min<
500 T,
501 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
502 !is_complex<T>::value && !std::is_void<T>::value>::type> {
503 static constexpr int value{type_count<T>::value};
504};
505
507template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
508 static constexpr int value{1};
509};
510
512template <typename T>
513struct type_count_min<
514 T,
515 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
516 static constexpr int value{subtype_count_min<typename T::value_type>::value};
517};
518
520template <typename T, std::size_t I>
521constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
522 return 0;
523}
524
526template <typename T, std::size_t I>
527 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
528 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
529}
530
532template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
533 static constexpr int value{tuple_type_size_min<T, 0>()};
534};
535
537template <typename T> struct subtype_count_min {
538 static constexpr int value{is_mutable_container<T>::value
539 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
540 : type_count_min<T>::value};
541};
542
544template <typename T, typename Enable = void> struct expected_count {
545 static const int value{0};
546};
547
549template <typename T>
550struct expected_count<T,
551 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
552 !std::is_void<T>::value>::type> {
553 static constexpr int value{1};
554};
556template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
557 static constexpr int value{expected_max_vector_size};
558};
559
561template <typename T>
562struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
563 static constexpr int value{expected_count<typename T::value_type>::value};
564};
565
566// Enumeration of the different supported categorizations of objects
567enum class object_category : int {
568 char_value = 1,
569 integral_value = 2,
570 unsigned_integral = 4,
571 enumeration = 6,
572 boolean_value = 8,
573 floating_point = 10,
574 number_constructible = 12,
575 double_constructible = 14,
576 integer_constructible = 16,
577 // string like types
578 string_assignable = 23,
579 string_constructible = 24,
580 wstring_assignable = 25,
581 wstring_constructible = 26,
582 other = 45,
583 // special wrapper or container types
584 wrapper_value = 50,
585 complex_number = 60,
586 tuple_value = 70,
587 container_value = 80,
588
589};
590
592
594template <typename T, typename Enable = void> struct classify_object {
595 static constexpr object_category value{object_category::other};
596};
597
599template <typename T>
600struct classify_object<
601 T,
602 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
603 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
604 static constexpr object_category value{object_category::integral_value};
605};
606
608template <typename T>
609struct classify_object<T,
610 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
611 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
612 static constexpr object_category value{object_category::unsigned_integral};
613};
614
616template <typename T>
617struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
618 static constexpr object_category value{object_category::char_value};
619};
620
622template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
623 static constexpr object_category value{object_category::boolean_value};
624};
625
627template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
628 static constexpr object_category value{object_category::floating_point};
629};
630#if defined _MSC_VER
631// in MSVC wstring should take precedence if available this isn't as useful on other compilers due to the broader use of
632// utf-8 encoding
633#define WIDE_STRING_CHECK \
634 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
635#define STRING_CHECK true
636#else
637#define WIDE_STRING_CHECK true
638#define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
639#endif
640
642template <typename T>
643struct classify_object<
644 T,
645 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
646 std::is_assignable<T &, std::string>::value>::type> {
647 static constexpr object_category value{object_category::string_assignable};
648};
649
651template <typename T>
652struct classify_object<
653 T,
654 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
655 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
656 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
657 static constexpr object_category value{object_category::string_constructible};
658};
659
661template <typename T>
662struct classify_object<T,
663 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
664 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
665 static constexpr object_category value{object_category::wstring_assignable};
666};
667
668template <typename T>
669struct classify_object<
670 T,
671 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
672 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
673 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
674 static constexpr object_category value{object_category::wstring_constructible};
675};
676
678template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
679 static constexpr object_category value{object_category::enumeration};
680};
681
682template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
683 static constexpr object_category value{object_category::complex_number};
684};
685
688template <typename T> struct uncommon_type {
689 using type = typename std::conditional<
690 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
691 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
692 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
693 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
694 std::true_type,
695 std::false_type>::type;
696 static constexpr bool value = type::value;
697};
698
700template <typename T>
701struct classify_object<T,
702 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
703 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
704 static constexpr object_category value{object_category::wrapper_value};
705};
706
708template <typename T>
709struct classify_object<T,
710 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
711 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
712 is_direct_constructible<T, int>::value>::type> {
713 static constexpr object_category value{object_category::number_constructible};
714};
715
717template <typename T>
718struct classify_object<T,
719 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
720 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
721 is_direct_constructible<T, int>::value>::type> {
722 static constexpr object_category value{object_category::integer_constructible};
723};
724
726template <typename T>
727struct classify_object<T,
728 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
729 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
730 !is_direct_constructible<T, int>::value>::type> {
731 static constexpr object_category value{object_category::double_constructible};
732};
733
735template <typename T>
736struct classify_object<
737 T,
738 typename std::enable_if<is_tuple_like<T>::value &&
739 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
740 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
741 !is_direct_constructible<T, int>::value) ||
742 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
743 static constexpr object_category value{object_category::tuple_value};
744 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
745 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
746 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
747 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
748 // those cases that are caught by other object classifications
749};
750
752template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
753 static constexpr object_category value{object_category::container_value};
754};
755
756// Type name print
757
761
762template <typename T,
763 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
764constexpr const char *type_name() {
765 return "CHAR";
766}
767
768template <typename T,
769 enable_if_t<classify_object<T>::value == object_category::integral_value ||
770 classify_object<T>::value == object_category::integer_constructible,
771 detail::enabler> = detail::dummy>
772constexpr const char *type_name() {
773 return "INT";
774}
775
776template <typename T,
777 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
778constexpr const char *type_name() {
779 return "UINT";
780}
781
782template <typename T,
783 enable_if_t<classify_object<T>::value == object_category::floating_point ||
784 classify_object<T>::value == object_category::number_constructible ||
785 classify_object<T>::value == object_category::double_constructible,
786 detail::enabler> = detail::dummy>
787constexpr const char *type_name() {
788 return "FLOAT";
789}
790
792template <typename T,
793 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
794constexpr const char *type_name() {
795 return "ENUM";
796}
797
799template <typename T,
800 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
801constexpr const char *type_name() {
802 return "BOOLEAN";
803}
804
806template <typename T,
807 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
808constexpr const char *type_name() {
809 return "COMPLEX";
810}
811
813template <typename T,
814 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
815 classify_object<T>::value <= object_category::other,
816 detail::enabler> = detail::dummy>
817constexpr const char *type_name() {
818 return "TEXT";
819}
821template <typename T,
822 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
823 detail::enabler> = detail::dummy>
824std::string type_name(); // forward declaration
825
827template <typename T,
828 enable_if_t<classify_object<T>::value == object_category::container_value ||
829 classify_object<T>::value == object_category::wrapper_value,
830 detail::enabler> = detail::dummy>
831std::string type_name(); // forward declaration
832
834template <typename T,
835 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
836 detail::enabler> = detail::dummy>
837inline std::string type_name() {
838 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
839}
840
842template <typename T, std::size_t I>
843inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
844 return std::string{};
845}
846
848template <typename T, std::size_t I>
849inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
850 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
851 tuple_name<T, I + 1>();
852 if(str.back() == ',')
853 str.pop_back();
854 return str;
855}
856
858template <typename T,
859 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
860 detail::enabler>>
861inline std::string type_name() {
862 auto tname = std::string(1, '[') + tuple_name<T, 0>();
863 tname.push_back(']');
864 return tname;
865}
866
868template <typename T,
869 enable_if_t<classify_object<T>::value == object_category::container_value ||
870 classify_object<T>::value == object_category::wrapper_value,
871 detail::enabler>>
872inline std::string type_name() {
873 return type_name<typename T::value_type>();
874}
875
876// Lexical cast
877
879template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
880bool integral_conversion(const std::string &input, T &output) noexcept {
881 if(input.empty() || input.front() == '-') {
882 return false;
883 }
884 char *val{nullptr};
885 errno = 0;
886 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
887 if(errno == ERANGE) {
888 return false;
889 }
890 output = static_cast<T>(output_ll);
891 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
892 return true;
893 }
894 val = nullptr;
895 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
896 if(val == (input.c_str() + input.size())) {
897 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
898 return (static_cast<std::int64_t>(output) == output_sll);
899 }
900 // remove separators
901 if(input.find_first_of("_'") != std::string::npos) {
902 std::string nstring = input;
903 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
904 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
905 return integral_conversion(nstring, output);
906 }
907 if(input.compare(0, 2, "0o") == 0) {
908 val = nullptr;
909 errno = 0;
910 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
911 if(errno == ERANGE) {
912 return false;
913 }
914 output = static_cast<T>(output_ll);
915 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
916 }
917 if(input.compare(0, 2, "0b") == 0) {
918 val = nullptr;
919 errno = 0;
920 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
921 if(errno == ERANGE) {
922 return false;
923 }
924 output = static_cast<T>(output_ll);
925 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
926 }
927 return false;
928}
929
931template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
932bool integral_conversion(const std::string &input, T &output) noexcept {
933 if(input.empty()) {
934 return false;
935 }
936 char *val = nullptr;
937 errno = 0;
938 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
939 if(errno == ERANGE) {
940 return false;
941 }
942 output = static_cast<T>(output_ll);
943 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
944 return true;
945 }
946 if(input == "true") {
947 // this is to deal with a few oddities with flags and wrapper int types
948 output = static_cast<T>(1);
949 return true;
950 }
951 // remove separators
952 if(input.find_first_of("_'") != std::string::npos) {
953 std::string nstring = input;
954 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
955 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
956 return integral_conversion(nstring, output);
957 }
958 if(input.compare(0, 2, "0o") == 0) {
959 val = nullptr;
960 errno = 0;
961 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
962 if(errno == ERANGE) {
963 return false;
964 }
965 output = static_cast<T>(output_ll);
966 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
967 }
968 if(input.compare(0, 2, "0b") == 0) {
969 val = nullptr;
970 errno = 0;
971 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
972 if(errno == ERANGE) {
973 return false;
974 }
975 output = static_cast<T>(output_ll);
976 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
977 }
978 return false;
979}
980
982inline std::int64_t to_flag_value(std::string val) noexcept {
983 static const std::string trueString("true");
984 static const std::string falseString("false");
985 if(val == trueString) {
986 return 1;
987 }
988 if(val == falseString) {
989 return -1;
990 }
991 val = detail::to_lower(val);
992 std::int64_t ret = 0;
993 if(val.size() == 1) {
994 if(val[0] >= '1' && val[0] <= '9') {
995 return (static_cast<std::int64_t>(val[0]) - '0');
996 }
997 switch(val[0]) {
998 case '0':
999 case 'f':
1000 case 'n':
1001 case '-':
1002 ret = -1;
1003 break;
1004 case 't':
1005 case 'y':
1006 case '+':
1007 ret = 1;
1008 break;
1009 default:
1010 errno = EINVAL;
1011 return -1;
1012 }
1013 return ret;
1014 }
1015 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1016 ret = 1;
1017 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1018 ret = -1;
1019 } else {
1020 char *loc_ptr{nullptr};
1021 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1022 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1023 errno = EINVAL;
1024 }
1025 }
1026 return ret;
1027}
1028
1030template <typename T,
1031 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1032 classify_object<T>::value == object_category::unsigned_integral,
1033 detail::enabler> = detail::dummy>
1034bool lexical_cast(const std::string &input, T &output) {
1035 return integral_conversion(input, output);
1036}
1037
1039template <typename T,
1040 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1041bool lexical_cast(const std::string &input, T &output) {
1042 if(input.size() == 1) {
1043 output = static_cast<T>(input[0]);
1044 return true;
1045 }
1046 return integral_conversion(input, output);
1047}
1048
1050template <typename T,
1051 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1052bool lexical_cast(const std::string &input, T &output) {
1053 errno = 0;
1054 auto out = to_flag_value(input);
1055 if(errno == 0) {
1056 output = (out > 0);
1057 } else if(errno == ERANGE) {
1058 output = (input[0] != '-');
1059 } else {
1060 return false;
1061 }
1062 return true;
1063}
1064
1066template <typename T,
1067 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1068bool lexical_cast(const std::string &input, T &output) {
1069 if(input.empty()) {
1070 return false;
1071 }
1072 char *val = nullptr;
1073 auto output_ld = std::strtold(input.c_str(), &val);
1074 output = static_cast<T>(output_ld);
1075 if(val == (input.c_str() + input.size())) {
1076 return true;
1077 }
1078 // remove separators
1079 if(input.find_first_of("_'") != std::string::npos) {
1080 std::string nstring = input;
1081 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1082 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1083 return lexical_cast(nstring, output);
1084 }
1085 return false;
1086}
1087
1089template <typename T,
1090 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1091bool lexical_cast(const std::string &input, T &output) {
1092 using XC = typename wrapped_type<T, double>::type;
1093 XC x{0.0}, y{0.0};
1094 auto str1 = input;
1095 bool worked = false;
1096 auto nloc = str1.find_last_of("+-");
1097 if(nloc != std::string::npos && nloc > 0) {
1098 worked = lexical_cast(str1.substr(0, nloc), x);
1099 str1 = str1.substr(nloc);
1100 if(str1.back() == 'i' || str1.back() == 'j')
1101 str1.pop_back();
1102 worked = worked && lexical_cast(str1, y);
1103 } else {
1104 if(str1.back() == 'i' || str1.back() == 'j') {
1105 str1.pop_back();
1106 worked = lexical_cast(str1, y);
1107 x = XC{0};
1108 } else {
1109 worked = lexical_cast(str1, x);
1110 y = XC{0};
1111 }
1112 }
1113 if(worked) {
1114 output = T{x, y};
1115 return worked;
1116 }
1117 return from_stream(input, output);
1118}
1119
1121template <typename T,
1122 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1123bool lexical_cast(const std::string &input, T &output) {
1124 output = input;
1125 return true;
1126}
1127
1129template <
1130 typename T,
1131 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1132bool lexical_cast(const std::string &input, T &output) {
1133 output = T(input);
1134 return true;
1135}
1136
1138template <
1139 typename T,
1140 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1141bool lexical_cast(const std::string &input, T &output) {
1142 output = widen(input);
1143 return true;
1144}
1145
1146template <
1147 typename T,
1148 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1149bool lexical_cast(const std::string &input, T &output) {
1150 output = T{widen(input)};
1151 return true;
1152}
1153
1155template <typename T,
1156 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1157bool lexical_cast(const std::string &input, T &output) {
1158 typename std::underlying_type<T>::type val;
1159 if(!integral_conversion(input, val)) {
1160 return false;
1161 }
1162 output = static_cast<T>(val);
1163 return true;
1164}
1165
1167template <typename T,
1168 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1169 std::is_assignable<T &, typename T::value_type>::value,
1170 detail::enabler> = detail::dummy>
1171bool lexical_cast(const std::string &input, T &output) {
1172 typename T::value_type val;
1173 if(lexical_cast(input, val)) {
1174 output = val;
1175 return true;
1176 }
1177 return from_stream(input, output);
1178}
1179
1180template <typename T,
1181 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1182 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1183 detail::enabler> = detail::dummy>
1184bool lexical_cast(const std::string &input, T &output) {
1185 typename T::value_type val;
1186 if(lexical_cast(input, val)) {
1187 output = T{val};
1188 return true;
1189 }
1190 return from_stream(input, output);
1191}
1192
1194template <
1195 typename T,
1196 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1197bool lexical_cast(const std::string &input, T &output) {
1198 int val = 0;
1199 if(integral_conversion(input, val)) {
1200 output = T(val);
1201 return true;
1202 }
1203
1204 double dval = 0.0;
1205 if(lexical_cast(input, dval)) {
1206 output = T{dval};
1207 return true;
1208 }
1209
1210 return from_stream(input, output);
1211}
1212
1214template <
1215 typename T,
1216 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1217bool lexical_cast(const std::string &input, T &output) {
1218 int val = 0;
1219 if(integral_conversion(input, val)) {
1220 output = T(val);
1221 return true;
1222 }
1223 return from_stream(input, output);
1224}
1225
1227template <
1228 typename T,
1229 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1230bool lexical_cast(const std::string &input, T &output) {
1231 double val = 0.0;
1232 if(lexical_cast(input, val)) {
1233 output = T{val};
1234 return true;
1235 }
1236 return from_stream(input, output);
1237}
1238
1240template <typename T,
1241 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1242 detail::enabler> = detail::dummy>
1243bool lexical_cast(const std::string &input, T &output) {
1244 int val = 0;
1245 if(integral_conversion(input, val)) {
1246#ifdef _MSC_VER
1247#pragma warning(push)
1248#pragma warning(disable : 4800)
1249#endif
1250 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1251 // so will most likely still work
1252 output = val;
1253#ifdef _MSC_VER
1254#pragma warning(pop)
1255#endif
1256 return true;
1257 }
1258 // LCOV_EXCL_START
1259 // This version of cast is only used for odd cases in an older compilers the fail over
1260 // from_stream is tested elsewhere an not relevant for coverage here
1261 return from_stream(input, output);
1262 // LCOV_EXCL_STOP
1263}
1264
1266template <typename T,
1267 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1268 is_istreamable<T>::value,
1269 detail::enabler> = detail::dummy>
1270bool lexical_cast(const std::string &input, T &output) {
1271 return from_stream(input, output);
1272}
1273
1276template <typename T,
1277 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1278 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1279 detail::enabler> = detail::dummy>
1280bool lexical_cast(const std::string & /*input*/, T & /*output*/) {
1281 static_assert(!std::is_same<T, T>::value, // Can't just write false here.
1282 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1283 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1284 return false;
1285}
1286
1289template <typename AssignTo,
1290 typename ConvertTo,
1291 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1292 (classify_object<AssignTo>::value == object_category::string_assignable ||
1293 classify_object<AssignTo>::value == object_category::string_constructible ||
1294 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1295 classify_object<AssignTo>::value == object_category::wstring_constructible),
1296 detail::enabler> = detail::dummy>
1297bool lexical_assign(const std::string &input, AssignTo &output) {
1298 return lexical_cast(input, output);
1299}
1300
1302template <typename AssignTo,
1303 typename ConvertTo,
1304 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1305 classify_object<AssignTo>::value != object_category::string_assignable &&
1306 classify_object<AssignTo>::value != object_category::string_constructible &&
1307 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1308 classify_object<AssignTo>::value != object_category::wstring_constructible,
1309 detail::enabler> = detail::dummy>
1310bool lexical_assign(const std::string &input, AssignTo &output) {
1311 if(input.empty()) {
1312 output = AssignTo{};
1313 return true;
1314 }
1315
1316 return lexical_cast(input, output);
1317}
1318
1320template <typename AssignTo,
1321 typename ConvertTo,
1322 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1323 classify_object<AssignTo>::value == object_category::wrapper_value,
1324 detail::enabler> = detail::dummy>
1325bool lexical_assign(const std::string &input, AssignTo &output) {
1326 if(input.empty()) {
1327 typename AssignTo::value_type emptyVal{};
1328 output = emptyVal;
1329 return true;
1330 }
1331 return lexical_cast(input, output);
1332}
1333
1336template <typename AssignTo,
1337 typename ConvertTo,
1338 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1339 classify_object<AssignTo>::value != object_category::wrapper_value &&
1340 std::is_assignable<AssignTo &, int>::value,
1341 detail::enabler> = detail::dummy>
1342bool lexical_assign(const std::string &input, AssignTo &output) {
1343 if(input.empty()) {
1344 output = 0;
1345 return true;
1346 }
1347 int val{0};
1348 if(lexical_cast(input, val)) {
1349#if defined(__clang__)
1350/* on some older clang compilers */
1351#pragma clang diagnostic push
1352#pragma clang diagnostic ignored "-Wsign-conversion"
1353#endif
1354 output = val;
1355#if defined(__clang__)
1356#pragma clang diagnostic pop
1357#endif
1358 return true;
1359 }
1360 return false;
1361}
1362
1364template <typename AssignTo,
1365 typename ConvertTo,
1366 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1367 detail::enabler> = detail::dummy>
1368bool lexical_assign(const std::string &input, AssignTo &output) {
1369 ConvertTo val{};
1370 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1371 if(parse_result) {
1372 output = val;
1373 }
1374 return parse_result;
1375}
1376
1378template <
1379 typename AssignTo,
1380 typename ConvertTo,
1381 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1382 std::is_move_assignable<AssignTo>::value,
1383 detail::enabler> = detail::dummy>
1384bool lexical_assign(const std::string &input, AssignTo &output) {
1385 ConvertTo val{};
1386 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1387 if(parse_result) {
1388 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1389 }
1390 return parse_result;
1391}
1392
1394template <typename AssignTo,
1395 typename ConvertTo,
1396 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1397 classify_object<AssignTo>::value <= object_category::wrapper_value,
1398 detail::enabler> = detail::dummy>
1399bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1400 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1401}
1402
1405template <typename AssignTo,
1406 typename ConvertTo,
1407 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1408 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1409 detail::enabler> = detail::dummy>
1410bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1411 // the remove const is to handle pair types coming from a container
1412 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1413 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1414 FirstType v1;
1415 SecondType v2;
1416 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1417 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1418 if(retval) {
1419 output = AssignTo{v1, v2};
1420 }
1421 return retval;
1422}
1423
1425template <class AssignTo,
1426 class ConvertTo,
1427 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1428 type_count<ConvertTo>::value == 1,
1429 detail::enabler> = detail::dummy>
1430bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1431 output.erase(output.begin(), output.end());
1432 if(strings.empty()) {
1433 return true;
1434 }
1435 if(strings.size() == 1 && strings[0] == "{}") {
1436 return true;
1437 }
1438 bool skip_remaining = false;
1439 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1440 skip_remaining = true;
1441 }
1442 for(const auto &elem : strings) {
1443 typename AssignTo::value_type out;
1444 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1445 if(!retval) {
1446 return false;
1447 }
1448 output.insert(output.end(), std::move(out));
1449 if(skip_remaining) {
1450 break;
1451 }
1452 }
1453 return (!output.empty());
1454}
1455
1457template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1458bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1459
1460 if(strings.size() >= 2 && !strings[1].empty()) {
1461 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1462 XC2 x{0.0}, y{0.0};
1463 auto str1 = strings[1];
1464 if(str1.back() == 'i' || str1.back() == 'j') {
1465 str1.pop_back();
1466 }
1467 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1468 if(worked) {
1469 output = ConvertTo{x, y};
1470 }
1471 return worked;
1472 }
1473 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1474}
1475
1477template <class AssignTo,
1478 class ConvertTo,
1479 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1480 (type_count<ConvertTo>::value == 1),
1481 detail::enabler> = detail::dummy>
1482bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1483 bool retval = true;
1484 output.clear();
1485 output.reserve(strings.size());
1486 for(const auto &elem : strings) {
1487
1488 output.emplace_back();
1489 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1490 }
1491 return (!output.empty()) && retval;
1492}
1493
1494// forward declaration
1495
1497template <class AssignTo,
1498 class ConvertTo,
1499 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1500 type_count_base<ConvertTo>::value == 2,
1501 detail::enabler> = detail::dummy>
1502bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1503
1505template <class AssignTo,
1506 class ConvertTo,
1507 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1508 type_count_base<ConvertTo>::value != 2 &&
1509 ((type_count<ConvertTo>::value > 2) ||
1510 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1511 detail::enabler> = detail::dummy>
1512bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1513
1515template <class AssignTo,
1516 class ConvertTo,
1517 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1518 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1519 type_count<ConvertTo>::value > 2),
1520 detail::enabler> = detail::dummy>
1521bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1522
1525template <typename AssignTo,
1526 typename ConvertTo,
1527 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1528 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1529 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1530 detail::enabler> = detail::dummy>
1531bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1532
1533 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1534 ConvertTo val;
1535 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1536 output = AssignTo{val};
1537 return retval;
1538 }
1539 output = AssignTo{};
1540 return true;
1541}
1542
1544template <class AssignTo, class ConvertTo, std::size_t I>
1545inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1546tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1547 return true;
1548}
1549
1551template <class AssignTo, class ConvertTo>
1552inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1553tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1554 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1555 strings.erase(strings.begin());
1556 return retval;
1557}
1558
1560template <class AssignTo, class ConvertTo>
1561inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1562 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1563 bool>::type
1564tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1565 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1566 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1567 return retval;
1568}
1569
1571template <class AssignTo, class ConvertTo>
1572inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1573 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1574 bool>::type
1575tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1576
1577 std::size_t index{subtype_count_min<ConvertTo>::value};
1578 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1579 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1580
1581 while(index < mx) {
1582 if(is_separator(strings[index])) {
1583 break;
1584 }
1585 ++index;
1586 }
1587 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1588 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1589 if(strings.size() > index) {
1590 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1591 } else {
1592 strings.clear();
1593 }
1594 return retval;
1595}
1596
1598template <class AssignTo, class ConvertTo, std::size_t I>
1599inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1600tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1601 bool retval = true;
1602 using ConvertToElement = typename std::
1603 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1604 if(!strings.empty()) {
1605 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1606 strings, std::get<I>(output));
1607 }
1608 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1609 return retval;
1610}
1611
1613template <class AssignTo,
1614 class ConvertTo,
1615 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1616 type_count_base<ConvertTo>::value == 2,
1617 detail::enabler>>
1618bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1619 output.clear();
1620 while(!strings.empty()) {
1621
1622 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1623 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1624 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1625 if(!strings.empty()) {
1626 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1627 }
1628 if(retval) {
1629 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1630 } else {
1631 return false;
1632 }
1633 }
1634 return (!output.empty());
1635}
1636
1638template <class AssignTo,
1639 class ConvertTo,
1640 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1641 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1642 type_count<ConvertTo>::value > 2),
1643 detail::enabler>>
1644bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1645 static_assert(
1646 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1647 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1648 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1649}
1650
1652template <class AssignTo,
1653 class ConvertTo,
1654 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1655 type_count_base<ConvertTo>::value != 2 &&
1656 ((type_count<ConvertTo>::value > 2) ||
1657 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1658 detail::enabler>>
1659bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1660 bool retval = true;
1661 output.clear();
1662 std::vector<std::string> temp;
1663 std::size_t ii{0};
1664 std::size_t icount{0};
1665 std::size_t xcm{type_count<ConvertTo>::value};
1666 auto ii_max = strings.size();
1667 while(ii < ii_max) {
1668 temp.push_back(strings[ii]);
1669 ++ii;
1670 ++icount;
1671 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1672 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1673 temp.pop_back();
1674 }
1675 typename AssignTo::value_type temp_out;
1676 retval = retval &&
1677 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1678 temp.clear();
1679 if(!retval) {
1680 return false;
1681 }
1682 output.insert(output.end(), std::move(temp_out));
1683 icount = 0;
1684 }
1685 }
1686 return retval;
1687}
1688
1690template <typename AssignTo,
1691 class ConvertTo,
1692 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1693 std::is_assignable<ConvertTo &, ConvertTo>::value,
1694 detail::enabler> = detail::dummy>
1695bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1696 if(strings.empty() || strings.front().empty()) {
1697 output = ConvertTo{};
1698 return true;
1699 }
1700 typename ConvertTo::value_type val;
1701 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1702 output = ConvertTo{val};
1703 return true;
1704 }
1705 return false;
1706}
1707
1709template <typename AssignTo,
1710 class ConvertTo,
1711 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1712 !std::is_assignable<AssignTo &, ConvertTo>::value,
1713 detail::enabler> = detail::dummy>
1714bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1715 using ConvertType = typename ConvertTo::value_type;
1716 if(strings.empty() || strings.front().empty()) {
1717 output = ConvertType{};
1718 return true;
1719 }
1720 ConvertType val;
1721 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1722 output = val;
1723 return true;
1724 }
1725 return false;
1726}
1727
1729inline std::string sum_string_vector(const std::vector<std::string> &values) {
1730 double val{0.0};
1731 bool fail{false};
1732 std::string output;
1733 for(const auto &arg : values) {
1734 double tv{0.0};
1735 auto comp = lexical_cast(arg, tv);
1736 if(!comp) {
1737 errno = 0;
1738 auto fv = detail::to_flag_value(arg);
1739 fail = (errno != 0);
1740 if(fail) {
1741 break;
1742 }
1743 tv = static_cast<double>(fv);
1744 }
1745 val += tv;
1746 }
1747 if(fail) {
1748 for(const auto &arg : values) {
1749 output.append(arg);
1750 }
1751 } else {
1752 std::ostringstream out;
1753 out.precision(16);
1754 out << val;
1755 output = out.str();
1756 }
1757 return output;
1758}
1759
1760} // namespace detail
1761// [CLI11:type_tools_hpp:end]
1762} // 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:292
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:437
Set of overloads to get the type size of an object.
Definition TypeTools.hpp:434
This will only trigger for actual void type.
Definition TypeTools.hpp:408
This will only trigger for actual void type.
Definition TypeTools.hpp:440
template to get the underlying value type if it exists or use a default
Definition TypeTools.hpp:398
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