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*;
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");
130 return linb::any_cast<T>(
_any);
134 auto res = convert<T>();
137 throw std::runtime_error( res.error() );
143 const std::type_info&
type() const noexcept
164 template <
typename DST>
171 return linb::any_cast<SafeAny::SimpleString>(
_any).toStdString();
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))
199 convertNumber<int64_t, DST>(linb::any_cast<int64_t>(
_any), out);
201 else if (
type ==
typeid(uint64_t))
203 convertNumber<uint64_t, DST>(linb::any_cast<uint64_t>(
_any), out);
205 else if (
type ==
typeid(
double))
207 convertNumber<double, DST>(linb::any_cast<double>(
_any), out);
210 return nonstd::make_unexpected( errorMsg<DST>() );
215 template <
typename DST>
218 using SafeAny::details::convertNumber;
222 if (
type ==
typeid(int64_t))
224 uint64_t out = linb::any_cast<int64_t>(
_any);
225 return static_cast<DST
>(out);
227 else if (
type ==
typeid(uint64_t))
229 uint64_t out = linb::any_cast<uint64_t>(
_any);
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