Monero
Loading...
Searching...
No Matches
portable_storage_val_converters.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26
27
28
29#pragma once
30
31#include <boost/regex.hpp>
32
33#include "misc_language.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
44namespace 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 {
55 CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value");
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);
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());
66DISABLE_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());
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 {
76DISABLE_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);
80 }
81
82 template<typename from_type, typename to_type, bool, bool> //is from signed, is from to signed
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 {//from signed to signed
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 {//from signed to unsigned
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 {//from unsigned to signed
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 //from unsigned to unsigned
118 convert_uint_to_any_int(from, to);
119 }
120 };
121
122 template<typename from_type, typename to_type, bool>
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 {
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 {
140 }
141 };
142
143 // For MyMonero/OpenMonero backend compatibility
144 // MyMonero backend sends amount, fees and timestamp values as strings.
145 // Until MM backend is updated, this is needed for compatibility between OpenMonero and MyMonero.
146 template<>
148 {
149 static void convert(const std::string& from, uint64_t& to)
150 {
151 MTRACE("Converting std::string to uint64_t. Source: " << from);
152 // String only contains digits
153 if(std::all_of(from.begin(), from.end(), epee::misc_utils::parse::isdigit))
154 to = boost::lexical_cast<uint64_t>(from);
155 // MyMonero ISO 8061 timestamp (2017-05-06T16:27:06Z)
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 // Convert to unix timestamp
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
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>
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 {
198 }
199 };
200
201
202 template<class from_type, class to_type>
203 void convert_t(const from_type& from, to_type& to)
204 {
206 }
207 }
208}
#define true
#define false
Definition compare.py:1
bool isdigit(char c)
Definition parserse_base_utils.h:91
Definition keyvalue_serialization_overloads.h:49
void convert_t(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:203
void convert_int_to_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:62
void convert_uint_to_any_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:72
void convert_int_to_uint(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:51
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition enums.h:68
const char * name
Definition options.c:30
#define ASSERT_AND_THROW_WRONG_CONVERSION()
Definition portable_storage_val_converters.h:48
unsigned __int64 uint64_t
Definition stdint.h:136
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:137
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:128
static void convert(const std::string &from, uint64_t &to)
Definition portable_storage_val_converters.h:149
Definition portable_storage_val_converters.h:123
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:195
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:186
Definition portable_storage_val_converters.h:181
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:115
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:106
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:97
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:88
Definition portable_storage_val_converters.h:83
Definition portable_storage_val_converters.h:178
#define DISABLE_GCC_AND_CLANG_WARNING(w)
Definition warnings.h:28
#define DISABLE_VS_WARNINGS(w)
Definition warnings.h:18
#define DISABLE_CLANG_WARNING(w)
Definition warnings.h:25
#define POP_WARNINGS
Definition warnings.h:17
#define PUSH_WARNINGS
Definition warnings.h:16