fix lvalue reference issue with stoi, get_cast now exists

v1
Brett 2023-08-18 19:22:12 -04:00
parent 0e17dff862
commit bbbf0ba2e5
1 changed files with 16 additions and 3 deletions

View File

@ -330,6 +330,16 @@ namespace blt
return std::holds_alternative<arg_data_internal_t>(v) && std::holds_alternative<T>(std::get<arg_data_internal_t>(v)); return std::holds_alternative<arg_data_internal_t>(v) && std::holds_alternative<T>(std::get<arg_data_internal_t>(v));
} }
template<typename T>
static inline T& get(arg_data_t& v)
{
if constexpr (std::is_same_v<T, arg_data_vec_t>)
return std::get<arg_data_vec_t>(v);
else
return std::get<T>(std::get<arg_data_internal_t>(v));
}
/** /**
* Attempt to cast the variant stored in the arg results to the requested type * Attempt to cast the variant stored in the arg results to the requested type
* if user is requesting an int, but holds a string, we are going to make the assumption the data can be converted * if user is requesting an int, but holds a string, we are going to make the assumption the data can be converted
@ -339,7 +349,7 @@ namespace blt
* @return * @return
*/ */
template<typename T> template<typename T>
static inline T& get(arg_data_t& v) static inline T get_cast(arg_data_t& v)
{ {
if constexpr (std::is_same_v<T, arg_data_vec_t>) if constexpr (std::is_same_v<T, arg_data_vec_t>)
return std::get<arg_data_vec_t>(v); return std::get<arg_data_vec_t>(v);
@ -348,8 +358,11 @@ namespace blt
auto t = std::get<arg_data_internal_t>(v); auto t = std::get<arg_data_internal_t>(v);
// user is requesting an int, but holds a string, we are going to make the assumption the data can be converted // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted
// it is up to the user to deal with the variant if they do not want this behaviour! // it is up to the user to deal with the variant if they do not want this behaviour!
if constexpr (std::holds_alternative<std::string>(t) && std::is_same_v<int32_t, T>) if constexpr (std::is_same_v<int32_t, T>)
{
if (std::holds_alternative<std::string>(t))
return std::stoi(std::get<std::string>(t)); return std::stoi(std::get<std::string>(t));
}
return std::get<T>(t); return std::get<T>(t);
} }
} }