Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::safe< T > Struct Template Reference

#include <safe.hpp>

Public Member Functions

template<typename O >
 safe (O o)
 
 safe ()
 
 safe (const safe &o)
 
safe operator- () const
 
safeoperator+= (const safe &b)
 
safeoperator-= (const safe &b)
 
safeoperator*= (const safe &b)
 
safeoperator/= (const safe &b)
 
safeoperator%= (const safe &b)
 
safeoperator++ ()
 
safe operator++ (int)
 
safeoperator-- ()
 
safe operator-- (int)
 

Static Public Member Functions

static safe min ()
 
static safe max ()
 

Public Attributes

T value = 0
 

Friends

safe operator+ (const safe &a, const safe &b)
 
safe operator- (const safe &a, const safe &b)
 
safe operator* (const safe &a, const safe &b)
 
safe operator/ (const safe &a, const safe &b)
 
safe operator% (const safe &a, const safe &b)
 
bool operator== (const safe &a, const safe &b)
 
bool operator== (const safe &a, const T &b)
 
bool operator== (const T &a, const safe &b)
 
bool operator< (const safe &a, const safe &b)
 
bool operator< (const safe &a, const T &b)
 
bool operator< (const T &a, const safe &b)
 
bool operator> (const safe &a, const safe &b)
 
bool operator> (const safe &a, const T &b)
 
bool operator> (const T &a, const safe &b)
 
bool operator!= (const safe &a, const safe &b)
 
bool operator!= (const safe &a, const T &b)
 
bool operator!= (const T &a, const safe &b)
 
bool operator<= (const safe &a, const safe &b)
 
bool operator<= (const safe &a, const T &b)
 
bool operator<= (const T &a, const safe &b)
 
bool operator>= (const safe &a, const safe &b)
 
bool operator>= (const safe &a, const T &b)
 
bool operator>= (const T &a, const safe &b)
 

Detailed Description

template<typename T>
struct fc::safe< T >

This type is designed to provide automatic checks for integer overflow and default initialization. It will throw an exception on overflow conditions.

It can only be used on built-in types. In particular, safe<uint128_t> is buggy and should not be used.

Implemented using spec from: https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow

Definition at line 42 of file variant.hpp.

Constructor & Destructor Documentation

◆ safe() [1/3]

template<typename T >
template<typename O >
fc::safe< T >::safe ( O o)
inline

Definition at line 26 of file safe.hpp.

26:value(o){}
T value
Definition safe.hpp:23

◆ safe() [2/3]

template<typename T >
fc::safe< T >::safe ( )
inline

Definition at line 27 of file safe.hpp.

27{}
Here is the caller graph for this function:

◆ safe() [3/3]

template<typename T >
fc::safe< T >::safe ( const safe< T > & o)
inline

Definition at line 28 of file safe.hpp.

28:value(o.value){}

Member Function Documentation

◆ max()

template<typename T >
static safe fc::safe< T >::max ( )
inlinestatic

Definition at line 34 of file safe.hpp.

35 {
36 return std::numeric_limits<T>::max();
37 }

◆ min()

template<typename T >
static safe fc::safe< T >::min ( )
inlinestatic

Definition at line 30 of file safe.hpp.

31 {
32 return std::numeric_limits<T>::min();
33 }

◆ operator%=()

template<typename T >
safe & fc::safe< T >::operator%= ( const safe< T > & b)
inline

Definition at line 119 of file safe.hpp.

120 {
121 value = (*this % b).value;
122 return *this;
123 }

◆ operator*=()

template<typename T >
safe & fc::safe< T >::operator*= ( const safe< T > & b)
inline

Definition at line 109 of file safe.hpp.

110 {
111 value = (*this * b).value;
112 return *this;
113 }

◆ operator++() [1/2]

template<typename T >
safe & fc::safe< T >::operator++ ( )
inline

Definition at line 125 of file safe.hpp.

126 {
127 *this += 1;
128 return *this;
129 }

◆ operator++() [2/2]

template<typename T >
safe fc::safe< T >::operator++ ( int )
inline

Definition at line 130 of file safe.hpp.

131 {
132 safe bak = *this;
133 *this += 1;
134 return bak;
135 }
safe()
Definition safe.hpp:27

◆ operator+=()

template<typename T >
safe & fc::safe< T >::operator+= ( const safe< T > & b)
inline

Definition at line 99 of file safe.hpp.

100 {
101 value = (*this + b).value;
102 return *this;
103 }

◆ operator-()

template<typename T >
safe fc::safe< T >::operator- ( ) const
inline

Definition at line 93 of file safe.hpp.

94 {
95 if( value == std::numeric_limits<T>::min() ) FC_CAPTURE_AND_THROW( overflow_exception, (*this) );
96 return safe( -value );
97 }
#define FC_CAPTURE_AND_THROW(EXCEPTION_TYPE,...)
Here is the call graph for this function:

◆ operator--() [1/2]

template<typename T >
safe & fc::safe< T >::operator-- ( )
inline

Definition at line 137 of file safe.hpp.

138 {
139 *this -= 1;
140 return *this;
141 }

◆ operator--() [2/2]

template<typename T >
safe fc::safe< T >::operator-- ( int )
inline

Definition at line 142 of file safe.hpp.

143 {
144 safe bak = *this;
145 *this -= 1;
146 return bak;
147 }

◆ operator-=()

template<typename T >
safe & fc::safe< T >::operator-= ( const safe< T > & b)
inline

Definition at line 104 of file safe.hpp.

105 {
106 value = (*this - b).value;
107 return *this;
108 }

◆ operator/=()

template<typename T >
safe & fc::safe< T >::operator/= ( const safe< T > & b)
inline

Definition at line 114 of file safe.hpp.

115 {
116 value = (*this / b).value;
117 return *this;
118 }

Friends And Related Symbol Documentation

◆ operator!= [1/3]

template<typename T >
bool operator!= ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 188 of file safe.hpp.

189 {
190 return !(a == b);
191 }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ operator!= [2/3]

template<typename T >
bool operator!= ( const safe< T > & a,
const T & b )
friend

Definition at line 192 of file safe.hpp.

193 {
194 return !(a == b);
195 }

◆ operator!= [3/3]

template<typename T >
bool operator!= ( const T & a,
const safe< T > & b )
friend

Definition at line 196 of file safe.hpp.

197 {
198 return !(a == b);
199 }

◆ operator%

template<typename T >
safe operator% ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 86 of file safe.hpp.

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 }

◆ operator*

template<typename T >
safe operator* ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 52 of file safe.hpp.

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 }
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

◆ operator+

template<typename T >
safe operator+ ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 39 of file safe.hpp.

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 }

◆ operator-

template<typename T >
safe operator- ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 45 of file safe.hpp.

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 }

◆ operator/

template<typename T >
safe operator/ ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 80 of file safe.hpp.

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 }

◆ operator< [1/3]

template<typename T >
bool operator< ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 162 of file safe.hpp.

163 {
164 return a.value < b.value;
165 }

◆ operator< [2/3]

template<typename T >
bool operator< ( const safe< T > & a,
const T & b )
friend

Definition at line 166 of file safe.hpp.

167 {
168 return a.value < b;
169 }

◆ operator< [3/3]

template<typename T >
bool operator< ( const T & a,
const safe< T > & b )
friend

Definition at line 170 of file safe.hpp.

171 {
172 return a < b.value;
173 }

◆ operator<= [1/3]

template<typename T >
bool operator<= ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 201 of file safe.hpp.

202 {
203 return !(a > b);
204 }

◆ operator<= [2/3]

template<typename T >
bool operator<= ( const safe< T > & a,
const T & b )
friend

Definition at line 205 of file safe.hpp.

206 {
207 return !(a > b);
208 }

◆ operator<= [3/3]

template<typename T >
bool operator<= ( const T & a,
const safe< T > & b )
friend

Definition at line 209 of file safe.hpp.

210 {
211 return !(a > b);
212 }

◆ operator== [1/3]

template<typename T >
bool operator== ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 149 of file safe.hpp.

150 {
151 return a.value == b.value;
152 }

◆ operator== [2/3]

template<typename T >
bool operator== ( const safe< T > & a,
const T & b )
friend

Definition at line 153 of file safe.hpp.

154 {
155 return a.value == b;
156 }

◆ operator== [3/3]

template<typename T >
bool operator== ( const T & a,
const safe< T > & b )
friend

Definition at line 157 of file safe.hpp.

158 {
159 return a == b.value;
160 }

◆ operator> [1/3]

template<typename T >
bool operator> ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 175 of file safe.hpp.

176 {
177 return a.value > b.value;
178 }

◆ operator> [2/3]

template<typename T >
bool operator> ( const safe< T > & a,
const T & b )
friend

Definition at line 179 of file safe.hpp.

180 {
181 return a.value > b;
182 }

◆ operator> [3/3]

template<typename T >
bool operator> ( const T & a,
const safe< T > & b )
friend

Definition at line 183 of file safe.hpp.

184 {
185 return a > b.value;
186 }

◆ operator>= [1/3]

template<typename T >
bool operator>= ( const safe< T > & a,
const safe< T > & b )
friend

Definition at line 214 of file safe.hpp.

215 {
216 return !(a < b);
217 }

◆ operator>= [2/3]

template<typename T >
bool operator>= ( const safe< T > & a,
const T & b )
friend

Definition at line 218 of file safe.hpp.

219 {
220 return !(a < b);
221 }

◆ operator>= [3/3]

template<typename T >
bool operator>= ( const T & a,
const safe< T > & b )
friend

Definition at line 222 of file safe.hpp.

223 {
224 return !(a < b);
225 }

Member Data Documentation

◆ value

template<typename T >
T fc::safe< T >::value = 0

Definition at line 23 of file safe.hpp.


The documentation for this struct was generated from the following files: