File indexing completed on 2026-05-05 08:35:17
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 : std::uint8_t {};
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 template <typename T>
0285 struct is_readable_container<
0286 T,
0287 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
0288 : public std::true_type {};
0289
0290
0291 template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
0292
0293
0294 template <typename T>
0295 struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
0296
0297
0298
0299 template <typename S> class is_tuple_like {
0300 template <typename SS, enable_if_t<!is_complex<SS>::value, detail::enabler> = detail::dummy>
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, typename Enable = void> struct type_count_base {
0312 static const int value{0};
0313 };
0314
0315
0316 template <typename T>
0317 struct type_count_base<T,
0318 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
0319 !std::is_void<T>::value>::type> {
0320 static constexpr int value{1};
0321 };
0322
0323
0324 template <typename T>
0325 struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
0326 static constexpr int value{
0327 std::tuple_size<typename std::decay<T>::type>::value};
0328 };
0329
0330
0331 template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0332 static constexpr int value{type_count_base<typename T::value_type>::value};
0333 };
0334
0335
0336 template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
0337 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
0338 return std::forward<T>(value);
0339 }
0340
0341
0342 template <typename T,
0343 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
0344 detail::enabler> = detail::dummy>
0345 std::string to_string(T &&value) {
0346 return std::string(value);
0347 }
0348
0349
0350 template <typename T,
0351 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0352 is_ostreamable<T>::value,
0353 detail::enabler> = detail::dummy>
0354 std::string to_string(T &&value) {
0355 std::stringstream stream;
0356 stream << value;
0357 return stream.str();
0358 }
0359
0360
0361
0362
0363 template <typename T,
0364 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0365 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
0366 detail::enabler> = detail::dummy>
0367 inline std::string to_string(T &&value);
0368
0369
0370 template <typename T,
0371 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0372 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
0373 detail::enabler> = detail::dummy>
0374 inline std::string to_string(T &&value);
0375
0376
0377 template <
0378 typename T,
0379 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0380 !is_ostreamable<T>::value && !is_readable_container<typename std::remove_const<T>::type>::value &&
0381 !is_tuple_like<T>::value,
0382 detail::enabler> = detail::dummy>
0383 inline std::string to_string(T &&) {
0384 return {};
0385 }
0386
0387
0388 template <typename T,
0389 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0390 !is_ostreamable<T>::value && is_readable_container<T>::value && !is_tuple_like<T>::value,
0391 detail::enabler> = detail::dummy>
0392 inline std::string to_string(T &&variable) {
0393 auto cval = variable.begin();
0394 auto end = variable.end();
0395 if(cval == end) {
0396 return {"{}"};
0397 }
0398 std::vector<std::string> defaults;
0399 while(cval != end) {
0400 defaults.emplace_back(CLI::detail::to_string(*cval));
0401 ++cval;
0402 }
0403 return {"[" + detail::join(defaults) + "]"};
0404 }
0405
0406
0407
0408
0409 template <typename T, std::size_t I>
0410 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && );
0411
0412
0413 template <typename T, std::size_t I>
0414 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value);
0415
0416
0417 template <typename T,
0418 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0419 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
0420 detail::enabler>>
0421 inline std::string to_string(T &&value) {
0422 return to_string(std::get<0>(value));
0423 }
0424
0425
0426 template <typename T,
0427 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
0428 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
0429 detail::enabler>>
0430 inline std::string to_string(T &&value) {
0431 auto tname = std::string(1, '[') + tuple_value_string<T, 0>(value);
0432 tname.push_back(']');
0433 return tname;
0434 }
0435
0436
0437 template <typename T, std::size_t I>
0438 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && ) {
0439 return std::string{};
0440 }
0441
0442
0443 template <typename T, std::size_t I>
0444 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value) {
0445 auto str = std::string{to_string(std::get<I>(value))} + ',' + tuple_value_string<T, I + 1>(value);
0446 if(str.back() == ',')
0447 str.pop_back();
0448 return str;
0449 }
0450
0451
0452 template <typename T1,
0453 typename T2,
0454 typename T,
0455 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0456 auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
0457 return to_string(std::forward<T>(value));
0458 }
0459
0460
0461 template <typename T1,
0462 typename T2,
0463 typename T,
0464 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
0465 std::string checked_to_string(T &&) {
0466 return std::string{};
0467 }
0468
0469 template <typename T, enable_if_t<std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0470 std::string value_string(const T &value) {
0471 return std::to_string(value);
0472 }
0473
0474 template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
0475 std::string value_string(const T &value) {
0476 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
0477 }
0478
0479 template <typename T,
0480 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
0481 auto value_string(const T &value) -> decltype(to_string(value)) {
0482 return to_string(value);
0483 }
0484
0485
0486 template <typename T, typename def, typename Enable = void> struct wrapped_type {
0487 using type = def;
0488 };
0489
0490
0491 template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
0492 using type = typename T::value_type;
0493 };
0494
0495
0496
0497
0498 template <typename T> struct subtype_count;
0499
0500
0501 template <typename T> struct subtype_count_min;
0502
0503
0504 template <typename T, typename Enable = void> struct type_count {
0505 static const int value{0};
0506 };
0507
0508
0509 template <typename T>
0510 struct type_count<T,
0511 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
0512 !std::is_void<T>::value>::type> {
0513 static constexpr int value{1};
0514 };
0515
0516
0517 template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
0518 static constexpr int value{2};
0519 };
0520
0521
0522 template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0523 static constexpr int value{subtype_count<typename T::value_type>::value};
0524 };
0525
0526
0527 template <typename T>
0528 struct type_count<T,
0529 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
0530 !is_mutable_container<T>::value>::type> {
0531 static constexpr int value{type_count<typename T::value_type>::value};
0532 };
0533
0534
0535 template <typename T, std::size_t I>
0536 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
0537 return 0;
0538 }
0539
0540
0541 template <typename T, std::size_t I>
0542 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
0543 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
0544 }
0545
0546
0547 template <typename T> struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
0548 static constexpr int value{tuple_type_size<T, 0>()};
0549 };
0550
0551
0552 template <typename T> struct subtype_count {
0553 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
0554 };
0555
0556
0557 template <typename T, typename Enable = void> struct type_count_min {
0558 static const int value{0};
0559 };
0560
0561
0562 template <typename T>
0563 struct type_count_min<
0564 T,
0565 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
0566 !is_complex<T>::value && !std::is_void<T>::value>::type> {
0567 static constexpr int value{type_count<T>::value};
0568 };
0569
0570
0571 template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
0572 static constexpr int value{1};
0573 };
0574
0575
0576 template <typename T>
0577 struct type_count_min<
0578 T,
0579 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
0580 static constexpr int value{subtype_count_min<typename T::value_type>::value};
0581 };
0582
0583
0584 template <typename T, std::size_t I>
0585 constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
0586 return 0;
0587 }
0588
0589
0590 template <typename T, std::size_t I>
0591 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
0592 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
0593 }
0594
0595
0596 template <typename T> struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
0597 static constexpr int value{tuple_type_size_min<T, 0>()};
0598 };
0599
0600
0601 template <typename T> struct subtype_count_min {
0602 static constexpr int value{is_mutable_container<T>::value
0603 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
0604 : type_count_min<T>::value};
0605 };
0606
0607
0608 template <typename T, typename Enable = void> struct expected_count {
0609 static const int value{0};
0610 };
0611
0612
0613 template <typename T>
0614 struct expected_count<T,
0615 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
0616 !std::is_void<T>::value>::type> {
0617 static constexpr int value{1};
0618 };
0619
0620 template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0621 static constexpr int value{expected_max_vector_size};
0622 };
0623
0624
0625 template <typename T>
0626 struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
0627 static constexpr int value{expected_count<typename T::value_type>::value};
0628 };
0629
0630
0631 enum class object_category : std::uint8_t {
0632 char_value = 1,
0633 integral_value = 2,
0634 unsigned_integral = 4,
0635 enumeration = 6,
0636 boolean_value = 8,
0637 floating_point = 10,
0638 number_constructible = 12,
0639 double_constructible = 14,
0640 integer_constructible = 16,
0641
0642 string_assignable = 23,
0643 string_constructible = 24,
0644 wstring_assignable = 25,
0645 wstring_constructible = 26,
0646 other = 45,
0647
0648 wrapper_value = 50,
0649 complex_number = 60,
0650 tuple_value = 70,
0651 container_value = 80,
0652
0653 };
0654
0655
0656
0657
0658 template <typename T, typename Enable = void> struct classify_object {
0659 static constexpr object_category value{object_category::other};
0660 };
0661
0662
0663 template <typename T>
0664 struct classify_object<
0665 T,
0666 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
0667 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
0668 static constexpr object_category value{object_category::integral_value};
0669 };
0670
0671
0672 template <typename T>
0673 struct classify_object<T,
0674 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
0675 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
0676 static constexpr object_category value{object_category::unsigned_integral};
0677 };
0678
0679
0680 template <typename T>
0681 struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
0682 static constexpr object_category value{object_category::char_value};
0683 };
0684
0685
0686 template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
0687 static constexpr object_category value{object_category::boolean_value};
0688 };
0689
0690
0691 template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
0692 static constexpr object_category value{object_category::floating_point};
0693 };
0694 #if defined _MSC_VER
0695
0696
0697 #define WIDE_STRING_CHECK \
0698 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
0699 #define STRING_CHECK true
0700 #else
0701 #define WIDE_STRING_CHECK true
0702 #define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
0703 #endif
0704
0705
0706 template <typename T>
0707 struct classify_object<
0708 T,
0709 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
0710 std::is_assignable<T &, std::string>::value>::type> {
0711 static constexpr object_category value{object_category::string_assignable};
0712 };
0713
0714
0715 template <typename T>
0716 struct classify_object<
0717 T,
0718 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0719 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
0720 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
0721 static constexpr object_category value{object_category::string_constructible};
0722 };
0723
0724
0725 template <typename T>
0726 struct classify_object<T,
0727 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0728 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
0729 static constexpr object_category value{object_category::wstring_assignable};
0730 };
0731
0732 template <typename T>
0733 struct classify_object<
0734 T,
0735 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0736 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
0737 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
0738 static constexpr object_category value{object_category::wstring_constructible};
0739 };
0740
0741
0742 template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
0743 static constexpr object_category value{object_category::enumeration};
0744 };
0745
0746 template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
0747 static constexpr object_category value{object_category::complex_number};
0748 };
0749
0750
0751
0752 template <typename T> struct uncommon_type {
0753 using type = typename std::conditional<
0754 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
0755 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
0756 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
0757 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
0758 std::true_type,
0759 std::false_type>::type;
0760 static constexpr bool value = type::value;
0761 };
0762
0763
0764 template <typename T>
0765 struct classify_object<T,
0766 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
0767 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
0768 static constexpr object_category value{object_category::wrapper_value};
0769 };
0770
0771
0772 template <typename T>
0773 struct classify_object<T,
0774 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0775 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0776 is_direct_constructible<T, int>::value>::type> {
0777 static constexpr object_category value{object_category::number_constructible};
0778 };
0779
0780
0781 template <typename T>
0782 struct classify_object<T,
0783 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0784 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
0785 is_direct_constructible<T, int>::value>::type> {
0786 static constexpr object_category value{object_category::integer_constructible};
0787 };
0788
0789
0790 template <typename T>
0791 struct classify_object<T,
0792 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
0793 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
0794 !is_direct_constructible<T, int>::value>::type> {
0795 static constexpr object_category value{object_category::double_constructible};
0796 };
0797
0798
0799 template <typename T>
0800 struct classify_object<
0801 T,
0802 typename std::enable_if<is_tuple_like<T>::value &&
0803 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
0804 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
0805 !is_direct_constructible<T, int>::value) ||
0806 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
0807 static constexpr object_category value{object_category::tuple_value};
0808
0809
0810
0811
0812
0813 };
0814
0815
0816 template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
0817 static constexpr object_category value{object_category::container_value};
0818 };
0819
0820
0821
0822
0823
0824
0825
0826 template <typename T,
0827 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
0828 constexpr const char *type_name() {
0829 return "CHAR";
0830 }
0831
0832 template <typename T,
0833 enable_if_t<classify_object<T>::value == object_category::integral_value ||
0834 classify_object<T>::value == object_category::integer_constructible,
0835 detail::enabler> = detail::dummy>
0836 constexpr const char *type_name() {
0837 return "INT";
0838 }
0839
0840 template <typename T,
0841 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
0842 constexpr const char *type_name() {
0843 return "UINT";
0844 }
0845
0846 template <typename T,
0847 enable_if_t<classify_object<T>::value == object_category::floating_point ||
0848 classify_object<T>::value == object_category::number_constructible ||
0849 classify_object<T>::value == object_category::double_constructible,
0850 detail::enabler> = detail::dummy>
0851 constexpr const char *type_name() {
0852 return "FLOAT";
0853 }
0854
0855
0856 template <typename T,
0857 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
0858 constexpr const char *type_name() {
0859 return "ENUM";
0860 }
0861
0862
0863 template <typename T,
0864 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
0865 constexpr const char *type_name() {
0866 return "BOOLEAN";
0867 }
0868
0869
0870 template <typename T,
0871 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
0872 constexpr const char *type_name() {
0873 return "COMPLEX";
0874 }
0875
0876
0877 template <typename T,
0878 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
0879 classify_object<T>::value <= object_category::other,
0880 detail::enabler> = detail::dummy>
0881 constexpr const char *type_name() {
0882 return "TEXT";
0883 }
0884
0885 template <typename T,
0886 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0887 detail::enabler> = detail::dummy>
0888 std::string type_name();
0889
0890
0891 template <typename T,
0892 enable_if_t<classify_object<T>::value == object_category::container_value ||
0893 classify_object<T>::value == object_category::wrapper_value,
0894 detail::enabler> = detail::dummy>
0895 std::string type_name();
0896
0897
0898 template <typename T,
0899 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
0900 detail::enabler> = detail::dummy>
0901 inline std::string type_name() {
0902 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
0903 }
0904
0905
0906 template <typename T, std::size_t I>
0907 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
0908 return std::string{};
0909 }
0910
0911
0912 template <typename T, std::size_t I>
0913 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
0914 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
0915 tuple_name<T, I + 1>();
0916 if(str.back() == ',')
0917 str.pop_back();
0918 return str;
0919 }
0920
0921
0922 template <typename T,
0923 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
0924 detail::enabler>>
0925 inline std::string type_name() {
0926 auto tname = std::string(1, '[') + tuple_name<T, 0>();
0927 tname.push_back(']');
0928 return tname;
0929 }
0930
0931
0932 template <typename T,
0933 enable_if_t<classify_object<T>::value == object_category::container_value ||
0934 classify_object<T>::value == object_category::wrapper_value,
0935 detail::enabler>>
0936 inline std::string type_name() {
0937 return type_name<typename T::value_type>();
0938 }
0939
0940
0941
0942
0943 template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
0944 bool integral_conversion(const std::string &input, T &output) noexcept {
0945 if(input.empty() || input.front() == '-') {
0946 return false;
0947 }
0948 char *val{nullptr};
0949 errno = 0;
0950 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
0951 if(errno == ERANGE) {
0952 return false;
0953 }
0954 output = static_cast<T>(output_ll);
0955 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
0956 return true;
0957 }
0958 val = nullptr;
0959 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
0960 if(val == (input.c_str() + input.size())) {
0961 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
0962 return (static_cast<std::int64_t>(output) == output_sll);
0963 }
0964
0965 auto group_separators = get_group_separators();
0966 if(input.find_first_of(group_separators) != std::string::npos) {
0967 std::string nstring = input;
0968 for(auto &separator : group_separators) {
0969 if(input.find_first_of(separator) != std::string::npos) {
0970 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
0971 }
0972 }
0973 return integral_conversion(nstring, output);
0974 }
0975
0976 if(std::isspace(static_cast<unsigned char>(input.back()))) {
0977 return integral_conversion(trim_copy(input), output);
0978 }
0979 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
0980 val = nullptr;
0981 errno = 0;
0982 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
0983 if(errno == ERANGE) {
0984 return false;
0985 }
0986 output = static_cast<T>(output_ll);
0987 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0988 }
0989 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
0990
0991
0992
0993 val = nullptr;
0994 errno = 0;
0995 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
0996 if(errno == ERANGE) {
0997 return false;
0998 }
0999 output = static_cast<T>(output_ll);
1000 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1001
1002 }
1003 return false;
1004 }
1005
1006
1007 template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1008 bool integral_conversion(const std::string &input, T &output) noexcept {
1009 if(input.empty()) {
1010 return false;
1011 }
1012 char *val = nullptr;
1013 errno = 0;
1014 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1015 if(errno == ERANGE) {
1016 return false;
1017 }
1018 output = static_cast<T>(output_ll);
1019 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1020 return true;
1021 }
1022 if(input == "true") {
1023
1024 output = static_cast<T>(1);
1025 return true;
1026 }
1027
1028 auto group_separators = get_group_separators();
1029 if(input.find_first_of(group_separators) != std::string::npos) {
1030 for(auto &separator : group_separators) {
1031 if(input.find_first_of(separator) != std::string::npos) {
1032 std::string nstring = input;
1033 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1034 return integral_conversion(nstring, output);
1035 }
1036 }
1037 }
1038 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1039 return integral_conversion(trim_copy(input), output);
1040 }
1041 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1042 val = nullptr;
1043 errno = 0;
1044 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1045 if(errno == ERANGE) {
1046 return false;
1047 }
1048 output = static_cast<T>(output_ll);
1049 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1050 }
1051 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1052
1053
1054
1055 val = nullptr;
1056 errno = 0;
1057 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1058 if(errno == ERANGE) {
1059 return false;
1060 }
1061 output = static_cast<T>(output_ll);
1062 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1063
1064 }
1065 return false;
1066 }
1067
1068
1069 inline std::int64_t to_flag_value(std::string val) noexcept {
1070 static const std::string trueString("true");
1071 static const std::string falseString("false");
1072 if(val == trueString) {
1073 return 1;
1074 }
1075 if(val == falseString) {
1076 return -1;
1077 }
1078 val = detail::to_lower(val);
1079 std::int64_t ret = 0;
1080 if(val.size() == 1) {
1081 if(val[0] >= '1' && val[0] <= '9') {
1082 return (static_cast<std::int64_t>(val[0]) - '0');
1083 }
1084 switch(val[0]) {
1085 case '0':
1086 case 'f':
1087 case 'n':
1088 case '-':
1089 ret = -1;
1090 break;
1091 case 't':
1092 case 'y':
1093 case '+':
1094 ret = 1;
1095 break;
1096 default:
1097 errno = EINVAL;
1098 return -1;
1099 }
1100 return ret;
1101 }
1102 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1103 ret = 1;
1104 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1105 ret = -1;
1106 } else {
1107 char *loc_ptr{nullptr};
1108 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1109 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1110 errno = EINVAL;
1111 }
1112 }
1113 return ret;
1114 }
1115
1116
1117 template <typename T,
1118 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1119 classify_object<T>::value == object_category::unsigned_integral,
1120 detail::enabler> = detail::dummy>
1121 bool lexical_cast(const std::string &input, T &output) {
1122 return integral_conversion(input, output);
1123 }
1124
1125
1126 template <typename T,
1127 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1128 bool lexical_cast(const std::string &input, T &output) {
1129 if(input.size() == 1) {
1130 output = static_cast<T>(input[0]);
1131 return true;
1132 }
1133 std::int8_t res{0};
1134
1135
1136 bool result = integral_conversion(input, res);
1137 if(result) {
1138 output = static_cast<T>(res);
1139 }
1140 return result;
1141 }
1142
1143
1144 template <typename T,
1145 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1146 bool lexical_cast(const std::string &input, T &output) {
1147 errno = 0;
1148 auto out = to_flag_value(input);
1149 if(errno == 0) {
1150 output = (out > 0);
1151 } else if(errno == ERANGE) {
1152 output = (input[0] != '-');
1153 } else {
1154 return false;
1155 }
1156 return true;
1157 }
1158
1159
1160 template <typename T,
1161 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1162 bool lexical_cast(const std::string &input, T &output) {
1163 if(input.empty()) {
1164 return false;
1165 }
1166 char *val = nullptr;
1167 auto output_ld = std::strtold(input.c_str(), &val);
1168 output = static_cast<T>(output_ld);
1169 if(val == (input.c_str() + input.size())) {
1170 return true;
1171 }
1172 while(std::isspace(static_cast<unsigned char>(*val))) {
1173 ++val;
1174 if(val == (input.c_str() + input.size())) {
1175 return true;
1176 }
1177 }
1178
1179
1180 auto group_separators = get_group_separators();
1181 if(input.find_first_of(group_separators) != std::string::npos) {
1182 for(auto &separator : group_separators) {
1183 if(input.find_first_of(separator) != std::string::npos) {
1184 std::string nstring = input;
1185 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1186 return lexical_cast(nstring, output);
1187 }
1188 }
1189 }
1190 return false;
1191 }
1192
1193
1194 template <typename T,
1195 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1196 bool lexical_cast(const std::string &input, T &output) {
1197 using XC = typename wrapped_type<T, double>::type;
1198 XC x{0.0}, y{0.0};
1199 auto str1 = input;
1200 bool worked = false;
1201 auto nloc = str1.find_last_of("+-");
1202 if(nloc != std::string::npos && nloc > 0) {
1203 worked = lexical_cast(str1.substr(0, nloc), x);
1204 str1 = str1.substr(nloc);
1205 if(str1.back() == 'i' || str1.back() == 'j')
1206 str1.pop_back();
1207 worked = worked && lexical_cast(str1, y);
1208 } else {
1209 if(str1.back() == 'i' || str1.back() == 'j') {
1210 str1.pop_back();
1211 worked = lexical_cast(str1, y);
1212 x = XC{0};
1213 } else {
1214 worked = lexical_cast(str1, x);
1215 y = XC{0};
1216 }
1217 }
1218 if(worked) {
1219 output = T{x, y};
1220 return worked;
1221 }
1222 return from_stream(input, output);
1223 }
1224
1225
1226 template <typename T,
1227 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1228 bool lexical_cast(const std::string &input, T &output) {
1229 output = input;
1230 return true;
1231 }
1232
1233
1234 template <
1235 typename T,
1236 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1237 bool lexical_cast(const std::string &input, T &output) {
1238 output = T(input);
1239 return true;
1240 }
1241
1242
1243 template <
1244 typename T,
1245 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1246 bool lexical_cast(const std::string &input, T &output) {
1247 output = widen(input);
1248 return true;
1249 }
1250
1251 template <
1252 typename T,
1253 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1254 bool lexical_cast(const std::string &input, T &output) {
1255 output = T{widen(input)};
1256 return true;
1257 }
1258
1259
1260 template <typename T,
1261 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1262 bool lexical_cast(const std::string &input, T &output) {
1263 typename std::underlying_type<T>::type val;
1264 if(!integral_conversion(input, val)) {
1265 return false;
1266 }
1267 output = static_cast<T>(val);
1268 return true;
1269 }
1270
1271
1272 template <typename T,
1273 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1274 std::is_assignable<T &, typename T::value_type>::value,
1275 detail::enabler> = detail::dummy>
1276 bool lexical_cast(const std::string &input, T &output) {
1277 typename T::value_type val;
1278 if(lexical_cast(input, val)) {
1279 output = val;
1280 return true;
1281 }
1282 return from_stream(input, output);
1283 }
1284
1285 template <typename T,
1286 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1287 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1288 detail::enabler> = detail::dummy>
1289 bool lexical_cast(const std::string &input, T &output) {
1290 typename T::value_type val;
1291 if(lexical_cast(input, val)) {
1292 output = T{val};
1293 return true;
1294 }
1295 return from_stream(input, output);
1296 }
1297
1298
1299 template <
1300 typename T,
1301 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1302 bool lexical_cast(const std::string &input, T &output) {
1303 int val = 0;
1304 if(integral_conversion(input, val)) {
1305 output = T(val);
1306 return true;
1307 }
1308
1309 double dval = 0.0;
1310 if(lexical_cast(input, dval)) {
1311 output = T{dval};
1312 return true;
1313 }
1314
1315 return from_stream(input, output);
1316 }
1317
1318
1319 template <
1320 typename T,
1321 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1322 bool lexical_cast(const std::string &input, T &output) {
1323 int val = 0;
1324 if(integral_conversion(input, val)) {
1325 output = T(val);
1326 return true;
1327 }
1328 return from_stream(input, output);
1329 }
1330
1331
1332 template <
1333 typename T,
1334 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1335 bool lexical_cast(const std::string &input, T &output) {
1336 double val = 0.0;
1337 if(lexical_cast(input, val)) {
1338 output = T{val};
1339 return true;
1340 }
1341 return from_stream(input, output);
1342 }
1343
1344
1345 template <typename T,
1346 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1347 detail::enabler> = detail::dummy>
1348 bool lexical_cast(const std::string &input, T &output) {
1349 int val = 0;
1350 if(integral_conversion(input, val)) {
1351 #ifdef _MSC_VER
1352 #pragma warning(push)
1353 #pragma warning(disable : 4800)
1354 #endif
1355
1356
1357 output = val;
1358 #ifdef _MSC_VER
1359 #pragma warning(pop)
1360 #endif
1361 return true;
1362 }
1363
1364
1365
1366 return from_stream(input, output);
1367
1368 }
1369
1370
1371 template <typename T,
1372 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1373 is_istreamable<T>::value,
1374 detail::enabler> = detail::dummy>
1375 bool lexical_cast(const std::string &input, T &output) {
1376 return from_stream(input, output);
1377 }
1378
1379
1380
1381 template <typename T,
1382 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1383 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1384 detail::enabler> = detail::dummy>
1385 bool lexical_cast(const std::string & , T & ) {
1386 static_assert(!std::is_same<T, T>::value,
1387 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1388 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1389 return false;
1390 }
1391
1392
1393
1394 template <typename AssignTo,
1395 typename ConvertTo,
1396 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1397 (classify_object<AssignTo>::value == object_category::string_assignable ||
1398 classify_object<AssignTo>::value == object_category::string_constructible ||
1399 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1400 classify_object<AssignTo>::value == object_category::wstring_constructible),
1401 detail::enabler> = detail::dummy>
1402 bool lexical_assign(const std::string &input, AssignTo &output) {
1403 return lexical_cast(input, output);
1404 }
1405
1406
1407 template <typename AssignTo,
1408 typename ConvertTo,
1409 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1410 classify_object<AssignTo>::value != object_category::string_assignable &&
1411 classify_object<AssignTo>::value != object_category::string_constructible &&
1412 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1413 classify_object<AssignTo>::value != object_category::wstring_constructible,
1414 detail::enabler> = detail::dummy>
1415 bool lexical_assign(const std::string &input, AssignTo &output) {
1416 if(input.empty()) {
1417 output = AssignTo{};
1418 return true;
1419 }
1420
1421 return lexical_cast(input, output);
1422 }
1423
1424
1425 template <typename AssignTo,
1426 typename ConvertTo,
1427 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1428 classify_object<AssignTo>::value == object_category::wrapper_value,
1429 detail::enabler> = detail::dummy>
1430 bool lexical_assign(const std::string &input, AssignTo &output) {
1431 if(input.empty()) {
1432 typename AssignTo::value_type emptyVal{};
1433 output = emptyVal;
1434 return true;
1435 }
1436 return lexical_cast(input, output);
1437 }
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::wrapper_value &&
1445 std::is_assignable<AssignTo &, int>::value,
1446 detail::enabler> = detail::dummy>
1447 bool lexical_assign(const std::string &input, AssignTo &output) {
1448 if(input.empty()) {
1449 output = 0;
1450 return true;
1451 }
1452 int val{0};
1453 if(lexical_cast(input, val)) {
1454 #if defined(__clang__)
1455
1456 #pragma clang diagnostic push
1457 #pragma clang diagnostic ignored "-Wsign-conversion"
1458 #endif
1459 output = val;
1460 #if defined(__clang__)
1461 #pragma clang diagnostic pop
1462 #endif
1463 return true;
1464 }
1465 return false;
1466 }
1467
1468
1469 template <typename AssignTo,
1470 typename ConvertTo,
1471 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1472 detail::enabler> = detail::dummy>
1473 bool lexical_assign(const std::string &input, AssignTo &output) {
1474 ConvertTo val{};
1475 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1476 if(parse_result) {
1477 output = val;
1478 }
1479 return parse_result;
1480 }
1481
1482
1483 template <
1484 typename AssignTo,
1485 typename ConvertTo,
1486 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1487 std::is_move_assignable<AssignTo>::value,
1488 detail::enabler> = detail::dummy>
1489 bool lexical_assign(const std::string &input, AssignTo &output) {
1490 ConvertTo val{};
1491 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1492 if(parse_result) {
1493 output = AssignTo(val);
1494 }
1495 return parse_result;
1496 }
1497
1498
1499 template <typename AssignTo,
1500 typename ConvertTo,
1501 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1502 classify_object<AssignTo>::value <= object_category::wrapper_value,
1503 detail::enabler> = detail::dummy>
1504 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1505 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1506 }
1507
1508
1509
1510 template <typename AssignTo,
1511 typename ConvertTo,
1512 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1513 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1514 detail::enabler> = detail::dummy>
1515 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1516
1517 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1518 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1519 FirstType v1;
1520 SecondType v2{};
1521 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1522 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1523 if(retval) {
1524 output = AssignTo{v1, v2};
1525 }
1526 return retval;
1527 }
1528
1529
1530 template <class AssignTo,
1531 class ConvertTo,
1532 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1533 type_count<ConvertTo>::value == 1,
1534 detail::enabler> = detail::dummy>
1535 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1536 output.erase(output.begin(), output.end());
1537 if(strings.empty()) {
1538 return true;
1539 }
1540 if(strings.size() == 1 && strings[0] == "{}") {
1541 return true;
1542 }
1543 bool skip_remaining = false;
1544 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1545 skip_remaining = true;
1546 }
1547 for(const auto &elem : strings) {
1548 typename AssignTo::value_type out;
1549 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1550 if(!retval) {
1551 return false;
1552 }
1553 output.insert(output.end(), std::move(out));
1554 if(skip_remaining) {
1555 break;
1556 }
1557 }
1558 return (!output.empty());
1559 }
1560
1561
1562 template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1563 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1564
1565 if(strings.size() >= 2 && !strings[1].empty()) {
1566 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1567 XC2 x{0.0}, y{0.0};
1568 auto str1 = strings[1];
1569 if(str1.back() == 'i' || str1.back() == 'j') {
1570 str1.pop_back();
1571 }
1572 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1573 if(worked) {
1574 output = ConvertTo{x, y};
1575 }
1576 return worked;
1577 }
1578 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1579 }
1580
1581
1582 template <class AssignTo,
1583 class ConvertTo,
1584 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1585 (type_count<ConvertTo>::value == 1),
1586 detail::enabler> = detail::dummy>
1587 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1588 bool retval = true;
1589 output.clear();
1590 output.reserve(strings.size());
1591 for(const auto &elem : strings) {
1592
1593 output.emplace_back();
1594 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1595 }
1596 return (!output.empty()) && retval;
1597 }
1598
1599
1600
1601
1602 template <class AssignTo,
1603 class ConvertTo,
1604 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1605 type_count_base<ConvertTo>::value == 2,
1606 detail::enabler> = detail::dummy>
1607 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1608
1609
1610 template <class AssignTo,
1611 class ConvertTo,
1612 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1613 type_count_base<ConvertTo>::value != 2 &&
1614 ((type_count<ConvertTo>::value > 2) ||
1615 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1616 detail::enabler> = detail::dummy>
1617 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1618
1619
1620 template <class AssignTo,
1621 class ConvertTo,
1622 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1623 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1624 type_count<ConvertTo>::value > 2),
1625 detail::enabler> = detail::dummy>
1626 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1627
1628
1629
1630 template <typename AssignTo,
1631 typename ConvertTo,
1632 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1633 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1634 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1635 detail::enabler> = detail::dummy>
1636 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1637
1638 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1639 ConvertTo val;
1640 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1641 output = AssignTo{val};
1642 return retval;
1643 }
1644 output = AssignTo{};
1645 return true;
1646 }
1647
1648
1649 template <class AssignTo, class ConvertTo, std::size_t I>
1650 inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1651 tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1652 return true;
1653 }
1654
1655
1656 template <class AssignTo, class ConvertTo>
1657 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1658 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1659 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1660 strings.erase(strings.begin());
1661 return retval;
1662 }
1663
1664
1665 template <class AssignTo, class ConvertTo>
1666 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1667 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1668 bool>::type
1669 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1670 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1671 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1672 return retval;
1673 }
1674
1675
1676 template <class AssignTo, class ConvertTo>
1677 inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1678 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1679 bool>::type
1680 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1681
1682 std::size_t index{subtype_count_min<ConvertTo>::value};
1683 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1684 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1685
1686 while(index < mx) {
1687 if(is_separator(strings[index])) {
1688 break;
1689 }
1690 ++index;
1691 }
1692 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1693 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1694 if(strings.size() > index) {
1695 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1696 } else {
1697 strings.clear();
1698 }
1699 return retval;
1700 }
1701
1702
1703 template <class AssignTo, class ConvertTo, std::size_t I>
1704 inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1705 tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1706 bool retval = true;
1707 using ConvertToElement = typename std::
1708 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1709 if(!strings.empty()) {
1710 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1711 strings, std::get<I>(output));
1712 }
1713 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1714 return retval;
1715 }
1716
1717
1718 template <class AssignTo,
1719 class ConvertTo,
1720 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1721 type_count_base<ConvertTo>::value == 2,
1722 detail::enabler>>
1723 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1724 output.clear();
1725 while(!strings.empty()) {
1726
1727 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1728 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1729 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1730 if(!strings.empty()) {
1731 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1732 }
1733 if(retval) {
1734 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1735 } else {
1736 return false;
1737 }
1738 }
1739 return (!output.empty());
1740 }
1741
1742
1743 template <class AssignTo,
1744 class ConvertTo,
1745 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1746 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1747 type_count<ConvertTo>::value > 2),
1748 detail::enabler>>
1749 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1750 static_assert(
1751 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1752 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1753 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1754 }
1755
1756
1757 template <class AssignTo,
1758 class ConvertTo,
1759 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1760 type_count_base<ConvertTo>::value != 2 &&
1761 ((type_count<ConvertTo>::value > 2) ||
1762 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1763 detail::enabler>>
1764 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1765 bool retval = true;
1766 output.clear();
1767 std::vector<std::string> temp;
1768 std::size_t ii{0};
1769 std::size_t icount{0};
1770 std::size_t xcm{type_count<ConvertTo>::value};
1771 auto ii_max = strings.size();
1772 while(ii < ii_max) {
1773 temp.push_back(strings[ii]);
1774 ++ii;
1775 ++icount;
1776 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1777 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1778 temp.pop_back();
1779 }
1780 typename AssignTo::value_type temp_out;
1781 retval = retval &&
1782 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1783 temp.clear();
1784 if(!retval) {
1785 return false;
1786 }
1787 output.insert(output.end(), std::move(temp_out));
1788 icount = 0;
1789 }
1790 }
1791 return retval;
1792 }
1793
1794
1795 template <typename AssignTo,
1796 class ConvertTo,
1797 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1798 std::is_assignable<ConvertTo &, ConvertTo>::value,
1799 detail::enabler> = detail::dummy>
1800 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1801 if(strings.empty() || strings.front().empty()) {
1802 output = ConvertTo{};
1803 return true;
1804 }
1805 typename ConvertTo::value_type val;
1806 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1807 output = ConvertTo{val};
1808 return true;
1809 }
1810 return false;
1811 }
1812
1813
1814 template <typename AssignTo,
1815 class ConvertTo,
1816 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1817 !std::is_assignable<AssignTo &, ConvertTo>::value,
1818 detail::enabler> = detail::dummy>
1819 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1820 using ConvertType = typename ConvertTo::value_type;
1821 if(strings.empty() || strings.front().empty()) {
1822 output = ConvertType{};
1823 return true;
1824 }
1825 ConvertType val;
1826 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1827 output = val;
1828 return true;
1829 }
1830 return false;
1831 }
1832
1833
1834 inline std::string sum_string_vector(const std::vector<std::string> &values) {
1835 double val{0.0};
1836 bool fail{false};
1837 std::string output;
1838 for(const auto &arg : values) {
1839 double tv{0.0};
1840 auto comp = lexical_cast(arg, tv);
1841 if(!comp) {
1842 errno = 0;
1843 auto fv = detail::to_flag_value(arg);
1844 fail = (errno != 0);
1845 if(fail) {
1846 break;
1847 }
1848 tv = static_cast<double>(fv);
1849 }
1850 val += tv;
1851 }
1852 if(fail) {
1853 for(const auto &arg : values) {
1854 output.append(arg);
1855 }
1856 } else {
1857 std::ostringstream out;
1858 out.precision(16);
1859 out << val;
1860 output = out.str();
1861 }
1862 return output;
1863 }
1864
1865 }
1866
1867 }