| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include <boost/regex.hpp> |
| 32 |
|
| 33 |
#include "misc_language.h" |
| 34 |
#include "portable_storage_base.h" |
| 35 |
#include "parserse_base_utils.h" |
| 36 |
#include "warnings.h" |
| 37 |
#include "misc_log_ex.h" |
| 38 |
|
| 39 |
#include <boost/lexical_cast.hpp> |
| 40 |
#include <boost/numeric/conversion/bounds.hpp> |
| 41 |
#include <typeinfo> |
| 42 |
#include <iomanip> |
| 43 |
|
| 44 |
namespace epee |
| 45 |
{ |
| 46 |
namespace serialization |
| 47 |
{ |
| 48 |
#define ASSERT_AND_THROW_WRONG_CONVERSION() ASSERT_MES_AND_THROW("WRONG DATA CONVERSION: from type=" << typeid(from).name() << " to type " << typeid(to).name()) |
| 49 |
|
| 50 |
template<typename from_type, typename to_type> |
| 51 |
void convert_int_to_uint(const from_type& from, to_type& to) |
| 52 |
{ |
| 53 |
PUSH_WARNINGS |
| 54 |
DISABLE_VS_WARNINGS(4018) |
| 55 |
CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value"); |
| 56 |
DISABLE_GCC_AND_CLANG_WARNING(sign-compare) |
| 57 |
CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max()); |
| 58 |
to = static_cast<to_type>(from); |
| 59 |
POP_WARNINGS |
| 60 |
} |
| 61 |
template<typename from_type, typename to_type> |
| 62 |
void convert_int_to_int(const from_type& from, to_type& to) |
| 63 |
{ |
| 64 |
CHECK_AND_ASSERT_THROW_MES(from >= boost::numeric::bounds<to_type>::lowest(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with lowest possible value = " << boost::numeric::bounds<to_type>::lowest()); |
| 65 |
PUSH_WARNINGS |
| 66 |
DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) |
| 67 |
CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max()); |
| 68 |
POP_WARNINGS |
| 69 |
to = static_cast<to_type>(from); |
| 70 |
} |
| 71 |
template<typename from_type, typename to_type> |
| 72 |
void convert_uint_to_any_int(const from_type& from, to_type& to) |
| 73 |
{ |
| 74 |
PUSH_WARNINGS |
| 75 |
DISABLE_VS_WARNINGS(4018) |
| 76 |
DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) |
| 77 |
CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "uint value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max()); |
| 78 |
to = static_cast<to_type>(from); |
| 79 |
POP_WARNINGS |
| 80 |
} |
| 81 |
|
| 82 |
template<typename from_type, typename to_type, bool, bool> |
| 83 |
struct convert_to_signed_unsigned; |
| 84 |
|
| 85 |
template<typename from_type, typename to_type> |
| 86 |
struct convert_to_signed_unsigned<from_type, to_type, true, true> |
| 87 |
{ |
| 88 |
static void convert(const from_type& from, to_type& to) |
| 89 |
{ |
| 90 |
convert_int_to_int(from, to); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
template<typename from_type, typename to_type> |
| 95 |
struct convert_to_signed_unsigned<from_type, to_type, true, false> |
| 96 |
{ |
| 97 |
static void convert(const from_type& from, to_type& to) |
| 98 |
{ |
| 99 |
convert_int_to_uint(from, to); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
template<typename from_type, typename to_type> |
| 104 |
struct convert_to_signed_unsigned<from_type, to_type, false, true> |
| 105 |
{ |
| 106 |
static void convert(const from_type& from, to_type& to) |
| 107 |
{ |
| 108 |
convert_uint_to_any_int(from, to); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
template<typename from_type, typename to_type> |
| 113 |
struct convert_to_signed_unsigned<from_type, to_type, false, false> |
| 114 |
{ |
| 115 |
static void convert(const from_type& from, to_type& to) |
| 116 |
{ |
| 117 |
|
| 118 |
convert_uint_to_any_int(from, to); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
template<typename from_type, typename to_type, bool> |
| 123 |
struct convert_to_integral; |
| 124 |
|
| 125 |
template<typename from_type, typename to_type> |
| 126 |
struct convert_to_integral<from_type, to_type, true> |
| 127 |
{ |
| 128 |
static void convert(const from_type& from, to_type& to) |
| 129 |
{ |
| 130 |
convert_to_signed_unsigned<from_type, to_type, std::is_signed<from_type>::value, std::is_signed<to_type>::value>::convert(from, to); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
template<typename from_type, typename to_type> |
| 135 |
struct convert_to_integral<from_type, to_type, false> |
| 136 |
{ |
| 137 |
static void convert(const from_type& from, to_type& to) |
| 138 |
{ |
| 139 |
ASSERT_AND_THROW_WRONG_CONVERSION(); |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
template<> |
| 147 |
struct convert_to_integral<std::string, uint64_t, false> |
| 148 |
{ |
| 149 |
static void convert(const std::string& from, uint64_t& to) |
| 150 |
{ |
| 151 |
MTRACE("Converting std::string to uint64_t. Source: " << from); |
| 152 |
|
| 153 |
if(std::all_of(from.begin(), from.end(), epee::misc_utils::parse::isdigit)) |
| 154 |
to = boost::lexical_cast<uint64_t>(from); |
| 155 |
|
| 156 |
else if (boost::regex_match (from, boost::regex("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\dZ"))) |
| 157 |
{ |
| 158 |
|
| 159 |
#ifdef HAVE_STRPTIME |
| 160 |
struct tm tm; |
| 161 |
if (strptime(from.c_str(), "%Y-%m-%dT%H:%M:%S", &tm)) |
| 162 |
#else |
| 163 |
std::tm tm = {}; |
| 164 |
std::istringstream ss(from); |
| 165 |
if (ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S")) |
| 166 |
#endif |
| 167 |
to = std::mktime(&tm); |
| 168 |
} else |
| 169 |
ASSERT_AND_THROW_WRONG_CONVERSION(); |
| 170 |
} |
| 171 |
}; |
| 172 |
|
| 173 |
template<class from_type, class to_type> |
| 174 |
struct is_convertable: std::integral_constant<bool, |
| 175 |
std::is_integral<to_type>::value && |
| 176 |
std::is_integral<from_type>::value && |
| 177 |
!std::is_same<from_type, bool>::value && |
| 178 |
!std::is_same<to_type, bool>::value > {}; |
| 179 |
|
| 180 |
template<typename from_type, typename to_type, bool> |
| 181 |
struct convert_to_same; |
| 182 |
|
| 183 |
template<typename from_type, typename to_type> |
| 184 |
struct convert_to_same<from_type, to_type, true> |
| 185 |
{ |
| 186 |
static void convert(const from_type& from, to_type& to) |
| 187 |
{ |
| 188 |
to = from; |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
template<typename from_type, typename to_type> |
| 193 |
struct convert_to_same<from_type, to_type, false> |
| 194 |
{ |
| 195 |
static void convert(const from_type& from, to_type& to) |
| 196 |
{ |
| 197 |
convert_to_integral<from_type, to_type, is_convertable<from_type, to_type>::value>::convert(from, to); |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
template<class from_type, class to_type> |
| 203 |
void convert_t(const from_type& from, to_type& to) |
| 204 |
{ |
| 205 |
convert_to_same<from_type, to_type, std::is_same<to_type, from_type>::value>::convert(from, to); |
| 206 |
} |
| 207 |
} |
| 208 |
} |