14 #include <type_traits> 41 template <
bool B,
class T =
void>
using enable_if_t =
typename std::enable_if<B, T>::type;
50 template <
bool B,
class T,
class F>
using conditional_t =
typename std::conditional<B, T, F>::type;
53 template <
typename T>
struct is_bool : std::false_type {};
56 template <>
struct is_bool<bool> : std::true_type {};
65 template <
typename T>
struct is_shared_ptr<const
std::shared_ptr<T>> : std::true_type {};
89 using type =
typename std::pointer_traits<T>::element_type;
97 template <
typename T,
typename _ =
void>
struct pair_adaptor : std::false_type {
99 using first_type =
typename std::remove_const<value_type>::type;
103 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
104 return std::forward<Q>(pair_value);
107 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
108 return std::forward<Q>(pair_value);
114 template <
typename T>
117 conditional_t<false,
void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
120 using first_type =
typename std::remove_const<typename value_type::first_type>::type;
121 using second_type =
typename std::remove_const<typename value_type::second_type>::type;
124 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
125 return std::get<0>(std::forward<Q>(pair_value));
128 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
129 return std::get<1>(std::forward<Q>(pair_value));
140 #pragma GCC diagnostic push 141 #pragma GCC diagnostic ignored "-Wnarrowing" 145 template <
typename TT,
typename CC>
146 static auto test(
int, std::true_type) -> decltype(
149 #pragma diag_suppress 2361
151 TT { std::declval<CC>() }
153 #pragma diag_default 2361 156 std::is_move_assignable<TT>());
158 template <
typename TT,
typename CC>
static auto test(
int, std::false_type) -> std::false_type;
160 template <
typename,
typename>
static auto test(...) -> std::false_type;
163 static constexpr
bool value = decltype(test<T, C>(0,
typename std::is_constructible<T, C>::type()))::
value;
166 #pragma GCC diagnostic pop 173 template <
typename TT,
typename SS>
174 static auto test(
int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
176 template <
typename,
typename>
static auto test(...) -> std::false_type;
179 static constexpr
bool value = decltype(test<T, S>(0))::
value;
184 template <
typename TT,
typename SS>
185 static auto test(
int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
187 template <
typename,
typename>
static auto test(...) -> std::false_type;
190 static constexpr
bool value = decltype(test<T, S>(0))::
value;
195 template <
typename TT>
196 static auto test(
int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
198 template <
typename>
static auto test(...) -> std::false_type;
207 std::istringstream is;
210 return !is.fail() && !is.rdbuf()->in_avail();
224 template <
typename T>
228 void_t<typename T::value_type,
229 decltype(std::declval<T>().end()),
230 decltype(std::declval<T>().clear()),
231 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
232 std::declval<const typename T::value_type &>()))>,
234 :
public conditional_t<std::is_constructible<T, std::string>::value, std::false_type, std::true_type> {};
242 template <
typename T>
245 conditional_t<false,
void_t<decltype(
std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
246 :
public std::true_type {};
249 template <
typename T,
typename _ =
void>
struct is_wrapper : std::false_type {};
252 template <
typename T>
257 template <
typename SS>
260 static auto test(
int) -> decltype(std::tuple_size<
typename std::decay<SS>::type>::
value, std::true_type{});
261 template <
typename>
static auto test(...) -> std::false_type;
269 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
270 return std::forward<T>(value);
274 template <
typename T,
278 return std::string(value);
282 template <
typename T,
287 std::stringstream stream;
293 template <
typename T,
295 !is_readable_container<typename std::remove_const<T>::type>::value,
298 return std::string{};
302 template <
typename T,
304 is_readable_container<T>::value,
307 std::vector<std::string> defaults;
308 auto cval = variable.begin();
309 auto end = variable.end();
318 template <
typename T1,
323 return to_string(std::forward<T>(value));
327 template <
typename T1,
332 return std::string{};
342 return std::to_string(
static_cast<typename std::underlying_type<T>::type
>(value));
345 template <
typename T,
352 template <
typename T,
typename def,
typename Enable =
void>
struct wrapped_type {
using type = def; };
355 template <
typename T,
typename def>
struct wrapped_type<T, def, typename
std::enable_if<is_wrapper<T>::value>
::type> {
356 using type =
typename T::value_type;
363 template <
typename T>
365 typename
std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
366 !std::is_void<T>::value>::type> {
371 template <
typename T>
372 struct type_count_base<T, typename
std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
373 static constexpr
int value{std::tuple_size<T>::value};
377 template <
typename T>
struct type_count_base<T, typename
std::enable_if<is_mutable_container<T>::value>::type> {
390 template <
typename T,
typename Enable =
void>
struct type_count {
static const int value{0}; };
393 template <
typename T>
395 typename
std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
396 !std::is_void<T>::value>::type> {
401 template <
typename T>
struct type_count<T, typename
std::enable_if<is_complex<T>::value>::type> {
406 template <
typename T>
struct type_count<T, typename
std::enable_if<is_mutable_container<T>::value>::type> {
411 template <
typename T>
413 typename
std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
414 !is_mutable_container<T>::value>::type> {
419 template <
typename T, std::
size_t I>
420 constexpr
typename std::enable_if<I == type_count_base<T>::value,
int>::type
tuple_type_size() {
425 template <
typename T, std::
size_t I>
426 constexpr
typename std::enable_if < I<type_count_base<T>::value,
int>::type
tuple_type_size() {
427 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
431 template <
typename T>
struct type_count<T, typename
std::enable_if<is_tuple_like<T>::value>::type> {
432 static constexpr
int value{tuple_type_size<T, 0>()};
436 template <
typename T>
struct subtype_count {
441 template <
typename T,
typename Enable =
void>
struct type_count_min {
static const int value{0}; };
444 template <
typename T>
445 struct type_count_min<
447 typename
std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
448 !is_complex<T>::value && !std::is_void<T>::value>::type> {
453 template <
typename T>
struct type_count_min<T, typename
std::enable_if<is_complex<T>::value>::type> {
454 static constexpr
int value{1};
458 template <
typename T>
459 struct type_count_min<
461 typename
std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
462 static constexpr
int value{subtype_count_min<typename T::value_type>::value};
466 template <
typename T, std::
size_t I>
467 constexpr
typename std::enable_if<I == type_count_base<T>::value,
int>::type tuple_type_size_min() {
472 template <
typename T, std::
size_t I>
473 constexpr
typename std::enable_if < I<type_count_base<T>::value,
int>::type tuple_type_size_min() {
474 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
478 template <
typename T>
struct type_count_min<T, typename
std::enable_if<is_tuple_like<T>::value>::type> {
479 static constexpr
int value{tuple_type_size_min<T, 0>()};
483 template <
typename T>
struct subtype_count_min {
484 static constexpr
int value{is_mutable_container<T>::value
486 : type_count_min<T>::value};
490 template <
typename T,
typename Enable =
void>
struct expected_count {
static const int value{0}; };
493 template <
typename T>
494 struct expected_count<T,
495 typename
std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
496 !std::is_void<T>::value>::type> {
497 static constexpr
int value{1};
500 template <
typename T>
struct expected_count<T, typename
std::enable_if<is_mutable_container<T>::value>::type> {
505 template <
typename T>
506 struct expected_count<T, typename
std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
507 static constexpr
int value{expected_count<typename T::value_type>::value};
511 enum class object_category : int {
514 unsigned_integral = 4,
518 number_constructible = 12,
519 double_constructible = 14,
520 integer_constructible = 16,
522 string_assignable = 23,
523 string_constructible = 24,
529 container_value = 80,
536 template <
typename T,
typename Enable =
void>
struct classify_object {
537 static constexpr object_category value{object_category::other};
541 template <
typename T>
542 struct classify_object<
544 typename
std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
545 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
546 static constexpr object_category value{object_category::integral_value};
550 template <
typename T>
551 struct classify_object<T,
552 typename
std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
553 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
554 static constexpr object_category value{object_category::unsigned_integral};
558 template <
typename T>
559 struct classify_object<T, typename
std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
560 static constexpr object_category value{object_category::char_value};
564 template <
typename T>
struct classify_object<T, typename
std::enable_if<is_bool<T>::value>::type> {
565 static constexpr object_category value{object_category::boolean_value};
569 template <
typename T>
struct classify_object<T, typename
std::enable_if<std::is_floating_point<T>::value>::type> {
570 static constexpr object_category value{object_category::floating_point};
574 template <
typename T>
575 struct classify_object<T,
576 typename
std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
577 std::is_assignable<T &, std::string>::value>::type> {
578 static constexpr object_category value{object_category::string_assignable};
582 template <
typename T>
583 struct classify_object<
585 typename
std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
586 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
587 std::is_constructible<T, std::string>::value>::type> {
588 static constexpr object_category value{object_category::string_constructible};
592 template <
typename T>
struct classify_object<T, typename
std::enable_if<std::is_enum<T>::value>::type> {
593 static constexpr object_category value{object_category::enumeration};
596 template <
typename T>
struct classify_object<T, typename
std::enable_if<is_complex<T>::value>::type> {
597 static constexpr object_category value{object_category::complex_number};
602 template <
typename T>
struct uncommon_type {
603 using type =
typename std::conditional<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
604 !std::is_assignable<T &, std::string>::value &&
606 !is_mutable_container<T>::value && !std::is_enum<T>::value,
608 std::false_type>::type;
609 static constexpr
bool value = type::value;
613 template <
typename T>
614 struct classify_object<T,
615 typename
std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
616 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
617 static constexpr object_category value{object_category::wrapper_value};
621 template <
typename T>
622 struct classify_object<T,
623 typename
std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
624 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
625 is_direct_constructible<T, int>::value>::type> {
626 static constexpr object_category value{object_category::number_constructible};
630 template <
typename T>
631 struct classify_object<T,
632 typename
std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
633 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
634 is_direct_constructible<T, int>::value>::type> {
635 static constexpr object_category value{object_category::integer_constructible};
639 template <
typename T>
640 struct classify_object<T,
641 typename
std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
642 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
643 !is_direct_constructible<T, int>::value>::type> {
644 static constexpr object_category value{object_category::double_constructible};
648 template <
typename T>
649 struct classify_object<
651 typename
std::enable_if<is_tuple_like<T>::value &&
652 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
653 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
654 !is_direct_constructible<T, int>::value))>::type> {
655 static constexpr object_category value{object_category::tuple_value};
664 template <
typename T>
struct classify_object<T, typename
std::enable_if<is_mutable_container<T>::value>::type> {
665 static constexpr object_category value{object_category::container_value};
674 template <
typename T,
680 template <
typename T,
681 enable_if_t<classify_object<T>::value == object_category::integral_value ||
682 classify_object<T>::value == object_category::integer_constructible,
688 template <
typename T,
694 template <
typename T,
695 enable_if_t<classify_object<T>::value == object_category::floating_point ||
696 classify_object<T>::value == object_category::number_constructible ||
697 classify_object<T>::value == object_category::double_constructible,
704 template <
typename T,
711 template <
typename T,
718 template <
typename T,
725 template <
typename T,
726 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
727 classify_object<T>::value <= object_category::other,
733 template <
typename T,
739 template <
typename T,
740 enable_if_t<classify_object<T>::value == object_category::container_value ||
741 classify_object<T>::value == object_category::wrapper_value,
746 template <
typename T,
750 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
754 template <
typename T, std::
size_t I>
755 inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
756 return std::string{};
760 template <
typename T, std::
size_t I>
761 inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
762 std::string str = std::string(
type_name<
typename std::decay<
typename std::tuple_element<I, T>::type>::type>()) +
763 ',' + tuple_name<T, I + 1>();
764 if(str.back() ==
',')
770 template <
typename T,
774 auto tname = std::string(1,
'[') + tuple_name<T, 0>();
775 tname.push_back(
']');
780 template <
typename T,
782 classify_object<T>::value == object_category::wrapper_value,
785 return type_name<typename T::value_type>();
797 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
798 output =
static_cast<T
>(output_ll);
799 return val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll;
809 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
810 output =
static_cast<T
>(output_ll);
811 return val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll;
816 static const std::string trueString(
"true");
817 static const std::string falseString(
"false");
818 if(val == trueString) {
821 if(val == falseString) {
826 if(val.size() == 1) {
827 if(val[0] >=
'1' && val[0] <=
'9') {
828 return (static_cast<std::int64_t>(val[0]) -
'0');
843 throw std::invalid_argument(
"unrecognized character");
847 if(val == trueString || val ==
"on" || val ==
"yes" || val ==
"enable") {
849 }
else if(val == falseString || val ==
"off" || val ==
"no" || val ==
"disable") {
852 ret = std::stoll(val);
858 template <
typename T,
860 classify_object<T>::value == object_category::unsigned_integral,
867 template <
typename T,
869 bool lexical_cast(
const std::string &input, T &output) {
870 if(input.size() == 1) {
871 output =
static_cast<T
>(input[0]);
878 template <
typename T,
880 bool lexical_cast(
const std::string &input, T &output) {
885 }
catch(
const std::invalid_argument &) {
887 }
catch(
const std::out_of_range &) {
890 output = (input[0] !=
'-');
896 template <
typename T,
898 bool lexical_cast(
const std::string &input, T &output) {
903 auto output_ld = std::strtold(input.c_str(), &val);
904 output =
static_cast<T
>(output_ld);
905 return val == (input.c_str() + input.size());
909 template <
typename T,
911 bool lexical_cast(
const std::string &input, T &output) {
916 auto nloc = str1.find_last_of(
"+-");
917 if(nloc != std::string::npos && nloc > 0) {
919 str1 = str1.substr(nloc);
920 if(str1.back() ==
'i' || str1.back() ==
'j')
924 if(str1.back() ==
'i' || str1.back() ==
'j') {
941 template <
typename T,
943 bool lexical_cast(
const std::string &input, T &output) {
952 bool lexical_cast(
const std::string &input, T &output) {
958 template <
typename T,
960 bool lexical_cast(
const std::string &input, T &output) {
961 typename std::underlying_type<T>::type val;
965 output =
static_cast<T
>(val);
970 template <
typename T,
971 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
972 std::is_assignable<T &, typename T::value_type>::value,
974 bool lexical_cast(
const std::string &input, T &output) {
975 typename T::value_type val;
983 template <
typename T,
984 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
985 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
987 bool lexical_cast(
const std::string &input, T &output) {
988 typename T::value_type val;
1000 bool lexical_cast(
const std::string &input, T &output) {
1019 bool lexical_cast(
const std::string &input, T &output) {
1032 bool lexical_cast(
const std::string &input, T &output) {
1042 template <
typename T,
1043 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1045 bool lexical_cast(
const std::string &input, T &output) {
1049 #pragma warning(push) 1050 #pragma warning(disable : 4800) 1056 #pragma warning(pop) 1068 template <
typename T,
1069 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value,
1071 bool lexical_cast(
const std::string &input, T &output) {
1073 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it " 1074 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1080 template <
typename AssignTo,
1082 enable_if_t<std::is_same<AssignTo, ConvertTo>::value &&
1083 (classify_object<AssignTo>::value == object_category::string_assignable ||
1084 classify_object<AssignTo>::value == object_category::string_constructible),
1091 template <
typename AssignTo,
1094 classify_object<AssignTo>::value != object_category::string_assignable &&
1095 classify_object<AssignTo>::value != object_category::string_constructible,
1097 bool lexical_assign(
const std::string &input, AssignTo &output) {
1099 output = AssignTo{};
1107 template <
typename AssignTo,
1109 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1110 classify_object<AssignTo>::value == object_category::wrapper_value,
1112 bool lexical_assign(
const std::string &input, AssignTo &output) {
1114 typename AssignTo::value_type emptyVal{};
1123 template <
typename AssignTo,
1125 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1126 classify_object<AssignTo>::value != object_category::wrapper_value &&
1127 std::is_assignable<AssignTo &, int>::value,
1129 bool lexical_assign(
const std::string &input, AssignTo &output) {
1143 template <
typename AssignTo,
1145 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1147 bool lexical_assign(
const std::string &input, AssignTo &output) {
1149 bool parse_result = (!input.empty()) ? lexical_cast<ConvertTo>(input, val) :
true;
1153 return parse_result;
1160 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1161 std::is_move_assignable<AssignTo>::value,
1163 bool lexical_assign(
const std::string &input, AssignTo &output) {
1165 bool parse_result = input.empty() ? true :
lexical_cast<ConvertTo>(input, val);
1167 output = AssignTo(val);
1169 return parse_result;
1173 template <
typename AssignTo,
1175 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1176 classify_object<AssignTo>::value <= object_category::wrapper_value,
1178 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1179 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1184 template <
typename AssignTo,
1186 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1189 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1191 typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type v1;
1192 typename std::tuple_element<1, ConvertTo>::type v2;
1193 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[0], v1);
1194 if(strings.size() > 1) {
1195 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[1], v2);
1198 output = AssignTo{v1, v2};
1204 template <
class AssignTo,
1206 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1209 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1210 output.erase(output.begin(), output.end());
1211 for(
const auto &elem : strings) {
1212 typename AssignTo::value_type out;
1213 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1217 output.insert(output.end(), std::move(out));
1219 return (!output.empty());
1224 bool lexical_conversion(
const std::vector<std::string> &strings, AssignTo &output) {
1226 if(strings.size() >= 2 && !strings[1].empty()) {
1229 auto str1 = strings[1];
1230 if(str1.back() ==
'i' || str1.back() ==
'j') {
1235 output = ConvertTo{x, y};
1239 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1244 template <
class AssignTo,
1246 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1249 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1252 output.reserve(strings.size());
1253 for(
const auto &elem : strings) {
1255 output.emplace_back();
1256 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1258 return (!output.empty()) && retval;
1264 template <
class AssignTo,
1266 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1269 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1272 template <
class AssignTo,
1274 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1279 bool lexical_conversion(
const std::vector<std::string> &strings, AssignTo &output);
1282 template <
class AssignTo,
1288 bool lexical_conversion(
const std::vector<std::string> &strings, AssignTo &output);
1292 template <
typename AssignTo,
1294 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1295 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1298 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1300 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1302 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1303 output = AssignTo{val};
1306 output = AssignTo{};
1311 template <
class AssignTo,
class ConvertTo, std::
size_t I>
1313 tuple_conversion(
const std::vector<std::string> &, AssignTo &) {
1318 template <
class AssignTo,
class ConvertTo>
1320 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1321 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1322 strings.erase(strings.begin());
1327 template <
class AssignTo,
class ConvertTo>
1331 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1332 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1338 template <
class AssignTo,
class ConvertTo>
1339 inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1342 tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1344 std::size_t index{subtype_count_min<ConvertTo>::value};
1345 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1346 const std::size_t mx{(std::max)(mx_count, strings.size())};
1354 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1355 std::vector<std::string>(strings.begin(), strings.begin() +
static_cast<std::ptrdiff_t
>(index)), output);
1356 strings.erase(strings.begin(), strings.begin() +
static_cast<std::ptrdiff_t
>(index) + 1);
1361 template <
class AssignTo,
class ConvertTo, std::
size_t I>
1362 inline typename std::enable_if<(I < type_count_base<AssignTo>::value),
bool>::type
1363 tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1365 using ConvertToElement =
typename std::
1366 conditional<is_tuple_like<ConvertTo>::value,
typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1367 if(!strings.empty()) {
1368 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1369 strings, std::get<I>(output));
1371 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1376 template <
class AssignTo,
1378 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1381 bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1383 while(!strings.empty()) {
1385 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1;
1386 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2;
1387 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1388 if(!strings.empty()) {
1389 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1392 output.insert(output.end(),
typename AssignTo::value_type{v1, v2});
1397 return (!output.empty());
1401 template <
class AssignTo,
1407 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1410 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1411 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1415 template <
class AssignTo,
1417 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1422 bool lexical_conversion(
const std::vector<std ::string> &strings, AssignTo &output) {
1425 std::vector<std::string> temp;
1427 std::size_t icount{0};
1429 auto ii_max = strings.size();
1430 while(ii < ii_max) {
1431 temp.push_back(strings[ii]);
1434 if(icount == xcm ||
is_separator(temp.back()) || ii == ii_max) {
1435 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value &&
is_separator(temp.back())) {
1438 typename AssignTo::value_type temp_out;
1440 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1445 output.insert(output.end(), std::move(temp_out));
1453 template <
typename AssignTo,
1455 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1456 std::is_assignable<ConvertTo &, ConvertTo>::value,
1458 bool lexical_conversion(
const std::vector<std::string> &strings, AssignTo &output) {
1459 if(strings.empty() || strings.front().empty()) {
1460 output = ConvertTo{};
1463 typename ConvertTo::value_type val;
1464 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1465 output = ConvertTo{val};
1472 template <
typename AssignTo,
1474 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1475 !std::is_assignable<AssignTo &, ConvertTo>::value,
1477 bool lexical_conversion(
const std::vector<std::string> &strings, AssignTo &output) {
1478 using ConvertType =
typename ConvertTo::value_type;
1479 if(strings.empty() || strings.front().empty()) {
1480 output = ConvertType{};
1484 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1497 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
1498 std::int64_t count{0};
1499 for(
auto &flag : flags) {
1502 output = (count > 0) ? static_cast<T>(count) : T{0};
1511 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
1512 std::int64_t count{0};
1513 for(
auto &flag : flags) {
1516 output =
static_cast<T
>(count);
1520 #pragma warning(push) 1521 #pragma warning(disable : 4800) 1531 template <
typename T,
1533 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
1534 std::int64_t count{0};
1535 for(
auto &flag : flags) {
1543 #pragma warning(pop) typename std::conditional< B, T, F >::type conditional_t
A copy of std::conditional_t from C++14 - same reasoning as enable_if_t, it does not hurt to redefine...
Definition: TypeTools.hpp:50
void type
Definition: TypeTools.hpp:44
typename std::remove_const< typename value_type::second_type >::type second_type
Definition: TypeTools.hpp:121
typename T::value_type value_type
Definition: TypeTools.hpp:119
static constexpr bool value
Definition: TypeTools.hpp:163
typename std::remove_const< value_type >::type second_type
Definition: TypeTools.hpp:100
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition: TypeTools.hpp:97
Definition: TypeTools.hpp:94
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:30
Check for complex.
Definition: TypeTools.hpp:194
A copy of std::void_t from C++17 (helper for C++11 and C++14)
Definition: TypeTools.hpp:44
static constexpr bool value
Definition: TypeTools.hpp:190
static auto first(Q &&pair_value) -> decltype(std::get< 0 >(std::forward< Q >(pair_value)))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:124
Check to see if something is bool (fail check by default)
Definition: TypeTools.hpp:53
bool is_separator(const std::string &str)
check if a string is a container segment separator (empty or "%%")
Definition: StringTools.hpp:248
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:99
bool lexical_assign(const std::string &input, AssignTo &output)
Assign a value through lexical cast operations.
Definition: TypeTools.hpp:1086
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:862
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:107
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:33
template to get the underlying value type if it exists or use a default
Definition: TypeTools.hpp:352
def type
Definition: TypeTools.hpp:352
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:269
bool integral_conversion(const std::string &input, T &output) noexcept
Convert to an unsigned integral.
Definition: TypeTools.hpp:792
This can be specialized to override the type deduction for IsMember.
Definition: TypeTools.hpp:73
typename make_void< Ts... >::type void_t
A copy of std::void_t from C++17 - same reasoning as enable_if_t, it does not hurt to redefine...
Definition: TypeTools.hpp:47
Definition: TypeTools.hpp:172
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:63
This will only trigger for actual void type.
Definition: TypeTools.hpp:360
typename std::remove_const< typename value_type::first_type >::type first_type
Definition: TypeTools.hpp:120
static bool const value
Definition: TypeTools.hpp:69
constexpr std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size() { return subtype_count< typename std::tuple_element< I, T >::type >::value+tuple_type_size< T, I+1 >);}template< typename T > struct type_count< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size< T, 0 >)};};template< typename T > struct subtype_count { static constexpr int value{is_mutable_container< T >::value ? expected_max_vector_size :type_count< T >::value};};template< typename T, typename Enable=void > struct type_count_min { static const int value{0};};template< typename T >struct type_count_min< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_tuple_like< T >::value &&!is_wrapper< T >::value &&!is_complex< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{type_count< T >::value};};template< typename T > struct type_count_min< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr int value{1};};template< typename T >struct type_count_min< T, typename std::enable_if< is_wrapper< T >::value &&!is_complex< T >::value &&!is_tuple_like< T >::value >::type > { static constexpr int value{subtype_count_min< typename T::value_type >::value};};template< typename T, std::size_t I >constexpr typename std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size_min() { return 0;}template< typename T, std::size_t I > constexpr typename std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size_min() { return subtype_count_min< typename std::tuple_element< I, T >::type >::value+tuple_type_size_min< T, I+1 >);}template< typename T > struct type_count_min< T, typename std::enable_if< is_tuple_like< T >::value >::type > { static constexpr int value{tuple_type_size_min< T, 0 >)};};template< typename T > struct subtype_count_min { static constexpr int value{is_mutable_container< T >::value ?((type_count< T >::value< expected_max_vector_size) ? type_count< T >::value :0) :type_count_min< T >::value};};template< typename T, typename Enable=void > struct expected_count { static const int value{0};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_wrapper< T >::value &&!std::is_void< T >::value >::type > { static constexpr int value{1};};template< typename T > struct expected_count< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr int value{expected_max_vector_size};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&is_wrapper< T >::value >::type > { static constexpr int value{expected_count< typename T::value_type >::value};};enum class object_category :int { char_value=1, integral_value=2, unsigned_integral=4, enumeration=6, boolean_value=8, floating_point=10, number_constructible=12, double_constructible=14, integer_constructible=16, string_assignable=23, string_constructible=24, other=45, wrapper_value=50, complex_number=60, tuple_value=70, container_value=80,};template< typename T, typename Enable=void > struct classify_object { static constexpr object_category value{object_category::other};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, char >::value &&std::is_signed< T >::value &&!is_bool< T >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::integral_value};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&!std::is_same< T, char >::value &&!is_bool< T >::value >::type > { static constexpr object_category value{object_category::unsigned_integral};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_same< T, char >::value &&!std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::char_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_bool< T >::value >::type > { static constexpr object_category value{object_category::boolean_value};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_floating_point< T >::value >::type > { static constexpr object_category value{object_category::floating_point};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&std::is_assignable< T &, std::string >::value >::type > { static constexpr object_category value{object_category::string_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&(type_count< T >::value==1) &&std::is_constructible< T, std::string >::value >::type > { static constexpr object_category value{object_category::string_constructible};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_enum< T >::value >::type > { static constexpr object_category value{object_category::enumeration};};template< typename T > struct classify_object< T, typename std::enable_if< is_complex< T >::value >::type > { static constexpr object_category value{object_category::complex_number};};template< typename T > struct uncommon_type { using type=typename std::conditional<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&!std::is_constructible< T, std::string >::value &&!is_complex< T >::value &&!is_mutable_container< T >::value &&!std::is_enum< T >::value, std::true_type, std::false_type >::type;static constexpr bool value=type::value;};template< typename T >struct classify_object< T, typename std::enable_if<(!is_mutable_container< T >::value &&is_wrapper< T >::value &&!is_tuple_like< T >::value &&uncommon_type< T >::value)>::type > { static constexpr object_category value{object_category::wrapper_value};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::number_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&!is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::integer_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value >::type > { static constexpr object_category value{object_category::double_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< is_tuple_like< T >::value &&((type_count< T >::value >=2 &&!is_wrapper< T >::value)||(uncommon_type< T >::value &&!is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value))>::type > { static constexpr object_category value{object_category::tuple_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_mutable_container< T >::value >::type > { static constexpr object_category value{object_category::container_value};};template< typename T, enable_if_t< classify_object< T >::value==object_category::char_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "CHAR";}template< typename T, enable_if_t< classify_object< T >::value==object_category::integral_value||classify_object< T >::value==object_category::integer_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "INT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::unsigned_integral, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "UINT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::floating_point||classify_object< T >::value==object_category::number_constructible||classify_object< T >::value==object_category::double_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "FLOAT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::enumeration, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "ENUM";}template< typename T, enable_if_t< classify_object< T >::value==object_category::boolean_value, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "BOOLEAN";}template< typename T, enable_if_t< classify_object< T >::value==object_category::complex_number, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "COMPLEX";}template< typename T, enable_if_t< classify_object< T >::value >=object_category::string_assignable &&classify_object< T >::value<=object_category::other, detail::enabler >=detail::dummy >constexpr const char *type_name() { return "TEXT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::container_value||classify_object< T >::value==object_category::wrapper_value, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value==1, detail::enabler >=detail::dummy >inline std::string type_name() { return type_name< typename std::decay< typename std::tuple_element< 0, T >::type >::type >);}template< typename T, std::size_t I >inline typename std::enable_if< I==type_count_base< T >::value, std::string >::type tuple_name() { return std::string{};}template< typename T, std::size_t I >inline typename std::enable_if<(I< type_count_base< T >::value), std::string >::type tuple_name() { std::string str=std::string(type_name< typename std::decay< typename std::tuple_element< I, T >::type >::type >))+','+tuple_name< T, I+1 >);if(str.back()==',') str.pop_back();return str;}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler > > std::string type_name()
Recursively generate the tuple type name.
Definition: TypeTools.hpp:773
forward declare the subtype_count_min structure
Definition: TypeTools.hpp:387
std::int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition: TypeTools.hpp:815
static const int value
Definition: TypeTools.hpp:360
auto checked_to_string(T &&value) -> decltype(to_string(std::forward< T >(value)))
special template overload
Definition: TypeTools.hpp:322
Set of overloads to get the type size of an object.
Definition: TypeTools.hpp:384
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:41
Definition: TypeTools.hpp:219
static const int value
Definition: TypeTools.hpp:390
not a pointer
Definition: TypeTools.hpp:86
static auto second(Q &&pair_value) -> decltype(std::get< 1 >(std::forward< Q >(pair_value)))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:128
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:259
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:103
Definition: TypeTools.hpp:249
typename element_type< T >::type::value_type type
Definition: TypeTools.hpp:94
This will only trigger for actual void type.
Definition: TypeTools.hpp:390
Check to see if something is a shared pointer.
Definition: TypeTools.hpp:59
typename std::pointer_traits< T >::element_type type
Definition: TypeTools.hpp:89
constexpr std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size()
0 if the index > tuple size
Definition: TypeTools.hpp:420
static constexpr bool value
Definition: TypeTools.hpp:264
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:336
Definition: TypeTools.hpp:256
T type
Definition: TypeTools.hpp:73
static constexpr bool value
Definition: TypeTools.hpp:179
typename T::value_type type
Definition: TypeTools.hpp:356
bool from_stream(const std::string &istring, T &obj)
Templated operation to get a value from a stream.
Definition: TypeTools.hpp:206
static constexpr bool value
Definition: TypeTools.hpp:201
Check to see if something is copyable pointer.
Definition: TypeTools.hpp:68
Check for input streamability.
Definition: TypeTools.hpp:183
Definition: TypeTools.hpp:144
typename T::value_type value_type
Definition: TypeTools.hpp:98
Definition: TypeTools.hpp:237
std::string type
Definition: TypeTools.hpp:76
std::string to_string(T &&value)
Convert an object to a string (streaming must be supported for that type)
Definition: TypeTools.hpp:286
constexpr int expected_max_vector_size
Definition: StringTools.hpp:43
T type
Definition: TypeTools.hpp:86