From 993cdcb42e9520f1998aa122af545bc9a1de6596 Mon Sep 17 00:00:00 2001 From: jzmaddock Date: Mon, 9 May 2016 11:38:13 +0100 Subject: [PATCH] Reduce dependencies by removing all use of Boost.Format. We made only cursory use of the lib, so it's just as easy to do with it. Also fixed a number of missing #includes which were hidden by including format.hpp. --- example/policy_eg_9.cpp | 1 + .../boost/math/policies/error_handling.hpp | 106 ++++++++---------- .../detail/bernoulli_details.hpp | 1 + 3 files changed, 49 insertions(+), 59 deletions(-) diff --git a/example/policy_eg_9.cpp b/example/policy_eg_9.cpp index d5335caba..c18952205 100644 --- a/example/policy_eg_9.cpp +++ b/example/policy_eg_9.cpp @@ -8,6 +8,7 @@ // and comments, don't change any of the special comment mark-ups! #include +#include using std::cout; using std::endl; using std::cerr; //[policy_eg_9 diff --git a/include/boost/math/policies/error_handling.hpp b/include/boost/math/policies/error_handling.hpp index 0de4a4df0..1ad0d5ed2 100644 --- a/include/boost/math/policies/error_handling.hpp +++ b/include/boost/math/policies/error_handling.hpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -18,6 +20,7 @@ #include #include #include +#include #include #ifdef BOOST_MSVC # pragma warning(push) // Quiet warnings in boost/format.hpp @@ -28,7 +31,7 @@ // Note that this only occurs when the compiler can deduce code is unreachable, // for example when policy macros are used to ignore errors rather than throw. #endif -#include +#include namespace boost{ namespace math{ @@ -68,29 +71,31 @@ T user_indeterminate_result_error(const char* function, const char* message, con namespace detail { -// -// Helper function to avoid binding rvalue to non-const-reference, -// in other words a warning suppression mechanism: -// -template -inline std::string do_format(Formatter& f, const Group& g) -{ - return (f % g).str(); -} template -BOOST_DEDUCED_TYPENAME disable_if_c::is_specialized, std::string >::type -prec_format(boost::format& fmt, const T& val) +std::string prec_format(const T& val) { - return (fmt % val).str(); + typedef typename boost::math::policies::precision >::type prec_type; + std::stringstream ss; + if(prec_type::value) + { + int prec = 2 + (prec_type::value * 30103UL) / 100000UL; + ss << std::setprecision(prec); + } + ss << val; + return ss.str(); } -template -BOOST_DEDUCED_TYPENAME enable_if_c::is_specialized, std::string >::type -prec_format(boost::format& fmt, const T& val) +inline void replace_all_in_string(std::string& result, const char* what, const char* with) { - int prec = 2 + (boost::math::policies::digits >() * 30103UL) / 100000UL; - return do_format(fmt, boost::io::group(std::setprecision(prec), val)); + std::string::size_type pos = 0; + std::string::size_type slen = std::strlen(what); + std::string::size_type rlen = std::strlen(with); + while((pos = result.find(what, pos)) != std::string::npos) + { + result.replace(pos, slen, with); + pos += rlen; + } } template @@ -115,23 +120,21 @@ inline const char* name_of() #endif template -void raise_error(const char* function, const char* message) +void raise_error(const char* pfunction, const char* message) { - if(function == 0) - function = "Unknown function operating on type %1%"; + if(pfunction == 0) + pfunction = "Unknown function operating on type %1%"; if(message == 0) message = "Cause unknown"; + std::string function(pfunction); std::string msg("Error in function "); #ifndef BOOST_NO_RTTI - boost::format ffmt(function); - if (ffmt.expected_args()) - msg += (ffmt % boost::math::policies::detail::name_of()).str(); - else - msg += function; + replace_all_in_string(function, "%1%", boost::math::policies::detail::name_of()); #else - msg += function; + replace_all_in_string(function, "%1%", "Unknown"); #endif + msg += function; msg += ": "; msg += message; @@ -140,30 +143,27 @@ void raise_error(const char* function, const char* message) } template -void raise_error(const char* function, const char* message, const T& val) +void raise_error(const char* pfunction, const char* pmessage, const T& val) { - if(function == 0) - function = "Unknown function operating on type %1%"; - if(message == 0) - message = "Cause unknown: error caused by bad argument with value %1%"; + if(pfunction == 0) + pfunction = "Unknown function operating on type %1%"; + if(pmessage == 0) + pmessage = "Cause unknown: error caused by bad argument with value %1%"; + std::string function(pfunction); + std::string message(pmessage); std::string msg("Error in function "); #ifndef BOOST_NO_RTTI - boost::format ffmt(function); - if (ffmt.expected_args()) - msg += (ffmt % boost::math::policies::detail::name_of()).str(); - else - msg += function; + replace_all_in_string(function, "%1%", boost::math::policies::detail::name_of()); #else - msg += function; + replace_all_in_string(function, "%1%", "Unknown"); #endif + msg += function; msg += ": "; - boost::format mfmt(message); - if (mfmt.expected_args()) - msg += prec_format(mfmt, val); - else - msg += message; + std::string sval = prec_format(val); + replace_all_in_string(message, "%1%", sval.c_str()); + msg += message; E e(msg); boost::throw_exception(e); @@ -344,23 +344,11 @@ inline T raise_overflow_error( const T& val, const ::boost::math::policies::overflow_error< ::boost::math::policies::user_error>&) { - std::string fmsg("Error in function "), msg; -#ifndef BOOST_NO_RTTI - boost::format ffmt(function); - if (ffmt.expected_args()) - fmsg += (ffmt % boost::math::policies::detail::name_of()).str(); - else - fmsg += function; -#else - fmsg += function; -#endif - boost::format mfmt(message); - if (mfmt.expected_args()) - msg = prec_format(mfmt, val); - else - msg = message; + std::string m(message ? message : ""); + std::string sval = prec_format(val); + replace_all_in_string(m, "%1%", sval.c_str()); - return user_overflow_error(fmsg.c_str(), msg.c_str(), std::numeric_limits::infinity()); + return user_overflow_error(function, m.c_str(), std::numeric_limits::infinity()); } template diff --git a/include/boost/math/special_functions/detail/bernoulli_details.hpp b/include/boost/math/special_functions/detail/bernoulli_details.hpp index c12a172b6..529d5619b 100644 --- a/include/boost/math/special_functions/detail/bernoulli_details.hpp +++ b/include/boost/math/special_functions/detail/bernoulli_details.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef BOOST_HAS_THREADS