2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

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.
This commit is contained in:
jzmaddock
2016-05-09 11:38:13 +01:00
parent c5084fc77e
commit 993cdcb42e
3 changed files with 49 additions and 59 deletions

View File

@@ -8,6 +8,7 @@
// and comments, don't change any of the special comment mark-ups!
#include <iostream>
#include <boost/format.hpp>
using std::cout; using std::endl; using std::cerr;
//[policy_eg_9

View File

@@ -11,6 +11,8 @@
#include <stdexcept>
#include <iomanip>
#include <string>
#include <cstring>
#include <typeinfo>
#include <cerrno>
#include <boost/config/no_tr1/complex.hpp>
#include <boost/config/no_tr1/cmath.hpp>
@@ -18,6 +20,7 @@
#include <boost/math/tools/config.hpp>
#include <boost/math/policies/policy.hpp>
#include <boost/math/tools/precision.hpp>
#include <boost/throw_exception.hpp>
#include <boost/cstdint.hpp>
#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 <boost/format.hpp>
#include <sstream>
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 <class Formatter, class Group>
inline std::string do_format(Formatter& f, const Group& g)
{
return (f % g).str();
}
template <class T>
BOOST_DEDUCED_TYPENAME disable_if_c<std::numeric_limits<T>::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<T, boost::math::policies::policy<> >::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 <class T>
BOOST_DEDUCED_TYPENAME enable_if_c<std::numeric_limits<T>::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<T, boost::math::policies::policy<> >() * 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 <class T>
@@ -115,23 +120,21 @@ inline const char* name_of<BOOST_MATH_FLOAT128_TYPE>()
#endif
template <class E, class T>
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<T>()).str();
else
msg += function;
replace_all_in_string(function, "%1%", boost::math::policies::detail::name_of<T>());
#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 <class E, class T>
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<T>()).str();
else
msg += function;
replace_all_in_string(function, "%1%", boost::math::policies::detail::name_of<T>());
#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<T>()).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<T>::infinity());
return user_overflow_error(function, m.c_str(), std::numeric_limits<T>::infinity());
}
template <class T>

View File

@@ -11,6 +11,7 @@
#include <boost/detail/lightweight_mutex.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/math/tools/toms748_solve.hpp>
#include <vector>
#ifdef BOOST_HAS_THREADS