Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57146 - in sandbox/numeric_adaptor: boost/numeric_adaptor libs/numeric_adaptor/test
From: bruno.lalande_at_[hidden]
Date: 2009-10-25 05:39:23


Author: bruno.lalande
Date: 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
New Revision: 57146
URL: http://svn.boost.org/trac/boost/changeset/57146

Log:
Made conversion from string more generic.
Text files modified:
   sandbox/numeric_adaptor/boost/numeric_adaptor/cln_value_type.hpp | 12 +++++++++++-
   sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_value_type.hpp | 14 +++++++++++++-
   sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp | 19 +++++++++++++++++++
   sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot | 2 +-
   sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_conversions.cpp | 36 +++++++++++++-----------------------
   5 files changed, 57 insertions(+), 26 deletions(-)

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/cln_value_type.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/cln_value_type.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/cln_value_type.hpp 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
@@ -137,9 +137,19 @@
     }
 
 
+ cln::cl_F m_value;
+};
 
 
- cln::cl_F m_value;
+template <>
+struct to_traits<cln_value_type, std::string>
+{
+ static cln_value_type apply(const std::string& source)
+ {
+ cln_value_type result;
+ result.m_value = source.c_str();
+ return result;
+ }
 };
 
 

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_value_type.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_value_type.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_value_type.hpp 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
@@ -175,11 +175,23 @@
     }
 
 
-
     mpf_t m_value;
 };
 
 
+template <>
+struct to_traits<gmp_value_type, std::string>
+{
+ static gmp_value_type apply(const std::string& source)
+ {
+ gmp_value_type result;
+ mpf_init(result.m_value);
+ mpf_set_str(result.m_value, source.c_str(), 10);
+ return result;
+ }
+};
+
+
 } // namespace numeric_adaptor
 
 

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
@@ -17,11 +17,26 @@
 #include <cmath>
 #include <boost/static_assert.hpp>
 #include <boost/math/special_functions/hypot.hpp>
+#include <boost/lexical_cast.hpp>
 
 
 namespace boost {
 
 
+namespace numeric_adaptor {
+
+
+template <class Target, class Source>
+struct to_traits
+{
+ static Target apply(const Source& source)
+ { return boost::lexical_cast<Target>(source); }
+};
+
+
+} // namespace numeric_adaptor
+
+
 template <class T>
 T abs(T value)
 { return std::abs(value); }
@@ -50,6 +65,10 @@
 T hypot(T a, T b)
 { return boost::math::hypot(a, b); }
 
+template <class Target, class Source>
+Target to(const Source& source)
+{ return numeric_adaptor::to_traits<Target, Source>::apply(source); }
+
 
 } // namespace boost
 

Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/test/Jamroot 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
@@ -21,4 +21,4 @@
 run test_heron.cpp ;
 run test_arithmetic.cpp ;
 run test_trig.cpp ;
-#run test_conversions.cpp ;
+run test_conversions.cpp ;

Modified: sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_conversions.cpp
==============================================================================
--- sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_conversions.cpp (original)
+++ sandbox/numeric_adaptor/libs/numeric_adaptor/test/test_conversions.cpp 2009-10-25 05:39:22 EDT (Sun, 25 Oct 2009)
@@ -7,50 +7,40 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
+#include <string>
 #include <boost/test/included/test_exec_monitor.hpp>
+#include <boost/test/floating_point_comparison.hpp>
 #include <boost/numeric_adaptor/numeric_adaptor.hpp>
-#include <boost/numeric_adaptor/ieee_policy.hpp>
 
 #if defined(HAVE_GMP)
-# include <boost/numeric_adaptor/gmp_policy.hpp>
+# include <boost/numeric_adaptor/gmp_value_type.hpp>
 #endif
 
 #if defined(HAVE_CLN)
-# include <boost/numeric_adaptor/cln_policy.hpp>
+# include <boost/numeric_adaptor/cln_value_type.hpp>
 #endif
 
 
-template <typename Policy>
+template <typename ValueType>
 void test_all()
 {
- typedef boost::numeric_adaptor::numeric_adaptor<Policy> num;
+ double epsilon = 0.0001;
 
- num n = 1234.5;
- BOOST_CHECK_EQUAL(int(n), 1234);
- BOOST_CHECK_EQUAL((unsigned int)(n), 1234);
- BOOST_CHECK_EQUAL((unsigned short)(n), 1234);
- BOOST_CHECK_EQUAL((unsigned long)(n), 1234);
- BOOST_CHECK_EQUAL(float(n), 1234.5);
- BOOST_CHECK_EQUAL(double(n), 1234.5);
- BOOST_CHECK_EQUAL(std::string(n), "1234.5");
-
- num n2 = "1234.5";
- BOOST_CHECK_EQUAL(double(n2), 1234.5);
-
- num n3("1234.5");
- BOOST_CHECK_EQUAL(double(n3), 1234.5);
+ BOOST_CHECK_CLOSE((double)boost::to<ValueType>(std::string("123.456")), 123.456, epsilon);
 }
 
 int test_main(int, char*[])
 {
- using namespace boost::numeric_adaptor;
- test_all<ieee_policy<double> >();
+ test_all<float>();
+ test_all<double>();
+ test_all<long double>();
+
 #if defined(HAVE_GMP)
- test_all<gmp_policy>();
+ test_all<boost::numeric_adaptor::gmp_value_type>();
 #endif
 
 #if defined(HAVE_CLN)
- test_all<cln_policy>();
+ test_all<boost::numeric_adaptor::cln_value_type>();
 #endif
 
     return 0;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk