Rational: since type erased numbers will always use at least 8 bytes it is faster to cast everything to either double, uint64_t or int64_t.
| 47 | // Rational: since type erased numbers will always use at least 8 bytes |
| 48 | // it is faster to cast everything to either double, uint64_t or int64_t. |
| 49 | class Any |
| 50 | { |
| 51 | template <typename T> |
| 52 | using EnableIntegral = typename std::enable_if<std::is_integral<T>::value || |
| 53 | std::is_enum<T>::value>::type*; |
| 54 | |
| 55 | template <typename T> |
| 56 | using EnableNonIntegral = typename std::enable_if<!std::is_integral<T>::value && |
| 57 | !std::is_enum<T>::value>::type*; |
| 58 | |
| 59 | template <typename T> |
| 60 | using EnableString = |
| 61 | typename std::enable_if<std::is_same<T, std::string>::value>::type*; |
| 62 | |
| 63 | template <typename T> |
| 64 | using EnableArithmetic = typename std::enable_if<std::is_arithmetic<T>::value>::type*; |
| 65 | |
| 66 | template <typename T> |
| 67 | using EnableEnum = typename std::enable_if<std::is_enum<T>::value>::type*; |
| 68 | |
| 69 | template <typename T> |
| 70 | using EnableUnknownType = |
| 71 | typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_enum<T>::value && |
| 72 | !std::is_same<T, std::string>::value>::type*; |
| 73 | |
| 74 | template <typename T> |
| 75 | nonstd::expected<T, std::string> stringToNumber() const; |
| 76 | |
| 77 | public: |
| 78 | Any() : _original_type(UndefinedAnyType) |
| 79 | {} |
| 80 | |
| 81 | ~Any() = default; |
| 82 | |
| 83 | Any(const Any& other) : _any(other._any), _original_type(other._original_type) |
| 84 | {} |
| 85 | |
| 86 | Any(Any&& other) noexcept |
| 87 | : _any(std::move(other._any)), _original_type(other._original_type) |
| 88 | {} |
| 89 | |
| 90 | explicit Any(const double& value) : _any(value), _original_type(typeid(double)) |
| 91 | {} |
| 92 | |
| 93 | explicit Any(const uint64_t& value) : _any(value), _original_type(typeid(uint64_t)) |
| 94 | {} |
| 95 | |
| 96 | explicit Any(const float& value) : _any(double(value)), _original_type(typeid(float)) |
| 97 | {} |
| 98 | |
| 99 | explicit Any(const std::string& str) |
| 100 | : _any(SafeAny::SimpleString(str)), _original_type(typeid(std::string)) |
| 101 | {} |
| 102 | |
| 103 | explicit Any(const char* str) |
| 104 | : _any(SafeAny::SimpleString(str)), _original_type(typeid(std::string)) |
| 105 | {} |
| 106 |