update variant access in arg_parse::get
parent
1d03938f95
commit
0e17dff862
|
@ -15,6 +15,7 @@
|
|||
#include <blt/std/hashmap.h>
|
||||
#include <variant>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
|
||||
namespace blt
|
||||
{
|
||||
|
@ -329,13 +330,28 @@ namespace blt
|
|||
return std::holds_alternative<arg_data_internal_t>(v) && std::holds_alternative<T>(std::get<arg_data_internal_t>(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* it is up to the user to deal with the variant if they do not want this behaviour!
|
||||
* @tparam T type to convert to
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
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));
|
||||
{
|
||||
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
|
||||
// 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>)
|
||||
return std::stoi(std::get<std::string>(t));
|
||||
return std::get<T>(t);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue