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 "misc_language.h"
33#include "warnings.h"
34#include "misc_log_ex.h"
35
36#include <boost/numeric/conversion/bounds.hpp>
37#include <cstdint>
38#include <limits>
39#include <string>
40#include <type_traits>
41#include <typeinfo>
42
43namespace 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());
65DISABLE_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 {
75DISABLE_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 template<>
144 {
145 static void convert(const std::string& from, uint64_t& to)
146 {
147 if (from.empty())
149
150 uint64_t value = 0;
151 for (const char c : from)
152 {
153 if (c < '0' || c > '9')
155 const uint64_t digit = c - '0';
156 if (value > (std::numeric_limits<uint64_t>::max() - digit) / 10)
158 value = value * 10 + digit;
159 }
160 to = value;
161 }
162 };
163
164 template<class from_type, class to_type>
165 struct is_convertable: std::integral_constant<bool,
166 std::is_integral<to_type>::value &&
167 std::is_integral<from_type>::value &&
168 !std::is_same<from_type, bool>::value &&
169 !std::is_same<to_type, bool>::value > {};
170
171 template<typename from_type, typename to_type, bool>
173
174 template<typename from_type, typename to_type>
175 struct convert_to_same<from_type, to_type, true>
176 {
177 static void convert(const from_type& from, to_type& to)
178 {
179 to = from;
180 }
181 };
182
183 template<typename from_type, typename to_type>
184 struct convert_to_same<from_type, to_type, false>
185 {
186 static void convert(const from_type& from, to_type& to)
187 {
189 }
190 };
191
192
193 template<class from_type, class to_type>
194 void convert_t(const from_type& from, to_type& to)
195 {
197 }
198 }
199}
#define true
#define false
Definition compare.py:1
Definition keyvalue_serialization_overloads.h:49
void convert_t(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:194
void convert_int_to_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:61
void convert_uint_to_any_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:71
void convert_int_to_uint(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:50
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition enums.h:68
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
#define ASSERT_AND_THROW_WRONG_CONVERSION()
Definition portable_storage_val_converters.h:47
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:127
static void convert(const std::string &from, uint64_t &to)
Definition portable_storage_val_converters.h:145
Definition portable_storage_val_converters.h:122
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:186
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:177
Definition portable_storage_val_converters.h:172
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:114
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:96
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:87
Definition portable_storage_val_converters.h:82
Definition portable_storage_val_converters.h:169
#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