Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
base::internal::CheckedNumeric< T > Class Template Reference

#include <checked_math.h>

Public Types

using type = T
 

Public Member Functions

constexpr CheckedNumeric ()=default
 
template<typename Src >
constexpr CheckedNumeric (const CheckedNumeric< Src > &rhs)
 
template<typename Src >
constexpr CheckedNumeric (Src value)
 
template<typename Src >
constexpr CheckedNumeric (StrictNumeric< Src > value)
 
template<typename Dst = T>
constexpr bool IsValid () const
 
template<typename Dst >
constexpr bool AssignIfValid (Dst *result) const
 
template<typename Dst = T, class CheckHandler = CheckOnFailure>
constexpr StrictNumeric< Dst > ValueOrDie () const
 
template<typename Dst = T, typename Src >
constexpr StrictNumeric< Dst > ValueOrDefault (const Src default_value) const
 
template<typename Dst >
constexpr CheckedNumeric< typename UnderlyingType< Dst >::typeCast () const
 
template<typename Src >
constexpr CheckedNumericoperator+= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator-= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator*= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator/= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator%= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator<<= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator>>= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator&= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator|= (const Src rhs)
 
template<typename Src >
constexpr CheckedNumericoperator^= (const Src rhs)
 
constexpr CheckedNumeric operator- () const
 
constexpr CheckedNumeric operator~ () const
 
constexpr CheckedNumeric Abs () const
 
template<typename U >
constexpr CheckedNumeric< typename MathWrapper< CheckedMaxOp, T, U >::typeMax (const U rhs) const
 
template<typename U >
constexpr CheckedNumeric< typename MathWrapper< CheckedMinOp, T, U >::typeMin (const U rhs) const
 
constexpr CheckedNumeric< typename UnsignedOrFloatForSize< T >::typeUnsignedAbs () const
 
constexpr CheckedNumericoperator++ ()
 
constexpr CheckedNumeric operator++ (int)
 
constexpr CheckedNumericoperator-- ()
 
constexpr CheckedNumeric operator-- (int)
 
template<template< typename, typename, typename > class M, typename R >
constexpr CheckedNumericMathOp (const R rhs)
 

Static Public Member Functions

template<template< typename, typename, typename > class M, typename L , typename R >
static constexpr CheckedNumeric MathOp (const L lhs, const R rhs)
 

Friends

template<typename Src >
class CheckedNumeric
 
template<typename U >
GetNumericValueForTest (const CheckedNumeric< U > &src)
 

Detailed Description

template<typename T>
class base::internal::CheckedNumeric< T >

Definition at line 18 of file checked_math.h.

Member Typedef Documentation

◆ type

template<typename T >
using base::internal::CheckedNumeric< T >::type = T

Definition at line 23 of file checked_math.h.

Constructor & Destructor Documentation

◆ CheckedNumeric() [1/4]

template<typename T >
constexpr base::internal::CheckedNumeric< T >::CheckedNumeric ( )
constexprdefault

◆ CheckedNumeric() [2/4]

template<typename T >
template<typename Src >
constexpr base::internal::CheckedNumeric< T >::CheckedNumeric ( const CheckedNumeric< Src > &  rhs)
inlineconstexpr

Definition at line 29 of file checked_math.h.

30 : state_(rhs.state_.value(), rhs.IsValid()) {}

◆ CheckedNumeric() [3/4]

template<typename T >
template<typename Src >
constexpr base::internal::CheckedNumeric< T >::CheckedNumeric ( Src  value)
inlineconstexpr

Definition at line 38 of file checked_math.h.

39 : state_(value) {
40 static_assert(std::is_arithmetic<Src>::value, "Argument must be numeric.");
41 }

◆ CheckedNumeric() [4/4]

template<typename T >
template<typename Src >
constexpr base::internal::CheckedNumeric< T >::CheckedNumeric ( StrictNumeric< Src >  value)
inlineconstexpr

Definition at line 46 of file checked_math.h.

48 : state_(static_cast<Src>(value)) {}

Member Function Documentation

◆ Abs()

template<typename T >
constexpr CheckedNumeric base::internal::CheckedNumeric< T >::Abs ( ) const
inlineconstexpr

Definition at line 159 of file checked_math.h.

159 {
160 return !IsValueNegative(state_.value()) ? *this : -*this;
161 }
constexpr bool IsValueNegative(T value)

◆ AssignIfValid()

template<typename T >
template<typename Dst >
constexpr bool base::internal::CheckedNumeric< T >::AssignIfValid ( Dst *  result) const
inlineconstexpr

Definition at line 69 of file checked_math.h.

