File indexing completed on 2025-09-16 08:52:38
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 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,
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 : int {
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 if(input.find_first_of("_'") != std::string::npos) {
0966 std::string nstring = input;
0967 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
0968 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
0969 return integral_conversion(nstring, output);
0970 }
0971 if(std::isspace(static_cast<unsigned char>(input.back()))) {
0972 return integral_conversion(trim_copy(input), output);
0973 }
0974 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
0975 val = nullptr;
0976 errno = 0;
0977 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
0978 if(errno == ERANGE) {
0979 return false;
0980 }
0981 output = static_cast<T>(output_ll);
0982 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0983 }
0984 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
0985
0986
0987
0988 val = nullptr;
0989 errno = 0;
0990 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
0991 if(errno == ERANGE) {
0992 return false;
0993 }
0994 output = static_cast<T>(output_ll);
0995 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
0996
0997 }
0998 return false;
0999 }
1000
1001
1002 template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1003 bool integral_conversion(const std::string &input, T &output) noexcept {
1004 if(input.empty()) {
1005 return false;
1006 }
1007 char *val = nullptr;
1008 errno = 0;
1009 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1010 if(errno == ERANGE) {
1011 return false;
1012 }
1013 output = static_cast<T>(output_ll);
1014 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1015 return true;
1016 }
1017 if(input == "true") {
1018
1019 output = static_cast<T>(1);
1020 return true;
1021 }
1022
1023 if(input.find_first_of("_'") != std::string::npos) {
1024 std::string nstring = input;
1025 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1026 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1027 return integral_conversion(nstring, output);
1028 }
1029 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1030 return integral_conversion(trim_copy(input), output);
1031 }
1032 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1033 val = nullptr;
1034 errno = 0;
1035 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1036 if(errno == ERANGE) {
1037 return false;
1038 }
1039 output = static_cast<T>(output_ll);
1040 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1041 }
1042 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1043
1044
1045
1046 val = nullptr;
1047 errno = 0;
1048 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1049 if(errno == ERANGE) {
1050 return false;
1051 }
1052 output = static_cast<T>(output_ll);
1053 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1054
1055 }
1056 return false;
1057 }
1058
1059
1060 inline std::int64_t to_flag_value(std::string val) noexcept {
1061 static const std::string trueString("true");
1062 static const std::string falseString("false");
1063 if(val == trueString) {
1064 return 1;
1065 }
1066 if(val == falseString) {
1067 return -1;
1068 }
1069 val = detail::to_lower(val);
1070 std::int64_t ret = 0;
1071 if(val.size() == 1) {
1072 if(val[0] >= '1' && val[0] <= '9') {
1073 return (static_cast<std::int64_t>(val[0]) - '0');
1074 }
1075 switch(val[0]) {
1076 case '0':
1077 case 'f':
1078 case 'n':
1079 case '-':
1080 ret = -1;
1081 break;
1082 case 't':
1083 case 'y':
1084 case '+':
1085 ret = 1;
1086 break;
1087 default:
1088 errno = EINVAL;
1089 return -1;
1090 }
1091 return ret;
1092 }
1093 if(val == trueString || val == "on" || val == "yes" || val == "enable") {
1094 ret = 1;
1095 } else if(val == falseString || val == "off" || val == "no" || val == "disable") {
1096 ret = -1;
1097 } else {
1098 char *loc_ptr{nullptr};
1099 ret = std::strtoll(val.c_str(), &loc_ptr, 0);
1100 if(loc_ptr != (val.c_str() + val.size()) && errno == 0) {
1101 errno = EINVAL;
1102 }
1103 }
1104 return ret;
1105 }
1106
1107
1108 template <typename T,
1109 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1110 classify_object<T>::value == object_category::unsigned_integral,
1111 detail::enabler> = detail::dummy>
1112 bool lexical_cast(const std::string &input, T &output) {
1113 return integral_conversion(input, output);
1114 }
1115
1116
1117 template <typename T,
1118 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1119 bool lexical_cast(const std::string &input, T &output) {
1120 if(input.size() == 1) {
1121 output = static_cast<T>(input[0]);
1122 return true;
1123 }
1124 return integral_conversion(input, output);
1125 }
1126
1127
1128 template <typename T,
1129 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1130 bool lexical_cast(const std::string &input, T &output) {
1131 errno = 0;
1132 auto out = to_flag_value(input);
1133 if(errno == 0) {
1134 output = (out > 0);
1135 } else if(errno == ERANGE) {
1136 output = (input[0] != '-');
1137 } else {
1138 return false;
1139 }
1140 return true;
1141 }
1142
1143
1144 template <typename T,
1145 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1146 bool lexical_cast(const std::string &input, T &output) {
1147 if(input.empty()) {
1148 return false;
1149 }
1150 char *val = nullptr;
1151 auto output_ld = std::strtold(input.c_str(), &val);
1152 output = static_cast<T>(output_ld);
1153 if(val == (input.c_str() + input.size())) {
1154 return true;
1155 }
1156 while(std::isspace(static_cast<unsigned char>(*val))) {
1157 ++val;
1158 if(val == (input.c_str() + input.size())) {
1159 return true;
1160 }
1161 }
1162
1163
1164 if(input.find_first_of("_'") != std::string::npos) {
1165 std::string nstring = input;
1166 nstring.erase(std::remove(nstring.begin(), nstring.end(), '_'), nstring.end());
1167 nstring.erase(std::remove(nstring.begin(), nstring.end(), '\''), nstring.end());
1168 return lexical_cast(nstring, output);
1169 }
1170 return false;
1171 }
1172
1173
1174 template <typename T,
1175 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1176 bool lexical_cast(const std::string &input, T &output) {
1177 using XC = typename wrapped_type<T, double>::type;
1178 XC x{0.0}, y{0.0};
1179 auto str1 = input;
1180 bool worked = false;
1181 auto nloc = str1.find_last_of("+-");
1182 if(nloc != std::string::npos && nloc > 0) {
1183 worked = lexical_cast(str1.substr(0, nloc), x);
1184 str1 = str1.substr(nloc);
1185 if(str1.back() == 'i' || str1.back() == 'j')
1186 str1.pop_back();
1187 worked = worked && lexical_cast(str1, y);
1188 } else {
1189 if(str1.back() == 'i' || str1.back() == 'j') {
1190 str1.pop_back();
1191 worked = lexical_cast(str1, y);
1192 x = XC{0};
1193 } else {
1194 worked = lexical_cast(str1, x);
1195 y = XC{0};
1196 }
1197 }
1198 if(worked) {
1199 output = T{x, y};
1200 return worked;
1201 }
1202 return from_stream(input, output);
1203 }
1204
1205
1206 template <typename T,
1207 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1208 bool lexical_cast(const std::string &input, T &output) {
1209 output = input;
1210 return true;
1211 }
1212
1213
1214 template <
1215 typename T,
1216 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1217 bool lexical_cast(const std::string &input, T &output) {
1218 output = T(input);
1219 return true;
1220 }
1221
1222
1223 template <
1224 typename T,
1225 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1226 bool lexical_cast(const std::string &input, T &output) {
1227 output = widen(input);
1228 return true;
1229 }
1230
1231 template <
1232 typename T,
1233 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1234 bool lexical_cast(const std::string &input, T &output) {
1235 output = T{widen(input)};
1236 return true;
1237 }
1238
1239
1240 template <typename T,
1241 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1242 bool lexical_cast(const std::string &input, T &output) {
1243 typename std::underlying_type<T>::type val;
1244 if(!integral_conversion(input, val)) {
1245 return false;
1246 }
1247 output = static_cast<T>(val);
1248 return true;
1249 }
1250
1251
1252 template <typename T,
1253 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1254 std::is_assignable<T &, typename T::value_type>::value,
1255 detail::enabler> = detail::dummy>
1256 bool lexical_cast(const std::string &input, T &output) {
1257 typename T::value_type val;
1258 if(lexical_cast(input, val)) {
1259 output = val;
1260 return true;
1261 }
1262 return from_stream(input, output);
1263 }
1264
1265 template <typename T,
1266 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1267 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1268 detail::enabler> = detail::dummy>
1269 bool lexical_cast(const std::string &input, T &output) {
1270 typename T::value_type val;
1271 if(lexical_cast(input, val)) {
1272 output = T{val};
1273 return true;
1274 }
1275 return from_stream(input, output);
1276 }
1277
1278
1279 template <
1280 typename T,
1281 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1282 bool lexical_cast(const std::string &input, T &output) {
1283 int val = 0;
1284 if(integral_conversion(input, val)) {
1285 output = T(val);
1286 return true;
1287 }
1288
1289 double dval = 0.0;
1290 if(lexical_cast(input, dval)) {
1291 output = T{dval};
1292 return true;
1293 }
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::integer_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 return from_stream(input, output);
1309 }
1310
1311
1312 template <
1313 typename T,
1314 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1315 bool lexical_cast(const std::string &input, T &output) {
1316 double val = 0.0;
1317 if(lexical_cast(input, val)) {
1318 output = T{val};
1319 return true;
1320 }
1321 return from_stream(input, output);
1322 }
1323
1324
1325 template <typename T,
1326 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1327 detail::enabler> = detail::dummy>
1328 bool lexical_cast(const std::string &input, T &output) {
1329 int val = 0;
1330 if(integral_conversion(input, val)) {
1331 #ifdef _MSC_VER
1332 #pragma warning(push)
1333 #pragma warning(disable : 4800)
1334 #endif
1335
1336
1337 output = val;
1338 #ifdef _MSC_VER
1339 #pragma warning(pop)
1340 #endif
1341 return true;
1342 }
1343
1344
1345
1346 return from_stream(input, output);
1347
1348 }
1349
1350
1351 template <typename T,
1352 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1353 is_istreamable<T>::value,
1354 detail::enabler> = detail::dummy>
1355 bool lexical_cast(const std::string &input, T &output) {
1356 return from_stream(input, output);
1357 }
1358
1359
1360
1361 template <typename T,
1362 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1363 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1364 detail::enabler> = detail::dummy>
1365 bool lexical_cast(const std::string & , T & ) {
1366 static_assert(!std::is_same<T, T>::value,
1367 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1368 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1369 return false;
1370 }
1371
1372
1373
1374 template <typename AssignTo,
1375 typename ConvertTo,
1376 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1377 (classify_object<AssignTo>::value == object_category::string_assignable ||
1378 classify_object<AssignTo>::value == object_category::string_constructible ||
1379 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1380 classify_object<AssignTo>::value == object_category::wstring_constructible),
1381 detail::enabler> = detail::dummy>
1382 bool lexical_assign(const std::string &input, AssignTo &output) {
1383 return lexical_cast(input, output);
1384 }
1385
1386
1387 template <typename AssignTo,
1388 typename ConvertTo,
1389 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1390 classify_object<AssignTo>::value != object_category::string_assignable &&
1391 classify_object<AssignTo>::value != object_category::string_constructible &&
1392 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1393 classify_object<AssignTo>::value != object_category::wstring_constructible,
1394 detail::enabler> = detail::dummy>
1395 bool lexical_assign(const std::string &input, AssignTo &output) {
1396 if(input.empty()) {
1397 output = AssignTo{};
1398 return true;
1399 }
1400
1401 return lexical_cast(input, output);
1402 }
1403
1404
1405 template <typename AssignTo,
1406 typename ConvertTo,
1407 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1408 classify_object<AssignTo>::value == object_category::wrapper_value,
1409 detail::enabler> = detail::dummy>
1410 bool lexical_assign(const std::string &input, AssignTo &output) {
1411 if(input.empty()) {
1412 typename AssignTo::value_type emptyVal{};
1413 output = emptyVal;
1414 return true;
1415 }
1416 return lexical_cast(input, output);
1417 }
1418
1419
1420
1421 template <typename AssignTo,
1422 typename ConvertTo,
1423 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1424 classify_object<AssignTo>::value != object_category::wrapper_value &&
1425 std::is_assignable<AssignTo &, int>::value,
1426 detail::enabler> = detail::dummy>
1427 bool lexical_assign(const std::string &input, AssignTo &output) {
1428 if(input.empty()) {
1429 output = 0;
1430 return true;
1431 }
1432 int val{0};
1433 if(lexical_cast(input, val)) {
1434 #if defined(__clang__)
1435
1436 #pragma clang diagnostic push
1437 #pragma clang diagnostic ignored "-Wsign-conversion"
1438 #endif
1439 output = val;
1440 #if defined(__clang__)
1441 #pragma clang diagnostic pop
1442 #endif
1443 return true;
1444 }
1445 return false;
1446 }
1447
1448
1449 template <typename AssignTo,
1450 typename ConvertTo,
1451 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1452 detail::enabler> = detail::dummy>
1453 bool lexical_assign(const std::string &input, AssignTo &output) {
1454 ConvertTo val{};
1455 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1456 if(parse_result) {
1457 output = val;
1458 }
1459 return parse_result;
1460 }
1461
1462
1463 template <
1464 typename AssignTo,
1465 typename ConvertTo,
1466 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1467 std::is_move_assignable<AssignTo>::value,
1468 detail::enabler> = detail::dummy>
1469 bool lexical_assign(const std::string &input, AssignTo &output) {
1470 ConvertTo val{};
1471 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1472 if(parse_result) {
1473 output = AssignTo(val);
1474 }
1475 return parse_result;
1476 }
1477
1478
1479 template <typename AssignTo,
1480 typename ConvertTo,
1481 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1482 classify_object<AssignTo>::value <= object_category::wrapper_value,
1483 detail::enabler> = detail::dummy>
1484 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1485 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1486 }
1487
1488
1489
1490 template <typename AssignTo,
1491 typename ConvertTo,
1492 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1493 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1494 detail::enabler> = detail::dummy>
1495 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1496
1497 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1498 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1499 FirstType v1;
1500 SecondType v2;
1501 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1502 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1503 if(retval) {
1504 output = AssignTo{v1, v2};
1505 }
1506 return retval;
1507 }
1508
1509
1510 template <class AssignTo,
1511 class ConvertTo,
1512 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1513 type_count<ConvertTo>::value == 1,
1514 detail::enabler> = detail::dummy>
1515 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1516 output.erase(output.begin(), output.end());
1517 if(strings.empty()) {
1518 return true;
1519 }
1520 if(strings.size() == 1 && strings[0] == "{}") {
1521 return true;
1522 }
1523 bool skip_remaining = false;
1524 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1525 skip_remaining = true;
1526 }
1527 for(const auto &elem : strings) {
1528 typename AssignTo::value_type out;
1529 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1530 if(!retval) {
1531 return false;
1532 }
1533 output.insert(output.end(), std::move(out));
1534 if(skip_remaining) {
1535 break;
1536 }
1537 }
1538 return (!output.empty());
1539 }
1540
1541
1542 template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1543 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1544
1545 if(strings.size() >= 2 && !strings[1].empty()) {
1546 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1547 XC2 x{0.0}, y{0.0};
1548 auto str1 = strings[1];
1549 if(str1.back() == 'i' || str1.back() == 'j') {
1550 str1.pop_back();
1551 }
1552 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1553 if(worked) {
1554 output = ConvertTo{x, y};
1555 }
1556 return worked;
1557 }
1558 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1559 }
1560
1561
1562 template <class AssignTo,
1563 class ConvertTo,
1564 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1565 (type_count<ConvertTo>::value == 1),
1566 detail::enabler> = detail::dummy>
1567 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1568 bool retval = true;
1569 output.clear();
1570 output.reserve(strings.size());
1571 for(const auto &elem : strings) {
1572
1573 output.emplace_back();
1574 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1575 }
1576 return (!output.empty()) && retval;
1577 }
1578
1579
1580
1581
1582 template <class AssignTo,
1583 class ConvertTo,
1584 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1585 type_count_base<ConvertTo>::value == 2,
1586 detail::enabler> = detail::dummy>
1587 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1588
1589
1590 template <class AssignTo,
1591 class ConvertTo,
1592 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1593 type_count_base<ConvertTo>::value != 2 &&
1594 ((type_count<ConvertTo>::value > 2) ||
1595 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1596 detail::enabler> = detail::dummy>
1597 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1598
1599
1600 template <class AssignTo,
1601 class ConvertTo,
1602 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1603 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1604 type_count<ConvertTo>::value > 2),
1605 detail::enabler> = detail::dummy>
1606 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1607
1608
1609
1610 template <typename AssignTo,
1611 typename ConvertTo,
1612 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1613 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1614 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1615 detail::enabler> = detail::dummy>
1616 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1617
1618 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1619 ConvertTo val;
1620 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1621 output = AssignTo{val};
1622 return retval;
1623 }
1624 output = AssignTo{};
1625 return true;
1626 }
1627
1628
1629 template <class AssignTo, class ConvertTo, std::size_t I>
1630 inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1631 tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1632 return true;
1633 }
1634
1635
1636 template <class AssignTo, class ConvertTo>
1637 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1638 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1639 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1640 strings.erase(strings.begin());
1641 return retval;
1642 }
1643
1644
1645 template <class AssignTo, class ConvertTo>
1646 inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1647 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1648 bool>::type
1649 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1650 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1651 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1652 return retval;
1653 }
1654
1655
1656 template <class AssignTo, class ConvertTo>
1657 inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1658 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1659 bool>::type
1660 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1661
1662 std::size_t index{subtype_count_min<ConvertTo>::value};
1663 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1664 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1665
1666 while(index < mx) {
1667 if(is_separator(strings[index])) {
1668 break;
1669 }
1670 ++index;
1671 }
1672 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1673 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1674 if(strings.size() > index) {
1675 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1676 } else {
1677 strings.clear();
1678 }
1679 return retval;
1680 }
1681
1682
1683 template <class AssignTo, class ConvertTo, std::size_t I>
1684 inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1685 tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1686 bool retval = true;
1687 using ConvertToElement = typename std::
1688 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1689 if(!strings.empty()) {
1690 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1691 strings, std::get<I>(output));
1692 }
1693 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1694 return retval;
1695 }
1696
1697
1698 template <class AssignTo,
1699 class ConvertTo,
1700 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1701 type_count_base<ConvertTo>::value == 2,
1702 detail::enabler>>
1703 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1704 output.clear();
1705 while(!strings.empty()) {
1706
1707 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1708 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1709 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1710 if(!strings.empty()) {
1711 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1712 }
1713 if(retval) {
1714 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1715 } else {
1716 return false;
1717 }
1718 }
1719 return (!output.empty());
1720 }
1721
1722
1723 template <class AssignTo,
1724 class ConvertTo,
1725 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1726 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1727 type_count<ConvertTo>::value > 2),
1728 detail::enabler>>
1729 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1730 static_assert(
1731 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1732 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1733 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1734 }
1735
1736
1737 template <class AssignTo,
1738 class ConvertTo,
1739 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1740 type_count_base<ConvertTo>::value != 2 &&
1741 ((type_count<ConvertTo>::value > 2) ||
1742 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1743 detail::enabler>>
1744 bool lexical_conversion(const std::vector<std ::string> &strings, AssignTo &output) {
1745 bool retval = true;
1746 output.clear();
1747 std::vector<std::string> temp;
1748 std::size_t ii{0};
1749 std::size_t icount{0};
1750 std::size_t xcm{type_count<ConvertTo>::value};
1751 auto ii_max = strings.size();
1752 while(ii < ii_max) {
1753 temp.push_back(strings[ii]);
1754 ++ii;
1755 ++icount;
1756 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1757 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1758 temp.pop_back();
1759 }
1760 typename AssignTo::value_type temp_out;
1761 retval = retval &&
1762 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1763 temp.clear();
1764 if(!retval) {
1765 return false;
1766 }
1767 output.insert(output.end(), std::move(temp_out));
1768 icount = 0;
1769 }
1770 }
1771 return retval;
1772 }
1773
1774
1775 template <typename AssignTo,
1776 class ConvertTo,
1777 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1778 std::is_assignable<ConvertTo &, ConvertTo>::value,
1779 detail::enabler> = detail::dummy>
1780 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1781 if(strings.empty() || strings.front().empty()) {
1782 output = ConvertTo{};
1783 return true;
1784 }
1785 typename ConvertTo::value_type val;
1786 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1787 output = ConvertTo{val};
1788 return true;
1789 }
1790 return false;
1791 }
1792
1793
1794 template <typename AssignTo,
1795 class ConvertTo,
1796 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1797 !std::is_assignable<AssignTo &, ConvertTo>::value,
1798 detail::enabler> = detail::dummy>
1799 bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1800 using ConvertType = typename ConvertTo::value_type;
1801 if(strings.empty() || strings.front().empty()) {
1802 output = ConvertType{};
1803 return true;
1804 }
1805 ConvertType val;
1806 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1807 output = val;
1808 return true;
1809 }
1810 return false;
1811 }
1812
1813
1814 inline std::string sum_string_vector(const std::vector<std::string> &values) {
1815 double val{0.0};
1816 bool fail{false};
1817 std::string output;
1818 for(const auto &arg : values) {
1819 double tv{0.0};
1820 auto comp = lexical_cast(arg, tv);
1821 if(!comp) {
1822 errno = 0;
1823 auto fv = detail::to_flag_value(arg);
1824 fail = (errno != 0);
1825 if(fail) {
1826 break;
1827 }
1828 tv = static_cast<double>(fv);
1829 }
1830 val += tv;
1831 }
1832 if(fail) {
1833 for(const auto &arg : values) {
1834 output.append(arg);
1835 }
1836 } else {
1837 std::ostringstream out;
1838 out.precision(16);
1839 out << val;
1840 output = out.str();
1841 }
1842 return output;
1843 }
1844
1845 }
1846
1847 }