Files
safe_numerics/include/safe_integer.hpp
Robert Ramey 7299770bdf miscellaaneous code clean up
added policies documentation
2015-06-02 22:42:39 -07:00

89 lines
2.1 KiB
C++

#ifndef BOOST_NUMERIC_SAFE_INTEGER_HPP
#define BOOST_NUMERIC_SAFE_INTEGER_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// 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 <boost/utility/enable_if.hpp>
#include <boost/mpl/or.hpp>
#include "safe_base_operations.hpp"
#include "policies.hpp"
#include "native.hpp"
#include "concept/numeric.hpp"
#include "boost/concept/assert.hpp"
typedef boost::numeric::policies<
boost::numeric::native,
boost::numeric::throw_exception
> default_policies;
namespace boost {
namespace numeric {
template<
class T,
class P = default_policies
>
struct safe : public safe_base<T, safe<T, P>, P>{
BOOST_CONCEPT_ASSERT((boost::numeric::Integer<T>));
BOOST_CONCEPT_ASSERT((boost::numeric::ExceptionPolicy<
typename get_exception_policy<P>::type
>));
typedef safe_base<T, safe<T, P>, P > base_type;
SAFE_NUMERIC_CONSTEXPR bool validate(const T & t) const {
return true;
}
SAFE_NUMERIC_CONSTEXPR bool validate(T && t) const {
return true;
}
SAFE_NUMERIC_CONSTEXPR safe() :
base_type()
{}
//SAFE_NUMERIC_CONSTEXPR safe(safe && s) :
// base_type()
//{}
template<class U>
SAFE_NUMERIC_CONSTEXPR safe(const U & u) :
base_type(u)
{}
};
} // numeric
} // boost
namespace std {
/////////////////////////////////////////////////////////////////
// numeric limits for safe<int> etc.
template<
class T,
class P
>
class numeric_limits<boost::numeric::safe<T, P> >
: public std::numeric_limits<T>
{
typedef boost::numeric::safe<T, P> SI;
public:
// these expressions are not SAFE_NUMERIC_CONSTEXPR until C++14 so re-implement them here
SAFE_NUMERIC_CONSTEXPR static SI min() noexcept { return std::numeric_limits<T>::min(); }
SAFE_NUMERIC_CONSTEXPR static SI max() noexcept { return std::numeric_limits<T>::max(); }
};
} // std
#endif // BOOST_NUMERIC_SAFE_INTEGER_HPP