1 #ifndef SAFE_ANY_VARNUMBER_H 2 #define SAFE_ANY_VARNUMBER_H 10 #include <type_traits> 26 typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value>
::type*;
30 typename std::enable_if<!std::is_integral<T>::value && !std::is_enum<T>::value>::type*;
33 using EnableString =
typename std::enable_if<std::is_same<T, std::string>::value>::type*;
39 using EnableEnum =
typename std::enable_if<std::is_enum<T>::value>::type*;
43 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_enum<T>::value &&
44 !std::is_same<T, std::string>::value>::type*;
95 static_assert(!std::is_reference<T>::value,
"Any can not contain references");
107 return _any.
type() ==
typeid(int64_t) ||
119 template <
typename T>
122 static_assert(!std::is_reference<T>::value,
"Any::cast uses value semantic, can not cast to reference");
126 throw std::runtime_error(
"Any::cast failed because it is empty");
134 auto res = convert<T>();
137 throw std::runtime_error( res.error() );
143 const std::type_info&
type() const noexcept
164 template <
typename DST>
173 else if (
type ==
typeid(int64_t))
177 else if (
type ==
typeid(uint64_t))
181 else if (
type ==
typeid(
double))
186 return nonstd::make_unexpected( errorMsg<DST>() );
189 template <
typename DST>
192 using SafeAny::details::convertNumber;
197 if (
type ==
typeid(int64_t))
201 else if (
type ==
typeid(uint64_t))
205 else if (
type ==
typeid(
double))
210 return nonstd::make_unexpected( errorMsg<DST>() );
215 template <
typename DST>
218 using SafeAny::details::convertNumber;
222 if (
type ==
typeid(int64_t))
225 return static_cast<DST
>(out);
227 else if (
type ==
typeid(uint64_t))
230 return static_cast<DST
>(out);
233 return nonstd::make_unexpected( errorMsg<DST>() );
236 template <
typename DST>
239 return nonstd::make_unexpected( errorMsg<DST>() );
242 template <
typename T>
245 return StrCat(
"[Any::convert]: no known safe conversion between [",
252 #endif // VARNUMBER_H nonstd::expected< DST, std::string > convert(EnableArithmetic< DST >=0) const
bool empty() const noexcept
Returns true if *this has no contained object, otherwise false.
const std::type_info & type() const noexcept
Any(const SafeAny::SimpleString &str)
typename std::enable_if< std::is_enum< T >::value >::type * EnableEnum
Any(const T &value, EnableNonIntegral< T >=0)
ValueType any_cast(const any &operand)
Performs *any_cast<add_const_t<remove_reference_t<ValueType>>>(&operand), or throws bad_any_cast on f...
nonstd::expected< DST, std::string > convert(EnableUnknownType< DST >=0) const
std::string demangle(char const *name)
const std::type_info & castedType() const noexcept
std::string errorMsg() const
Any & operator=(const Any &other)
bool empty() const noexcept
Any(const uint64_t &value)
typename std::enable_if< std::is_integral< T >::value||std::is_enum< T >::value >::type * EnableIntegral
typename std::enable_if< std::is_same< T, std::string >::value >::type * EnableString
const std::type_info * _original_type
nonstd::expected< DST, std::string > convert(EnableString< DST >=0) const
nonstd::expected< DST, std::string > convert(EnableEnum< DST >=0) const
const std::type_info & type() const noexcept
If *this has a contained object of type T, typeid(T); otherwise typeid(void).
Any(const T &value, EnableIntegral< T >=0)
std::basic_string< CharT, Traits > to_string(basic_string_view< CharT, Traits > v)
typename std::enable_if<!std::is_arithmetic< T >::value &&!std::is_enum< T >::value &&!std::is_same< T, std::string >::value >::type * EnableUnknownType
typename std::enable_if<!std::is_integral< T >::value &&!std::is_enum< T >::value >::type * EnableNonIntegral
Any(const std::string &str)
typename std::enable_if< std::is_arithmetic< T >::value >::type * EnableArithmetic