File indexing completed on 2026-12-01 10:05:20
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009
0010
0011
0012 #include <algorithm>
0013 #include <cctype>
0014 #include <cerrno>
0015 #include <cmath>
0016 #include <cstdint>
0017 #include <cstdlib>
0018 #include <exception>
0019 #include <limits>
0020 #include <memory>
0021 #include <string>
0022 #include <type_traits>
0023 #include <utility>
0024 #include <vector>
0025
0026
0027 #include "Encoding.hpp"
0028 #include "StringTools.hpp"
0029
0030 namespace CLI {
0031
0032
0033
0034
0035
0036 namespace detail {
0037
0038
0039 enum class enabler : std::uint8_t {};
0040
0041
0042 CLI11_MODULE_INLINE constexpr enabler dummy = {};
0043 }
0044
0045
0046
0047
0048
0049
0050 template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
0051
0052
0053 template <typename... Ts> struct make_void {
0054 using type = void;
0055 };
0056
0057
0058 template <typename... Ts> using void_t = typename make_void<Ts...>::type;
0059
0060
0061 template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
0062
0063
0064 template <typename T> struct is_bool : std::false_type {};
0065
0066
0067 template <> struct is_bool<bool> : std::true_type {};
0068
0069
0070 template <typename T> struct is_shared_ptr : std::false_type {};
0071
0072
0073 template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
0074
0075
0076 template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
0077
0078
0079 template <typename T> struct is_copyable_ptr {
0080 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
0081 };
0082
0083
0084 template <typename T> struct IsMemberType {
0085 using type = T;
0086 };
0087
0088
0089 template <> struct IsMemberType<const char *> {
0090 using type = std::string;
0091 };
0092
0093 namespace adl_detail {
0094
0095
0096
0097
0098
0099 template <typename T, typename S = std::string> class is_lexical_castable {
0100 template <typename TT, typename SS>
0101 static auto test(int) -> decltype(lexical_cast(std::declval<const SS &>(), std::declval<TT &>()), std::true_type());
0102
0103 template <typename, typename> static auto test(...) -> std::false_type;
0104
0105 public:
0106 static constexpr bool value = decltype(test<T, S>(0))::value;
0107 };
0108 }
0109
0110 namespace detail {
0111
0112
0113
0114
0115
0116
0117
0118 template <typename T, typename Enable = void> struct element_type {
0119 using type = T;
0120 };
0121
0122 template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
0123 using type = typename std::pointer_traits<T>::element_type;
0124 };
0125
0126
0127
0128 template <typename T> struct element_value_type {
0129 using type = typename element_type<T>::type::value_type;
0130 };
0131
0132
0133 template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
0134 using value_type = typename T::value_type;
0135 using first_type = typename std::remove_const<value_type>::type;
0136 using second_type = typename std::remove_const<value_type>::type;
0137
0138
0139 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
0140 return std::forward<Q>(pair_value);
0141 }
0142
0143 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
0144 return std::forward<Q>(pair_value);
0145 }
0146 };
0147
0148
0149
0150 template <typename T>
0151 struct pair_adaptor<
0152 T,
0153 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
0154 : std::true_type {
0155 using value_type = typename T::value_type;
0156 using first_type = typename std::remove_const<typename value_type::first_type>::type;
0157 using second_type = typename std::remove_const<typename value_type::second_type>::type;
0158
0159
0160 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
0161 return std::get<0>(std::forward<Q>(pair_value));
0162 }
0163
0164 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
0165 return std::get<1>(std::forward<Q>(pair_value));
0166 }
0167 };
0168
0169
0170
0171
0172
0173
0174
0175 #ifdef __GNUC__
0176 #pragma GCC diagnostic push
0177 #pragma GCC diagnostic ignored "-Wnarrowing"
0178 #endif
0179
0180 template <typename T, typename C> class is_direct_constructible {
0181 template <typename TT, typename CC>
0182 static auto test(int, std::true_type) -> decltype(
0183
0184 #ifdef __CUDACC__
0185 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
0186 #pragma nv_diag_suppress 2361
0187 #else
0188 #pragma diag_suppress 2361
0189 #endif
0190 #endif
0191 TT{std::declval<CC>()}
0192 #ifdef __CUDACC__
0193 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
0194 #pragma nv_diag_default 2361
0195 #else
0196 #pragma diag_default 2361
0197 #endif
0198 #endif
0199 ,
0200 std::is_move_assignable<TT>());
0201
0202 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
0203
0204 template <typename, typename> static auto test(...) -> std::false_type;
0205
0206 public:
0207 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
0208 };
0209 #ifdef __GNUC__
0210 #pragma GCC diagnostic pop
0211 #endif
0212
0213
0214
0215
0216 template <typename T, typename S = std::ostringstream> class is_ostreamable {
0217 template <typename TT, typename SS>
0218 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
0219
0220 template <typename, typename> static auto test(...) -> std::false_type;
0221
0222 public:
0223 static constexpr bool value = decltype(test<T, S>(0))::value;
0224 };
0225
0226
0227 template <typename T, typename S = std::istringstream> class is_istreamable {
0228 template <typename TT, typename SS>
0229 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
0230
0231 template <typename, typename> static auto test(...) -> std::false_type;
0232
0233 public:
0234 static constexpr bool value = decltype(test<T, S>(0))::value;
0235 };
0236
0237
0238 template <typename T> class is_complex {
0239 template <typename TT>
0240 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
0241
0242 template <typename> static auto test(...) -> std::false_type;
0243
0244 public:
0245 static constexpr bool value = decltype(test<T>(0))::value;
0246 };
0247
0248
0249 template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
0250 bool from_stream(const std::string &istring, T &obj) {
0251 std::istringstream is;
0252 is.str(istring);
0253 is >> obj;
0254 return !is.fail() && !is.rdbuf()->in_avail();
0255 }
0256
0257 template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
0258 bool from_stream(const std::string & , T & ) {
0259 return false;
0260 }
0261
0262
0263 template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
0264
0265
0266
0267
0268 template <typename T>
0269 struct is_mutable_container<
0270 T,
0271 conditional_t<false,
0272 void_t<typename T::value_type,
0273 decltype(std::declval<T>().end()),
0274 decltype(std::declval<T>().clear()),
0275 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
0276 std::declval<const typename T::value_type &>()))>,
0277 void>> : public conditional_t<std::is_constructible<T, std::string>::value ||
0278 std::is_constructible<T, std::wstring>::value,
0279 std::false_type,
0280 std::true_type> {};
0281
0282
0283 template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
0284
0285
0286
0287 template <typename T>
0288 struct is_readable_container<
0289 T,
0290 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
0291 : public std::true_type {};
0292
0293
0294 template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
0295
0296
0297 template <typename T>
0298 struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
0299
0300
0301
0302 template <typename S> class is_tuple_like {
0303 template <typename SS, enable_if_t<!is_complex<SS>::value, detail::enabler> = detail::dummy>
0304
0305
0306 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
0307 template <typename> static auto test(...) -> std::false_type;
0308
0309 public:
0310 static constexpr bool value = decltype(test<S>(0))::value;
0311 };
0312
0313
0314 template <typename T, typename Enable = void> struct type_count_base {
0315 static const int value{0};
0316 };
0317
0318
0319 template <typename T>
0320 struct type_count_base<T,
0321 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
0322 !std::is_void<T>::value>::type> {
0323 static constexpr int value{1};
0324 };
0325
0326
0327 template <typename T>
0328 struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
0329 static constexpr int value{
0330 std::tuple_size<typename std::decay<T>::type>::value};
0331 };
0332
0333
0334 template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0335 static constexpr int value{type_count_base<typename T::value_type>::value};
0336 };
0337
0338
0339 template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
0340 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
0341 return std::forward<T>(value);
0342 }
0343
0344
0345 template <typename T,
0346 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
0347 detail::enabler> = detail::dummy>
0348 std::string to_string(T &&value) {
0349 return std::string(value);
0350 }
0351
0352
0353 template <typename T,
0354 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0355 is_ostreamable<T>::value,
0356 detail::enabler> = detail::dummy>
0357 std::string to_string(T &&value) {
0358 std::stringstream stream;
0359 stream << value;
0360 return stream.str();
0361 }
0362
0363
0364
0365
0366 template <typename T,
0367 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0368 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
0369 detail::enabler> = detail::dummy>
0370 inline std::string to_string(T &&value);
0371
0372
0373 template <typename T,
0374 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0375 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
0376 detail::enabler> = detail::dummy>
0377 inline std::string to_string(T &&value);
0378
0379
0380 template <
0381 typename T,
0382 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0383 !is_ostreamable<T>::value && !is_readable_container<typename std::remove_const<T>::type>::value &&
0384 !is_tuple_like<T>::value,
0385 detail::enabler> = detail::dummy>
0386 inline std::string to_string(T &&) {
0387 return {};
0388 }
0389
0390
0391 template <typename T,
0392 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0393 !is_ostreamable<T>::value && is_readable_container<T>::value && !is_tuple_like<T>::value,
0394 detail::enabler> = detail::dummy>
0395 inline std::string to_string(T &&variable) {
0396 auto cval = variable.begin();
0397 auto end = variable.end();
0398 if(cval == end) {
0399 return {"{}"};
0400 }
0401 std::vector<std::string> defaults;
0402 while(cval != end) {
0403 defaults.emplace_back(CLI::detail::to_string(*cval));
0404 ++cval;
0405 }
0406 return {"[" + detail::join(defaults) + "]"};
0407 }
0408
0409
0410
0411
0412 template <typename T, std::size_t I>
0413 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && );
0414
0415
0416 template <typename T, std::size_t I>
0417 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value);
0418
0419
0420 template <typename T,
0421 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0422 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
0423 detail::enabler>>
0424 inline std::string to_string(T &&value) {
0425 return to_string(std::get<0>(value));
0426 }
0427
0428
0429 template <typename T,
0430 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0431 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
0432 detail::enabler>>
0433 inline std::string to_string(T &&value) {
0434 auto tname = std::string(1, '[') + tuple_value_string<T, 0>(value);
0435 tname.push_back(']');
0436 return tname;
0437 }
0438
0439
0440 template <typename T, std::size_t I>
0441 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && ) {
0442 return std::string{};
0443 }
0444
0445
0446 template <typename T, std::size_t I>
0447 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value) {
0448 auto str = std::string{to_string(std::get<I>(value))} + ',' + tuple_value_string<T, I + 1>(value);
0449 if(str.back() == ',')
0450 str.pop_back();
0451 return str;
0452 }
0453
0454
0455 template <typename T1,
0456 typename T2,
0457 typename T,
0458 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0459 auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
0460 return to_string(std::forward<T>(value));
0461 }
0462
0463
0464 template <typename T1,
0465 typename T2,
0466 typename T,
0467 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0468 std::string checked_to_string(T &&) {
0469 return std::string{};
0470 }
0471
0472 template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0473 std::string value_string(const T &value) {
0474 return std::to_string(value);
0475 }
0476
0477 template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
0478 std::string value_string(const T &value) {
0479 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
0480 }
0481
0482 template <typename T,
0483 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0484 auto value_string(const T &value) -> decltype(to_string(value)) {
0485 return to_string(value);
0486 }
0487
0488
0489 template <typename T, typename def, typename Enable = void> struct wrapped_type {
0490 using type = def;
0491 };
0492
0493
0494 template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
0495 using type = typename T::value_type;
0496 };
0497
0498
0499
0500
0501 template <typename T> struct subtype_count;
0502
0503
0504 template <typename T> struct subtype_count_min;
0505
0506
0507 template <typename T, typename Enable = void> struct type_count {
0508 static const int value{0};
0509 };
0510
0511
0512 template <typename T>
0513 struct type_count<T,
0514 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
0515 !std::is_void<T>::value>::type> {
0516 static constexpr int value{1};
0517 };
0518
0519
0520 template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
0521 static constexpr int value{2};
0522 };
0523
0524
0525 template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0526 static constexpr int value{subtype_count<typename T::value_type>::value};
0527 };
0528
0529
0530 template <typename T>
0531 struct type_count<T,
0532 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
0533 !is_mutable_container<T>::value>::type> {
0534 static constexpr int value{type_count<typename T::value_type>::value};
0535 };
0536
0537
0538 template <typename T, std::size_t I>
0539 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
0540 return 0;
0541 }
0542
0543
0544 template <typename T, std::size_t I>
0545 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
0546 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
0547 }
0548
0549
0550 template <typename T>
0551 struct type_count<T, typename std::enable_if<is_tuple_like<T>::value && !is_complex<T>::value>::type> {
0552 static constexpr int value{tuple_type_size<T, 0>()};
0553 };
0554
0555
0556 template <typename T> struct subtype_count {
0557 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
0558 };
0559
0560
0561 template <typename T, typename Enable = void> struct type_count_min {
0562 static const int value{0};
0563 };
0564
0565
0566 template <typename T>
0567 struct type_count_min<
0568 T,
0569 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
0570 !is_complex<T>::value && !std::is_void<T>::value>::type> {
0571 static constexpr int value{type_count<T>::value};
0572 };
0573
0574
0575 template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
0576 static constexpr int value{1};
0577 };
0578
0579
0580 template <typename T>
0581 struct type_count_min<
0582 T,
0583 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
0584 static constexpr int value{subtype_count_min<typename T::value_type>::value};
0585 };
0586
0587
0588 template <typename T, std::size_t I>
0589 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
0590 return 0;
0591 }
0592
0593
0594 template <typename T, std::size_t I>
0595 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
0596 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
0597 }
0598
0599
0600 template <typename T>
0601 struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value && !is_complex<T>::value>::type> {
0602 static constexpr int value{tuple_type_size_min<T, 0>()};
0603 };
0604
0605
0606 template <typename T> struct subtype_count_min {
0607 static constexpr int value{is_mutable_container<T>::value
0608 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
0609 : type_count_min<T>::value};
0610 };
0611
0612
0613 template <typename T, typename Enable = void> struct expected_count {
0614 static const int value{0};
0615 };
0616
0617
0618 template <typename T>
0619 struct expected_count<T,
0620 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
0621 !std::is_void<T>::value>::type> {
0622 static constexpr int value{1};
0623 };
0624
0625 template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0626 static constexpr int value{expected_max_vector_size};
0627 };
0628
0629
0630 template <typename T>
0631 struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
0632 static constexpr int value{expected_count<typename T::value_type>::value};
0633 };
0634
0635
0636 enum class object_category : std::uint8_t {
0637 char_value = 1,
0638 integral_value = 2,
0639 unsigned_integral = 4,
0640 enumeration = 6,
0641 boolean_value = 8,
0642 floating_point = 10,
0643 number_constructible = 12,
0644 double_constructible = 14,
0645 integer_constructible = 16,
0646
0647 string_assignable = 23,
0648 string_constructible = 24,
0649 wstring_assignable = 25,
0650 wstring_constructible = 26,
0651 other = 45,
0652
0653 wrapper_value = 50,
0654 complex_number = 60,
0655 tuple_value = 70,
0656 container_value = 80,
0657
0658 };
0659
0660
0661
0662
0663 template <typename T, typename Enable = void> struct classify_object {
0664 static constexpr object_category value{object_category::other};
0665 };
0666
0667
0668 template <typename T>
0669 struct classify_object<
0670 T,
0671 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
0672 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
0673 static constexpr object_category value{object_category::integral_value};
0674 };
0675
0676
0677 template <typename T>
0678 struct classify_object<T,
0679 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
0680 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
0681 static constexpr object_category value{object_category::unsigned_integral};
0682 };
0683
0684
0685 template <typename T>
0686 struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
0687 static constexpr object_category value{object_category::char_value};
0688 };
0689
0690
0691 template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
0692 static constexpr object_category value{object_category::boolean_value};
0693 };
0694
0695
0696 template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
0697 static constexpr object_category value{object_category::floating_point};
0698 };
0699 #if defined _MSC_VER
0700
0701
0702 #define WIDE_STRING_CHECK \
0703 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
0704 #define STRING_CHECK true
0705 #else
0706 #define WIDE_STRING_CHECK true
0707 #define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
0708 #endif
0709
0710
0711 template <typename T>
0712 struct classify_object<
0713 T,
0714 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
0715 std::is_assignable<T &, std::string>::value>::type> {
0716 static constexpr object_category value{object_category::string_assignable};
0717 };
0718
0719
0720 template <typename T>
0721 struct classify_object<
0722 T,
0723 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0724 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
0725 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
0726 static constexpr object_category value{object_category::string_constructible};
0727 };
0728
0729
0730 template <typename T>
0731 struct classify_object<T,
0732 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0733 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
0734 static constexpr object_category value{object_category::wstring_assignable};
0735 };
0736
0737 template <typename T>
0738 struct classify_object<
0739 T,
0740 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0741 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
0742 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
0743 static constexpr object_category value{object_category::wstring_constructible};
0744 };
0745
0746
0747 template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
0748 static constexpr object_category value{object_category::enumeration};
0749 };
0750
0751 template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
0752 static constexpr object_category value{object_category::complex_number};
0753 };
0754
0755
0756
0757 template <typename T> struct uncommon_type {
0758 using type = typename std::conditional<
0759 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0760 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
0761 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
0762 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
0763 std::true_type,
0764 std::false_type>::type;
0765 static constexpr bool value = type::value;
0766 };
0767
0768
0769 template <typename T>
0770 struct classify_object<T,
0771 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
0772 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
0773 static constexpr object_category value{object_category::wrapper_value};
0774 };
0775
0776
0777 template <typename T>
0778 struct classify_object<T,
0779 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0780 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0781 is_direct_constructible<T, int>::value>::type> {
0782 static constexpr object_category value{object_category::number_constructible};
0783 };
0784
0785
0786 template <typename T>
0787 struct classify_object<T,
0788 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0789 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
0790 is_direct_constructible<T, int>::value>::type> {
0791 static constexpr object_category value{object_category::integer_constructible};
0792 };
0793
0794
0795 template <typename T>
0796 struct classify_object<T,
0797 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0798 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0799 !is_direct_constructible<T, int>::value>::type> {
0800 static constexpr object_category value{object_category::double_constructible};
0801 };
0802
0803
0804 template <typename T>
0805 struct classify_object<
0806 T,
0807 typename std::enable_if<is_tuple_like<T>::value &&
0808 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
0809 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
0810 !is_direct_constructible<T, int>::value) ||
0811 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
0812 static constexpr object_category value{object_category::tuple_value};
0813
0814
0815
0816
0817
0818 };
0819
0820
0821 template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0822 static constexpr object_category value{object_category::container_value};
0823 };
0824
0825
0826
0827
0828
0829
0830
0831 template <typename T,
0832 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
0833 constexpr const char *type_name() {
0834 return "CHAR";
0835 }
0836
0837 template <typename T,
0838 enable_if_t<classify_object<T>::value == object_category::integral_value ||
0839 classify_object<T>::value == object_category::integer_constructible,
0840 detail::enabler> = detail::dummy>
0841 constexpr const char *type_name() {
0842 return "INT";
0843 }
0844
0845 template <typename T,
0846 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
0847 constexpr const char *type_name() {
0848 return "UINT";
0849 }
0850
0851 template <typename T,
0852 enable_if_t<classify_object<T>::value == object_category::floating_point ||
0853 classify_object<T>::value == object_category::number_constructible ||
0854 classify_object<T>::value == object_category::double_constructible,
0855 detail::enabler> = detail::dummy>
0856 constexpr const char *type_name() {
0857 return "FLOAT";
0858 }
0859
0860
0861 template <typename T,
0862 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
0863 constexpr const char *type_name() {
0864 return "ENUM";
0865 }
0866
0867
0868 template <typename T,
0869 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
0870 constexpr const char *type_name() {
0871 return "BOOLEAN";
0872 }
0873
0874
0875 template <typename T,
0876 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
0877 constexpr const char *type_name() {
0878 return "COMPLEX";
0879 }
0880
0881
0882 template <typename T,
0883 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
0884 classify_object<T>::value <= object_category::other,
0885 detail::enabler> = detail::dummy>
0886 constexpr const char *type_name() {
0887 return "TEXT";
0888 }
0889
0890 template <typename T,
0891 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0892 detail::enabler> = detail::dummy>
0893 std::string type_name();
0894
0895
0896 template <typename T,
0897 enable_if_t<classify_object<T>::value == object_category::container_value ||
0898 classify_object<T>::value == object_category::wrapper_value,
0899 detail::enabler> = detail::dummy>
0900 std::string type_name();
0901
0902
0903 template <typename T,
0904 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
0905 detail::enabler> = detail::dummy>
0906 inline std::string type_name() {
0907 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
0908 }
0909
0910
0911 template <typename T, std::size_t I>
0912 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
0913 return std::string{};
0914 }
0915
0916
0917 template <typename T, std::size_t I>
0918 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
0919 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
0920 tuple_name<T, I + 1>();
0921 if(str.back() == ',')
0922 str.pop_back();
0923 return str;
0924 }
0925
0926
0927 template <typename T,
0928 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0929 detail::enabler>>
0930 inline std::string type_name() {
0931 auto tname = std::string(1, '[') + tuple_name<T, 0>();
0932 tname.push_back(']');
0933 return tname;
0934 }
0935
0936
0937 template <typename T,
0938 enable_if_t<classify_object<T>::value == object_category::container_value ||
0939 classify_object<T>::value == object_category::wrapper_value,
0940 detail::enabler>>
0941 inline std::string type_name() {
0942 return type_name<typename T::value_type>();
0943 }
0944
0945
0946
0947
0948 template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
0949 bool integral_conversion(const std::string &input, T &output) noexcept {
0950 if(input.empty()) {
0951 return false;
0952 }
0953
0954
0955 auto first_non_ws = input.find_first_not_of(" \t\n\v\f\r");
0956 if(first_non_ws != std::string::npos && input[first_non_ws] == '-') {
0957 return false;
0958 }
0959 char *val{nullptr};
0960 errno = 0;
0961 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
0962 if(errno == ERANGE) {
0963 return false;
0964 }
0965 output = static_cast<T>(output_ll);
0966 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
0967 return true;
0968 }
0969 val = nullptr;
0970 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
0971 if(val == (input.c_str() + input.size())) {
0972 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
0973 return (static_cast<std::int64_t>(output) == output_sll);
0974 }
0975
0976 auto group_separators = get_group_separators();
0977 if(input.find_first_of(group_separators) != std::string::npos) {
0978 std::string nstring = input;
0979 for(auto &separator : group_separators) {
0980 if(input.find_first_of(separator) != std::string::npos) {
0981 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
0982 }
0983 }
0984 return integral_conversion(nstring, output);
0985 }
0986
0987 if(std::isspace(static_cast<unsigned char>(input.back()))) {
0988 return integral_conversion(trim_copy(input), output);
0989 }
0990 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
0991 val = nullptr;
0992 errno = 0;
0993 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
0994 if(errno == ERANGE) {
0995 return false;
0996 }
0997 output = static_cast<T>(output_ll);
0998 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0999 }
1000 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1001
1002
1003
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
1013 }
1014 return false;
1015 }
1016
1017
1018 template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1019 bool 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
1035 output = static_cast<T>(1);
1036 return true;
1037 }
1038
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
1064
1065
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
1075 }
1076 return false;
1077 }
1078
1079
1080 inline 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
1127
1128 template <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>
1132 bool lexical_cast(const std::string &input, T &output) {
1133 return integral_conversion(input, output);
1134 }
1135
1136
1137 template <typename T,
1138 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1139 bool 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
1146
1147 bool result = integral_conversion(input, res);
1148 if(result) {
1149 output = static_cast<T>(res);
1150 }
1151 return result;
1152 }
1153
1154
1155 template <typename T,
1156 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1157 bool 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
1170
1171 template <typename T,
1172 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1173 bool 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
1180
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
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
1209
1210 template <typename T,
1211 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1212 bool 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
1241
1242 template <typename T,
1243 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1244 bool lexical_cast(const std::string &input, T &output) {
1245 output = input;
1246 return true;
1247 }
1248
1249
1250 template <
1251 typename T,
1252 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1253 bool lexical_cast(const std::string &input, T &output) {
1254 output = T(input);
1255 return true;
1256 }
1257
1258
1259 template <
1260 typename T,
1261 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1262 bool lexical_cast(const std::string &input, T &output) {
1263 output = widen(input);
1264 return true;
1265 }
1266
1267 template <
1268 typename T,
1269 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1270 bool lexical_cast(const std::string &input, T &output) {
1271 output = T{widen(input)};
1272 return true;
1273 }
1274
1275
1276 template <typename T,
1277 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1278 bool 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
1287
1288 template <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>
1292 bool 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
1301 template <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>
1305 bool 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
1314
1315 template <
1316 typename T,
1317 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1318 bool 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
1334
1335 template <
1336 typename T,
1337 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1338 bool 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
1347
1348 template <
1349 typename T,
1350 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1351 bool 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
1360
1361 template <typename T,
1362 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1363 detail::enabler> = detail::dummy>
1364 bool 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
1372
1373 output = val;
1374 #ifdef _MSC_VER
1375 #pragma warning(pop)
1376 #endif
1377 return true;
1378 }
1379
1380
1381
1382 return from_stream(input, output);
1383
1384 }
1385
1386
1387 template <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>
1391 bool lexical_cast(const std::string &input, T &output) {
1392 return from_stream(input, output);
1393 }
1394
1395
1396
1397 template <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>
1401 bool lexical_cast(const std::string & , T & ) {
1402 static_assert(!std::is_same<T, T>::value,
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
1408
1409
1410 template <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>
1418 bool lexical_assign(const std::string &input, AssignTo &output) {
1419 return lexical_cast(input, output);
1420 }
1421
1422
1423
1424 template <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>
1432 bool 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
1440
1441 template <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>
1449 bool 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 }
1457
1458
1459 template <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>
1464 bool 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
1473
1474
1475 template <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>
1481 bool 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
1490 #pragma clang diagnostic push
1491 #pragma clang diagnostic ignored "-Wsign-conversion"
1492 #elif defined(__GNUC__) && (__GNUC__ == 8)
1493
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
1508
1509 template <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>
1513 bool 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
1522
1523 template <
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>
1529 bool 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);
1534 }
1535 return parse_result;
1536 }
1537
1538
1539 template <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>
1544 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1545 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1546 }
1547
1548
1549
1550 template <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>
1555 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1556
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
1569
1570 template <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>
1575 bool 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
1601
1602 template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1603 bool 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
1621
1622 template <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>
1627 bool 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
1640
1641
1642 template <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>
1647 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1648
1649
1650 template <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>
1657 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1658
1659
1660 template <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>
1666 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1667
1668
1669
1670 template <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>
1676 bool 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
1688
1689 template <class AssignTo, class ConvertTo, std::size_t I>
1690 inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1691 tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1692 return true;
1693 }
1694
1695
1696 template <class AssignTo, class ConvertTo>
1697 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1698 tuple_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
1704
1705 template <class AssignTo, class ConvertTo>
1706 inline 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
1709 tuple_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
1715
1716 template <class AssignTo, class ConvertTo>
1717 inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1718 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1719 bool>::type
1720 tuple_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
1742
1743 template <class AssignTo, class ConvertTo, std::size_t I>
1744 inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1745 tuple_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
1757
1758 template <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>>
1763 bool 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
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
1785
1786 template <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>>
1792 bool 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
1799
1800 template <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>>
1807 bool 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
1837
1838 template <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>
1843 bool 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
1856
1857 template <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>
1862 bool 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
1876
1877 inline std::string sum_string_vector(const std::vector<std::string> &values) {
1878
1879
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
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 }
1931
1932 }