Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75814 - in trunk: boost/units boost/units/detail boost/units/systems/detail libs/units/test
From: steven_at_[hidden]
Date: 2011-12-05 11:56:56


Author: steven_watanabe
Date: 2011-12-05 11:56:55 EST (Mon, 05 Dec 2011)
New Revision: 75814
URL: http://svn.boost.org/trac/boost/changeset/75814

Log:
Make pow and root work for constants.
Text files modified:
   trunk/boost/units/detail/static_rational_power.hpp | 4 +-
   trunk/boost/units/quantity.hpp | 36 +++++++++++++++++++++
   trunk/boost/units/systems/detail/constants.hpp | 68 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/units/test/test_constants.cpp | 13 +++++++
   4 files changed, 119 insertions(+), 2 deletions(-)

Modified: trunk/boost/units/detail/static_rational_power.hpp
==============================================================================
--- trunk/boost/units/detail/static_rational_power.hpp (original)
+++ trunk/boost/units/detail/static_rational_power.hpp 2011-12-05 11:56:55 EST (Mon, 05 Dec 2011)
@@ -93,7 +93,7 @@
         typedef typename next::type type;
         static type call(const Y& y, const R& r)
         {
- const Y square = y * y;
+ const square_type square = y * y;
             return(next::call(square, r));
         }
     };
@@ -185,7 +185,7 @@
 {
     typedef typename static_int_power_sign_impl<N>::template apply<Y> impl;
     typedef typename impl::type type;
- static Y call(const Y& y)
+ static type call(const Y& y)
     {
         return(impl::call(y));
     }

Modified: trunk/boost/units/quantity.hpp
==============================================================================
--- trunk/boost/units/quantity.hpp (original)
+++ trunk/boost/units/quantity.hpp 2011-12-05 11:56:55 EST (Mon, 05 Dec 2011)
@@ -680,6 +680,15 @@
     typedef quantity<unit_type,value_type> type;
 };
 
+/// disambiguate
+/// INTERNAL ONLY
+template<class Unit,
+ class Y>
+struct multiply_typeof_helper< one,quantity<Unit,Y> >
+{
+ typedef quantity<Unit,Y> type;
+};
+
 /// quantity times scalar typeof helper
 /// INTERNAL ONLY
 template<class Unit,
@@ -692,6 +701,15 @@
     typedef quantity<unit_type,value_type> type;
 };
 
+/// disambiguate
+/// INTERNAL ONLY
+template<class Unit,
+ class X>
+struct multiply_typeof_helper< quantity<Unit,X>,one >
+{
+ typedef quantity<Unit,X> type;
+};
+
 /// unit times quantity typeof helper
 /// INTERNAL ONLY
 template<class Unit,
@@ -767,6 +785,15 @@
     typedef quantity<unit_type,value_type> type;
 };
 
+/// disambiguate
+/// INTERNAL ONLY
+template<class Unit,
+ class Y>
+struct divide_typeof_helper< one,quantity<Unit,Y> >
+{
+ typedef quantity<Unit,Y> type;
+};
+
 /// quantity divided by scalar typeof helper
 /// INTERNAL ONLY
 template<class Unit,
@@ -779,6 +806,15 @@
     typedef quantity<unit_type,value_type> type;
 };
 
+/// disambiguate
+/// INTERNAL ONLY
+template<class Unit,
+ class X>
+struct divide_typeof_helper< quantity<Unit,X>,one >
+{
+ typedef quantity<Unit,X> type;
+};
+
 /// unit divided by quantity typeof helper
 /// INTERNAL ONLY
 template<class Unit,

Modified: trunk/boost/units/systems/detail/constants.hpp
==============================================================================
--- trunk/boost/units/systems/detail/constants.hpp (original)
+++ trunk/boost/units/systems/detail/constants.hpp 2011-12-05 11:56:55 EST (Mon, 05 Dec 2011)
@@ -20,6 +20,8 @@
 #include <boost/units/static_constant.hpp>
 #include <boost/units/units_fwd.hpp>
 #include <boost/units/operators.hpp>
