11 #include <type_traits>
33 template <
bool B,
class T =
void>
using enable_if_t =
typename std::enable_if<B, T>::type;
42 template <
bool B,
class T,
class F>
using conditional_t =
typename std::conditional<B, T, F>::type;
45 template <
typename T>
struct is_vector : std::false_type {};
48 template <
class T,
class A>
struct is_vector<std::vector<T, A>> : std::true_type {};
51 template <
class T,
class A>
struct is_vector<const std::vector<T, A>> : std::true_type {};
54 template <
typename T>
struct is_bool : std::false_type {};
57 template <>
struct is_bool<bool> : std::true_type {};
63 template <
typename T>
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
66 template <
typename T>
struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
89 template <
typename T>
struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>
::type> {
90 using type =
typename std::pointer_traits<T>::element_type;
98 template <
typename T,
typename _ =
void>
struct pair_adaptor : std::false_type {
100 using first_type =
typename std::remove_const<value_type>::type;
104 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
105 return std::forward<Q>(pair_value);
108 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
109 return std::forward<Q>(pair_value);
115 template <
typename T>
118 conditional_t<false,
void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
121 using first_type =
typename std::remove_const<typename value_type::first_type>::type;
122 using second_type =
typename std::remove_const<typename value_type::second_type>::type;
125 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
126 return std::get<0>(std::forward<Q>(pair_value));
129 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
130 return std::get<1>(std::forward<Q>(pair_value));
141 #pragma GCC diagnostic push
142 #pragma GCC diagnostic ignored "-Wnarrowing"
146 template <
typename TT,
typename CC>
147 static auto test(
int, std::true_type) -> decltype(
150 #pragma diag_suppress 2361
152 TT { std::declval<CC>() }
154 #pragma diag_default 2361
157 std::is_move_assignable<TT>());
159 template <
typename TT,
typename CC>
static auto test(
int, std::false_type) -> std::false_type;
161 template <
typename,
typename>
static auto test(...) -> std::false_type;
164 static constexpr
bool value = decltype(test<T, C>(0,
typename std::is_constructible<T, C>::type()))::
value;
167 #pragma GCC diagnostic pop
174 template <
typename TT,
typename SS>
175 static auto test(
int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
177 template <
typename,
typename>
static auto test(...) -> std::false_type;
180 static constexpr
bool value = decltype(test<T, S>(0))::
value;
185 template <
typename TT,
typename SS>
186 static auto test(
int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
188 template <
typename,
typename>
static auto test(...) -> std::false_type;
191 static constexpr
bool value = decltype(test<T, S>(0))::
value;
197 std::istringstream is;
200 return !is.fail() && !is.rdbuf()->in_avail();
210 template <
typename SS>
213 static auto test(
int) -> decltype(std::tuple_size<SS>::value, std::true_type{});
214 template <
typename>
static auto test(...) -> std::false_type;
222 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
223 return std::forward<T>(value);
227 template <
typename T,
231 std::stringstream stream;
237 template <
typename T,
242 return std::string{};
246 template <
typename T,
248 is_vector<typename std::remove_reference<typename std::remove_const<T>::type>::type>::value,
251 std::vector<std::string> defaults;
252 defaults.reserve(variable.size());
253 auto cval = variable.begin();
254 auto end = variable.end();
263 template <
typename T1,
268 return to_string(std::forward<T>(value));
272 template <
typename T1,
277 return std::string{};
287 return std::to_string(
static_cast<typename std::underlying_type<T>::type
>(value));
290 template <
typename T,
297 template <
typename T,
typename Enable =
void>
struct type_count {
static const int value{0}; };
300 template <
typename T>
struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
301 static constexpr
int value{std::tuple_size<T>::value};
304 template <
typename T>
307 typename std::enable_if<!is_vector<T>::value && !is_tuple_like<T>::value && !std::is_void<T>::value>::type> {
312 template <
typename T>
struct type_count<T, typename std::enable_if<is_vector<T>::value>::type> {
321 template <
typename T>
322 struct expected_count<T, typename std::enable_if<!is_vector<T>::value && !std::is_void<T>::value>::type> {
326 template <
typename T>
struct expected_count<T, typename std::enable_if<is_vector<T>::value>::type> {
355 template <
typename T>
357 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value &&
358 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
363 template <
typename T>
366 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && !is_bool<T>::value>::type> {
371 template <
typename T>
struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
376 template <
typename T>
struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
381 template <
typename T>
384 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
385 std::is_assignable<T &, std::string>::value && !is_vector<T>::value>::type> {
390 template <
typename T>
393 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
394 !std::is_assignable<T &, std::string>::value &&
395 std::is_constructible<T, std::string>::value && !is_vector<T>::value>::type> {
400 template <
typename T>
struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
407 using type =
typename std::conditional<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
408 !std::is_assignable<T &, std::string>::value &&
410 !std::is_enum<T>::value,
413 static constexpr
bool value = type::value;
417 template <
typename T>
419 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
420 is_direct_constructible<T, double>::value &&
421 is_direct_constructible<T, int>::value>::type> {
426 template <
typename T>
428 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
429 !is_direct_constructible<T, double>::value &&
430 is_direct_constructible<T, int>::value>::type> {
435 template <
typename T>
437 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
438 is_direct_constructible<T, double>::value &&
439 !is_direct_constructible<T, int>::value>::type> {
444 template <
typename T>
446 typename std::enable_if<(type_count<T>::value >= 2 && !is_vector<T>::value) ||
447 (is_tuple_like<T>::value && uncommon_type<T>::value &&
448 !is_direct_constructible<T, double>::value &&
449 !is_direct_constructible<T, int>::value)>::type> {
454 template <
typename T>
struct classify_object<T, typename std::enable_if<is_vector<T>::value>::type> {
464 template <
typename T,
472 template <
typename T,
478 template <
typename T,
488 template <
typename T,
495 template <
typename T,
502 template <
typename T,
509 template <
typename T,
513 return type_name<typename std::tuple_element<0, T>::type>();
517 template <
typename T, std::
size_t I>
518 inline typename std::enable_if<I == type_count<T>::value, std::string>::type
tuple_name() {
519 return std::string{};
523 template <
typename T, std::
size_t I>
524 inline typename std::enable_if < I<type_count<T>::value, std::string>::type
tuple_name() {
525 std::string str = std::string(
type_name<
typename std::tuple_element<I, T>::type>()) +
',' + tuple_name<T, I + 1>();
526 if(str.back() ==
',')
532 template <
typename T,
536 auto tname = std::string(1,
'[') + tuple_name<T, 0>();
537 tname.push_back(
']');
542 template <
typename T,
545 return type_name<typename T::value_type>();
552 static const std::string trueString(
"true");
553 static const std::string falseString(
"false");
554 if(val == trueString) {
557 if(val == falseString) {
562 if(val.size() == 1) {
563 if(val[0] >=
'1' && val[0] <=
'9') {
564 return (
static_cast<int64_t
>(val[0]) -
'0');
579 throw std::invalid_argument(
"unrecognized character");
583 if(val == trueString || val ==
"on" || val ==
"yes" || val ==
"enable") {
585 }
else if(val == falseString || val ==
"off" || val ==
"no" || val ==
"disable") {
588 ret = std::stoll(val);
594 template <
typename T,
599 std::int64_t output_ll = std::stoll(input, &n, 0);
600 output =
static_cast<T
>(output_ll);
601 return n == input.size() &&
static_cast<std::int64_t
>(output) == output_ll;
602 }
catch(
const std::invalid_argument &) {
604 }
catch(
const std::out_of_range &) {
610 template <
typename T,
612 bool lexical_cast(
const std::string &input, T &output) {
613 if(!input.empty() && input.front() ==
'-')
618 std::uint64_t output_ll = std::stoull(input, &n, 0);
619 output =
static_cast<T
>(output_ll);
620 return n == input.size() &&
static_cast<std::uint64_t
>(output) == output_ll;
621 }
catch(
const std::invalid_argument &) {
623 }
catch(
const std::out_of_range &) {
629 template <
typename T,
631 bool lexical_cast(
const std::string &input, T &output) {
636 }
catch(
const std::invalid_argument &) {
638 }
catch(
const std::out_of_range &) {
641 output = (input[0] !=
'-');
647 template <
typename T,
649 bool lexical_cast(
const std::string &input, T &output) {
652 output =
static_cast<T
>(std::stold(input, &n));
653 return n == input.size();
654 }
catch(
const std::invalid_argument &) {
656 }
catch(
const std::out_of_range &) {
662 template <
typename T,
664 bool lexical_cast(
const std::string &input, T &output) {
673 bool lexical_cast(
const std::string &input, T &output) {
679 template <
typename T,
681 bool lexical_cast(
const std::string &input, T &output) {
682 typename std::underlying_type<T>::type val;
687 output =
static_cast<T
>(val);
695 bool lexical_cast(
const std::string &input, T &output) {
714 bool lexical_cast(
const std::string &input, T &output) {
727 bool lexical_cast(
const std::string &input, T &output) {
738 bool lexical_cast(
const std::string &input, T &output) {
740 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
741 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
757 template <
typename T,
777 bool parse_result = (!input.empty()) ? lexical_cast<XC>(input, val) :
true;
785 template <
typename T,
787 enable_if_t<!std::is_same<T, XC>::value && !std::is_assignable<T &, XC &>::value &&
788 std::is_move_assignable<T>::value,
792 bool parse_result = input.empty() ? true : lexical_cast<XC>(input, val);
805 return lexical_assign<T, XC>(strings[0], output);
809 template <
typename T,
813 typename std::tuple_element<0, XC>::type v1;
814 typename std::tuple_element<1, XC>::type v2;
815 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[0], v1);
816 if(strings.size() > 1) {
817 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[1], v2);
833 output.reserve(strings.size());
834 for(
const auto &elem : strings) {
836 output.emplace_back();
837 bool retval = lexical_assign<typename T::value_type, typename XC::value_type>(elem, output.back());
842 return (!output.empty());
853 for(std::size_t ii = 0; ii < strings.size(); ii += 2) {
855 typename std::tuple_element<0, typename XC::value_type>::type v1;
856 typename std::tuple_element<1, typename XC::value_type>::type v2;
857 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[ii], v1);
858 if(strings.size() > ii + 1) {
859 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[ii + 1], v2);
862 output.emplace_back(v1, v2);
867 return (!output.empty());
879 output.reserve(strings.size());
880 for(
const auto &elem : strings) {
882 output.emplace_back();
883 retval = retval && lexical_assign<typename T::value_type, XC>(elem, output.back());
885 return (!output.empty()) && retval;
889 template <
typename T,
891 enable_if_t<!is_tuple_like<T>::value && !is_vector<T>::value && is_vector<XC>::value,
detail::enabler> =
895 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
897 auto retval = lexical_conversion<XC, XC>(strings, val);
906 template <
class T,
class XC, std::
size_t I>
912 template <
class T,
class XC, std::
size_t I>
913 inline typename std::enable_if <
914 I<type_count<T>::value,
bool>::type
tuple_conversion(
const std::vector<std::string> &strings, T &output) {
916 if(strings.size() > I) {
917 retval = retval && lexical_assign<typename std::tuple_element<I, T>::type,
918 typename std::conditional<is_tuple_like<XC>::value,
919 typename std::tuple_element<I, XC>::type,
920 XC>::type>(strings[I], std::get<I>(output));
922 retval = retval && tuple_conversion<T, XC, I + 1>(strings, output);
931 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
932 return tuple_conversion<T, XC, 0>(strings, output);
944 std::vector<std::string> temp;
946 std::size_t icount = 0;
948 while(ii < strings.size()) {
949 temp.push_back(strings[ii]);
952 if(icount == xcm || temp.back().empty()) {
956 output.emplace_back();
957 retval = retval && lexical_conversion<typename T::value_type, typename XC::value_type>(temp, output.back());
972 template <
typename T,
974 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
976 for(
auto &flag : flags) {
979 output = (count > 0) ?
static_cast<T
>(count) : T{0};
987 template <
typename T,
989 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
991 for(
auto &flag : flags) {
994 output =
static_cast<T
>(count);