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 <limits>
38#include <type_traits>
39#include <typeinfo>
40
41namespace epee
42{
43 namespace serialization
44 {
45#define ASSERT_AND_THROW_WRONG_CONVERSION() ASSERT_MES_AND_THROW("WRONG DATA CONVERSION: from type=" << typeid(from).name() << " to type " << typeid(to).name())
46
47 template<typename from_type, typename to_type>
48 void convert_int_to_uint(const from_type& from, to_type& to)
49 {
52 CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value");
54 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());
55 to = static_cast<to_type>(from);
57 }
58 template<typename from_type, typename to_type>
59 void convert_int_to_int(const from_type& from, to_type& to)
60 {
61 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());
63DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
64 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());
66 to = static_cast<to_type>(from);
67 }
68 template<typename from_type, typename to_type>
69 void convert_uint_to_any_int(const from_type& from, to_type& to)
70 {
73DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
74 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());
75 to = static_cast<to_type>(from);
77 }
78
79 template<typename from_type, typename to_type, bool, bool> //is from signed, is from to signed
81
82 template<typename from_type, typename to_type>
83 struct convert_to_signed_unsigned<from_type, to_type, true, true>
84 {
85 static void convert(const from_type& from, to_type& to)
86 {//from signed to signed
87 convert_int_to_int(from, to);
88 }
89 };
90
91 template<typename from_type, typename to_type>
92 struct convert_to_signed_unsigned<from_type, to_type, true, false>
93 {
94 static void convert(const from_type& from, to_type& to)
95 {//from signed to unsigned
96 convert_int_to_uint(from, to);
97 }
98 };
99
100 template<typename from_type, typename to_type>
101 struct convert_to_signed_unsigned<from_type, to_type, false, true>
102 {
103 static void convert(const from_type& from, to_type& to)
104 {//from unsigned to signed
105 convert_uint_to_any_int(from, to);
106 }
107 };
108
109 template<typename from_type, typename to_type>
110 struct convert_to_signed_unsigned<from_type, to_type, false, false>
111 {
112 static void convert(const from_type& from, to_type& to)
113 {
114 //from unsigned to unsigned
115 convert_uint_to_any_int(from, to);
116 }
117 };
118
119 template<typename from_type, typename to_type, bool>
121
122 template<typename from_type, typename to_type>
123 struct convert_to_integral<from_type, to_type, true>
124 {
125 static void convert(const from_type& from, to_type& to)
126 {
128 }
129 };
130
131 template<typename from_type, typename to_type>
132 struct convert_to_integral<from_type, to_type, false>
133 {
134 static void convert(const from_type& from, to_type& to)
135 {
137 }
138 };
139
140 template<class from_type, class to_type>
141 struct is_convertable: std::integral_constant<bool,
142 std::is_integral<to_type>::value &&
143 std::is_integral<from_type>::value &&
144 !std::is_same<from_type, bool>::value &&
145 !std::is_same<to_type, bool>::value > {};
146
147 template<typename from_type, typename to_type, bool>
149
150 template<typename from_type, typename to_type>
151 struct convert_to_same<from_type, to_type, true>
152 {
153 static void convert(const from_type& from, to_type& to)
154 {
155 to = from;
156 }
157 };
158
159 template<typename from_type, typename to_type>
160 struct convert_to_same<from_type, to_type, false>
161 {
162 static void convert(const from_type& from, to_type& to)
163 {
165 }
166 };
167
168
169 template<class from_type, class to_type>
170 void convert_t(const from_type& from, to_type& to)
171 {
173 }
174 }
175}
#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:170
void convert_int_to_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:59
void convert_uint_to_any_int(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:69
void convert_int_to_uint(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:48
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
#define ASSERT_AND_THROW_WRONG_CONVERSION()
Definition portable_storage_val_converters.h:45
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:134
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:125
Definition portable_storage_val_converters.h:120
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:162
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:153
Definition portable_storage_val_converters.h:148
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:112
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:103
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:94
static void convert(const from_type &from, to_type &to)
Definition portable_storage_val_converters.h:85
Definition portable_storage_val_converters.h:80
Definition portable_storage_val_converters.h:145
#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