14 #include <type_traits>
37 template <
bool B,
class T =
void>
using enable_if_t =
typename std::enable_if<B, T>::type;
46 template <
bool B,
class T,
class F>
using conditional_t =
typename std::conditional<B, T, F>::type;
49 template <
typename T>
struct is_vector : std::false_type {};
52 template <
class T,
class A>
struct is_vector<std::vector<T, A>> : std::true_type {};
55 template <
class T,
class A>
struct is_vector<const std::vector<T, A>> : std::true_type {};
58 template <
typename T>
struct is_bool : std::false_type {};
61 template <>
struct is_bool<bool> : std::true_type {};
67 template <
typename T>
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
70 template <
typename T>
struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
93 template <
typename T>
struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>
::type> {
94 using type =
typename std::pointer_traits<T>::element_type;
102 template <
typename T,
typename _ =
void>
struct pair_adaptor : std::false_type {
104 using first_type =
typename std::remove_const<value_type>::type;
108 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
109 return std::forward<Q>(pair_value);
112 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
113 return std::forward<Q>(pair_value);
119 template <
typename T>
122 conditional_t<false,
void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
125 using first_type =
typename std::remove_const<typename value_type::first_type>::type;
126 using second_type =
typename std::remove_const<typename value_type::second_type>::type;
129 template <
typename Q>
static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
130 return std::get<0>(std::forward<Q>(pair_value));
133 template <
typename Q>
static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
134 return std::get<1>(std::forward<Q>(pair_value));
145 #pragma GCC diagnostic push
146 #pragma GCC diagnostic ignored "-Wnarrowing"
150 template <
typename TT,
typename CC>
151 static auto test(
int, std::true_type) -> decltype(
154 #pragma diag_suppress 2361
156 TT { std::declval<CC>() }
158 #pragma diag_default 2361
161 std::is_move_assignable<TT>());
163 template <
typename TT,
typename CC>
static auto test(
int, std::false_type) -> std::false_type;
165 template <
typename,
typename>
static auto test(...) -> std::false_type;
168 static constexpr
bool value = decltype(test<T, C>(0,
typename std::is_constructible<T, C>::type()))::
value;
171 #pragma GCC diagnostic pop
178 template <
typename TT,
typename SS>
179 static auto test(
int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
181 template <
typename,
typename>
static auto test(...) -> std::false_type;
184 static constexpr
bool value = decltype(test<T, S>(0))::
value;
189 template <
typename TT,
typename SS>
190 static auto test(
int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
192 template <
typename,
typename>
static auto test(...) -> std::false_type;
195 static constexpr
bool value = decltype(test<T, S>(0))::
value;
201 std::istringstream is;
204 return !is.fail() && !is.rdbuf()->in_avail();
214 template <
typename SS>
217 static auto test(
int) -> decltype(std::tuple_size<SS>::value, std::true_type{});
218 template <
typename>
static auto test(...) -> std::false_type;
226 auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
227 return std::forward<T>(value);
231 template <
typename T,
235 return std::string(value);
239 template <
typename T,
244 std::stringstream stream;
250 template <
typename T,
255 return std::string{};
259 template <
typename T,
261 is_vector<typename std::remove_reference<typename std::remove_const<T>::type>::type>::value,
264 std::vector<std::string> defaults;
265 defaults.reserve(variable.size());
266 auto cval = variable.begin();
267 auto end = variable.end();
276 template <
typename T1,
281 return to_string(std::forward<T>(value));
285 template <
typename T1,
290 return std::string{};
300 return std::to_string(
static_cast<typename std::underlying_type<T>::type
>(value));
303 template <
typename T,
310 template <
typename T,
typename Enable =
void>
struct type_count {
static const int value{0}; };
313 template <
typename T>
struct type_count<T, typename std::enable_if<is_tuple_like<T>::value>::type> {
314 static constexpr
int value{std::tuple_size<T>::value};
317 template <
typename T>
320 typename std::enable_if<!is_vector<T>::value && !is_tuple_like<T>::value && !std::is_void<T>::value>::type> {
325 template <
typename T>
struct type_count<T, typename std::enable_if<is_vector<T>::value>::type> {
334 template <
typename T>
335 struct expected_count<T, typename std::enable_if<!is_vector<T>::value && !std::is_void<T>::value>::type> {
339 template <
typename T>
struct expected_count<T, typename std::enable_if<is_vector<T>::value>::type> {
368 template <
typename T>
370 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value &&
371 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
376 template <
typename T>
379 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && !is_bool<T>::value>::type> {
384 template <
typename T>
struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
389 template <
typename T>
struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
394 template <
typename T>
397 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
398 std::is_assignable<T &, std::string>::value && !is_vector<T>::value>::type> {
403 template <
typename T>
406 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
407 !std::is_assignable<T &, std::string>::value &&
408 std::is_constructible<T, std::string>::value && !is_vector<T>::value>::type> {
413 template <
typename T>
struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
420 using type =
typename std::conditional<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
421 !std::is_assignable<T &, std::string>::value &&
423 !std::is_enum<T>::value,
426 static constexpr
bool value = type::value;
430 template <
typename T>
432 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
433 is_direct_constructible<T, double>::value &&
434 is_direct_constructible<T, int>::value>::type> {
439 template <
typename T>
441 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
442 !is_direct_constructible<T, double>::value &&
443 is_direct_constructible<T, int>::value>::type> {
448 template <
typename T>
450 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
451 is_direct_constructible<T, double>::value &&
452 !is_direct_constructible<T, int>::value>::type> {
457 template <
typename T>
459 typename std::enable_if<(type_count<T>::value >= 2 && !is_vector<T>::value) ||
460 (is_tuple_like<T>::value && uncommon_type<T>::value &&
461 !is_direct_constructible<T, double>::value &&
462 !is_direct_constructible<T, int>::value)>::type> {
467 template <
typename T>
struct classify_object<T, typename std::enable_if<is_vector<T>::value>::type> {
477 template <
typename T,
485 template <
typename T,
491 template <
typename T,
501 template <
typename T,
508 template <
typename T,
515 template <
typename T,
522 template <
typename T,
526 return type_name<typename std::tuple_element<0, T>::type>();
530 template <
typename T, std::
size_t I>
531 inline typename std::enable_if<I == type_count<T>::value, std::string>::type
tuple_name() {
532 return std::string{};
536 template <
typename T, std::
size_t I>
537 inline typename std::enable_if < I<type_count<T>::value, std::string>::type
tuple_name() {
538 std::string str = std::string(
type_name<
typename std::tuple_element<I, T>::type>()) +
',' + tuple_name<T, I + 1>();
539 if(str.back() ==
',')
545 template <
typename T,
549 auto tname = std::string(1,
'[') + tuple_name<T, 0>();
550 tname.push_back(
']');
555 template <
typename T,
558 return type_name<typename T::value_type>();
565 static const std::string trueString(
"true");
566 static const std::string falseString(
"false");
567 if(val == trueString) {
570 if(val == falseString) {
575 if(val.size() == 1) {
576 if(val[0] >=
'1' && val[0] <=
'9') {
577 return (
static_cast<std::int64_t
>(val[0]) -
'0');
592 throw std::invalid_argument(
"unrecognized character");
596 if(val == trueString || val ==
"on" || val ==
"yes" || val ==
"enable") {
598 }
else if(val == falseString || val ==
"off" || val ==
"no" || val ==
"disable") {
601 ret = std::stoll(val);
607 template <
typename T,
612 std::int64_t output_ll = std::stoll(input, &n, 0);
613 output =
static_cast<T
>(output_ll);
614 return n == input.size() &&
static_cast<std::int64_t
>(output) == output_ll;
615 }
catch(
const std::invalid_argument &) {
617 }
catch(
const std::out_of_range &) {
623 template <
typename T,
625 bool lexical_cast(
const std::string &input, T &output) {
626 if(!input.empty() && input.front() ==
'-')
631 std::uint64_t output_ll = std::stoull(input, &n, 0);
632 output =
static_cast<T
>(output_ll);
633 return n == input.size() &&
static_cast<std::uint64_t
>(output) == output_ll;
634 }
catch(
const std::invalid_argument &) {
636 }
catch(
const std::out_of_range &) {
642 template <
typename T,
644 bool lexical_cast(
const std::string &input, T &output) {
649 }
catch(
const std::invalid_argument &) {
651 }
catch(
const std::out_of_range &) {
654 output = (input[0] !=
'-');
660 template <
typename T,
662 bool lexical_cast(
const std::string &input, T &output) {
665 output =
static_cast<T
>(std::stold(input, &n));
666 return n == input.size();
667 }
catch(
const std::invalid_argument &) {
669 }
catch(
const std::out_of_range &) {
675 template <
typename T,
677 bool lexical_cast(
const std::string &input, T &output) {
686 bool lexical_cast(
const std::string &input, T &output) {
692 template <
typename T,
694 bool lexical_cast(
const std::string &input, T &output) {
695 typename std::underlying_type<T>::type val;
700 output =
static_cast<T
>(val);
708 bool lexical_cast(
const std::string &input, T &output) {
727 bool lexical_cast(
const std::string &input, T &output) {
740 bool lexical_cast(
const std::string &input, T &output) {
751 bool lexical_cast(
const std::string &input, T &output) {
753 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
754 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
770 template <
typename T,
790 bool parse_result = (!input.empty()) ? lexical_cast<XC>(input, val) :
true;
798 template <
typename T,
800 enable_if_t<!std::is_same<T, XC>::value && !std::is_assignable<T &, XC &>::value &&
801 std::is_move_assignable<T>::value,
805 bool parse_result = input.empty() ? true : lexical_cast<XC>(input, val);
818 return lexical_assign<T, XC>(strings[0], output);
822 template <
typename T,
826 typename std::tuple_element<0, XC>::type v1;
827 typename std::tuple_element<1, XC>::type v2;
828 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[0], v1);
829 if(strings.size() > 1) {
830 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[1], v2);
846 output.reserve(strings.size());
847 for(
const auto &elem : strings) {
849 output.emplace_back();
850 bool retval = lexical_assign<typename T::value_type, typename XC::value_type>(elem, output.back());
855 return (!output.empty());
866 for(std::size_t ii = 0; ii < strings.size(); ii += 2) {
868 typename std::tuple_element<0, typename XC::value_type>::type v1;
869 typename std::tuple_element<1, typename XC::value_type>::type v2;
870 bool retval = lexical_assign<decltype(v1), decltype(v1)>(strings[ii], v1);
871 if(strings.size() > ii + 1) {
872 retval = retval && lexical_assign<decltype(v2), decltype(v2)>(strings[ii + 1], v2);
875 output.emplace_back(v1, v2);
880 return (!output.empty());
892 output.reserve(strings.size());
893 for(
const auto &elem : strings) {
895 output.emplace_back();
896 retval = retval && lexical_assign<typename T::value_type, XC>(elem, output.back());
898 return (!output.empty()) && retval;
902 template <
typename T,
904 enable_if_t<!is_tuple_like<T>::value && !is_vector<T>::value && is_vector<XC>::value,
detail::enabler> =
908 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
910 auto retval = lexical_conversion<XC, XC>(strings, val);
919 template <
class T,
class XC, std::
size_t I>
925 template <
class T,
class XC, std::
size_t I>
926 inline typename std::enable_if <
927 I<type_count<T>::value,
bool>::type
tuple_conversion(
const std::vector<std::string> &strings, T &output) {
929 if(strings.size() > I) {
930 retval = retval && lexical_assign<typename std::tuple_element<I, T>::type,
931 typename std::conditional<is_tuple_like<XC>::value,
932 typename std::tuple_element<I, XC>::type,
933 XC>::type>(strings[I], std::get<I>(output));
935 retval = retval && tuple_conversion<T, XC, I + 1>(strings, output);
944 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
945 return tuple_conversion<T, XC, 0>(strings, output);
957 std::vector<std::string> temp;
959 std::size_t icount = 0;
961 while(ii < strings.size()) {
962 temp.push_back(strings[ii]);
965 if(icount == xcm || temp.back().empty()) {
969 output.emplace_back();
970 retval = retval && lexical_conversion<typename T::value_type, typename XC::value_type>(temp, output.back());
985 template <
typename T,
987 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
988 std::int64_t count{0};
989 for(
auto &flag : flags) {
992 output = (count > 0) ?
static_cast<T
>(count) : T{0};
1000 template <
typename T,
1002 void sum_flag_vector(
const std::vector<std::string> &flags, T &output) {
1003 std::int64_t count{0};
1004 for(
auto &flag : flags) {
1007 output =
static_cast<T
>(count);
Definition: TypeTools.hpp:149
static constexpr bool value
Definition: TypeTools.hpp:168
Check for input streamability.
Definition: TypeTools.hpp:188
static constexpr bool value
Definition: TypeTools.hpp:195
Definition: TypeTools.hpp:177
static constexpr bool value
Definition: TypeTools.hpp:184
Definition: TypeTools.hpp:213
static constexpr bool value
Definition: TypeTools.hpp:221
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:29
constexpr const char * type_name()
Print name for enumeration types.
Definition: TypeTools.hpp:481
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:226
auto checked_to_string(T &&value) -> decltype(to_string(std::forward< T >(value)))
special template overload
Definition: TypeTools.hpp:280
bool lexical_assign(const std::string &input, T &output)
Assign a value through lexical cast operations.
Definition: TypeTools.hpp:765
bool from_stream(const std::string &istring, T &obj)
Templated operation to get a value from a stream.
Definition: TypeTools.hpp:200
constexpr int expected_max_vector_size
Definition: StringTools.hpp:39
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:294
std::string to_string(T &&value)
Convert an object to a string (streaming must be supported for that type)
Definition: TypeTools.hpp:243
bool lexical_conversion(const std::vector< std ::string > &strings, T &output)
Lexical conversion if there is only one element.
Definition: TypeTools.hpp:817
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:59
bool ::type tuple_conversion(const std::vector< std::string > &, T &)
Definition: TypeTools.hpp:920
object_category
Definition: TypeTools.hpp:344
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:199
std::size_t I std::enable_if< I==type_count< T >::value, std::string >::type tuple_name()
Definition: TypeTools.hpp:531
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:26
bool lexical_cast(const std::string &input, T &output)
Signed integers.
Definition: TypeTools.hpp:609
std::int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition: TypeTools.hpp:564
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:37
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:46
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:43
std::string type
Definition: TypeTools.hpp:81
This can be specialized to override the type deduction for IsMember.
Definition: TypeTools.hpp:78
T type
Definition: TypeTools.hpp:78
some type that is not otherwise recognized
Definition: TypeTools.hpp:363
static constexpr object_category value
Definition: TypeTools.hpp:364
typename std::pointer_traits< T >::element_type type
Definition: TypeTools.hpp:94
not a pointer
Definition: TypeTools.hpp:91
T type
Definition: TypeTools.hpp:91
Definition: TypeTools.hpp:99
typename element_type< T >::type::value_type type
Definition: TypeTools.hpp:99
This will only trigger for actual void type.
Definition: TypeTools.hpp:331
static const int value
Definition: TypeTools.hpp:331
typename std::remove_const< typename value_type::first_type >::type first_type
Definition: TypeTools.hpp:125
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:129
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:133
typename T::value_type value_type
Definition: TypeTools.hpp:124
typename std::remove_const< typename value_type::second_type >::type second_type
Definition: TypeTools.hpp:126
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition: TypeTools.hpp:102
typename T::value_type value_type
Definition: TypeTools.hpp:103
typename std::remove_const< value_type >::type second_type
Definition: TypeTools.hpp:105
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition: TypeTools.hpp:112
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:104
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:108
This will only trigger for actual void type.
Definition: TypeTools.hpp:310
static const int value
Definition: TypeTools.hpp:310
Definition: TypeTools.hpp:419
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_vector< T >::value &&!std::is_enum< T >::value, std::true_type, std::false_type >::type type
Definition: TypeTools.hpp:425
static constexpr bool value
Definition: TypeTools.hpp:426
Check to see if something is bool (fail check by default)
Definition: TypeTools.hpp:58
Check to see if something is copyable pointer.
Definition: TypeTools.hpp:73
static bool const value
Definition: TypeTools.hpp:74
Check to see if something is a shared pointer.
Definition: TypeTools.hpp:64
Check to see if something is a vector (fail check by default)
Definition: TypeTools.hpp:49
A copy of std::void_t from C++17 (helper for C++11 and C++14)
Definition: TypeTools.hpp:40
void type
Definition: TypeTools.hpp:40