File indexing completed on 2025-07-09 08:29:25
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009
0010
0011
0012 #include <algorithm>
0013 #include <cmath>
0014 #include <cstdint>
0015 #include <exception>
0016 #include <limits>
0017 #include <memory>
0018 #include <string>
0019 #include <type_traits>
0020 #include <utility>
0021 #include <vector>
0022
0023
0024 #include "Encoding.hpp"
0025 #include "StringTools.hpp"
0026
0027 namespace CLI {
0028
0029
0030
0031
0032
0033 namespace detail {
0034
0035
0036 enum class enabler {};
0037
0038
0039 constexpr enabler dummy = {};
0040 }
0041
0042
0043
0044
0045
0046
0047 template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
0048
0049
0050 template <typename... Ts> struct make_void {
0051 using type = void;
0052 };
0053
0054
0055 template <typename... Ts> using void_t = typename make_void<Ts...>::type;
0056
0057
0058 template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
0059
0060
0061 template <typename T> struct is_bool : std::false_type {};
0062
0063
0064 template <> struct is_bool<bool> : std::true_type {};
0065
0066
0067 template <typename T> struct is_shared_ptr : std::false_type {};
0068
0069
0070 template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
0071
0072
0073 template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
0074
0075
0076 template <typename T> struct is_copyable_ptr {
0077 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
0078 };
0079
0080
0081 template <typename T> struct IsMemberType {
0082 using type = T;
0083 };
0084
0085
0086 template <> struct IsMemberType<const char *> {
0087 using type = std::string;
0088 };
0089
0090 namespace adl_detail {
0091
0092
0093
0094
0095
0096 template <typename T, typename S = std::string> class is_lexical_castable {
0097 template <typename TT, typename SS>
0098 static auto test(int) -> decltype(lexical_cast(std::declval<const SS &>(), std::declval<TT &>()), std::true_type());
0099
0100 template <typename, typename> static auto test(...) -> std::false_type;
0101
0102 public:
0103 static constexpr bool value = decltype(test<T, S>(0))::value;
0104 };
0105 }
0106
0107 namespace detail {
0108
0109
0110
0111
0112
0113
0114
0115 template <typename T, typename Enable = void> struct element_type {
0116 using type = T;
0117 };
0118
0119 template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
0120 using type = typename std::pointer_traits<T>::element_type;
0121 };
0122
0123
0124
0125 template <typename T> struct element_value_type {
0126 using type = typename element_type<T>::type::value_type;
0127 };
0128
0129
0130 template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
0131 using value_type = typename T::value_type;
0132 using first_type = typename std::remove_const<value_type>::type;
0133 using second_type = typename std::remove_const<value_type>::type;
0134
0135
0136 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
0137 return std::forward<Q>(pair_value);
0138 }
0139
0140 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
0141 return std::forward<Q>(pair_value);
0142 }
0143 };
0144
0145
0146
0147 template <typename T>
0148 struct pair_adaptor<
0149 T,
0150 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
0151 : std::true_type {
0152 using value_type = typename T::value_type;
0153 using first_type = typename std::remove_const<typename value_type::first_type>::type;
0154 using second_type = typename std::remove_const<typename value_type::second_type>::type;
0155
0156
0157 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
0158 return std::get<0>(std::forward<Q>(pair_value));
0159 }
0160
0161 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
0162 return std::get<1>(std::forward<Q>(pair_value));
0163 }
0164 };
0165
0166
0167
0168
0169
0170
0171
0172 #ifdef __GNUC__
0173 #pragma GCC diagnostic push
0174 #pragma GCC diagnostic ignored "-Wnarrowing"
0175 #endif
0176
0177 template <typename T, typename C> class is_direct_constructible {
0178 template <typename TT, typename CC>
0179 static auto test(int, std::true_type) -> decltype(
0180
0181 #ifdef __CUDACC__
0182 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
0183 #pragma nv_diag_suppress 2361
0184 #else
0185 #pragma diag_suppress 2361
0186 #endif
0187 #endif
0188 TT{std::declval<CC>()}
0189 #ifdef __CUDACC__
0190 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
0191 #pragma nv_diag_default 2361
0192 #else
0193 #pragma diag_default 2361
0194 #endif
0195 #endif
0196 ,
0197 std::is_move_assignable<TT>());
0198
0199 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
0200
0201 template <typename, typename> static auto test(...) -> std::false_type;
0202
0203 public:
0204 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
0205 };
0206 #ifdef __GNUC__
0207 #pragma GCC diagnostic pop
0208 #endif
0209
0210
0211
0212
0213 template <typename T, typename S = std::ostringstream> class is_ostreamable {
0214 template <typename TT, typename SS>
0215 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
0216
0217 template <typename, typename> static auto test(...) -> std::false_type;
0218
0219 public:
0220 static constexpr bool value = decltype(test<T, S>(0))::value;
0221 };
0222
0223
0224 template <typename T, typename S = std::istringstream> class is_istreamable {
0225 template <typename TT, typename SS>
0226 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
0227
0228 template <typename, typename> static auto test(...) -> std::false_type;
0229
0230 public:
0231 static constexpr bool value = decltype(test<T, S>(0))::value;
0232 };
0233
0234
0235 template <typename T> class is_complex {
0236 template <typename TT>
0237 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
0238
0239 template <typename> static auto test(...) -> std::false_type;
0240
0241 public:
0242 static constexpr bool value = decltype(test<T>(0))::value;
0243 };
0244
0245
0246 template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
0247 bool from_stream(const std::string &istring, T &obj) {
0248 std::istringstream is;
0249 is.str(istring);
0250 is >> obj;
0251 return !is.fail() && !is.rdbuf()->in_avail();
0252 }
0253
0254 template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
0255 bool from_stream(const std::string & , T & ) {
0256 return false;
0257 }
0258
0259
0260 template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
0261
0262
0263
0264
0265 template <typename T>
0266 struct is_mutable_container<
0267 T,
0268 conditional_t<false,
0269 void_t<typename T::value_type,
0270 decltype(std::declval<T>().end()),
0271 decltype(std::declval<T>().clear()),
0272 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
0273 std::declval<const typename T::value_type &>()))>,
0274 void>> : public conditional_t<std::is_constructible<T, std::string>::value ||
0275 std::is_constructible<T, std::wstring>::value,
0276 std::false_type,
0277 std::true_type> {};
0278
0279
0280 template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
0281
0282
0283
0284
0285 template <typename T>
0286 struct is_readable_container<
0287 T,
0288 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
0289 : public std::true_type {};
0290
0291
0292 template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
0293
0294
0295 template <typename T>
0296 struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
0297
0298
0299 template <typename S> class is_tuple_like {
0300 template <typename SS>
0301
0302
0303 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
0304 template <typename> static auto test(...) -> std::false_type;
0305
0306 public:
0307 static constexpr bool value = decltype(test<S>(0))::value;
0308 };
0309
0310
0311 template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
0312 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
0313 return std::forward<T>(value);
0314 }
0315
0316
0317 template <typename T,
0318 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
0319 detail::enabler> = detail::dummy>
0320 std::string to_string(const T &value) {
0321 return std::string(value);
0322 }
0323
0324
0325 template <typename T,
0326 enable_if_t<!std::is_convertible<std::string, T>::value && !std::is_constructible<std::string, T>::value &&
0327 is_ostreamable<T>::value,
0328 detail::enabler> = detail::dummy>
0329 std::string to_string(T &&value) {
0330 std::stringstream stream;
0331 stream << value;
0332 return stream.str();
0333 }
0334
0335
0336 template <typename T,
0337 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
0338 !is_readable_container<typename std::remove_const<T>::type>::value,
0339 detail::enabler> = detail::dummy>
0340 std::string to_string(T &&) {
0341 return {};
0342 }
0343
0344
0345 template <typename T,
0346 enable_if_t<!std::is_constructible<std::string, T>::value && !is_ostreamable<T>::value &&
0347 is_readable_container<T>::value,
0348 detail::enabler> = detail::dummy>
0349 std::string to_string(T &&variable) {
0350 auto cval = variable.begin();
0351 auto end = variable.end();
0352 if(cval == end) {
0353 return {"{}"};
0354 }
0355 std::vector<std::string> defaults;
0356 while(cval != end) {
0357 defaults.emplace_back(CLI::detail::to_string(*cval));
0358 ++cval;
0359 }
0360 return {"[" + detail::join(defaults) + "]"};
0361 }
0362
0363
0364 template <typename T1,
0365 typename T2,
0366 typename T,
0367 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0368 auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
0369 return to_string(std::forward<T>(value));
0370 }
0371
0372
0373 template <typename T1,
0374 typename T2,
0375 typename T,
0376 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0377 std::string checked_to_string(T &&) {
0378 return std::string{};
0379 }
0380
0381 template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0382 std::string value_string(const T &value) {
0383 return std::to_string(value);
0384 }
0385
0386 template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
0387 std::string value_string(const T &value) {
0388 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
0389 }
0390
0391 template <typename T,
0392 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0393 auto value_string(const T &value) -> decltype(to_string(value)) {
0394 return to_string(value);
0395 }
0396
0397
0398 template <typename T, typename def, typename Enable = void> struct wrapped_type {
0399 using type = def;
0400 };
0401
0402
0403 template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
0404 using type = typename T::value_type;
0405 };
0406
0407
0408 template <typename T, typename Enable = void> struct type_count_base {
0409 static const int value{0};
0410 };
0411
0412
0413 template <typename T>
0414 struct type_count_base<T,
0415 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
0416 !std::is_void<T>::value>::type> {
0417 static constexpr int value{1};
0418 };
0419
0420
0421 template <typename T>
0422 struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
0423 static constexpr int value{std::tuple_size<T>::value};
0424 };
0425
0426
0427 template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0428 static constexpr int value{type_count_base<typename T::value_type>::value};
0429 };
0430
0431
0432
0433
0434 template <typename T> struct subtype_count;
0435
0436
0437 template <typename T> struct subtype_count_min;
0438
0439
0440 template <typename T, typename Enable = void> struct type_count {
0441 static const int value{0};
0442 };
0443
0444
0445 template <typename T>
0446 struct type_count<T,
0447 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
0448 !std::is_void<T>::value>::type> {
0449 static constexpr int value{1};
0450 };
0451
0452
0453 template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
0454 static constexpr int value{2};
0455 };
0456
0457
0458 template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0459 static constexpr int value{subtype_count<typename T::value_type>::value};
0460 };
0461
0462
0463 template <typename T>
0464 struct type_count<T,
0465 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
0466 !is_mutable_container<T>::value>::type> {
0467 static constexpr int value{type_count<typename T::value_type>::value};
0468 };
0469
0470
0471 template <typename T, std::size_t I>
0472 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
0473 return 0;
0474 }
0475
0476
0477 template <typename T, std::size_t I>
0478 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
0479 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
0480 }
0481
0482
0483 template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
0484 static constexpr int value{tuple_type_size<T, 0>()};
0485 };
0486
0487
0488 template <typename T> struct subtype_count {
0489 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
0490 };
0491
0492
0493 template <typename T, typename Enable = void> struct type_count_min {
0494 static const int value{0};
0495 };
0496
0497
0498 template <typename T>
0499 struct type_count_min<
0500 T,
0501 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
0502 !is_complex<T>::value && !std::is_void<T>::value>::type> {
0503 static constexpr int value{type_count<T>::value};
0504 };
0505
0506
0507 template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
0508 static constexpr int value{1};
0509 };
0510
0511
0512 template <typename T>
0513 struct type_count_min<
0514 T,
0515 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
0516 static constexpr int value{subtype_count_min<typename T::value_type>::value};
0517 };
0518
0519
0520 template <typename T, std::size_t I>
0521 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
0522 return 0;
0523 }
0524
0525
0526 template <typename T, std::size_t I>
0527 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
0528 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
0529 }
0530
0531
0532 template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
0533 static constexpr int value{tuple_type_size_min<T, 0>()};
0534 };
0535
0536
0537 template <typename T> struct subtype_count_min {
0538 static constexpr int value{is_mutable_container<T>::value
0539 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
0540 : type_count_min<T>::value};
0541 };
0542
0543
0544 template <typename T, typename Enable = void> struct expected_count {
0545 static const int value{0};
0546 };
0547
0548
0549 template <typename T>
0550 struct expected_count<T,
0551 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
0552 !std::is_void<T>::value>::type> {
0553 static constexpr int value{1};
0554 };
0555
0556 template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0557 static constexpr int value{expected_max_vector_size};
0558 };
0559
0560
0561 template <typename T>
0562 struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
0563 static constexpr int value{expected_count<typename T::value_type>::value};
0564 };
0565
0566
0567 enum class object_category : int {
0568 char_value = 1,
0569 integral_value = 2,
0570 unsigned_integral = 4,
0571 enumeration = 6,
0572 boolean_value = 8,
0573 floating_point = 10,
0574 number_constructible = 12,
0575 double_constructible = 14,
0576 integer_constructible = 16,
0577
0578 string_assignable = 23,
0579 string_constructible = 24,
0580 wstring_assignable = 25,
0581 wstring_constructible = 26,
0582 other = 45,
0583
0584 wrapper_value = 50,
0585 complex_number = 60,
0586 tuple_value = 70,
0587 container_value = 80,
0588
0589 };
0590
0591
0592
0593
0594 template <typename T, typename Enable = void> struct classify_object {
0595 static constexpr object_category value{object_category::other};
0596 };
0597
0598
0599 template <typename T>
0600 struct classify_object<
0601 T,
0602 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
0603 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
0604 static constexpr object_category value{object_category::integral_value};
0605 };
0606
0607
0608 template <typename T>
0609 struct classify_object<T,
0610 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
0611 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
0612 static constexpr object_category value{object_category::unsigned_integral};
0613 };
0614
0615
0616 template <typename T>
0617 struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
0618 static constexpr object_category value{object_category::char_value};
0619 };
0620
0621
0622 template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
0623 static constexpr object_category value{object_category::boolean_value};
0624 };
0625
0626
0627 template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
0628 static constexpr object_category value{object_category::floating_point};
0629 };
0630 #if defined _MSC_VER
0631
0632
0633 #define WIDE_STRING_CHECK \
0634 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
0635 #define STRING_CHECK true
0636 #else
0637 #define WIDE_STRING_CHECK true
0638 #define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
0639 #endif
0640
0641
0642 template <typename T>
0643 struct classify_object<
0644 T,
0645 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
0646 std::is_assignable<T &, std::string>::value>::type> {
0647 static constexpr object_category value{object_category::string_assignable};
0648 };
0649
0650
0651 template <typename T>
0652 struct classify_object<
0653 T,
0654 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0655 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
0656 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
0657 static constexpr object_category value{object_category::string_constructible};
0658 };
0659
0660
0661 template <typename T>
0662 struct classify_object<T,
0663 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0664 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
0665 static constexpr object_category value{object_category::wstring_assignable};
0666 };
0667
0668 template <typename T>
0669 struct classify_object<
0670 T,
0671 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0672 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
0673 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
0674 static constexpr object_category value{object_category::wstring_constructible};
0675 };
0676
0677
0678 template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
0679 static constexpr object_category value{object_category::enumeration};
0680 };
0681
0682 template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
0683 static constexpr object_category value{object_category::complex_number};
0684 };
0685
0686
0687
0688 template <typename T> struct uncommon_type {
0689 using type = typename std::conditional<
0690 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0691 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
0692 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
0693 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
0694 std::true_type,
0695 std::false_type>::type;
0696 static constexpr bool value = type::value;
0697 };
0698
0699
0700 template <typename T>
0701 struct classify_object<T,
0702 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
0703 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
0704 static constexpr object_category value{object_category::wrapper_value};
0705 };
0706
0707
0708 template <typename T>
0709 struct classify_object<T,
0710 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0711 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0712 is_direct_constructible<T, int>::value>::type> {
0713 static constexpr object_category value{object_category::number_constructible};
0714 };
0715
0716
0717 template <typename T>
0718 struct classify_object<T,
0719 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0720 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
0721 is_direct_constructible<T, int>::value>::type> {
0722 static constexpr object_category value{object_category::integer_constructible};
0723 };
0724
0725
0726 template <typename T>
0727 struct classify_object<T,
0728 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0729 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0730 !is_direct_constructible<T, int>::value>::type> {
0731 static constexpr object_category value{object_category::double_constructible};
0732 };
0733
0734
0735 template <typename T>
0736 struct classify_object<
0737 T,
0738 typename std::enable_if<is_tuple_like<T>::value &&
0739 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
0740 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
0741 !is_direct_constructible<T, int>::value) ||
0742 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
0743 static constexpr object_category value{object_category::tuple_value};
0744
0745
0746
0747
0748
0749 };
0750
0751
0752 template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0753 static constexpr object_category value{object_category::container_value};
0754 };
0755
0756
0757
0758
0759
0760
0761
0762 template <typename T,
0763 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
0764 constexpr const char *type_name() {
0765 return "CHAR";
0766 }
0767
0768 template <typename T,
0769 enable_if_t<classify_object<T>::value == object_category::integral_value ||
0770 classify_object<T>::value == object_category::integer_constructible,
0771 detail::enabler> = detail::dummy>
0772 constexpr const char *type_name() {
0773 return "INT";
0774 }
0775
0776 template <typename T,
0777 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
0778 constexpr const char *type_name() {
0779 return "UINT";
0780 }
0781
0782 template <typename T,
0783 enable_if_t<classify_object<T>::value == object_category::floating_point ||
0784 classify_object<T>::value == object_category::number_constructible ||
0785 classify_object<T>::value == object_category::double_constructible,
0786 detail::enabler> = detail::dummy>
0787 constexpr const char *type_name() {
0788 return "FLOAT";
0789 }
0790
0791
0792 template <typename T,
0793 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
0794 constexpr const char *type_name() {
0795 return "ENUM";
0796 }
0797
0798
0799 template <typename T,
0800 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
0801 constexpr const char *type_name() {
0802 return "BOOLEAN";
0803 }
0804
0805
0806 template <typename T,
0807 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
0808 constexpr const char *type_name() {
0809 return "COMPLEX";
0810 }
0811
0812
0813 template <typename T,
0814 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
0815 classify_object<T>::value <= object_category::other,
0816 detail::enabler> = detail::dummy>
0817 constexpr const char *type_name() {
0818 return "TEXT";
0819 }
0820
0821 template <typename T,
0822 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0823 detail::enabler> = detail::dummy>
0824 std::string type_name();
0825
0826
0827 template <typename T,
0828 enable_if_t<classify_object<T>::value == object_category::container_value ||
0829 classify_object<T>::value == object_category::wrapper_value,
0830 detail::enabler> = detail::dummy>
0831 std::string type_name();
0832
0833
0834 template <typename T,
0835 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
0836 detail::enabler> = detail::dummy>
0837 inline std::string type_name() {
0838 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
0839 }
0840
0841
0842 template <typename T, std::size_t I>
0843 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
0844 return std::string{};
0845 }
0846
0847
0848 template <typename T, std::size_t I>
0849 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
0850 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
0851 tuple_name<T, I + 1>();
0852 if(str.back() == ',')
0853 str.pop_back();
0854 return str;
0855 }
0856
0857
0858 template <typename T,
0859 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0860 detail::enabler>>
0861 inline std::string type_name() {
0862 auto tname = std::string(1, '[') + tuple_name<T, 0>();
0863 tname.push_back(']');
0864 return tname;
0865 }
0866
0867
0868 template <typename T,
0869 enable_if_t<classify_object<T>::value == object_category::container_value ||
0870 classify_object<T>::value == object_category::wrapper_value,
0871 detail::enabler>>
0872 inline std::string type_name() {
0873 return type_name<typename T::value_type>();
0874 }
0875
0876
0877
0878
0879 template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
0880 bool integral_conversion(const std::string &input, T &output) noexcept {
0881 if(input.empty() || input.front() == '-') {
0882 return false;
0883 }
0884 char *val{nullptr};
0885 errno = 0;
0886 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
0887 if(errno == ERANGE) {
0888 return false;
0889 }
0890 output = static_cast<T>(output_ll);
0891 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
0892 return true;
0893 }
0894 val = nullptr;
0895 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
0896 if(val == (input.c_str() + input.size())) {
0897 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
0898 return (static_cast<std::int64_t>(output) == output_sll);
0899 }
0900
0901 if(input.find_first_of("_'") != std::string::npos) {
0902 std::string nstring = input;
0903 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
0904 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
0905 return integral_conversion(nstring, output);
0906 }
0907 if(input.compare(0, 2, "0o") == 0) {
0908 val = nullptr;
0909 errno = 0;
0910 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
0911 if(errno == ERANGE) {
0912 return false;
0913 }
0914 output = static_cast<T>(output_ll);
0915 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0916 }
0917 if(input.compare(0, 2, "0b") == 0) {
0918 val = nullptr;
0919 errno = 0;
0920 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
0921 if(errno == ERANGE) {
0922 return false;
0923 }
0924 output = static_cast<T>(output_ll);
0925 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0926 }
0927 return false;
0928 }
0929
0930
0931 template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
0932 bool integral_conversion(const std::string &input, T &output) noexcept {
0933 if(input.empty()) {
0934 return false;
0935 }
0936 char *val = nullptr;
0937 errno = 0;
0938 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
0939 if(errno == ERANGE) {
0940 return false;
0941 }
0942 output = static_cast<T>(output_ll);
0943 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
0944 return true;
0945 }
0946 if(input == "true") {
0947
0948 output = static_cast<T>(1);
0949 return true;
0950 }
0951
0952 if(input.find_first_of("_'") != std::string::npos) {
0953 std::string nstring = input;
0954 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
0955 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
0956 return integral_conversion(nstring, output);
0957 }
0958 if(input.compare(0, 2, "0o") == 0) {
0959 val = nullptr;
0960 errno = 0;
0961 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
0962 if(errno == ERANGE) {
0963 return false;
0964 }
0965 output = static_cast<T>(output_ll);
0966 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
0967 }
0968 if(input.compare(0, 2, "0b") == 0) {
0969 val = nullptr;
0970 errno = 0;
0971 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
0972 if(errno == ERANGE) {
0973 return false;
0974 }
0975 output = static_cast<T>(output_ll);
0976 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
0977 }
0978 return false;
0979 }
0980
0981
0982 inline std::int64_t to_flag_value(std::string val) noexcept {
0983 static const std::string trueString("true");
0984 static const std::string falseString("false");
0985 if(val == trueString) {
0986 return 1;
0987 }
0988 if(val == falseString) {
0989 return -1;
0990 }
0991 val = detail::to_lower(val);
0992 std::int64_t ret = 0;
0993 if(val.size() == 1) {
0994 if(val[0] >= '1' && val[0] <= '9') {
0995 return (static_cast<std::int64_t>(val[0]) - '0');
0996 }
0997 switch(val[0]) {
0998 case '0':
0999 case 'f':
1000 case 'n':
1001 case '-':
1002 ret = -1;
1003 break;
1004 case 't':
1005 case 'y':
1006 case '+':
1007 ret = 1;
1008 break;
1009 default:
1010 errno = EINVAL;
1011 return -1;
1012 }
1013 return ret;
1014 }
1015 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1016 ret = 1;
1017 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1018 ret = -1;
1019 } else {
1020 char *loc_ptr{nullptr};
1021 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1022 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1023 errno = EINVAL;
1024 }
1025 }
1026 return ret;
1027 }
1028
1029
1030 template <typename T,
1031 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1032 classify_object<T>::value == object_category::unsigned_integral,
1033 detail::enabler> = detail::dummy>
1034 bool lexical_cast(const std::string &input, T &output) {
1035 return integral_conversion(input, output);
1036 }
1037
1038
1039 template <typename T,
1040 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1041 bool lexical_cast(const std::string &input, T &output) {
1042 if(input.size() == 1) {
1043 output = static_cast<T>(input[0]);
1044 return true;
1045 }
1046 return integral_conversion(input, output);
1047 }
1048
1049
1050 template <typename T,
1051 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1052 bool lexical_cast(const std::string &input, T &output) {
1053 errno = 0;
1054 auto out = to_flag_value(input);
1055 if(errno == 0) {
1056 output = (out > 0);
1057 } else if(errno == ERANGE) {
1058 output = (input[0] != '-');
1059 } else {
1060 return false;
1061 }
1062 return true;
1063 }
1064
1065
1066 template <typename T,
1067 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1068 bool lexical_cast(const std::string &input, T &output) {
1069 if(input.empty()) {
1070 return false;
1071 }
1072 char *val = nullptr;
1073 auto output_ld = std::strtold(input.c_str(), &val);
1074 output = static_cast<T>(output_ld);
1075 if(val == (input.c_str() + input.size())) {
1076 return true;
1077 }
1078
1079 if(input.find_first_of("_'") != std::string::npos) {
1080 std::string nstring = input;
1081 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1082 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1083 return lexical_cast(nstring, output);
1084 }
1085 return false;
1086 }
1087
1088
1089 template <typename T,
1090 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1091 bool lexical_cast(const std::string &input, T &output) {
1092 using XC = typename wrapped_type<T, double>::type;
1093 XC x{0.0}, y{0.0};
1094 auto str1 = input;
1095 bool worked = false;
1096 auto nloc = str1.find_last_of("+-");
1097 if(nloc != std::string::npos && nloc > 0) {
1098 worked = lexical_cast(str1.substr(0, nloc), x);
1099 str1 = str1.substr(nloc);
1100 if(str1.back() == 'i' || str1.back() == 'j')
1101 str1.pop_back();
1102 worked = worked && lexical_cast(str1, y);
1103 } else {
1104 if(str1.back() == 'i' || str1.back() == 'j') {
1105 str1.pop_back();
1106 worked = lexical_cast(str1, y);
1107 x = XC{0};
1108 } else {
1109 worked = lexical_cast(str1, x);
1110 y = XC{0};
1111 }
1112 }
1113 if(worked) {
1114 output = T{x, y};
1115 return worked;
1116 }
1117 return from_stream(input, output);
1118 }
1119
1120
1121 template <typename T,
1122 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1123 bool lexical_cast(const std::string &input, T &output) {
1124 output = input;
1125 return true;
1126 }
1127
1128
1129 template <
1130 typename T,
1131 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1132 bool lexical_cast(const std::string &input, T &output) {
1133 output = T(input);
1134 return true;
1135 }
1136
1137
1138 template <
1139 typename T,
1140 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1141 bool lexical_cast(const std::string &input, T &output) {
1142 output = widen(input);
1143 return true;
1144 }
1145
1146 template <
1147 typename T,
1148 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1149 bool lexical_cast(const std::string &input, T &output) {
1150 output = T{widen(input)};
1151 return true;
1152 }
1153
1154
1155 template <typename T,
1156 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1157 bool lexical_cast(const std::string &input, T &output) {
1158 typename std::underlying_type<T>::type val;
1159 if(!integral_conversion(input, val)) {
1160 return false;
1161 }
1162 output = static_cast<T>(val);
1163 return true;
1164 }
1165
1166
1167 template <typename T,
1168 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1169 std::is_assignable<T &, typename T::value_type>::value,
1170 detail::enabler> = detail::dummy>
1171 bool lexical_cast(const std::string &input, T &output) {
1172 typename T::value_type val;
1173 if(lexical_cast(input, val)) {
1174 output = val;
1175 return true;
1176 }
1177 return from_stream(input, output);
1178 }
1179
1180 template <typename T,
1181 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1182 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1183 detail::enabler> = detail::dummy>
1184 bool lexical_cast(const std::string &input, T &output) {
1185 typename T::value_type val;
1186 if(lexical_cast(input, val)) {
1187 output = T{val};
1188 return true;
1189 }
1190 return from_stream(input, output);
1191 }
1192
1193
1194 template <
1195 typename T,
1196 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1197 bool lexical_cast(const std::string &input, T &output) {
1198 int val = 0;
1199 if(integral_conversion(input, val)) {
1200 output = T(val);
1201 return true;
1202 }
1203
1204 double dval = 0.0;
1205 if(lexical_cast(input, dval)) {
1206 output = T{dval};
1207 return true;
1208 }
1209
1210 return from_stream(input, output);
1211 }
1212
1213
1214 template <
1215 typename T,
1216 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1217 bool lexical_cast(const std::string &input, T &output) {
1218 int val = 0;
1219 if(integral_conversion(input, val)) {
1220 output = T(val);
1221 return true;
1222 }
1223 return from_stream(input, output);
1224 }
1225
1226
1227 template <
1228 typename T,
1229 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1230 bool lexical_cast(const std::string &input, T &output) {
1231 double val = 0.0;
1232 if(lexical_cast(input, val)) {
1233 output = T{val};
1234 return true;
1235 }
1236 return from_stream(input, output);
1237 }
1238
1239
1240 template <typename T,
1241 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1242 detail::enabler> = detail::dummy>
1243 bool lexical_cast(const std::string &input, T &output) {
1244 int val = 0;
1245 if(integral_conversion(input, val)) {
1246 #ifdef _MSC_VER
1247 #pragma warning(push)
1248 #pragma warning(disable : 4800)
1249 #endif
1250
1251
1252 output = val;
1253 #ifdef _MSC_VER
1254 #pragma warning(pop)
1255 #endif
1256 return true;
1257 }
1258
1259
1260
1261 return from_stream(input, output);
1262
1263 }
1264
1265
1266 template <typename T,
1267 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1268 is_istreamable<T>::value,
1269 detail::enabler> = detail::dummy>
1270 bool lexical_cast(const std::string &input, T &output) {
1271 return from_stream(input, output);
1272 }
1273
1274
1275
1276 template <typename T,
1277 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1278 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1279 detail::enabler> = detail::dummy>
1280 bool lexical_cast(const std::string & , T & ) {
1281 static_assert(!std::is_same<T, T>::value,
1282 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1283 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1284 return false;
1285 }
1286
1287
1288
1289 template <typename AssignTo,
1290 typename ConvertTo,
1291 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1292 (classify_object<AssignTo>::value == object_category::string_assignable ||
1293 classify_object<AssignTo>::value == object_category::string_constructible ||
1294 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1295 classify_object<AssignTo>::value == object_category::wstring_constructible),
1296 detail::enabler> = detail::dummy>
1297 bool lexical_assign(const std::string &input, AssignTo &output) {
1298 return lexical_cast(input, output);
1299 }
1300
1301
1302 template <typename AssignTo,
1303 typename ConvertTo,
1304 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1305 classify_object<AssignTo>::value != object_category::string_assignable &&
1306 classify_object<AssignTo>::value != object_category::string_constructible &&
1307 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1308 classify_object<AssignTo>::value != object_category::wstring_constructible,
1309 detail::enabler> = detail::dummy>
1310 bool lexical_assign(const std::string &input, AssignTo &output) {
1311 if(input.empty()) {
1312 output = AssignTo{};
1313 return true;
1314 }
1315
1316 return lexical_cast(input, output);
1317 }
1318
1319
1320 template <typename AssignTo,
1321 typename ConvertTo,
1322 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1323 classify_object<AssignTo>::value == object_category::wrapper_value,
1324 detail::enabler> = detail::dummy>
1325 bool lexical_assign(const std::string &input, AssignTo &output) {
1326 if(input.empty()) {
1327 typename AssignTo::value_type emptyVal{};
1328 output = emptyVal;
1329 return true;
1330 }
1331 return lexical_cast(input, output);
1332 }
1333
1334
1335
1336 template <typename AssignTo,
1337 typename ConvertTo,
1338 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1339 classify_object<AssignTo>::value != object_category::wrapper_value &&
1340 std::is_assignable<AssignTo &, int>::value,
1341 detail::enabler> = detail::dummy>
1342 bool lexical_assign(const std::string &input, AssignTo &output) {
1343 if(input.empty()) {
1344 output = 0;
1345 return true;
1346 }
1347 int val{0};
1348 if(lexical_cast(input, val)) {
1349 #if defined(__clang__)
1350
1351 #pragma clang diagnostic push
1352 #pragma clang diagnostic ignored "-Wsign-conversion"
1353 #endif
1354 output = val;
1355 #if defined(__clang__)
1356 #pragma clang diagnostic pop
1357 #endif
1358 return true;
1359 }
1360 return false;
1361 }
1362
1363
1364 template <typename AssignTo,
1365 typename ConvertTo,
1366 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1367 detail::enabler> = detail::dummy>
1368 bool lexical_assign(const std::string &input, AssignTo &output) {
1369 ConvertTo val{};
1370 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1371 if(parse_result) {
1372 output = val;
1373 }
1374 return parse_result;
1375 }
1376
1377
1378 template <
1379 typename AssignTo,
1380 typename ConvertTo,
1381 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1382 std::is_move_assignable<AssignTo>::value,
1383 detail::enabler> = detail::dummy>
1384 bool lexical_assign(const std::string &input, AssignTo &output) {
1385 ConvertTo val{};
1386 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1387 if(parse_result) {
1388 output = AssignTo(val);
1389 }
1390 return parse_result;
1391 }
1392
1393
1394 template <typename AssignTo,
1395 typename ConvertTo,
1396 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1397 classify_object<AssignTo>::value <= object_category::wrapper_value,
1398 detail::enabler> = detail::dummy>
1399 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1400 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1401 }
1402
1403
1404
1405 template <typename AssignTo,
1406 typename ConvertTo,
1407 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1408 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1409 detail::enabler> = detail::dummy>
1410 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1411
1412 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1413 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1414 FirstType v1;
1415 SecondType v2;
1416 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1417 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1418 if(retval) {
1419 output = AssignTo{v1, v2};
1420 }
1421 return retval;
1422 }
1423
1424
1425 template <class AssignTo,
1426 class ConvertTo,
1427 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1428 type_count<ConvertTo>::value == 1,
1429 detail::enabler> = detail::dummy>
1430 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1431 output.erase(output.begin(), output.end());
1432 if(strings.empty()) {
1433 return true;
1434 }
1435 if(strings.size() == 1 && strings[0] == "{}") {
1436 return true;
1437 }
1438 bool skip_remaining = false;
1439 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1440 skip_remaining = true;
1441 }
1442 for(const auto &elem : strings) {
1443 typename AssignTo::value_type out;
1444 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1445 if(!retval) {
1446 return false;
1447 }
1448 output.insert(output.end(), std::move(out));
1449 if(skip_remaining) {
1450 break;
1451 }
1452 }
1453 return (!output.empty());
1454 }
1455
1456
1457 template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1458 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1459
1460 if(strings.size() >= 2 && !strings[1].empty()) {
1461 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1462 XC2 x{0.0}, y{0.0};
1463 auto str1 = strings[1];
1464 if(str1.back() == 'i' || str1.back() == 'j') {
1465 str1.pop_back();
1466 }
1467 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1468 if(worked) {
1469 output = ConvertTo{x, y};
1470 }
1471 return worked;
1472 }
1473 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1474 }
1475
1476
1477 template <class AssignTo,
1478 class ConvertTo,
1479 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1480 (type_count<ConvertTo>::value == 1),
1481 detail::enabler> = detail::dummy>
1482 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1483 bool retval = true;
1484 output.clear();
1485 output.reserve(strings.size());
1486 for(const auto &elem : strings) {
1487
1488 output.emplace_back();
1489 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1490 }
1491 return (!output.empty()) && retval;
1492 }
1493
1494
1495
1496
1497 template <class AssignTo,
1498 class ConvertTo,
1499 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1500 type_count_base<ConvertTo>::value == 2,
1501 detail::enabler> = detail::dummy>
1502 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1503
1504
1505 template <class AssignTo,
1506 class ConvertTo,
1507 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1508 type_count_base<ConvertTo>::value != 2 &&
1509 ((type_count<ConvertTo>::value > 2) ||
1510 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1511 detail::enabler> = detail::dummy>
1512 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1513
1514
1515 template <class AssignTo,
1516 class ConvertTo,
1517 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1518 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1519 type_count<ConvertTo>::value > 2),
1520 detail::enabler> = detail::dummy>
1521 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1522
1523
1524
1525 template <typename AssignTo,
1526 typename ConvertTo,
1527 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1528 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1529 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1530 detail::enabler> = detail::dummy>
1531 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1532
1533 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1534 ConvertTo val;
1535 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1536 output = AssignTo{val};
1537 return retval;
1538 }
1539 output = AssignTo{};
1540 return true;
1541 }
1542
1543
1544 template <class AssignTo, class ConvertTo, std::size_t I>
1545 inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1546 tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1547 return true;
1548 }
1549
1550
1551 template <class AssignTo, class ConvertTo>
1552 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1553 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1554 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1555 strings.erase(strings.begin());
1556 return retval;
1557 }
1558
1559
1560 template <class AssignTo, class ConvertTo>
1561 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1562 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1563 bool>::type
1564 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1565 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1566 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1567 return retval;
1568 }
1569
1570
1571 template <class AssignTo, class ConvertTo>
1572 inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1573 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1574 bool>::type
1575 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1576
1577 std::size_t index{subtype_count_min<ConvertTo>::value};
1578 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1579 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1580
1581 while(index < mx) {
1582 if(is_separator(strings[index])) {
1583 break;
1584 }
1585 ++index;
1586 }
1587 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1588 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1589 if(strings.size() > index) {
1590 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1591 } else {
1592 strings.clear();
1593 }
1594 return retval;
1595 }
1596
1597
1598 template <class AssignTo, class ConvertTo, std::size_t I>
1599 inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1600 tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1601 bool retval = true;
1602 using ConvertToElement = typename std::
1603 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1604 if(!strings.empty()) {
1605 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1606 strings, std::get<I>(output));
1607 }
1608 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1609 return retval;
1610 }
1611
1612
1613 template <class AssignTo,
1614 class ConvertTo,
1615 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1616 type_count_base<ConvertTo>::value == 2,
1617 detail::enabler>>
1618 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1619 output.clear();
1620 while(!strings.empty()) {
1621
1622 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1623 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1624 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1625 if(!strings.empty()) {
1626 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1627 }
1628 if(retval) {
1629 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1630 } else {
1631 return false;
1632 }
1633 }
1634 return (!output.empty());
1635 }
1636
1637
1638 template <class AssignTo,
1639 class ConvertTo,
1640 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1641 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1642 type_count<ConvertTo>::value > 2),
1643 detail::enabler>>
1644 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1645 static_assert(
1646 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1647 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1648 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1649 }
1650
1651
1652 template <class AssignTo,
1653 class ConvertTo,
1654 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1655 type_count_base<ConvertTo>::value != 2 &&
1656 ((type_count<ConvertTo>::value > 2) ||
1657 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1658 detail::enabler>>
1659 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1660 bool retval = true;
1661 output.clear();
1662 std::vector<std::string> temp;
1663 std::size_t ii{0};
1664 std::size_t icount{0};
1665 std::size_t xcm{type_count<ConvertTo>::value};
1666 auto ii_max = strings.size();
1667 while(ii < ii_max) {
1668 temp.push_back(strings[ii]);
1669 ++ii;
1670 ++icount;
1671 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1672 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1673 temp.pop_back();
1674 }
1675 typename AssignTo::value_type temp_out;
1676 retval = retval &&
1677 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1678 temp.clear();
1679 if(!retval) {
1680 return false;
1681 }
1682 output.insert(output.end(), std::move(temp_out));
1683 icount = 0;
1684 }
1685 }
1686 return retval;
1687 }
1688
1689
1690 template <typename AssignTo,
1691 class ConvertTo,
1692 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1693 std::is_assignable<ConvertTo &, ConvertTo>::value,
1694 detail::enabler> = detail::dummy>
1695 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1696 if(strings.empty() || strings.front().empty()) {
1697 output = ConvertTo{};
1698 return true;
1699 }
1700 typename ConvertTo::value_type val;
1701 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1702 output = ConvertTo{val};
1703 return true;
1704 }
1705 return false;
1706 }
1707
1708
1709 template <typename AssignTo,
1710 class ConvertTo,
1711 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1712 !std::is_assignable<AssignTo &, ConvertTo>::value,
1713 detail::enabler> = detail::dummy>
1714 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1715 using ConvertType = typename ConvertTo::value_type;
1716 if(strings.empty() || strings.front().empty()) {
1717 output = ConvertType{};
1718 return true;
1719 }
1720 ConvertType val;
1721 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1722 output = val;
1723 return true;
1724 }
1725 return false;
1726 }
1727
1728
1729 inline std::string sum_string_vector(const std::vector<std::string> &values) {
1730 double val{0.0};
1731 bool fail{false};
1732 std::string output;
1733 for(const auto &arg : values) {
1734 double tv{0.0};
1735 auto comp = lexical_cast(arg, tv);
1736 if(!comp) {
1737 errno = 0;
1738 auto fv = detail::to_flag_value(arg);
1739 fail = (errno != 0);
1740 if(fail) {
1741 break;
1742 }
1743 tv = static_cast<double>(fv);
1744 }
1745 val += tv;
1746 }
1747 if(fail) {
1748 for(const auto &arg : values) {
1749 output.append(arg);
1750 }
1751 } else {
1752 std::ostringstream out;
1753 out.precision(16);
1754 out << val;
1755 output = out.str();
1756 }
1757 return output;
1758 }
1759
1760 }
1761
1762 }