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