Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53344 - sandbox/numeric_adaptor/boost/numeric_adaptor
From: barend.gehrels_at_[hidden]
Date: 2009-05-28 11:42:49


Author: barendgehrels
Date: 2009-05-28 11:42:48 EDT (Thu, 28 May 2009)
New Revision: 53344
URL: http://svn.boost.org/trac/boost/changeset/53344

Log:
Added Bruno's string conversion solution
Added namespaces
Bugfix in CLN string-assignment (should have precision otherwise too low)
Added sample "heron"
Added "HAVE_xxx" in samples

Text files modified:
   sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp | 26 +++++++++--------
   sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp | 11 ++++---
   sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp | 8 ++--
   sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp | 8 ++--
   sandbox/numeric_adaptor/boost/numeric_adaptor/numeric_adaptor.hpp | 59 ++++++++++++++-------------------------
   5 files changed, 50 insertions(+), 62 deletions(-)

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/cln_policy.hpp 2009-05-28 11:42:48 EDT (Thu, 28 May 2009)
@@ -7,8 +7,8 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
-#ifndef NUMERIC_ADAPTOR_CLN_POLICY_HPP_
-#define NUMERIC_ADAPTOR_CLN_POLICY_HPP_
+#ifndef NUMERIC_ADAPTOR_CLN_POLICY_HPP
+#define NUMERIC_ADAPTOR_CLN_POLICY_HPP
 
 #include <string>
 
@@ -18,7 +18,7 @@
 #include <cln/float.h>
 
 
-namespace numeric_adaptor {
+namespace boost { namespace numeric_adaptor {
 
 
 /*
@@ -32,19 +32,21 @@
 {
     typedef cln::cl_F type;
 
- template <typename OtherType>
- static inline void set(type& value, OtherType const& v)
+ template <typename FromType>
+ static inline void set(type& value, FromType const& v)
     {
         // Conversions from the C built-in type `double' are provided for the
         // classes cl_DF, cl_F, cl_R, cl_N and cl_number
-
         value = cln::cl_float(v, cln::float_format(256));
     }
 
     static inline void set(type& value, std::string const& v)
     {
- // CLN documentation 4.1.3:
- value = v.c_str();
+ // CLN documentation 4.1.3 + 5.1 ("A precision specifier of the form prec may be appended")
+ std::string copy(v);
+ copy += "_256";
+ value = copy.c_str();
+ //value = cln::cl_float(atof(v.c_str()), cln::float_format(256));
     }
 
     static inline void abs(type& r, type const& a)
@@ -74,8 +76,8 @@
     }
 
 
- template <typename OtherType>
- static inline OtherType big_numeric_cast(type const& b)
+ template <typename ToType>
+ static inline ToType big_numeric_cast(type const& b)
     {
         /*
             Conversions from the classes cl_I, cl_RA, cl_SF, cl_FF,
@@ -91,13 +93,13 @@
             0 is returned. If abs(x) is too large (overflow),
             an IEEE infinity is returned.
         */
- return OtherType(double_approx(b));
+ return ToType(double_approx(b));
     }
 
 };
 
 
-} // namespace numeric_adaptor
+}} // namespace boost::numeric_adaptor
 
 
 #endif

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/default_policy.hpp 2009-05-28 11:42:48 EDT (Thu, 28 May 2009)
@@ -7,13 +7,14 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
-#ifndef NUMERIC_ADAPTOR_DEFAULT_POLICY_HPP_
-#define NUMERIC_ADAPTOR_DEFAULT_POLICY_HPP_
+#ifndef NUMERIC_ADAPTOR_DEFAULT_POLICY_HPP
+#define NUMERIC_ADAPTOR_DEFAULT_POLICY_HPP
 
 #include <sstream>
+#include <iomanip>
 
 
-namespace numeric_adaptor {
+namespace boost { namespace numeric_adaptor {
 
 
 template <typename T>
@@ -44,13 +45,13 @@
     static inline std::string as_string(T const& a)
     {
         std::ostringstream out;
- out << a;
+ out << std::setprecision(20) << a;
         return out.str();
     }
 };
 
 
-} // namespace numeric_adaptor
+}} // namespace boost::numeric_adaptor
 
 
 #endif

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/gmp_policy.hpp 2009-05-28 11:42:48 EDT (Thu, 28 May 2009)
@@ -7,8 +7,8 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
-#ifndef NUMERIC_ADAPTOR_GMP_POLICY_HPP_
-#define NUMERIC_ADAPTOR_GMP_POLICY_HPP_
+#ifndef NUMERIC_ADAPTOR_GMP_POLICY_HPP
+#define NUMERIC_ADAPTOR_GMP_POLICY_HPP
 
 
 #include <string>