+#include <boost/units/static_rational.hpp>
+#include <boost/units/detail/one.hpp>
 
 namespace boost {
 
@@ -134,6 +136,72 @@
 
 #undef BOOST_UNITS_DEFINE_HELPER
 
+#define BOOST_UNITS_DEFINE_HELPER(name, symbol) \
+ \
+template<class T1> \
+struct name ## _typeof_helper<constant<T1>, one> \
+{ \
+ typedef typename name ## _typeof_helper<typename T1::value_type, one>::type type;\
+}; \
+ \
+template<class T2> \
+struct name ## _typeof_helper<one, constant<T2> > \
+{ \
+ typedef typename name ## _typeof_helper<one, typename T2::value_type>::type type;\
+}; \
+ \
+template<class T1> \
+typename name ## _typeof_helper<typename T1::value_type, one>::type \
+operator symbol(const constant<T1>& t, const one& u) \
+{ \
+ return(t.value() symbol u); \
+} \
+ \
+template<class T2> \
+typename name ## _typeof_helper<one, typename T2::value_type>::type \
+operator symbol(const one& t, const constant<T2>& u) \
+{ \
+ return(t symbol u.value()); \
+}
+
+BOOST_UNITS_DEFINE_HELPER(multiply, *)
+BOOST_UNITS_DEFINE_HELPER(divide, /)
+
+#undef BOOST_UNITS_DEFINE_HELPER
+
+template<class T1, long N, long D>
+struct power_typeof_helper<constant<T1>, static_rational<N,D> >
+{
+ typedef power_typeof_helper<typename T1::value_type, static_rational<N,D> > base;
+ typedef typename base::type type;
+ static type value(const constant<T1>& arg)
+ {
+ return base::value(arg.value());
+ }
+};
+
+#define BOOST_UNITS_DEFINE_HELPER(name, symbol) \
+ \
+template<class T1, class E> \
+struct name ## _typeof_helper<constant<T1> > \
+{ \
+ typedef typename name ## _typeof_helper<typename T1::value_type, E>::type type;\
+}; \
+ \
+template<class T1> \
+typename name ## _typeof_helper<typename T1::value_type, one>::type \
+operator symbol(const constant<T1>& t, const one& u) \
+{ \
+ return(t.value() symbol u); \
+} \
+ \
+template<class T2> \
+typename name ## _typeof_helper<one, typename T2::value_type>::type \
+operator symbol(const one& t, const constant<T2>& u) \
+{ \
+ return(t symbol u.value()); \
+}
+
 #define BOOST_UNITS_PHYSICAL_CONSTANT(name, type, value_, uncertainty_) \
 struct name ## _t { \
     typedef type value_type; \

Modified: trunk/libs/units/test/test_constants.cpp
==============================================================================
--- trunk/libs/units/test/test_constants.cpp (original)
+++ trunk/libs/units/test/test_constants.cpp 2011-12-05 11:56:55 EST (Mon, 05 Dec 2011)
@@ -20,6 +20,7 @@
 
 #include <boost/units/systems/detail/constants.hpp>
 #include <boost/units/quantity.hpp>
+#include <boost/units/pow.hpp>
 #include <boost/units/systems/si/length.hpp>
 #include <boost/units/systems/si/time.hpp>
 
@@ -27,6 +28,9 @@
 using boost::units::si::length;
 using boost::units::si::meters;
 using boost::units::si::seconds;
+using boost::units::static_rational;
+using boost::units::pow;
+using boost::units::root;
 
 BOOST_UNITS_PHYSICAL_CONSTANT(length_constant, quantity<length>, 2.0 * meters, 0.5 * meters);
 
@@ -72,3 +76,12 @@
     BOOST_UNITS_CHECK_RESULT(length_constant, /, seconds);
     BOOST_UNITS_CHECK_RESULT(seconds, /, length_constant);
 }
+
+void test_pow() {
+ check_same(pow<2>(length_constant), pow<2>(unwrap(length_constant)));
+ check_same(root<2>(length_constant), root<2>(unwrap(length_constant)));
+ check_same(pow<5>(length_constant), pow<5>(unwrap(length_constant)));
+ check_same(root<5>(length_constant), root<5>(unwrap(length_constant)));
+ check_same(pow<static_rational<2, 3> >(length_constant), pow<static_rational<2, 3> >(unwrap(length_constant)));
+ check_same(root<static_rational<2, 3> >(length_constant), root<static_rational<2, 3> >(unwrap(length_constant)));
+}


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