69 {
70 return BASE_NUMERICS_LIKELY(IsValid<Dst>())
71 ? ((*result = static_cast<Dst>(state_.value())), true)
72 : false;
73 }
GAsyncResult * result
#define BASE_NUMERICS_LIKELY(x)

◆ Cast()

template<typename T >
template<typename Dst >
constexpr CheckedNumeric< typename UnderlyingType< Dst >::type > base::internal::CheckedNumeric< T >::Cast ( ) const
inlineconstexpr

Definition at line 107 of file checked_math.h.

107 {
108 return *this;
109 }

◆ IsValid()

template<typename T >
template<typename Dst = T>
constexpr bool base::internal::CheckedNumeric< T >::IsValid ( ) const
inlineconstexpr

Definition at line 54 of file checked_math.h.

54 {
55 return state_.is_valid() &&
56 IsValueInRangeForNumericType<Dst>(state_.value());
57 }

◆ MathOp() [1/2]

template<typename T >
template<template< typename, typename, typename > class M, typename L , typename R >
static constexpr CheckedNumeric base::internal::CheckedNumeric< T >::MathOp ( const L  lhs,
const R  rhs 
)
inlinestaticconstexpr

Definition at line 229 of file checked_math.h.

229 {
230 using Math = typename MathWrapper<M, L, R>::math;
231 T result = 0;
232 bool is_valid =
233 Wrapper<L>::is_valid(lhs) && Wrapper<R>::is_valid(rhs) &&
234 Math::Do(Wrapper<L>::value(lhs), Wrapper<R>::value(rhs), &result);
235 return CheckedNumeric<T>(result, is_valid);
236 }
static bool is_valid(SkISize dim)
#define T
M< typename UnderlyingType< L >::type, typename UnderlyingType< R >::type, void > math

◆ MathOp() [2/2]

template<typename T >
template<template< typename, typename, typename > class M, typename R >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::MathOp ( const R  rhs)
inlineconstexpr

Definition at line 240 of file checked_math.h.

240 {
241 using Math = typename MathWrapper<M, T, R>::math;
242 T result = 0; // Using T as the destination saves a range check.
243 bool is_valid = state_.is_valid() && Wrapper<R>::is_valid(rhs) &&
244 Math::Do(state_.value(), Wrapper<R>::value(rhs), &result);
245 *this = CheckedNumeric<T>(result, is_valid);
246 return *this;
247 }

◆ Max()

template<typename T >
template<typename U >
constexpr CheckedNumeric< typename MathWrapper< CheckedMaxOp, T, U >::type > base::internal::CheckedNumeric< T >::Max ( const U  rhs) const
inlineconstexpr

Definition at line 164 of file checked_math.h.

165 {
166 using R = typename UnderlyingType<U>::type;
167 using result_type = typename MathWrapper<CheckedMaxOp, T, U>::type;
168 // TODO(jschuh): This can be converted to the MathOp version and remain
169 // constexpr once we have C++14 support.
170 return CheckedNumeric<result_type>(
171 static_cast<result_type>(
172 IsGreater<T, R>::Test(state_.value(), Wrapper<U>::value(rhs))
173 ? state_.value()
174 : Wrapper<U>::value(rhs)),
175 state_.is_valid() && Wrapper<U>::is_valid(rhs));
176 }
uint8_t value
#define R(r)
static constexpr bool Test(const L lhs, const R rhs)
typename math::result_type type
typename ArithmeticOrUnderlyingEnum< T >::type type

◆ Min()

template<typename T >
template<typename U >
constexpr CheckedNumeric< typename MathWrapper< CheckedMinOp, T, U >::type > base::internal::CheckedNumeric< T >::Min ( const U  rhs) const
inlineconstexpr

Definition at line 179 of file checked_math.h.

180 {
181 using R = typename UnderlyingType<U>::type;
182 using result_type = typename MathWrapper<CheckedMinOp, T, U>::type;
183 // TODO(jschuh): This can be converted to the MathOp version and remain
184 // constexpr once we have C++14 support.
185 return CheckedNumeric<result_type>(
186 static_cast<result_type>(
187 IsLess<T, R>::Test(state_.value(), Wrapper<U>::value(rhs))
188 ? state_.value()
189 : Wrapper<U>::value(rhs)),
190 state_.is_valid() && Wrapper<U>::is_valid(rhs));
191 }
static constexpr bool Test(const L lhs, const R rhs)

◆ operator%=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator%= ( const Src  rhs)
constexpr

◆ operator&=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator&= ( const Src  rhs)
constexpr

◆ operator*=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator*= ( const Src  rhs)
constexpr

◆ operator++() [1/2]

template<typename T >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator++ ( )
inlineconstexpr

Definition at line 202 of file checked_math.h.

202 {
203 *this += 1;
204 return *this;
205 }

◆ operator++() [2/2]