@@ -17,7 +17,7 @@
 #include <gmp.h>
 
 
-namespace numeric_adaptor {
+namespace boost { namespace numeric_adaptor {
 
 
 struct gmp_policy
@@ -145,7 +145,7 @@
 };
 
 
-} // namespace numeric_adaptor
+}} // namespace boost::numeric_adaptor
 
 
 #endif

Modified: sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp
==============================================================================
--- sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp (original)
+++ sandbox/numeric_adaptor/boost/numeric_adaptor/ieee_policy.hpp 2009-05-28 11:42:48 EDT (Thu, 28 May 2009)
@@ -7,8 +7,8 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
-#ifndef NUMERIC_ADAPTOR_IEEE_POLICY_HPP_
-#define NUMERIC_ADAPTOR_IEEE_POLICY_HPP_
+#ifndef NUMERIC_ADAPTOR_IEEE_POLICY_HPP
+#define NUMERIC_ADAPTOR_IEEE_POLICY_HPP
 
 
 #include <cmath>
@@ -23,7 +23,7 @@
 #include <boost/math/special_functions/hypot.hpp>
 
 
-namespace numeric_adaptor {
+namespace boost { namespace numeric_adaptor {
 
 
 template <typename T>
@@ -55,7 +55,7 @@
 };
 
 
-} // namespace numeric_adaptor
+}} // namespace boost::numeric_adaptor
 
 
 #endif

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-05-28 11:42:48 EDT (Thu, 28 May 2009)
@@ -7,43 +7,23 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 
 
-#ifndef NUMERIC_ADAPTOR_NUMERIC_ADAPTOR_HPP_
-#define NUMERIC_ADAPTOR_NUMERIC_ADAPTOR_HPP_
+#ifndef NUMERIC_ADAPTOR_NUMERIC_ADAPTOR_HPP
+#define NUMERIC_ADAPTOR_NUMERIC_ADAPTOR_HPP
 
 
 #include <boost/static_assert.hpp>
 
+#include <string>
 
-namespace numeric_adaptor {
+namespace boost { namespace numeric_adaptor {
 
 
-template <typename Policy, typename ToType, bool IsCasted>
-struct caster
+template <typename Base, typename T>
+struct enable_cast
 {
- static inline ToType cast(typename Policy::type const& value)
+ inline operator T()
     {
- return Policy::template big_numeric_cast<ToType>(value);
- }
-};
-
-// specialization for strings: casting is not possible, getting is OK
-template <typename Policy>
-struct caster<Policy, std::string, true>
-{
- struct CAST_TO_STRING_IS_NOT_POSSIBLE {};
-
- // PROHIBITED!
- static inline CAST_TO_STRING_IS_NOT_POSSIBLE cast(typename Policy::type const& )
- {
- }
-};
-
-template <typename Policy>
-struct caster<Policy, std::string, false>
-{
- static inline std::string cast(typename Policy::type const& value)
- {
- return Policy::as_string(value);
+ return static_cast<Base*>(this)->template big_numeric_cast<T>();
     }
 };
 
@@ -51,7 +31,14 @@
 
 
 template <typename Policy>
-struct numeric_adaptor
+struct numeric_adaptor :
+ enable_cast<numeric_adaptor<Policy>, char>,
+ enable_cast<numeric_adaptor<Policy>, int>,
+ enable_cast<numeric_adaptor<Policy>, short int>,
+ enable_cast<numeric_adaptor<Policy>, long int>,
+ enable_cast<numeric_adaptor<Policy>, float>,
+ enable_cast<numeric_adaptor<Policy>, double>,
+ enable_cast<numeric_adaptor<Policy>, long double>
 {
     inline numeric_adaptor()
     {
@@ -107,18 +94,16 @@
         return *this;
     }
 
- // Cast to normal IEEE type but NOT to std::string because of compilation problems
- template <typename ToType>
- inline operator ToType() const
+ template <class ToType>
+ inline ToType big_numeric_cast()
     {
- return caster<Policy, ToType, true>::cast(value);
+ return Policy::template big_numeric_cast<ToType>(this->value);
     }
 
     // tuple/fusion/variant-like get template function
- template <typename ToType>
- inline ToType get() const
+ inline operator std::string()
     {
- return caster<Policy, ToType, false>::cast(value);
+ return Policy::as_string(this->value);
     }
 
 
@@ -252,7 +237,7 @@
 };
 
 
-} // namespace numeric_adaptor
+}} // namespace boost::numeric_adaptor
 
 
 #endif


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