Monero
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"
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 <typeinfo>
41 #include <iomanip>
42 
43 namespace epee
44 {
45  namespace serialization
46  {
47 #define ASSERT_AND_THROW_WRONG_CONVERSION() ASSERT_MES_AND_THROW("WRONG DATA CONVERSION: from type=" << typeid(from).name() << " to type " << typeid(to).name())
48 
49  template<typename from_type, typename to_type>
50  void convert_int_to_uint(const from_type& from, to_type& to)
51  {
54  CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value");
56  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());
57  to = static_cast<to_type>(from);
59  }
60  template<typename from_type, typename to_type>
61  void convert_int_to_int(const from_type& from, to_type& to)
62  {
63  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 DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
66  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  to = static_cast<to_type>(from);
69  }
70  template<typename from_type, typename to_type>
71  void convert_uint_to_any_int(const from_type& from, to_type& to)
72  {
75 DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
76  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());
77  to = static_cast<to_type>(from);
79  }
80 
81  template<typename from_type, typename to_type, bool, bool> //is from signed, is from to signed
83 
84  template<typename from_type, typename to_type>
85  struct convert_to_signed_unsigned<from_type, to_type, true, true>
86  {
87  static void convert(const from_type& from, to_type& to)
88  {//from signed to signed
89  convert_int_to_int(from, to);
90  }
91  };
92 
93  template<typename from_type, typename to_type>
94  struct convert_to_signed_unsigned<from_type, to_type, true, false>
95  {
96  static void convert(const from_type& from, to_type& to)
97  {//from signed to unsigned
98  convert_int_to_uint(from, to);
99  }
100  };
101 
102  template<typename from_type, typename to_type>
103  struct convert_to_signed_unsigned<from_type, to_type, false, true>
104  {
105  static void convert(const from_type& from, to_type& to)
106  {//from unsigned to signed
107  convert_uint_to_any_int(from, to);
108  }
109  };
110 
111  template<typename from_type, typename to_type>
112  struct convert_to_signed_unsigned<from_type, to_type, false, false>
113  {
114  static void convert(const from_type& from, to_type& to)
115  {
116  //from unsigned to unsigned
117  convert_uint_to_any_int(from, to);
118  }
119  };
120 
121  template<typename from_type, typename to_type, bool>
123 
124  template<typename from_type, typename to_type>
125  struct convert_to_integral<from_type, to_type, true>
126  {
127  static void convert(const from_type& from, to_type& to)
128  {
130  }
131  };
132 
133  template<typename from_type, typename to_type>
134  struct convert_to_integral<from_type, to_type, false>
135  {
136  static void convert(const from_type& from, to_type& to)
137  {
139  }
140  };
141 
142  // For MyMonero/OpenMonero backend compatibility
143  // MyMonero backend sends amount, fees and timestamp values as strings.
144  // Until MM backend is updated, this is needed for compatibility between OpenMonero and MyMonero.
145  template<>
147  {
148  static void convert(const std::string& from, uint64_t& to)
149  {
150  MTRACE("Converting std::string to uint64_t. Source: " << from);
151  // String only contains digits
152  if(std::all_of(from.begin(), from.end(), epee::misc_utils::parse::isdigit))
153  to = boost::lexical_cast<uint64_t>(from);
154  // MyMonero ISO 8061 timestamp (2017-05-06T16:27:06Z)
155  else if (boost::regex_match (from, boost::regex("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\dZ")))
156  {
157  // Convert to unix timestamp
158 #ifdef HAVE_STRPTIME
159  struct tm tm;
160  if (strptime(from.c_str(), "%Y-%m-%dT%H:%M:%S", &tm))
161 #else
162  std::tm tm = {};
163  std::istringstream ss(from);
164  if (ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"))
165 #endif
166  to = std::mktime(&tm);
167  } else
169  }
170  };
171 
172  template<class from_type, class to_type>
173  struct is_convertable: std::integral_constant<bool,
174  std::is_integral<to_type>::value &&
175  std::is_integral<from_type>::value &&
176  !std::is_same<from_type, bool>::value &&
177  !std::is_same<to_type, bool>::value > {};
178 
179  template<typename from_type, typename to_type, bool>
181 
182  template<typename from_type, typename to_type>
183  struct convert_to_same<from_type, to_type, true>
184  {
185  static void convert(const from_type& from, to_type& to)
186  {
187  to = from;
188  }
189  };
190 
191  template<typename from_type, typename to_type>
192  struct convert_to_same<from_type, to_type, false>
193  {
194  static void convert(const from_type& from, to_type& to)
195  {
197  }
198  };
199 
200 
201  template<class from_type, class to_type>
202  void convert_t(const from_type& from, to_type& to)
203  {
205  }
206  }
207 }
def sign(data)
Definition: cointool.py:602
Definition: binary_utils.h:36
#define ASSERT_AND_THROW_WRONG_CONVERSION()
Definition: portable_storage_val_converters.h:47
#define DISABLE_GCC_AND_CLANG_WARNING(w)
Definition: warnings.h:28
PUSH_WARNINGS
Definition: hash-ops.h:53
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:185
bool isdigit(char c)
Definition: parserse_base_utils.h:91
Definition: portable_storage_val_converters.h:82
::std::string string
Definition: gtest-port.h:1097
std::string convert(char val)
Definition: abstract_http_client.cpp:74
#define DISABLE_CLANG_WARNING(w)
Definition: warnings.h:25
Definition: compare.py:1
Definition: enums.h:67
void convert_t(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:202
Definition: portable_storage_val_converters.h:122
Definition: portable_storage_val_converters.h:180
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:114
Definition: portable_storage_val_converters.h:173
void convert_int_to_int(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:61
#define POP_WARNINGS
Definition: warnings.h:17
static void convert(const std::string &from, uint64_t &to)
Definition: portable_storage_val_converters.h:148
void convert_uint_to_any_int(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:71
unsigned __int64 uint64_t
Definition: stdint.h:136
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:136
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:105
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:194
#define false
Definition: stdbool.h:37
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:96
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
void convert_int_to_uint(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:50
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:87
const char * name
Definition: options.c:30
static void convert(const from_type &from, to_type &to)
Definition: portable_storage_val_converters.h:127
#define true
Definition: stdbool.h:36