123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713 |
- #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 <istream>
- #include <ostream>
- #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 inline Stored safe_base<Stored, Min, Max, P, E>::
- validated_cast(const T & t) const {
- return validate_detail<Stored,Min,Max,E>::return_value(t);
- }
- /////////////////////////////////////////////////////////////////
- // constructors
- // default constructor
- template<class Stored, Stored Min, Stored Max, class P, class E>
- constexpr inline /*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>
- constexpr inline /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const Stored & rhs,
- skip_validation
- ) :
- m_t(rhs)
- {}
- // construct an instance from an instance of a convertible underlying type.
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<
- class T,
- typename std::enable_if<
- std::is_convertible<T, Stored>::value,
- bool
- >::type
- >
- constexpr inline /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(const T &t) :
- m_t(validated_cast(t))
- {}
- // construct an instance of a safe type from a literal value
- template<class Stored, Stored Min, Stored Max, class P, class E>
- template<typename T, T N, class Px, class Ex>
- constexpr inline /*explicit*/ safe_base<Stored, Min, Max, P, E>::safe_base(
- const safe_literal_impl<T, N, Px, Ex> & t
- ) :
- m_t(validated_cast(t))
- {}
- /////////////////////////////////////////////////////////////////
- // 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 inline safe_base<Stored, Min, Max, P, E>::
- operator R () const {
- // if static values don't overlap, the program can never function
- 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"
- );
- return validate_detail<
- R,
- std::numeric_limits<R>::min(),
- std::numeric_limits<R>::max(),
- E
- >::return_value(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>
- constexpr inline static std::pair<R, R> 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_contents.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_contents.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_contents.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 inline 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 inline 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_contents.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;
- }
- 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 inline 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 inline 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_contents.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;
- }
- 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,
- multiplication_result<T, U>
- >::type
- constexpr inline 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 inline 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 const 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,
- }
- );
- }
- 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(){
- 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 inline 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 inline 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 const 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,
- }
- );
- }
- 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(){
- 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline operator!=(const T & lhs, const U & rhs) {
- return ! (lhs == rhs);
- }
- /////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////
- // The following operators only make sense when applied to integet types
- /////////////////////////////////////////////////////////////////////////
- // 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_contents.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);
- }
- 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 const auto rl = return_interval.l;
- constexpr static const 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 inline operator<<(const T & t, const U & u){
- // INT13-CPP
- // C++ standards document N4618 & 5.8.2
- static_assert(
- boost::safe_numerics::Integer<T>::value,
- "shifted value must be an integer"
- );
- static_assert(
- boost::safe_numerics::Integer<U>::value,
- "bit shift count 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 inline 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_contents.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());
- }
- 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(){
- 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 inline operator>>(const T & t, const U & u){
- // INT13-CPP
- static_assert(
- boost::safe_numerics::Integer<T>::value,
- "shifted value must be an integer"
- );
- static_assert(
- boost::safe_numerics::Integer<U>::value,
- "bit shift count 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 inline 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>;
- 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 inline operator|(const T & t, const U & u){
- static_assert(
- boost::safe_numerics::Integer<T>::value,
- "bitwise or arguments must be an integers"
- );
- static_assert(
- boost::safe_numerics::Integer<U>::value,
- "bitwise or arguments must be an integers"
- );
- 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 inline 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>;
- 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 inline operator&(const T & t, const U & u){
- static_assert(
- boost::safe_numerics::Integer<T>::value,
- "bitwise and arguments must be an integers"
- );
- static_assert(
- boost::safe_numerics::Integer<U>::value,
- "bitwise and arguments must be an integers"
- );
- 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 inline 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>;
- 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 inline operator^(const T & t, const U & u){
- static_assert(
- boost::safe_numerics::Integer<T>::value,
- "bitwise xor arguments must be an integers"
- );
- static_assert(
- boost::safe_numerics::Integer<U>::value,
- "bitwise xor arguments must be an integers"
- );
- 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 inline 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
- >
- inline 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
- >
- inline 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{
- if(std::is_unsigned<T>::value){
- // reading a negative number into an unsigned variable cannot result in
- // a correct result. But, C++ reads the absolute value, multiplies
- // it by -1 and stores the resulting value. This is crazy - but there
- // it is! Oh, and it doesn't set the failbit. We fix this behavior here
- is >> std::ws;
- int x = is.peek();
- // if the input string starts with a '-', we know its an error
- if(x == '-'){
- // set fail bit
- is.setstate(std::ios_base::failbit);
- }
- }
- is >> m_t;
- if(is.fail()){
- boost::safe_numerics::dispatch<
- E,
- boost::safe_numerics::safe_numerics_error::domain_error
- >(
- "error in file input"
- );
- }
- else
- validated_cast(m_t);
- }
- }
- } // safe_numerics
- } // boost
- #endif // BOOST_NUMERIC_SAFE_BASE_OPERATIONS_HPP
|