template<typename T >
constexpr CheckedNumeric base::internal::CheckedNumeric< T >::operator++ ( int  )
inlineconstexpr

Definition at line 207 of file checked_math.h.

207 {
208 CheckedNumeric value = *this;
209 *this += 1;
210 return value;
211 }

◆ operator+=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator+= ( const Src  rhs)
constexpr

◆ operator-()

template<typename T >
constexpr CheckedNumeric base::internal::CheckedNumeric< T >::operator- ( ) const
inlineconstexpr

Definition at line 139 of file checked_math.h.

139 {
140 // The negation of two's complement int min is int min, so we simply
141 // check for that in the constexpr case.
142 // We use an optimized code path for a known run-time variable.
143 return MustTreatAsConstexpr(state_.value()) || !std::is_signed<T>::value ||
144 std::is_floating_point<T>::value
145 ? CheckedNumeric<T>(
146 NegateWrapper(state_.value()),
147 IsValid() && (!std::is_signed<T>::value ||
148 std::is_floating_point<T>::value ||
149 NegateWrapper(state_.value()) !=
150 std::numeric_limits<T>::lowest()))
151 : FastRuntimeNegate();
152 }
constexpr bool IsValid() const
constexpr T NegateWrapper(T value)
constexpr bool MustTreatAsConstexpr(const T v)

◆ operator--() [1/2]

template<typename T >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator-- ( )
inlineconstexpr

Definition at line 213 of file checked_math.h.

213 {
214 *this -= 1;
215 return *this;
216 }

◆ operator--() [2/2]

template<typename T >
constexpr CheckedNumeric base::internal::CheckedNumeric< T >::operator-- ( int  )
inlineconstexpr

Definition at line 218 of file checked_math.h.

218 {
219 CheckedNumeric value = *this;
220 *this -= 1;
221 return value;
222 }

◆ operator-=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator-= ( const Src  rhs)
constexpr

◆ operator/=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator/= ( const Src  rhs)
constexpr

◆ operator<<=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator<<= ( const Src  rhs)
constexpr

◆ operator>>=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator>>= ( const Src  rhs)
constexpr

◆ operator^=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator^= ( const Src  rhs)
constexpr

◆ operator|=()

template<typename T >
template<typename Src >
constexpr CheckedNumeric & base::internal::CheckedNumeric< T >::operator|= ( const Src  rhs)
constexpr

◆ operator~()

template<typename T >
constexpr CheckedNumeric base::internal::CheckedNumeric< T >::operator~ ( ) const
inlineconstexpr

Definition at line 154 of file checked_math.h.

154 {
155 return CheckedNumeric<decltype(InvertWrapper(T()))>(
156 InvertWrapper(state_.value()), IsValid());
157 }
constexpr std::make_unsigned< T >::type InvertWrapper(T value)

◆ UnsignedAbs()

template<typename T >
constexpr CheckedNumeric< typename UnsignedOrFloatForSize< T >::type > base::internal::CheckedNumeric< T >::UnsignedAbs ( ) const
inlineconstexpr

Definition at line 197 of file checked_math.h.

197 {
198 return CheckedNumeric<typename UnsignedOrFloatForSize<T>::type>(
199 SafeUnsignedAbs(state_.value()), state_.is_valid());
200 }
constexpr std::make_unsigned< T >::type SafeUnsignedAbs(T value)

◆ ValueOrDefault()

template<typename T >
template<typename Dst = T, typename Src >
constexpr StrictNumeric< Dst > base::internal::CheckedNumeric< T >::ValueOrDefault ( const Src  default_value) const
inlineconstexpr

Definition at line 97 of file checked_math.h.

97 {
98 return BASE_NUMERICS_LIKELY(IsValid<Dst>())
99 ? static_cast<Dst>(state_.value())
100 : checked_cast<Dst>(default_value);
101 }
constexpr Dst checked_cast(Src value)

◆ ValueOrDie()

template<typename T >
template<typename Dst = T, class CheckHandler = CheckOnFailure>
constexpr StrictNumeric< Dst > base::internal::CheckedNumeric< T >::ValueOrDie ( ) const
inlineconstexpr

Definition at line 84 of file checked_math.h.

84 {
85 return BASE_NUMERICS_LIKELY(IsValid<Dst>())
86 ? static_cast<Dst>(state_.value())
87 : CheckHandler::template HandleFailure<Dst>();
88 }

Friends And Related Symbol Documentation

◆ CheckedNumeric

template<typename T >
template<typename Src >
friend class CheckedNumeric
friend

Definition at line 33 of file checked_math.h.

◆ GetNumericValueForTest

template<typename T >
template<typename U >
U GetNumericValueForTest ( const CheckedNumeric< U > &  src)
friend

The documentation for this class was generated from the following file: