From 07f052fb6e38320d633378361bf254252f5cbc87 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 18 Mar 2013 19:12:21 +0900 Subject: [PATCH] add SPROUT_ASSERT --- sprout/assert.hpp | 195 ++++++++++++++++++++ sprout/bit/reverse.hpp | 6 +- sprout/breed/expr.hpp | 3 +- sprout/current_function.hpp | 43 +++++ sprout/detail/char_conversion.hpp | 11 +- sprout/iterator/next.hpp | 8 +- sprout/math/bernoulli.hpp | 7 +- sprout/math/ceil.hpp | 2 +- sprout/math/factorial.hpp | 7 +- sprout/math/floor.hpp | 2 +- sprout/math/iceil.hpp | 4 +- sprout/math/ifloor.hpp | 4 +- sprout/math/iround.hpp | 4 +- sprout/math/itrunc.hpp | 4 +- sprout/math/round.hpp | 2 +- sprout/math/trunc.hpp | 2 +- sprout/optional/optional.hpp | 26 +-- sprout/random/bernoulli_distribution.hpp | 22 +-- sprout/random/binomial_distribution.hpp | 26 +-- sprout/random/detail/const_mod.hpp | 51 +++-- sprout/random/geometric_distribution.hpp | 22 +-- sprout/random/inversive_congruential.hpp | 27 +-- sprout/random/linear_congruential.hpp | 23 +-- sprout/random/normal_distribution.hpp | 26 +-- sprout/random/shuffle_order.hpp | 4 +- sprout/random/uniform_int_distribution.hpp | 21 +-- sprout/random/uniform_real_distribution.hpp | 56 ++---- sprout/random/uniform_smallint.hpp | 22 +-- sprout/range/algorithm/fit/transform.hpp | 6 +- sprout/rational/rational.hpp | 2 +- sprout/uuid/string_generator.hpp | 8 +- sprout/variant/variant.hpp | 24 +-- 32 files changed, 386 insertions(+), 284 deletions(-) create mode 100644 sprout/assert.hpp create mode 100644 sprout/current_function.hpp diff --git a/sprout/assert.hpp b/sprout/assert.hpp new file mode 100644 index 00000000..6a594af0 --- /dev/null +++ b/sprout/assert.hpp @@ -0,0 +1,195 @@ +#ifndef SPROUT_ASEERT_HPP +#define SPROUT_ASEERT_HPP + +#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG) +# include +#elif defined(SPROUT_ENABLE_ASSERT_HANDLER) +# include +# include +#else +# include +# include +# include +#endif +#if !(defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)) +# include +#endif + +#if !(defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)) + // + // SPROUT_ASSERTION_FAILED_FORMAT + // +# ifndef SPROUT_ASSERTION_FAILED_FORMAT +# define SPROUT_ASSERTION_FAILED_FORMAT(expr, file, line) \ + "***** Internal Program Error - assertion (" #expr ") failed: " file "(" SPROUT_PP_STRINGIZE(line) ")" +# endif +#endif + +// +// SPROUT_ASSERT +// + +#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG) + +namespace sprout { + namespace detail { + inline SPROUT_CONSTEXPR bool + assertion_disabled() SPROUT_NOEXCEPT { + return true; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_ASSERT(expr) \ + (sprout::detail::assertion_disabled()) + +#elif defined(SPROUT_ENABLE_ASSERT_HANDLER) + +namespace sprout_adl { + SPROUT_CONSTEXPR sprout::not_found_via_adl assertion_failed(...); +} // namespace sprout_adl + +namespace sprout_assertion_detail { + using sprout_adl::assertion_failed; + + //template + inline SPROUT_CONSTEXPR bool + call_assertion_failed(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) { + return cond ? true + : (assertion_failed(expr, function, file, line), false) + ; + } +} // namespace sprout + +namespace sprout { +// // +// // assertion_failed +// // * user defined +// // +// void +// assertion_failed(char const* expr, char const* function, char const* file, long line); + + namespace detail { + inline SPROUT_CONSTEXPR bool + assertion_check(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) { + return cond ? true + : sprout_assertion_detail::call_assertion_failed(cond, formatted, expr, function, file, line) + ; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_ASSERT(expr) ( \ + sprout::detail::assertion_check( \ + (expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \ + #expr, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__) \ + ) + +#else + +namespace sprout { + namespace detail { + inline bool + assertion_failed(char const* formatted) { + return (std::cerr << formatted << std::endl), std::abort(), false; + } + inline SPROUT_CONSTEXPR bool + assertion_check(bool cond, char const* formatted) { + return cond ? true + : sprout::detail::assertion_failed(formatted) + ; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_ASSERT(expr) \ + (sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__))) + +#endif + +// +// SPROUT_ASSERT_MSG +// + +#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG) + +# define SPROUT_ASSERT_MSG(expr, msg) \ + (sprout::detail::assertion_disabled()) + +#elif defined(SPROUT_ENABLE_ASSERT_HANDLER) + +namespace sprout { + // + // assertion_failed_msg + // * user defined + // + void + assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* file, long line); + + namespace detail { + inline bool + assertion_failed_msg(char const* formatted, char const* expr, char const* msg, char const* function, char const* file, long line) { + return sprout::assertion_failed_msg(expr, msg, function, file, line), false; + } + inline SPROUT_CONSTEXPR bool + assertion_check_msg(bool cond, char const* formatted, char const* expr, char const* msg, char const* function, char const* file, long line) { + return cond ? true + : sprout::detail::assertion_failed_msg(formatted, expr, msg, function, file, line) + ; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_ASSERT_MSG(expr, msg) ( \ + sprout::detail::assertion_check_msg( \ + (expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \ + #expr, msg, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__) \ + ) + +#else + +namespace sprout { + namespace detail { + inline bool + assertion_failed_msg(char const* formatted, char const* msg) { + return (std::cerr << formatted << ": " << msg << std::endl), std::abort(), false; + } + inline SPROUT_CONSTEXPR bool + assertion_check_msg(bool cond, char const* formatted, char const* msg) { + return cond ? true + : sprout::detail::assertion_failed_msg(formatted, msg) + ; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_ASSERT_MSG(expr, msg) \ + (sprout::detail::assertion_check_msg((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), msg)) + +#endif + +// +// SPROUT_VERIFY +// + +#if defined(SPROUT_DISABLE_ASSERTS) || (!defined(SPROUT_ENABLE_ASSERT_HANDLER) && defined(NDEBUG)) + +namespace sprout { + namespace detail { + inline SPROUT_CONSTEXPR bool + verification_disabled(bool) SPROUT_NOEXCEPT { + return true; + } + } // namespace detail +} // namespace sprout + +# define SPROUT_VERIFY(expr) \ + (sprout::detail::verification_disabled((expr))) + +#else + +# define SPROUT_VERIFY(expr) SPROUT_ASSERT(expr) + +#endif + +#endif // #ifndef SPROUT_ASEERT_HPP diff --git a/sprout/bit/reverse.hpp b/sprout/bit/reverse.hpp index b6b5887a..a1b4b93b 100644 --- a/sprout/bit/reverse.hpp +++ b/sprout/bit/reverse.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace sprout { namespace detail { @@ -72,12 +73,11 @@ namespace sprout { >::type bit_reverse_in(IntType x, std::size_t length) { typedef typename std::make_unsigned::type unsigned_type; - return length <= sizeof(IntType) * CHAR_BIT - ? static_cast( + return SPROUT_ASSERT(length <= sizeof(IntType) * CHAR_BIT), + static_cast( sprout::detail::bit_rev().template operator()(x) >> (sizeof(IntType) * CHAR_BIT - length) ) - : throw std::invalid_argument("invalid length") ; } } // namespace sprout diff --git a/sprout/breed/expr.hpp b/sprout/breed/expr.hpp index b387bf18..755b355f 100644 --- a/sprout/breed/expr.hpp +++ b/sprout/breed/expr.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace sprout { namespace breed { @@ -34,7 +35,7 @@ namespace sprout { } template SPROUT_CONSTEXPR Expr make_terminal(T const (&t)[N], Expr const*, sprout::breed::term const*) { - return throw "Sorry, not implemented.", Expr(); + return SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), Expr(); } template diff --git a/sprout/current_function.hpp b/sprout/current_function.hpp new file mode 100644 index 00000000..02f450fa --- /dev/null +++ b/sprout/current_function.hpp @@ -0,0 +1,43 @@ +#ifndef SPROUT_CURRENT_FUNCTION_HPP +#define SPROUT_CURRENT_FUNCTION_HPP + +#include + +namespace sprout { + namespace detail { + inline void + current_function_helper() { +#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__) + +# define SPROUT_CURRENT_FUNCTION __PRETTY_FUNCTION__ + +#elif defined(__DMC__) && (__DMC__ >= 0x810) + +# define SPROUT_CURRENT_FUNCTION __PRETTY_FUNCTION__ + +#elif defined(__FUNCSIG__) + +# define SPROUT_CURRENT_FUNCTION __FUNCSIG__ + +#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500)) + +# define SPROUT_CURRENT_FUNCTION __FUNCTION__ + +#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550) + +# define SPROUT_CURRENT_FUNCTION __FUNC__ + +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901) + +# define SPROUT_CURRENT_FUNCTION __func__ + +#else + +# define SPROUT_CURRENT_FUNCTION "(unknown)" + +#endif + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_CURRENT_FUNCTION_HPP diff --git a/sprout/detail/char_conversion.hpp b/sprout/detail/char_conversion.hpp index 3428de97..f855ced5 100644 --- a/sprout/detail/char_conversion.hpp +++ b/sprout/detail/char_conversion.hpp @@ -5,22 +5,23 @@ #include #include #include +#include namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR Elem int_to_char(IntType val, int base) { - return val >= 0 && val < 10 ? static_cast('0') + val - : val >= 10 && val < static_cast(base) ? static_cast('a') + (val - 10) - : throw std::invalid_argument("value out of bounds") + return SPROUT_ASSERT(2 <= base && base <= 36), SPROUT_ASSERT(IntType(0) <= val && val < static_cast(base)), + val < 10 ? static_cast('0') + val + : static_cast('a') + (val - 10) ; } template inline SPROUT_CONSTEXPR Elem int_to_char(IntType val) { - return val >= 0 && val < 10 ? static_cast('0') + val - : throw std::invalid_argument("value out of bounds") + return SPROUT_ASSERT(IntType(0) <= val && val < IntType(10)), + static_cast('0') + val ; } diff --git a/sprout/iterator/next.hpp b/sprout/iterator/next.hpp index e72ddc34..046f929d 100644 --- a/sprout/iterator/next.hpp +++ b/sprout/iterator/next.hpp @@ -3,11 +3,11 @@ #include #include -#include #include #include #include #include +#include namespace sprout_adl { sprout::not_found_via_adl iterator_next(...); @@ -82,9 +82,9 @@ namespace sprout { std::forward_iterator_tag* ) { - return n == 0 ? it - : n > 0 ? sprout::iterator_detail::next_impl_2(it, n) - : throw std::domain_error("next: nagative distance is invalid ForwardIterator") + return SPROUT_ASSERT(n >= 0), + n == 0 ? it + : sprout::iterator_detail::next_impl_2(it, n) ; } diff --git a/sprout/math/bernoulli.hpp b/sprout/math/bernoulli.hpp index 5481bf95..6dbfac89 100644 --- a/sprout/math/bernoulli.hpp +++ b/sprout/math/bernoulli.hpp @@ -2,10 +2,10 @@ #define SPROUT_MATH_BERNOULLI_HPP #include -#include #include #include #include +#include namespace sprout { namespace math { @@ -233,11 +233,10 @@ namespace sprout { template::value>::type> inline SPROUT_CONSTEXPR T bernoulli_number(std::size_t x) { typedef typename std::remove_cv::type type; - return x <= sprout::math::bernoulli_number_limit() - ? x == 1 ? type(-1) / 2 + return SPROUT_ASSERT(x <= sprout::math::bernoulli_number_limit()), + x == 1 ? type(-1) / 2 : x % 2 ? type(0) : sprout::math::detail::bernoulli_numbers::table[x / 2] - : throw std::invalid_argument("bernoulli_number(): argument limit exceeded") ; } } // namespace math diff --git a/sprout/math/ceil.hpp b/sprout/math/ceil.hpp index 2ee870c4..e853494f 100644 --- a/sprout/math/ceil.hpp +++ b/sprout/math/ceil.hpp @@ -30,7 +30,7 @@ namespace sprout { : x == std::numeric_limits::infinity() ? std::numeric_limits::infinity() : x == -std::numeric_limits::infinity() ? -std::numeric_limits::infinity() : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("ceil: large float rounding."), x) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("ceil: large float rounding."), x) : x < 0 ? -static_cast(static_cast(-x)) : sprout::math::detail::ceil_impl(x, static_cast(static_cast(x))) ; diff --git a/sprout/math/factorial.hpp b/sprout/math/factorial.hpp index f7f6857e..c26aaa68 100644 --- a/sprout/math/factorial.hpp +++ b/sprout/math/factorial.hpp @@ -2,12 +2,12 @@ #define SPROUT_MATH_FACTORIAL_HPP #include -#include #include #include #include #include #include +#include namespace sprout { namespace math { @@ -767,9 +767,8 @@ namespace sprout { template::value>::type> inline SPROUT_CONSTEXPR T factorial(std::size_t x) { typedef typename std::remove_cv::type type; - return x <= sprout::math::factorial_limit() - ? sprout::math::detail::factorials::table[x] - : throw std::invalid_argument("factorial(): argument limit exceeded") + return SPROUT_ASSERT(x <= sprout::math::factorial_limit()), + sprout::math::detail::factorials::table[x] ; } } // namespace math diff --git a/sprout/math/floor.hpp b/sprout/math/floor.hpp index 6f56ba14..684af967 100644 --- a/sprout/math/floor.hpp +++ b/sprout/math/floor.hpp @@ -30,7 +30,7 @@ namespace sprout { : x == std::numeric_limits::infinity() ? std::numeric_limits::infinity() : x == -std::numeric_limits::infinity() ? -std::numeric_limits::infinity() : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("floor: large float rounding."), x) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("floor: large float rounding."), x) : x < 0 ? sprout::math::detail::floor_impl(x, -static_cast(static_cast(-x))) : static_cast(static_cast(x)) ; diff --git a/sprout/math/iceil.hpp b/sprout/math/iceil.hpp index b0fa5d69..5e8f4f03 100644 --- a/sprout/math/iceil.hpp +++ b/sprout/math/iceil.hpp @@ -21,7 +21,7 @@ namespace sprout { inline SPROUT_CONSTEXPR To iceil_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("iceil: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iceil: large float rounding."), static_cast(x)) : static_cast(x) ; } @@ -51,7 +51,7 @@ namespace sprout { iceil(FloatType x) { return x == 0 ? To(0) : std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("iceil: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iceil: large float rounding."), static_cast(x)) : sprout::math::detail::iceil_impl(x, static_cast(x)) ; } diff --git a/sprout/math/ifloor.hpp b/sprout/math/ifloor.hpp index 45d6e0b3..a68ab205 100644 --- a/sprout/math/ifloor.hpp +++ b/sprout/math/ifloor.hpp @@ -21,7 +21,7 @@ namespace sprout { inline SPROUT_CONSTEXPR To ifloor_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("ifloor: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("ifloor: large float rounding."), static_cast(x)) : static_cast(x) ; } @@ -51,7 +51,7 @@ namespace sprout { ifloor(FloatType x) { return x == 0 ? To(0) : std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("ifloor: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("ifloor: large float rounding."), static_cast(x)) : sprout::math::detail::ifloor_impl(x, static_cast(x)) ; } diff --git a/sprout/math/iround.hpp b/sprout/math/iround.hpp index 1ae6fa9e..9190ddea 100644 --- a/sprout/math/iround.hpp +++ b/sprout/math/iround.hpp @@ -21,7 +21,7 @@ namespace sprout { inline SPROUT_CONSTEXPR To iround_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("iround: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float rounding."), static_cast(x)) : static_cast(x) ; } @@ -58,7 +58,7 @@ namespace sprout { iround(FloatType x) { return x == 0 ? To(0) : std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("iround: large float irounding."), x) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float irounding."), x) : x < 0 ? sprout::math::detail::iround_impl_nagative(x, static_cast(x)) : sprout::math::detail::iround_impl_positive(x, static_cast(x)) ; diff --git a/sprout/math/itrunc.hpp b/sprout/math/itrunc.hpp index 9f1e44fb..d0e0f6a4 100644 --- a/sprout/math/itrunc.hpp +++ b/sprout/math/itrunc.hpp @@ -19,7 +19,7 @@ namespace sprout { inline SPROUT_CONSTEXPR To itrunc_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("itrunc: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("itrunc: large float rounding."), static_cast(x)) : static_cast(x) ; } @@ -42,7 +42,7 @@ namespace sprout { itrunc(FloatType x) { return x == 0 ? To(0) : std::numeric_limits::max() < x || std::numeric_limits::min() > x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("itrunc: large float rounding."), static_cast(x)) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("itrunc: large float rounding."), static_cast(x)) : static_cast(x) ; } diff --git a/sprout/math/round.hpp b/sprout/math/round.hpp index ea539f19..689a1ec0 100644 --- a/sprout/math/round.hpp +++ b/sprout/math/round.hpp @@ -36,7 +36,7 @@ namespace sprout { : x == std::numeric_limits::infinity() ? std::numeric_limits::infinity() : x == -std::numeric_limits::infinity() ? -std::numeric_limits::infinity() : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("round: large float rounding."), x) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("round: large float rounding."), x) : x < 0 ? sprout::math::detail::round_impl_nagative(x, -static_cast(static_cast(-x))) : sprout::math::detail::round_impl_positive(x, static_cast(static_cast(x))) ; diff --git a/sprout/math/trunc.hpp b/sprout/math/trunc.hpp index 8cfce95c..41123696 100644 --- a/sprout/math/trunc.hpp +++ b/sprout/math/trunc.hpp @@ -22,7 +22,7 @@ namespace sprout { : x == std::numeric_limits::infinity() ? std::numeric_limits::infinity() : x == -std::numeric_limits::infinity() ? -std::numeric_limits::infinity() : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("trunc: large float rounding."), x) + ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("trunc: large float rounding."), x) : x < 0 ? -static_cast(static_cast(-x)) : static_cast(static_cast(x)) ; diff --git a/sprout/optional/optional.hpp b/sprout/optional/optional.hpp index 6665092c..bc60fd59 100644 --- a/sprout/optional/optional.hpp +++ b/sprout/optional/optional.hpp @@ -1,12 +1,12 @@ #ifndef SPROUT_OPTIONAL_OPTIONAL_HPP #define SPROUT_OPTIONAL_OPTIONAL_HPP -#include #include #include #include #include #include +#include namespace sprout { // @@ -120,13 +120,13 @@ namespace sprout { return get(); } SPROUT_CONSTEXPR reference_const_type get() const { - return is_initialized() ? val.get() - : (throw std::domain_error("optional: value not initialized"), val.get()) + return SPROUT_ASSERT(is_initialized()) ? val.get() + : val.get() ; } reference_type get() { - return is_initialized() ? val.get() - : (throw std::domain_error("optional: value not initialized"), val.get()) + return SPROUT_ASSERT(is_initialized()) ? val.get() + : val.get() ; } SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const { @@ -141,23 +141,23 @@ namespace sprout { } SPROUT_CONSTEXPR pointer_const_type operator->() const { - return is_initialized() ? val.get_pointer() - : throw std::domain_error("optional: value not initialized") + return SPROUT_ASSERT(is_initialized()), + val.get_pointer() ; } pointer_type operator->() { - return is_initialized() ? val.get_pointer() - : throw std::domain_error("optional: value not initialized") + return SPROUT_ASSERT(is_initialized()), + val.get_pointer() ; } SPROUT_CONSTEXPR pointer_const_type get_pointer() const { - return is_initialized() ? val.get_pointer() - : throw std::domain_error("optional: value not initialized") + return SPROUT_ASSERT(is_initialized()), + val.get_pointer() ; } pointer_type get_pointer() { - return is_initialized() ? val.get_pointer() - : throw std::domain_error("optional: value not initialized") + return SPROUT_ASSERT(is_initialized()), + val.get_pointer() ; } SPROUT_CONSTEXPR pointer_const_type get_ptr() const { diff --git a/sprout/random/bernoulli_distribution.hpp b/sprout/random/bernoulli_distribution.hpp index 308316b9..39e667d9 100644 --- a/sprout/random/bernoulli_distribution.hpp +++ b/sprout/random/bernoulli_distribution.hpp @@ -2,9 +2,9 @@ #define SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP #include -#include #include #include +#include namespace sprout { namespace random { @@ -16,16 +16,6 @@ namespace sprout { public: typedef int input_type; typedef bool result_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) { - return p_arg >= 0 && p_arg <= 1; - } - static SPROUT_CONSTEXPR RealType arg_check(RealType p_arg) { - return arg_check_nothrow(p_arg) - ? p_arg - : throw std::invalid_argument("bernoulli_distribution<>: invalid argument (p_arg >= 0 && p_arg <= 1)") - ; - } public: // // param_type @@ -33,10 +23,6 @@ namespace sprout { class param_type { public: typedef bernoulli_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) { - return distribution_type::arg_check_nothrow(p_arg); - } private: RealType p_; public: @@ -44,7 +30,7 @@ namespace sprout { : p_(RealType(0.5)) {} explicit SPROUT_CONSTEXPR param_type(RealType p_arg) - : p_(arg_check(p_arg)) + : p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg)) {} SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT { return p_; @@ -57,7 +43,7 @@ namespace sprout { { RealType p; if (lhs >> p) { - if (arg_check_nothrow(p)) { + if (p >= RealType(0) && p <= RealType(1)) { rhs.p_ = p; } else { lhs.setstate(std::ios_base::failbit); @@ -99,7 +85,7 @@ namespace sprout { : p_(RealType(0.5)) {} explicit SPROUT_CONSTEXPR bernoulli_distribution(RealType p_arg) - : p_(arg_check(p_arg)) + : p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg)) {} explicit SPROUT_CONSTEXPR bernoulli_distribution(param_type const& parm) : p_(parm.p()) diff --git a/sprout/random/binomial_distribution.hpp b/sprout/random/binomial_distribution.hpp index 33129b7c..567c6441 100644 --- a/sprout/random/binomial_distribution.hpp +++ b/sprout/random/binomial_distribution.hpp @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -14,6 +13,7 @@ #include #include #include +#include #ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE # include #endif @@ -69,16 +69,6 @@ namespace sprout { RealType v_r; RealType u_rv_r; }; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType t_arg, RealType p_arg) { - return t_arg >= IntType(0) && RealType(0) <= p_arg && p_arg <= RealType(1); - } - static SPROUT_CONSTEXPR IntType arg_check(IntType t_arg, RealType p_arg) { - return arg_check_nothrow(t_arg, p_arg) - ? t_arg - : throw std::invalid_argument("binomial_distribution<>: invalid argument (t_arg >= 0 && 0 <= p_arg && p_arg <= 1)") - ; - } public: // // param_type @@ -86,10 +76,6 @@ namespace sprout { class param_type { public: typedef binomial_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType t_arg, RealType p_arg) { - return distribution_type::arg_check_nothrow(t_arg, p_arg); - } private: IntType t_; RealType p_; @@ -99,8 +85,8 @@ namespace sprout { , p_(0.5) {} explicit SPROUT_CONSTEXPR param_type(IntType t_arg, RealType p_arg = RealType(0.5)) - : t_(arg_check(t_arg, p_arg)) - , p_(p_arg) + : t_((SPROUT_ASSERT(t_arg >= IntType(0)), t_arg)) + , p_((SPROUT_ASSERT(RealType(0) <= p_arg && p_arg <= RealType(1)), p_arg)) {} SPROUT_CONSTEXPR IntType t() const SPROUT_NOEXCEPT { return t_; @@ -117,7 +103,7 @@ namespace sprout { IntType t; RealType p; if (lhs >> t >> std::ws >> p) { - if (arg_check_nothrow(t, p)) { + if (t >= IntType(0) && RealType(0) <= p && p <= RealType(1)) { rhs.t_ = t; rhs.p_ = p; } else { @@ -622,8 +608,8 @@ namespace sprout { , q_n_(init_use_inversion(1, RealType(0.5)) ? init_q_n(1, RealType(0.5)) : RealType()) {} explicit SPROUT_CONSTEXPR binomial_distribution(IntType t_arg, RealType p_arg = RealType(0.5)) - : t_(arg_check(t_arg, p_arg)) - , p_(p_arg) + : t_((SPROUT_ASSERT(t_arg >= IntType(0)), t_arg)) + , p_((SPROUT_ASSERT(RealType(0) <= p_arg && p_arg <= RealType(1)), p_arg)) , m_(init_m(t_arg, p_arg)) , btrd_(!init_use_inversion(t_arg, p_arg) ? init_btrd(t_arg, p_arg) : btrd_type()) , q_n_(init_use_inversion(t_arg, p_arg) ? init_q_n(t_arg, p_arg) : RealType()) diff --git a/sprout/random/detail/const_mod.hpp b/sprout/random/detail/const_mod.hpp index 7d80f032..00132241 100644 --- a/sprout/random/detail/const_mod.hpp +++ b/sprout/random/detail/const_mod.hpp @@ -2,9 +2,9 @@ #define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP #include -#include #include #include +#include #ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE # include #endif @@ -17,10 +17,10 @@ namespace sprout { private: typedef typename std::make_unsigned::type unsigned_type; private: - SPROUT_STATIC_CONSTEXPR IntType suppress_warnings = m == 0; - SPROUT_STATIC_CONSTEXPR IntType modulus = m + suppress_warnings; + SPROUT_STATIC_CONSTEXPR IntType supress_warnings = m == 0; + SPROUT_STATIC_CONSTEXPR IntType modulus = m + supress_warnings; private: - static_assert(suppress_warnings == 0, "suppress_warnings == 0"); + static_assert(supress_warnings == 0, "supress_warnings == 0"); static_assert(modulus == m, "modulus == m"); private: static SPROUT_CONSTEXPR IntType pow_1(IntType a, std::uintmax_t exponent, IntType result = 1) { @@ -30,12 +30,11 @@ namespace sprout { ; } static SPROUT_CONSTEXPR IntType mult_small(IntType a, IntType x) { - return a * x % (m + suppress_warnings); + return a * x % (m + supress_warnings); } static SPROUT_CONSTEXPR IntType mult_schrage_1(IntType a, IntType value, IntType q, IntType r) { - return r < q - ? sub(a * (value % q), r * (value / q)) - : throw std::domain_error("const_mod<>: domain error (r < q)") + return SPROUT_ASSERT(r < q), + sub(a * (value % q), r * (value / q)) ; } static SPROUT_CONSTEXPR IntType mult_schrage(IntType a, IntType value) { @@ -45,7 +44,7 @@ namespace sprout { return std::uintmax_t(modulus) <= std::numeric_limits::max() / modulus ? static_cast(std::uintmax_t(a) * b % modulus) //: static_cast(sprout::random::detail::mulmod(a, b, modulus)) // ??? - : throw std::domain_error("const_mod<>: Sorry, not implemented.") + : (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), IntType()) ; } static SPROUT_CONSTEXPR IntType sub(IntType a, IntType b) { @@ -81,9 +80,8 @@ namespace sprout { } template static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) { - return c > 0 - ? c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m) - : throw std::domain_error("const_mod<>: domain error (c > 0)") + return SPROUT_ASSERT(c > 0), + c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m) ; } template @@ -108,12 +106,11 @@ namespace sprout { } template static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) { - return std::numeric_limits::max() % n != n - 1 - ? invert_euclidian0_2( + return SPROUT_ASSERT_MSG(std::numeric_limits::max() % n != n - 1, "c must be relatively prime to m."), + invert_euclidian0_2( c, l1 + (std::numeric_limits::max() / n) * l2, l2, n, std::numeric_limits::max() - (std::numeric_limits::max() / n) * n + 1 ) - : throw std::domain_error("const_mod<>: domain error (numeric_limits::max() % n != n - 1)") ; } template @@ -122,9 +119,8 @@ namespace sprout { } template static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) { - return c > 0 - ? c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m) - : throw std::domain_error("const_mod<>: domain error (c > 0)") + return SPROUT_ASSERT(c > 0), + c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m) ; } template @@ -142,9 +138,8 @@ namespace sprout { return invert_euclidian_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n); } static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) { - return c > 0 - ? c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m) - : throw std::domain_error("const_mod<>: domain error (c > 0)") + return SPROUT_ASSERT(c > 0), + c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m) ; } static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) { @@ -154,18 +149,16 @@ namespace sprout { return p == 0 ? l2 : invert_euclidian0_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p); } static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) { - return std::numeric_limits::max() % n != n - 1 - ? invert_euclidian0_2( + return SPROUT_ASSERT_MSG(std::numeric_limits::max() % n != n - 1, "c must be relatively prime to m."), + invert_euclidian0_2( c, l1 + (std::numeric_limits::max() / n) * l2, l2, n, std::numeric_limits::max() - (std::numeric_limits::max() / n) * n + 1 ) - : throw std::domain_error("const_mod<>: domain error (numeric_limits::max() % n != n - 1)") ; } static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) { - return c > 0 - ? c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m) - : throw std::domain_error("const_mod<>: domain error (c > 0)") + return SPROUT_ASSERT(c > 0), + c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m) ; } #endif @@ -173,7 +166,7 @@ namespace sprout { static SPROUT_CONSTEXPR IntType apply(IntType x) { return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(x)) & (unsigned_m() - 1) - : x % (m + suppress_warnings) + : x % (m + supress_warnings) ; } static SPROUT_CONSTEXPR IntType add(IntType x, IntType c) { @@ -195,7 +188,7 @@ namespace sprout { static SPROUT_CONSTEXPR IntType mult_add(IntType a, IntType x, IntType c) { return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1) : a == 0 ? c - : m <= (std::numeric_limits::max() - c) / a ? (a * x + c) % (m + suppress_warnings) + : m <= (std::numeric_limits::max() - c) / a ? (a * x + c) % (m + supress_warnings) : add(mult(a, x), c) ; } diff --git a/sprout/random/geometric_distribution.hpp b/sprout/random/geometric_distribution.hpp index 48824623..088314cb 100644 --- a/sprout/random/geometric_distribution.hpp +++ b/sprout/random/geometric_distribution.hpp @@ -3,12 +3,12 @@ #include #include -#include #include #include #include #include #include +#include namespace sprout { namespace random { @@ -20,16 +20,6 @@ namespace sprout { public: typedef RealType input_type; typedef IntType result_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) { - return RealType(0) < p_arg && p_arg < RealType(1); - } - static SPROUT_CONSTEXPR RealType arg_check(RealType p_arg) { - return arg_check_nothrow(p_arg) - ? p_arg - : throw std::invalid_argument("geometric_distribution<>: invalid argument (0 < p_arg && p_arg < 1)") - ; - } public: // // param_type @@ -37,10 +27,6 @@ namespace sprout { class param_type { public: typedef geometric_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) { - return distribution_type::arg_check_nothrow(p_arg); - } private: RealType p_; public: @@ -48,7 +34,7 @@ namespace sprout { : p_(RealType(0.5)) {} explicit SPROUT_CONSTEXPR param_type(RealType p_arg) - : p_(arg_check(p_arg)) + : p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg)) {} SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT { return p_; @@ -61,7 +47,7 @@ namespace sprout { { RealType p; if (lhs >> p) { - if (arg_check_nothrow(p)) { + if (RealType(0) < p && p < RealType(1)) { rhs.p_ = p; } else { lhs.setstate(std::ios_base::failbit); @@ -112,7 +98,7 @@ namespace sprout { , log_1mp_(init_log_1mp(RealType(0.5))) {} explicit SPROUT_CONSTEXPR geometric_distribution(RealType p_arg) - : p_(arg_check(p_arg)) + : p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg)) , log_1mp_(init_log_1mp(p_arg)) {} explicit SPROUT_CONSTEXPR geometric_distribution(param_type const& parm) diff --git a/sprout/random/inversive_congruential.hpp b/sprout/random/inversive_congruential.hpp index 567a945b..bce277cf 100644 --- a/sprout/random/inversive_congruential.hpp +++ b/sprout/random/inversive_congruential.hpp @@ -4,11 +4,12 @@ #include #include #include -#include #include #include #include #include +#include +#include namespace sprout { namespace random { @@ -33,27 +34,11 @@ namespace sprout { static SPROUT_CONSTEXPR result_type static_max() SPROUT_NOEXCEPT { return modulus - 1; } - private: - template - static SPROUT_CONSTEXPR typename std::enable_if::value, bool>::type - arg_check_nothrow(T const& x0) { - return x0 <= static_max(); - } - template - static SPROUT_CONSTEXPR typename std::enable_if::value), bool>::type - arg_check_nothrow(T const& x0) { - return x0 >= static_min() && x0 <= static_max(); - } - static SPROUT_CONSTEXPR IntType arg_check(IntType const& x0) { - return arg_check_nothrow(x0) - ? x0 - : throw std::invalid_argument( - "inversive_congruential_engine<>: invalid argument (x0 >= static_min() && x0 <= static_max())" - ) - ; + static SPROUT_CONSTEXPR IntType init_seed_3(IntType const& x0) { + return SPROUT_ASSERT(sprout::math::greater_equal(x0, static_min())), SPROUT_ASSERT(x0 <= static_max()), x0; } static SPROUT_CONSTEXPR IntType init_seed_2(IntType const& x0) { - return arg_check(increment == 0 && x0 == 0 ? 1 : x0); + return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0); } static SPROUT_CONSTEXPR IntType init_seed_1(IntType const& x0) { return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0); @@ -105,7 +90,7 @@ namespace sprout { { IntType x; if (lhs >> x) { - if(arg_check_nothrow(x)) { + if (sprout::math::greater_equal(x, static_min()) && x <= static_max()) { rhs.x_ = x; } else { lhs.setstate(std::ios_base::failbit); diff --git a/sprout/random/linear_congruential.hpp b/sprout/random/linear_congruential.hpp index 5ddd76e5..d35d8585 100644 --- a/sprout/random/linear_congruential.hpp +++ b/sprout/random/linear_congruential.hpp @@ -4,11 +4,12 @@ #include #include #include -#include #include #include #include #include +#include +#include namespace sprout { namespace random { @@ -37,23 +38,11 @@ namespace sprout { return m - 1; } private: - template - static SPROUT_CONSTEXPR typename std::enable_if::value, bool>::type - arg_check_nothrow(T const& x0) { - return x0 <= static_max(); - } - template - static SPROUT_CONSTEXPR typename std::enable_if::value), bool>::type - arg_check_nothrow(T const& x0) { - return x0 >= static_min() && x0 <= static_max(); - } - static SPROUT_CONSTEXPR UIntType arg_check(UIntType const& x0) { - return arg_check_nothrow(x0) ? x0 - : throw std::invalid_argument("linear_congruential_engine<>: invalid argument (x0 >= static_min() && x0 <= static_max())") - ; + static SPROUT_CONSTEXPR UIntType init_seed_3(UIntType const& x0) { + return SPROUT_ASSERT(sprout::math::greater_equal(x0, static_min())), SPROUT_ASSERT(x0 <= static_max()), x0; } static SPROUT_CONSTEXPR UIntType init_seed_2(UIntType const& x0) { - return arg_check(increment == 0 && x0 == 0 ? 1 : x0); + return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0); } static SPROUT_CONSTEXPR UIntType init_seed_1(UIntType const& x0) { return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0); @@ -103,7 +92,7 @@ namespace sprout { { UIntType x; if (lhs >> x) { - if(arg_check_nothrow(x)) { + if (sprout::math::greater_equal(x, static_min()) && x <= static_max()) { rhs.x_ = x; } else { lhs.setstate(std::ios_base::failbit); diff --git a/sprout/random/normal_distribution.hpp b/sprout/random/normal_distribution.hpp index 3f95fe9e..ce0db648 100644 --- a/sprout/random/normal_distribution.hpp +++ b/sprout/random/normal_distribution.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -13,6 +12,7 @@ #include #include #include +#include namespace sprout { namespace random { @@ -26,16 +26,6 @@ namespace sprout { typedef RealType result_type; private: struct private_constructor_tag {}; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType mean_arg, RealType sigma_arg) { - return sigma_arg >= RealType(0); - } - static SPROUT_CONSTEXPR RealType arg_check(RealType mean_arg, RealType sigma_arg) { - return arg_check_nothrow(mean_arg, sigma_arg) - ? mean_arg - : throw std::invalid_argument("normal_distribution<>: invalid argument (sigma_arg >= 0)") - ; - } public: // // param_type @@ -43,10 +33,6 @@ namespace sprout { class param_type { public: typedef normal_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType mean_arg, RealType sigma_arg) { - return distribution_type::arg_check_nothrow(mean_arg, sigma_arg); - } private: RealType mean_; RealType sigma_; @@ -56,8 +42,8 @@ namespace sprout { , sigma_(RealType(1.0)) {} explicit SPROUT_CONSTEXPR param_type(RealType mean_arg, RealType sigma_arg = RealType(1.0)) - : mean_(arg_check(mean_arg, sigma_arg)) - , sigma_(sigma_arg) + : mean_(mean_arg) + , sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg)) {} SPROUT_CONSTEXPR RealType mean() const SPROUT_NOEXCEPT { return mean_; @@ -77,7 +63,7 @@ namespace sprout { RealType mean; RealType sigma; if (lhs >> mean >> std::ws >> sigma) { - if (arg_check_nothrow(mean, sigma)) { + if (sigma >= RealType(0)) { rhs.mean_ = mean; rhs.sigma_ = sigma; } else { @@ -174,8 +160,8 @@ namespace sprout { , valid_(false) {} explicit SPROUT_CONSTEXPR normal_distribution(RealType mean_arg, RealType sigma_arg = RealType(1.0)) - : mean_(arg_check(mean_arg, sigma_arg)) - , sigma_(sigma_arg) + : mean_(mean_arg) + , sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg)) , r1_(0) , r2_(0) , cached_rho_(0) diff --git a/sprout/random/shuffle_order.hpp b/sprout/random/shuffle_order.hpp index 0b7495cb..9e6a364d 100644 --- a/sprout/random/shuffle_order.hpp +++ b/sprout/random/shuffle_order.hpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -15,6 +14,7 @@ #include #include #include +#include namespace sprout { namespace random { @@ -117,7 +117,7 @@ namespace sprout { : sprout::math::less(brange, std::numeric_limits::max() / k) ? static_cast(static_cast(off) * k / (static_cast(brange) + 1)) //: static_cast(sprout::random::detail::muldiv(off, k, static_cast(brange) + 1)) // ??? - : throw std::domain_error("shuffle_order_engine<>: Sorry, not implemented.") + : (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), sprout::random::random_result()) ); } public: diff --git a/sprout/random/uniform_int_distribution.hpp b/sprout/random/uniform_int_distribution.hpp index 55fa5b3a..59b200d6 100644 --- a/sprout/random/uniform_int_distribution.hpp +++ b/sprout/random/uniform_int_distribution.hpp @@ -4,12 +4,12 @@ #include #include #include -#include #include #include #include #include #include +#include namespace sprout { namespace random { @@ -288,15 +288,6 @@ namespace sprout { public: typedef IntType input_type; typedef IntType result_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) { - return min_arg <= max_arg; - } - static SPROUT_CONSTEXPR IntType arg_check(IntType min_arg, IntType max_arg) { - return arg_check_nothrow(min_arg, max_arg) ? min_arg - : throw std::invalid_argument("uniform_int_distribution<>: invalid argument (min_arg <= max_arg)") - ; - } public: // // param_type @@ -304,10 +295,6 @@ namespace sprout { class param_type { public: typedef uniform_int_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) { - return distribution_type::arg_check_nothrow(min_arg, max_arg); - } private: IntType min_; IntType max_; @@ -317,7 +304,7 @@ namespace sprout { , max_(9) {} explicit SPROUT_CONSTEXPR param_type(IntType min_arg, IntType max_arg = 9) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} SPROUT_CONSTEXPR IntType a() const SPROUT_NOEXCEPT { @@ -335,7 +322,7 @@ namespace sprout { IntType min; IntType max; if (lhs >> min >> std::ws >> max) { - if (arg_check_nothrow(min, max)) { + if (min <= max) { rhs.min_ = min; rhs.max_ = max; } else { @@ -377,7 +364,7 @@ namespace sprout { , max_(9) {} explicit SPROUT_CONSTEXPR uniform_int_distribution(IntType min_arg, IntType max_arg = 9) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} explicit SPROUT_CONSTEXPR uniform_int_distribution(param_type const& parm) diff --git a/sprout/random/uniform_real_distribution.hpp b/sprout/random/uniform_real_distribution.hpp index 48f9bb61..819dc1ee 100644 --- a/sprout/random/uniform_real_distribution.hpp +++ b/sprout/random/uniform_real_distribution.hpp @@ -3,11 +3,11 @@ #include #include -#include #include #include #include #include +#include namespace sprout { namespace random { @@ -47,16 +47,13 @@ namespace sprout { T divisor ) { - return divisor > 0 - ? numerator >= 0 && numerator <= divisor - ? sprout::random::detail::generate_uniform_real_false_3( - eng, - min_value, - max_value, - numerator / divisor * (max_value - min_value) + min_value - ) - : throw std::domain_error("generate_uniform_real(): domain error (numerator >= 0 && numerator <= divisor)") - : throw std::domain_error("generate_uniform_real(): domain error (divisor > 0)") + return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor), + sprout::random::detail::generate_uniform_real_false_3( + eng, + min_value, + max_value, + numerator / divisor * (max_value - min_value) + min_value + ) ; } template @@ -116,16 +113,13 @@ namespace sprout { T divisor ) { - return divisor > 0 - ? numerator >= 0 && numerator <= divisor - ? sprout::random::detail::generate_uniform_real_true_3( - eng, - min_value, - max_value, - numerator / divisor * (max_value - min_value) + min_value - ) - : throw std::domain_error("generate_uniform_real(): domain error (numerator >= 0 && numerator <= divisor)") - : throw std::domain_error("generate_uniform_real(): domain error (divisor > 0)") + return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor), + sprout::random::detail::generate_uniform_real_true_3( + eng, + min_value, + max_value, + numerator / divisor * (max_value - min_value) + min_value + ) ; } template @@ -182,16 +176,6 @@ namespace sprout { public: typedef RealType input_type; typedef RealType result_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType min_arg, RealType max_arg) { - return min_arg <= max_arg; - } - static SPROUT_CONSTEXPR RealType arg_check(RealType min_arg, RealType max_arg) { - return arg_check_nothrow(min_arg, max_arg) - ? min_arg - : throw std::invalid_argument("uniform_real_distribution<>: invalid argument (min_arg <= max_arg)") - ; - } public: // // param_type @@ -199,10 +183,6 @@ namespace sprout { class param_type { public: typedef uniform_real_distribution distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType min_arg, RealType max_arg) { - return distribution_type::arg_check_nothrow(min_arg, max_arg); - } private: RealType min_; RealType max_; @@ -212,7 +192,7 @@ namespace sprout { , max_(RealType(1.0)) {} explicit SPROUT_CONSTEXPR param_type(RealType min_arg, RealType max_arg = RealType(1.0)) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} SPROUT_CONSTEXPR RealType a() const SPROUT_NOEXCEPT { @@ -230,7 +210,7 @@ namespace sprout { RealType min; RealType max; if (lhs >> min >> std::ws >> max) { - if (arg_check_nothrow(min, max)) { + if (min <= max) { rhs.min_ = min; rhs.max_ = max; } else { @@ -272,7 +252,7 @@ namespace sprout { , max_(RealType(1.0)) {} explicit SPROUT_CONSTEXPR uniform_real_distribution(RealType min_arg, RealType max_arg = RealType(1.0)) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} explicit SPROUT_CONSTEXPR uniform_real_distribution(param_type const& parm) diff --git a/sprout/random/uniform_smallint.hpp b/sprout/random/uniform_smallint.hpp index 049bdbdf..30af0116 100644 --- a/sprout/random/uniform_smallint.hpp +++ b/sprout/random/uniform_smallint.hpp @@ -3,12 +3,12 @@ #include #include -#include #include #include #include #include #include +#include namespace sprout { namespace random { @@ -20,16 +20,6 @@ namespace sprout { public: typedef IntType input_type; typedef IntType result_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) { - return min_arg <= max_arg; - } - static SPROUT_CONSTEXPR IntType arg_check(IntType min_arg, IntType max_arg) { - return arg_check_nothrow(min_arg, max_arg) - ? min_arg - : throw std::invalid_argument("uniform_smallint<>: invalid argument (min_arg <= max_arg)") - ; - } public: // // param_type @@ -37,10 +27,6 @@ namespace sprout { class param_type { public: typedef uniform_smallint distribution_type; - private: - static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) { - return distribution_type::arg_check_nothrow(min_arg, max_arg); - } private: IntType min_; IntType max_; @@ -50,7 +36,7 @@ namespace sprout { , max_(9) {} explicit SPROUT_CONSTEXPR param_type(IntType min_arg, IntType max_arg = 9) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} SPROUT_CONSTEXPR IntType a() const SPROUT_NOEXCEPT { @@ -68,7 +54,7 @@ namespace sprout { IntType min; IntType max; if (lhs >> min >> std::ws >> max) { - if (arg_check_nothrow(min, max)) { + if (min <= max) { rhs.min_ = min; rhs.max_ = max; } else { @@ -207,7 +193,7 @@ namespace sprout { , max_(9) {} explicit SPROUT_CONSTEXPR uniform_smallint(IntType min_arg, IntType max_arg = 9) - : min_(arg_check(min_arg, max_arg)) + : min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg)) , max_(max_arg) {} explicit SPROUT_CONSTEXPR uniform_smallint(param_type const& parm) diff --git a/sprout/range/algorithm/fit/transform.hpp b/sprout/range/algorithm/fit/transform.hpp index bcd738ba..f64fd975 100644 --- a/sprout/range/algorithm/fit/transform.hpp +++ b/sprout/range/algorithm/fit/transform.hpp @@ -18,10 +18,10 @@ namespace sprout { transform(InputRange const& rng, Result const& result, UnaryOperation op) { return sprout::fit::transform(sprout::begin(rng), sprout::end(rng), result, op); } - template + template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - transform(Input1 const& input1, Input2 const& input2, Result const& result, BinaryOperation op) { - return sprout::fit::transform(sprout::begin(input1), sprout::end(input1), sprout::begin(input2), result, op); + transform(InputRange1 const& rng1, InputRange2 const& rng2, Result const& result, BinaryOperation op) { + return sprout::fit::transform(sprout::begin(rng1), sprout::end(rng1), sprout::begin(rng2), result, op); } } // namespace fit } // namespace range diff --git a/sprout/rational/rational.hpp b/sprout/rational/rational.hpp index fb431504..b426d0b0 100644 --- a/sprout/rational/rational.hpp +++ b/sprout/rational/rational.hpp @@ -137,7 +137,7 @@ namespace sprout { } rational& operator/=(rational const& rhs) { if (rhs.num_ == IntType(0)) { - throw bad_rational(); + throw sprout::bad_rational(); } if (num_ == IntType(0)) { return *this; diff --git a/sprout/uuid/string_generator.hpp b/sprout/uuid/string_generator.hpp index 36325a11..bee1f33c 100644 --- a/sprout/uuid/string_generator.hpp +++ b/sprout/uuid/string_generator.hpp @@ -33,7 +33,7 @@ namespace sprout { SPROUT_CONSTEXPR next_char(Iterator f, Iterator l) : c(f != l ? *f - : throw std::domain_error("string_generator: invalid uuid string (out of range)") + : throw std::runtime_error("string_generator: invalid uuid string (out of range)") ) , first(sprout::next(f)) , last(l) @@ -46,7 +46,7 @@ namespace sprout { SPROUT_CONSTEXPR std::uint8_t value_at(std::size_t i) const { return i < 22 ? sprout::uuids::detail::values::table[i] - : throw std::domain_error("string_generator: invalid uuid string (invalid character)") + : throw std::runtime_error("string_generator: invalid uuid string (invalid character)") ; } template @@ -117,7 +117,7 @@ namespace sprout { return has_dashes ? is_dash(nc.c) ? generate_2_2(nc.next(), open_brace, has_dashes, args...) - : throw std::domain_error("string_generator: invalid uuid string (dashes not found)") + : throw std::runtime_error("string_generator: invalid uuid string (dashes not found)") : generate_2_2(nc, open_brace, has_dashes, args...) ; } @@ -148,7 +148,7 @@ namespace sprout { generate_2(next_char nc, Char open_brace, bool has_dashes, Args... args) const { return !open_brace || (open_brace && is_close_brace(nc.next().c, open_brace)) ? result_type{{args...}} - : throw std::domain_error("string_generator: invalid uuid string (brace not closed)") + : throw std::runtime_error("string_generator: invalid uuid string (brace not closed)") ; } template diff --git a/sprout/variant/variant.hpp b/sprout/variant/variant.hpp index aae6f2c1..1f31b735 100644 --- a/sprout/variant/variant.hpp +++ b/sprout/variant/variant.hpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace sprout { namespace detail { @@ -201,10 +202,7 @@ namespace sprout { I == sizeof...(Types) - 1, Result >::type visit(Tuple&& t, Visitor&& v, int which) { - return I == which - ? sprout::forward(v)(sprout::tuples::get(sprout::forward(t))) - : throw std::domain_error("variant<>: bad visit") - ; + return sprout::forward(v)(sprout::tuples::get(sprout::forward(t))); } template static SPROUT_CONSTEXPR typename std::enable_if< @@ -293,9 +291,8 @@ namespace sprout { I != sizeof...(Types), typename sprout::tuples::tuple_element::type const& >::type get_at() const { - return I == static_cast(which_) - ? sprout::tuples::get(tuple_) - : (throw std::domain_error("variant<>: bad get"), sprout::tuples::get(tuple_)) + return SPROUT_ASSERT(I == which_) ? sprout::tuples::get(tuple_) + : sprout::tuples::get(tuple_) ; } template @@ -303,9 +300,8 @@ namespace sprout { I != sizeof...(Types), typename sprout::tuples::tuple_element::type& >::type get_at() { - return I == which_ - ? sprout::tuples::get(tuple_) - : (throw std::domain_error("variant<>: bad get"), sprout::tuples::get(tuple_)) + return SPROUT_ASSERT(I == which_) ? sprout::tuples::get(tuple_) + : sprout::tuples::get(tuple_) ; } template @@ -327,13 +323,17 @@ namespace sprout { SPROUT_CONSTEXPR typename visitor_result::type, variant const>::type apply_visitor(Visitor&& visitor) const { typedef typename visitor_result::type, variant const>::type result_type; - return visit(tuple_, sprout::forward(visitor), which_); + return SPROUT_ASSERT(0 <= which_ && sprout::math::less(which_, sizeof...(Types))), + visit(tuple_, sprout::forward(visitor), which_) + ; } template typename visitor_result::type, variant>::type apply_visitor(Visitor&& visitor) { typedef typename visitor_result::type, variant>::type result_type; - return visit(tuple_, sprout::forward(visitor), which_); + return SPROUT_ASSERT(0 <= which_ && sprout::math::less(which_, sizeof...(Types))), + visit(tuple_, sprout::forward(visitor), which_) + ; } };