Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
safe.hpp
Go to the documentation of this file.
1#pragma once
4
5#include <limits>
6
7namespace fc {
8
20 template<typename T>
21 struct safe
22 {
23 T value = 0;
24
25 template<typename O>
26 safe( O o ):value(o){}
27 safe(){}
28 safe( const safe& o ):value(o.value){}
29
30 static safe min()
31 {
32 return std::numeric_limits<T>::min();
33 }
34 static safe max()
35 {
36 return std::numeric_limits<T>::max();
37 }
38
39 friend safe operator + ( const safe& a, const safe& b )
40 {
41 if( b.value > 0 && a.value > (std::numeric_limits<T>::max() - b.value) ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
42 if( b.value < 0 && a.value < (std::numeric_limits<T>::min() - b.value) ) FC_CAPTURE_AND_THROW( underflow_exception, (a)(b) );
43 return safe( a.value + b.value );
44 }
45 friend safe operator - ( const safe& a, const safe& b )
46 {
47 if( b.value > 0 && a.value < (std::numeric_limits<T>::min() + b.value) ) FC_CAPTURE_AND_THROW( underflow_exception, (a)(b) );
48 if( b.value < 0 && a.value > (std::numeric_limits<T>::max() + b.value) ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
49 return safe( a.value - b.value );
50 }
51
52 friend safe operator * ( const safe& a, const safe& b )
53 {
54 if( a.value > 0 )
55 {
56 if( b.value > 0 )
57 {
58 if( a.value > (std::numeric_limits<T>::max() / b.value) ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
59 }
60 else
61 {
62 if( b.value < (std::numeric_limits<T>::min() / a.value) ) FC_CAPTURE_AND_THROW( underflow_exception, (a)(b) );
63 }
64 }
65 else
66 {
67 if( b.value > 0 )
68 {
69 if( a.value < (std::numeric_limits<T>::min() / b.value) ) FC_CAPTURE_AND_THROW( underflow_exception, (a)(b) );
70 }
71 else
72 {
73 if( a.value != 0 && b.value < (std::numeric_limits<T>::max() / a.value) ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
74 }
75 }
76
77 return safe( a.value * b.value );
78 }
79
80 friend safe operator / ( const safe& a, const safe& b )
81 {
82 if( b.value == 0 ) FC_CAPTURE_AND_THROW( divide_by_zero_exception, (a)(b) );
83 if( a.value == std::numeric_limits<T>::min() && b.value == -1 ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
84 return safe( a.value / b.value );
85 }
86 friend safe operator % ( const safe& a, const safe& b )
87 {
88 if( b.value == 0 ) FC_CAPTURE_AND_THROW( divide_by_zero_exception, (a)(b) );
89 if( a.value == std::numeric_limits<T>::min() && b.value == -1 ) FC_CAPTURE_AND_THROW( overflow_exception, (a)(b) );
90 return safe( a.value % b.value );
91 }
92
94 {
95 if( value == std::numeric_limits<T>::min() ) FC_CAPTURE_AND_THROW( overflow_exception, (*this) );
96 return safe( -value );
97 }
98
99 safe& operator += ( const safe& b )
100 {
101 value = (*this + b).value;
102 return *this;
103 }
104 safe& operator -= ( const safe& b )
105 {
106 value = (*this - b).value;
107 return *this;
108 }
109 safe& operator *= ( const safe& b )
110 {
111 value = (*this * b).value;
112 return *this;
113 }
114 safe& operator /= ( const safe& b )
115 {
116 value = (*this / b).value;
117 return *this;
118 }
119 safe& operator %= ( const safe& b )
120 {
121 value = (*this % b).value;
122 return *this;
123 }
124
126 {
127 *this += 1;
128 return *this;
129 }
131 {
132 safe bak = *this;
133 *this += 1;
134 return bak;
135 }
136
138 {
139 *this -= 1;
140 return *this;
141 }
143 {
144 safe bak = *this;
145 *this -= 1;
146 return bak;
147 }
148
149 friend bool operator == ( const safe& a, const safe& b )
150 {
151 return a.value == b.value;
152 }
153 friend bool operator == ( const safe& a, const T& b )
154 {
155 return a.value == b;
156 }
157 friend bool operator == ( const T& a, const safe& b )
158 {
159 return a == b.value;
160 }
161
162 friend bool operator < ( const safe& a, const safe& b )
163 {
164 return a.value < b.value;
165 }
166 friend bool operator < ( const safe& a, const T& b )
167 {
168 return a.value < b;
169 }
170 friend bool operator < ( const T& a, const safe& b )
171 {
172 return a < b.value;
173 }
174
175 friend bool operator > ( const safe& a, const safe& b )
176 {
177 return a.value > b.value;
178 }
179 friend bool operator > ( const safe& a, const T& b )
180 {
181 return a.value > b;
182 }
183 friend bool operator > ( const T& a, const safe& b )
184 {
185 return a > b.value;
186 }
187
188 friend bool operator != ( const safe& a, const safe& b )
189 {
190 return !(a == b);
191 }
192 friend bool operator != ( const safe& a, const T& b )
193 {
194 return !(a == b);
195 }
196 friend bool operator != ( const T& a, const safe& b )
197 {
198 return !(a == b);
199 }
200
201 friend bool operator <= ( const safe& a, const safe& b )
202 {
203 return !(a > b);
204 }
205 friend bool operator <= ( const safe& a, const T& b )
206 {
207 return !(a > b);
208 }
209 friend bool operator <= ( const T& a, const safe& b )
210 {
211 return !(a > b);
212 }
213
214 friend bool operator >= ( const safe& a, const safe& b )
215 {
216 return !(a < b);
217 }
218 friend bool operator >= ( const safe& a, const T& b )
219 {
220 return !(a < b);
221 }
222 friend bool operator >= ( const T& a, const safe& b )
223 {
224 return !(a < b);
225 }
226 };
227
228}
229
230FC_REFLECT_TEMPLATE( (typename T), safe<T>, (value) )
Defines exception's used by fc.
#define FC_CAPTURE_AND_THROW(EXCEPTION_TYPE,...)
namespace sysio::chain
Definition authority.cpp:3
file_not_found_exception parse_error_exception invalid_arg_exception invalid_operation_exception key_not_found_exception bad_cast_exception out_of_range_exception canceled_exception assert_exception eof_exception unknown_host_exception null_optional udt_exception aes_exception overflow_exception underflow_exception(divide_by_zero_exception)) namespace detail
Definition exception.cpp:27
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
#define FC_REFLECT_TEMPLATE(TEMPLATE_ARGS, TYPE, MEMBERS)
Definition reflect.hpp:314
safe()
Definition safe.hpp:27
safe & operator--()
Definition safe.hpp:137
friend safe operator%(const safe &a, const safe &b)
Definition safe.hpp:86
safe & operator+=(const safe &b)
Definition safe.hpp:99
friend bool operator>(const safe &a, const safe &b)
Definition safe.hpp:175
friend safe operator-(const safe &a, const safe &b)
Definition safe.hpp:45
friend safe operator*(const safe &a, const safe &b)
Definition safe.hpp:52
friend bool operator==(const safe &a, const safe &b)
Definition safe.hpp:149
safe(O o)
Definition safe.hpp:26
friend bool operator!=(const safe &a, const safe &b)
Definition safe.hpp:188
safe & operator++()
Definition safe.hpp:125
safe & operator*=(const safe &b)
Definition safe.hpp:109
safe(const safe &o)
Definition safe.hpp:28
friend bool operator>=(const safe &a, const safe &b)
Definition safe.hpp:214
T value
Definition safe.hpp:23
friend safe operator+(const safe &a, const safe &b)
Definition safe.hpp:39
safe & operator-=(const safe &b)
Definition safe.hpp:104
safe & operator/=(const safe &b)
Definition safe.hpp:114
safe operator--(int)
Definition safe.hpp:142
friend bool operator<(const safe &a, const safe &b)
Definition safe.hpp:162
safe & operator%=(const safe &b)
Definition safe.hpp:119
static safe min()
Definition safe.hpp:30
friend bool operator<=(const safe &a, const safe &b)
Definition safe.hpp:201
friend safe operator/(const safe &a, const safe &b)
Definition safe.hpp:80
safe operator++(int)
Definition safe.hpp:130
static safe max()
Definition safe.hpp:34