mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-08 23:53:52 +00:00
Trying enable if, new version
This commit is contained in:
parent
aae31265b3
commit
b73d1bd9b2
@ -36,6 +36,18 @@ std::string join(const T& v, std::string delim = ",") {
|
|||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
|
||||||
|
// Based generally on https://rmf.io/cxx11/almost-static-if
|
||||||
|
namespace detail {
|
||||||
|
enum class enabler {};
|
||||||
|
}
|
||||||
|
constexpr detail::enabler dummy = {};
|
||||||
|
|
||||||
|
template <typename Condition>
|
||||||
|
using EnableIf = typename std::enable_if<Condition::value, detail::enabler>::type;
|
||||||
|
template <typename Condition>
|
||||||
|
using DisableIf = typename std::enable_if<!Condition::value, detail::enabler>::type;
|
||||||
|
|
||||||
|
|
||||||
struct Combiner {
|
struct Combiner {
|
||||||
int num;
|
int num;
|
||||||
bool positional;
|
bool positional;
|
||||||
@ -252,9 +264,8 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T, EnableIf<std::is_integral<T>> = dummy>
|
||||||
typename std::enable_if<std::is_integral<T>::value, bool>::type
|
bool lexical_cast(std::string input, T& output) {
|
||||||
lexical_cast(std::string input, T& output) {
|
|
||||||
logit("Int lexical cast " + input);
|
logit("Int lexical cast " + input);
|
||||||
try{
|
try{
|
||||||
output = (T) std::stoll(input);
|
output = (T) std::stoll(input);
|
||||||
@ -266,9 +277,8 @@ lexical_cast(std::string input, T& output) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T, EnableIf<std::is_floating_point<T>> = dummy>
|
||||||
typename std::enable_if<std::is_floating_point<T>::value, bool>::type
|
bool lexical_cast(std::string input, T& output) {
|
||||||
lexical_cast(std::string input, T& output) {
|
|
||||||
logit("Floating lexical cast " + input);
|
logit("Floating lexical cast " + input);
|
||||||
try{
|
try{
|
||||||
output = (T) std::stold(input);
|
output = (T) std::stold(input);
|
||||||
@ -281,11 +291,9 @@ lexical_cast(std::string input, T& output) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// String and similar
|
// String and similar
|
||||||
template<typename T>
|
template<typename T,
|
||||||
typename std::enable_if<
|
typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler>::type = dummy>
|
||||||
!std::is_floating_point<T>::value
|
bool lexical_cast(std::string input, T& output) {
|
||||||
&& !std::is_integral<T>::value, bool>::type
|
|
||||||
lexical_cast(std::string input, T& output) {
|
|
||||||
logit("Direct lexical cast: " + input);
|
logit("Direct lexical cast: " + input);
|
||||||
output = input;
|
output = input;
|
||||||
return true;
|
return true;
|
||||||
@ -375,9 +383,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add option for string
|
/// Add option for string
|
||||||
template<typename T>
|
template<typename T, DisableIf<std::is_array<T>> = dummy>
|
||||||
typename std::enable_if<!std::is_array<T>::value, void>::type
|
void add_option(
|
||||||
add_option(
|
|
||||||
std::string name, ///< The name, long,short
|
std::string name, ///< The name, long,short
|
||||||
T &variable, ///< The variable to set
|
T &variable, ///< The variable to set
|
||||||
std::string discription="", ///< Discription string
|
std::string discription="", ///< Discription string
|
||||||
@ -440,11 +447,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add option for flag
|
/// Add option for flag
|
||||||
template<typename T>
|
template<typename T, EnableIf<std::is_integral<T>> = dummy>
|
||||||
typename std::enable_if<std::is_integral<T>::value, void>::type
|
void add_flag(
|
||||||
add_flag(
|
|
||||||
std::string name, ///< The name, short,long
|
std::string name, ///< The name, short,long
|
||||||
T &count, ///< A varaible holding the count
|
T &count, ///< A varaible holding the count
|
||||||
std::string discription="" ///< Discription string
|
std::string discription="" ///< Discription string
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user