12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745 |
- #ifndef BOOST_NUMERIC_SAFE_BASE_OPERATIONS_HPP
- #define BOOST_NUMERIC_SAFE_BASE_OPERATIONS_HPP
- // Copyright (c) 2012 Robert Ramey
- //
- // Distributed under the Boost Software License, Version 1.0. (See
- // accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #include <limits>
- #include <type_traits> // is_base_of, is_same, is_floating_point, conditional
- #include <algorithm> // max
- #include <cassert>
- #include <boost/config.hpp>
- #include <boost/core/enable_if.hpp> // lazy_enable_if
- #include <boost/integer.hpp>
- #include <boost/logic/tribool.hpp>
- #include "checked_integer.hpp"
- #include "checked_result.hpp"
- #include "safe_base.hpp"
- #include "interval.hpp"
- #include "utility.hpp"
- namespace boost {
- namespace safe_numerics {
- /////////////////////////////////////////////////////////////////
- // validation
- template<typename R, R Min, R Max, typename E>
- struct validate_detail {
- using r_type = checked_result<R>;
- struct exception_possible {
- template<typename T>
- constexpr static R return_value(
- const T & t
- ){
- // INT08-C
- const r_type rx = heterogeneous_checked_operation<
- R,
- Min,
- Max,
- typename base_type<T>::type,
- dispatch_and_return<E, R>
- >::cast(t);
- return rx;
- }
- };
- struct exception_not_possible {
- template<typename T>
- constexpr static R return_value(
- const T & t
- ){
- return static_cast<R>(base_value(t));
- }
- };
- template<typename T>
- constexpr static R return_value(const T & t){
- constexpr const interval<r_type> t_interval{
- checked::cast<R>(base_value(std::numeric_limits<T>::min())),
- checked::cast<R>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const interval<r_type> r_interval{r_type(Min), r_type(Max)};
- static_assert(
- true != static_cast<bool>(r_interval.excludes(t_interval)),
- "can't cast from ranges that don't overlap"
- );
- return std::conditional<
- static_cast<bool>(r_interval.includes(t_interval)),
- exception_not_possible,
- exception_possible
- >::type::return_value(t);
- }
- };
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<class T>
- constexpr Stored safe_base<Stored, Min, Max, P, E>::
- validated_cast(const T & t) const {
- return validate_detail<Stored,Min,Max,E>::return_value(t);
- }
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<typename T, T N, class P1, class E1>
- constexpr Stored safe_base<Stored, Min, Max, P, E>::
- validated_cast(const safe_literal_impl<T, N, P1, E1> &) const {
- constexpr const interval<Stored> this_interval{};
- // if static values don't overlap, the program can never function
- static_assert(
- this_interval.includes(N),
- "safe type cannot be constructed from this value"
- );
- return static_cast<Stored>(N);
- }
- /////////////////////////////////////////////////////////////////
- // constructors
- template<class Stored, Stored Min, Stored Max, class P, class E>
- constexpr /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const Stored & rhs,
- skip_validation
- ) :
- m_t(rhs)
- {}
- template<class Stored, Stored Min, Stored Max, class P, class E>
- constexpr /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(){
- dispatch<E, safe_numerics_error::uninitialized_value>(
- "safe values must be initialized"
- );
- }
- // construct an instance of a safe type
- // from an instance of a convertible underlying type.
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<class T>
- constexpr /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const T & t,
- typename std::enable_if<
- is_safe<T>::value,
- bool
- >::type
- ) :
- m_t(validated_cast(t))
- {}
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<class T>
- constexpr /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const T & t,
- typename std::enable_if<
- std::is_integral<T>::value,
- bool
- >::type
- ) :
- m_t(validated_cast(t))
- {}
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<class T, T value>
- constexpr /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const std::integral_constant<T, value> &
- ) :
- m_t(validated_cast(value))
- {}
- /////////////////////////////////////////////////////////////////
- // casting operators
- // cast to a builtin type from a safe type
- template< class Stored, Stored Min, Stored Max, class P, class E>
- template<
- class R,
- typename std::enable_if<
- ! boost::safe_numerics::is_safe<R>::value,
- int
- >::type
- >
- constexpr safe_base<Stored, Min, Max, P, E>::
- operator R () const {
- // if static values don't overlap, the program can never function
- #if 1
- constexpr const interval<R> r_interval;
- constexpr const interval<Stored> this_interval(Min, Max);
- static_assert(
- ! r_interval.excludes(this_interval),
- "safe type cannot be constructed with this type"
- );
- #endif
- return validate_detail<
- R,
- std::numeric_limits<R>::min(),
- std::numeric_limits<R>::max(),
- E
- >::return_value(*this);
- }
- // cast to the underlying builtin type from a safe type
- template<class Stored, Stored Min, Stored Max, class P, class E>
- constexpr safe_base<Stored, Min, Max, P, E>::
- operator Stored () const {
- return m_t;
- }
- /////////////////////////////////////////////////////////////////
- // binary operators
- template<class T, class U>
- struct common_exception_policy {
- static_assert(is_safe<T>::value || is_safe<U>::value,
- "at least one type must be a safe type"
- );
- using t_exception_policy = typename get_exception_policy<T>::type;
- using u_exception_policy = typename get_exception_policy<U>::type;
- static_assert(
- std::is_same<t_exception_policy, u_exception_policy>::value
- || std::is_same<t_exception_policy, void>::value
- || std::is_same<void, u_exception_policy>::value,
- "if the exception policies are different, one must be void!"
- );
- static_assert(
- ! (std::is_same<t_exception_policy, void>::value
- && std::is_same<void, u_exception_policy>::value),
- "at least one exception policy must not be void"
- );
- using type =
- typename std::conditional<
- !std::is_same<void, u_exception_policy>::value,
- u_exception_policy,
- typename std::conditional<
- !std::is_same<void, t_exception_policy>::value,
- t_exception_policy,
- //
- void
- >::type >::type;
- static_assert(
- !std::is_same<void, type>::value,
- "exception_policy is void"
- );
- };
- template<class T, class U>
- struct common_promotion_policy {
- static_assert(is_safe<T>::value || is_safe<U>::value,
- "at least one type must be a safe type"
- );
- using t_promotion_policy = typename get_promotion_policy<T>::type;
- using u_promotion_policy = typename get_promotion_policy<U>::type;
- static_assert(
- std::is_same<t_promotion_policy, u_promotion_policy>::value
- ||std::is_same<t_promotion_policy, void>::value
- ||std::is_same<void, u_promotion_policy>::value,
- "if the promotion policies are different, one must be void!"
- );
- static_assert(
- ! (std::is_same<t_promotion_policy, void>::value
- && std::is_same<void, u_promotion_policy>::value),
- "at least one promotion policy must not be void"
- );
- using type =
- typename std::conditional<
- ! std::is_same<void, u_promotion_policy>::value,
- u_promotion_policy,
- typename std::conditional<
- ! std::is_same<void, t_promotion_policy>::value,
- t_promotion_policy,
- //
- void
- >::type >::type;
- static_assert(
- ! std::is_same<void, type>::value,
- "promotion_policy is void"
- );
- };
- // give the resultant base type, figure out what the final result
- // type will be. Note we currently need this because we support
- // return of only safe integer types. Someday ..., we'll support
- // all other safe types including float and user defined ones.
- // helper - cast arguments to binary operators to a specified
- // result type
- template<class EP, class R, class T, class U>
- std::pair<R, R>
- constexpr static casting_helper(const T & t, const U & u){
- using r_type = checked_result<R>;
- const r_type tx = heterogeneous_checked_operation<
- R,
- std::numeric_limits<R>::min(),
- std::numeric_limits<R>::max(),
- typename base_type<T>::type,
- dispatch_and_return<EP, R>
- >::cast(base_value(t));
- const R tr = tx.exception()
- ? static_cast<R>(t)
- : tx.m_r;
- const r_type ux = heterogeneous_checked_operation<
- R,
- std::numeric_limits<R>::min(),
- std::numeric_limits<R>::max(),
- typename base_type<U>::type,
- dispatch_and_return<EP, R>
- >::cast(base_value(u));
- const R ur = ux.exception()
- ? static_cast<R>(u)
- : ux.m_r;
- return std::pair<R, R>(tr, ur);
- }
- // Note: the following global operators will be found via
- // argument dependent lookup.
- /////////////////////////////////////////////////////////////////
- // addition
- template<class T, class U>
- struct addition_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template addition_result<T,U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- + static_cast<result_base_type>(base_value(u));
- }
- // if exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- const r_type rx = checked_operation<
- result_base_type,
- dispatch_and_return<exception_policy, result_base_type>
- >::add(r.first, r.second);
- return
- rx.exception()
- ? r.first + r.second
- : rx.m_r;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static const r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval_t u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- return t_interval + u_interval;
- }
- constexpr static const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- if(r_type_interval.l.exception())
- return true;
- if(r_type_interval.u.exception())
- return true;
- if(! return_interval.includes(r_type_interval))
- return true;
- return false;
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- addition_result<T, U>
- >::type
- constexpr operator+(const T & t, const U & u){
- return addition_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator+=(T & t, const U & u){
- t = static_cast<T>(t + u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // subtraction
- template<class T, class U>
- struct subtraction_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template subtraction_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- - static_cast<result_base_type>(base_value(u));
- }
- // if exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- const r_type rx = checked_operation<
- result_base_type,
- dispatch_and_return<exception_policy, result_base_type>
- >::subtract(r.first, r.second);
- return
- rx.exception()
- ? r.first + r.second
- : rx.m_r;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static const r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval_t u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- return t_interval - u_interval;
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- if(r_type_interval.l.exception())
- return true;
- if(r_type_interval.u.exception())
- return true;
- if(! return_interval.includes(r_type_interval))
- return true;
- return false;
- }
- public:
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- subtraction_result<T, U>
- >::type
- constexpr operator-(const T & t, const U & u){
- return subtraction_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator-=(T & t, const U & u){
- t = static_cast<T>(t - u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // multiplication
- template<class T, class U>
- struct multiplication_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template multiplication_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- * static_cast<result_base_type>(base_value(u));
- }
- // if exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
-
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- const r_type rx = checked_operation<
- result_base_type,
- dispatch_and_return<exception_policy, result_base_type>
- >::multiply(r.first, r.second);
- return
- rx.exception()
- ? r.first * r.second
- : rx.m_r;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval_t u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- return t_interval * u_interval;
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- if(r_type_interval.l.exception())
- return true;
- if(r_type_interval.u.exception())
- return true;
- if(! return_interval.includes(r_type_interval))
- return true;
- return false;
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- multiplication_result<T, U>
- >::type
- constexpr operator*(const T & t, const U & u){
- // argument dependent lookup should guarentee that we only get here
- return multiplication_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator*=(T & t, const U & u){
- t = static_cast<T>(t * u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // division
- // key idea here - result will never be larger than T
- template<class T, class U>
- struct division_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template division_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- / static_cast<result_base_type>(base_value(u));
- }
- // if exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- constexpr static int bits = std::min(
- std::numeric_limits<std::uintmax_t>::digits,
- std::max(std::initializer_list<int>{
- std::numeric_limits<result_base_type>::digits,
- std::numeric_limits<typename base_type<T>::type>::digits,
- std::numeric_limits<typename base_type<U>::type>::digits
- }) + (std::numeric_limits<result_base_type>::is_signed ? 1 : 0)
- );
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- using temp_base = typename std::conditional<
- std::numeric_limits<result_base_type>::is_signed,
- typename boost::int_t<bits>::least,
- typename boost::uint_t<bits>::least
- >::type;
- using t_type = checked_result<temp_base>;
- const std::pair<t_type, t_type> r = casting_helper<
- exception_policy,
- temp_base
- >(t, u);
- const t_type rx = checked_operation<
- temp_base,
- dispatch_and_return<exception_policy, temp_base>
- >::divide(r.first, r.second);
- return
- rx.exception()
- ? r.first / r.second
- : rx;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static r_type_interval_t t_interval(){
- return r_type_interval_t{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- };
- constexpr static r_type_interval_t u_interval(){
- return r_type_interval_t{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- };
- constexpr static r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t = t_interval();
- constexpr const r_type_interval_t u = u_interval();
- if(u.u < r_type(0) || u.l > r_type(0))
- return t / u;
- return utility::minmax(
- std::initializer_list<r_type> {
- t.l / u.l,
- t.l / r_type(-1),
- t.l / r_type(1),
- t.l / u.u,
- t.u / u.l,
- t.u / r_type(-1),
- t.u / r_type(1),
- t.u / u.u,
- }
- );
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- constexpr const r_type_interval_t ri = get_r_type_interval();
- constexpr const r_type_interval_t ui = u_interval();
- return
- static_cast<bool>(ui.includes(r_type(0)))
- || ri.l.exception()
- || ri.u.exception();
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- division_result<T, U>
- >::type
- constexpr operator/(const T & t, const U & u){
- return division_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator/=(T & t, const U & u){
- t = static_cast<T>(t / u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // modulus
- template<class T, class U>
- struct modulus_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type = typename promotion_policy::template modulus_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- % static_cast<result_base_type>(base_value(u));
- }
- // if exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- constexpr static int bits = std::min(
- std::numeric_limits<std::uintmax_t>::digits,
- std::max(std::initializer_list<int>{
- std::numeric_limits<result_base_type>::digits,
- std::numeric_limits<typename base_type<T>::type>::digits,
- std::numeric_limits<typename base_type<U>::type>::digits
- }) + (std::numeric_limits<result_base_type>::is_signed ? 1 : 0)
- );
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- using temp_base = typename std::conditional<
- std::numeric_limits<result_base_type>::is_signed,
- typename boost::int_t<bits>::least,
- typename boost::uint_t<bits>::least
- >::type;
- using t_type = checked_result<temp_base>;
-
- const std::pair<t_type, t_type> r = casting_helper<
- exception_policy,
- temp_base
- >(t, u);
- const t_type rx = checked_operation<
- temp_base,
- dispatch_and_return<exception_policy, temp_base>
- >::modulus(r.first, r.second);
- return
- rx.exception()
- ? r.first % r.second
- : rx;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static const r_type_interval_t t_interval(){
- return r_type_interval_t{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- };
- constexpr static const r_type_interval_t u_interval(){
- return r_type_interval_t{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- };
- constexpr static const r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t = t_interval();
- constexpr const r_type_interval_t u = u_interval();
- if(u.u < r_type(0)
- || u.l > r_type(0))
- return t % u;
- return utility::minmax(
- std::initializer_list<r_type> {
- t.l % u.l,
- t.l % r_type(-1),
- t.l % r_type(1),
- t.l % u.u,
- t.u % u.l,
- t.u % r_type(-1),
- t.u % r_type(1),
- t.u % u.u,
- }
- );
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- constexpr const r_type_interval_t ri = get_r_type_interval();
- constexpr const r_type_interval_t ui = u_interval();
- return
- static_cast<bool>(ui.includes(r_type(0)))
- || ri.l.exception()
- || ri.u.exception();
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- modulus_result<T, U>
- >::type
- constexpr operator%(const T & t, const U & u){
- // see https://en.wikipedia.org/wiki/Modulo_operation
- return modulus_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator%=(T & t, const U & u){
- t = static_cast<T>(t % u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // comparison
- // less than
- template<class T, class U>
- struct less_than_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template comparison_result<T, U>::type;
- // if exception not possible
- constexpr static bool
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- < static_cast<result_base_type>(base_value(u));
- }
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
- // if exception possible
- constexpr static bool
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- return safe_compare::less_than(r.first, r.second);
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static bool interval_open(const r_type_interval_t & t){
- return t.l.exception() || t.u.exception();
- }
- public:
- constexpr static bool
- return_value(const T & t, const U & u){
- constexpr const r_type_interval_t t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval_t u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- if(t_interval < u_interval)
- return true;
- if(t_interval > u_interval)
- return false;
- constexpr bool exception_possible
- = interval_open(t_interval) || interval_open(u_interval);
- return return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible>()
- );
- }
- };
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator<(const T & lhs, const U & rhs) {
- return less_than_result<T, U>::return_value(lhs, rhs);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator>(const T & lhs, const U & rhs) {
- return rhs < lhs;
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator>=(const T & lhs, const U & rhs) {
- return ! ( lhs < rhs );
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator<=(const T & lhs, const U & rhs) {
- return ! ( lhs > rhs );
- }
- // equal
- template<class T, class U>
- struct equal_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template comparison_result<T, U>::type;
- // if exception not possible
- constexpr static bool
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- == static_cast<result_base_type>(base_value(u));
- }
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
- // exception possible
- constexpr static bool
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- return safe_compare::equal(r.first, r.second);
- }
- using r_type_interval = interval<r_type>;
- constexpr static bool interval_open(const r_type_interval & t){
- return t.l.exception() || t.u.exception();
- }
- public:
- constexpr static bool
- return_value(const T & t, const U & u){
- constexpr const r_type_interval t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- if(! intersect(t_interval, u_interval))
- return false;
- constexpr bool exception_possible
- = interval_open(t_interval) || interval_open(u_interval);
- return return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible>()
- );
- }
- };
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator==(const T & lhs, const U & rhs) {
- return equal_result<T, U>::return_value(lhs, rhs);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- bool
- >::type
- constexpr operator!=(const T & lhs, const U & rhs) {
- return ! (lhs == rhs);
- }
- /////////////////////////////////////////////////////////////////
- // shift operators
- // left shift
- template<class T, class U>
- struct left_shift_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template left_shift_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- << static_cast<result_base_type>(base_value(u));
- }
- // exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
-
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- const r_type rx = checked_operation<
- result_base_type,
- dispatch_and_return<exception_policy, result_base_type>
- >::left_shift(r.first, r.second);
- return
- rx.exception()
- ? r.first << r.second
- : rx.m_r;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static r_type_interval_t get_r_type_interval(){
- constexpr const r_type_interval_t t_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- };
- constexpr const r_type_interval_t u_interval{
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- };
- return (t_interval << u_interval);
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- if(r_type_interval.l.exception())
- return true;
- if(r_type_interval.u.exception())
- return true;
- if(! return_interval.includes(r_type_interval))
- return true;
- return false;
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- // handle safe<T> << int, int << safe<U>, safe<T> << safe<U>
- // exclude std::ostream << ...
- (! std::is_base_of<std::ios_base, T>::value)
- && (is_safe<T>::value || is_safe<U>::value),
- left_shift_result<T, U>
- >::type
- constexpr operator<<(const T & t, const U & u){
- // INT13-CPP
- // C++ standards document N4618 & 5.8.2
- static_assert(
- std::numeric_limits<T>::is_integer, "shifted value must be an integer"
- );
- static_assert(
- std::numeric_limits<U>::is_integer, "shift amount must be an integer"
- );
- return left_shift_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator<<=(T & t, const U & u){
- t = static_cast<T>(t << u);
- return t;
- }
- // right shift
- template<class T, class U>
- struct right_shift_result {
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template right_shift_result<T, U>::type;
- // if exception not possible
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::false_type){
- return
- static_cast<result_base_type>(base_value(t))
- >> static_cast<result_base_type>(base_value(u));
- }
- // exception possible
- using exception_policy = typename common_exception_policy<T, U>::type;
- using r_type = checked_result<result_base_type>;
- constexpr static result_base_type
- return_value(const T & t, const U & u, std::true_type){
- const std::pair<result_base_type, result_base_type> r = casting_helper<
- exception_policy,
- result_base_type
- >(t, u);
- const r_type rx = checked_operation<
- result_base_type,
- dispatch_and_return<exception_policy, result_base_type>
- >::right_shift(r.first, r.second);
- return
- rx.exception()
- ? r.first >> r.second
- : rx.m_r;
- }
- using r_type_interval_t = interval<r_type>;
- constexpr static r_type_interval_t t_interval(){
- return r_type_interval_t(
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<T>::max()))
- );
- };
- constexpr static r_type_interval_t u_interval(){
- return r_type_interval_t(
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::min())),
- checked::cast<result_base_type>(base_value(std::numeric_limits<U>::max()))
- );
- }
- constexpr static r_type_interval_t get_r_type_interval(){;
- return (t_interval() >> u_interval());
- }
- static constexpr const r_type_interval_t r_type_interval = get_r_type_interval();
- constexpr static const interval<result_base_type> return_interval{
- r_type_interval.l.exception()
- ? std::numeric_limits<result_base_type>::min()
- : static_cast<result_base_type>(r_type_interval.l),
- r_type_interval.u.exception()
- ? std::numeric_limits<result_base_type>::max()
- : static_cast<result_base_type>(r_type_interval.u)
- };
- constexpr static bool exception_possible(){
- constexpr const r_type_interval_t ri = r_type_interval;
- constexpr const r_type_interval_t ti = t_interval();
- constexpr const r_type_interval_t ui = u_interval();
- return static_cast<bool>(
- // note undesirable coupling with checked::shift right here !
- ui.u > checked_result<result_base_type>(
- std::numeric_limits<result_base_type>::digits
- )
- || ti.l < checked_result<result_base_type>(0)
- || ui.l < checked_result<result_base_type>(0)
- || ri.l.exception()
- || ri.u.exception()
- );
- }
- constexpr static auto rl = return_interval.l;
- constexpr static auto ru = return_interval.u;
- public:
- using type =
- safe_base<
- result_base_type,
- rl,
- ru,
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- return_value(
- t,
- u,
- std::integral_constant<bool, exception_possible()>()
- ),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- (! std::is_base_of<std::ios_base, T>::value)
- && (is_safe<T>::value || is_safe<U>::value),
- right_shift_result<T, U>
- >::type
- constexpr operator>>(const T & t, const U & u){
- // INT13-CPP
- static_assert(
- std::numeric_limits<T>::is_integer, "shifted value must be an integer"
- );
- static_assert(
- std::numeric_limits<U>::is_integer, "shift amount must be an integer"
- );
- return right_shift_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator>>=(T & t, const U & u){
- t = static_cast<T>(t >> u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // bitwise operators
- // operator |
- template<class T, class U>
- struct bitwise_or_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template bitwise_or_result<T, U>::type;
- // according to the C++ standard, the bitwise operators are executed as if
- // the operands are consider a logical array of bits. That is, there is no
- // sense that these are signed numbers.
- using r_type = typename std::make_unsigned<result_base_type>::type;
- using r_type_interval_t = interval<r_type>;
- #if 0
- // breaks compilation for earlier versions of clant
- constexpr static const r_type_interval_t r_interval{
- r_type(0),
- utility::round_out(
- std::max(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- )
- };
- #endif
- using exception_policy = typename common_exception_policy<T, U>::type;
- public:
- // lazy_enable_if_c depends on this
- using type = safe_base<
- result_base_type,
- //r_interval.l,
- r_type(0),
- //r_interval.u,
- utility::round_out(
- std::max(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- ),
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- static_cast<result_base_type>(base_value(t))
- | static_cast<result_base_type>(base_value(u)),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- bitwise_or_result<T, U>
- >::type
- constexpr operator|(const T & t, const U & u){
- return bitwise_or_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator|=(T & t, const U & u){
- t = static_cast<T>(t | u);
- return t;
- }
- // operator &
- template<class T, class U>
- struct bitwise_and_result {
- private:
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template bitwise_and_result<T, U>::type;
- // according to the C++ standard, the bitwise operators are executed as if
- // the operands are consider a logical array of bits. That is, there is no
- // sense that these are signed numbers.
- using r_type = typename std::make_unsigned<result_base_type>::type;
- using r_type_interval_t = interval<r_type>;
- #if 0
- // breaks compilation for earlier versions of clant
- constexpr static const r_type_interval_t r_interval{
- r_type(0),
- utility::round_out(
- std::min(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- )
- };
- #endif
- using exception_policy = typename common_exception_policy<T, U>::type;
- public:
- // lazy_enable_if_c depends on this
- using type = safe_base<
- result_base_type,
- //r_interval.l,
- r_type(0),
- //r_interval.u,
- utility::round_out(
- std::min(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- ),
- promotion_policy,
- exception_policy
- >;
-
- constexpr static type return_value(const T & t, const U & u){
- return type(
- static_cast<result_base_type>(base_value(t))
- & static_cast<result_base_type>(base_value(u)),
- typename type::skip_validation()
- );
- }
- };
-
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- bitwise_and_result<T, U>
- >::type
- constexpr operator&(const T & t, const U & u){
- return bitwise_and_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator&=(T & t, const U & u){
- t = static_cast<T>(t & u);
- return t;
- }
- // operator ^
- template<class T, class U>
- struct bitwise_xor_result {
- using promotion_policy = typename common_promotion_policy<T, U>::type;
- using result_base_type =
- typename promotion_policy::template bitwise_xor_result<T, U>::type;
- // according to the C++ standard, the bitwise operators are executed as if
- // the operands are consider a logical array of bits. That is, there is no
- // sense that these are signed numbers.
- using r_type = typename std::make_unsigned<result_base_type>::type;
- using r_type_interval_t = interval<r_type>;
- #if 0
- // breaks compilation for earlier versions of clant
- constexpr static const r_type_interval_t r_interval{
- r_type(0),
- utility::round_out(
- std::max(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- )
- };
- #endif
- using exception_policy = typename common_exception_policy<T, U>::type;
- public:
- // lazy_enable_if_c depends on this
- using type = safe_base<
- result_base_type,
- //r_interval.l,
- r_type(0),
- //r_interval.u,
- utility::round_out(
- std::max(
- static_cast<r_type>(base_value(std::numeric_limits<T>::max())),
- static_cast<r_type>(base_value(std::numeric_limits<U>::max()))
- )
- ),
- promotion_policy,
- exception_policy
- >;
- constexpr static type return_value(const T & t, const U & u){
- return type(
- static_cast<result_base_type>(base_value(t))
- ^ static_cast<result_base_type>(base_value(u)),
- typename type::skip_validation()
- );
- }
- };
- template<class T, class U>
- typename boost::lazy_enable_if_c<
- is_safe<T>::value || is_safe<U>::value,
- bitwise_xor_result<T, U>
- >::type
- constexpr operator^(const T & t, const U & u){
- return bitwise_xor_result<T, U>::return_value(t, u);
- }
- template<class T, class U>
- typename std::enable_if<
- is_safe<T>::value || is_safe<U>::value,
- T
- >::type
- constexpr operator^=(T & t, const U & u){
- t = static_cast<T>(t ^ u);
- return t;
- }
- /////////////////////////////////////////////////////////////////
- // stream helpers
- template<
- class T,
- T Min,
- T Max,
- class P, // promotion polic
- class E // exception policy
- >
- template<
- class CharT,
- class Traits
- >
- void safe_base<T, Min, Max, P, E>::output(
- std::basic_ostream<CharT, Traits> & os
- ) const {
- os << (
- (std::is_same<T, signed char>::value
- || std::is_same<T, unsigned char>::value
- || std::is_same<T, wchar_t>::value
- ) ?
- static_cast<int>(m_t)
- :
- m_t
- );
- }
- template<
- class T,
- T Min,
- T Max,
- class P, // promotion polic
- class E // exception policy
- >
- template<
- class CharT,
- class Traits
- >
- void safe_base<T, Min, Max, P, E>::input(
- std::basic_istream<CharT, Traits> & is
- ){
- if(std::is_same<T, signed char>::value
- || std::is_same<T, unsigned char>::value
- || std::is_same<T, wchar_t>::value
- ){
- int x;
- is >> x;
- m_t = validated_cast(x);
- }
- else{
- is >> m_t;
- validated_cast(m_t);
- }
- if(is.fail()){
- boost::safe_numerics::dispatch<
- E,
- boost::safe_numerics::safe_numerics_error::domain_error
- >(
- "error in file input"
- );
- }
- }
- } // safe_numerics
- } // boost
- #endif // BOOST_NUMERIC_SAFE_BASE_OPERATIONS_HPP
|