|
Boost-Commit : |
From: boost_at_[hidden]
Date: 2008-05-26 13:18:03
Author: matthiasschabel
Date: 2008-05-26 13:18:02 EDT (Mon, 26 May 2008)
New Revision: 45776
URL: http://svn.boost.org/trac/boost/changeset/45776
Log:
fix some problems with output
scaled unit/quantity output still has bugs, demonstrated in examples/composite_output.cpp
Text files modified:
sandbox/units/boost/units/io.hpp | 29 +++++++++++++----------------
sandbox/units/boost/units/scale.hpp | 38 ++++++++++++++++++++++++++------------
sandbox/units/libs/units/example/composite_output.cpp | 26 ++++++++++++++++++++++----
sandbox/units/libs/units/example/kitchen_sink.cpp | 38 +++++++++++++++++++++++++++++++++++++-
4 files changed, 98 insertions(+), 33 deletions(-)
Modified: sandbox/units/boost/units/io.hpp
==============================================================================
--- sandbox/units/boost/units/io.hpp (original)
+++ sandbox/units/boost/units/io.hpp 2008-05-26 13:18:02 EDT (Mon, 26 May 2008)
@@ -26,6 +26,7 @@
#include <boost/units/units_fwd.hpp>
#include <boost/units/heterogeneous_system.hpp>
#include <boost/units/quantity.hpp>
+#include <boost/units/scale.hpp>
#include <boost/units/static_rational.hpp>
#include <boost/units/unit.hpp>
@@ -159,32 +160,30 @@
return(ios);
}
-// by default, return result of static symbol() method for class
-template<class T>
-inline std::string symbol_string(const T&)
+namespace detail {
+
+template<integer_type N, integer_type D>
+std::string exponent_string(const static_rational<N,D>& r)
{
- return T::symbol();
+ return '^' + to_string(r);
}
-// by default, return result of static name() method for class
-template<class T>
-inline std::string name_string(const T&)
+template<>
+inline std::string exponent_string(const static_rational<1>& r)
{
- return T::name();
+ return "";
}
-namespace detail {
-
template<class T>
std::string base_unit_symbol_string(const T&)
{
- return base_unit_info<typename T::tag_type>::symbol() + '^' + to_string(typename T::value_type());
+ return base_unit_info<typename T::tag_type>::symbol() + exponent_string(typename T::value_type());
}
template<class T>
std::string base_unit_name_string(const T&)
{
- return base_unit_info<typename T::tag_type>::name() + '^' + to_string(typename T::value_type());
+ return base_unit_info<typename T::tag_type>::name() + exponent_string(typename T::value_type());
}
// stringify with symbols
@@ -238,7 +237,7 @@
{
static void value(std::string& str)
{
- str += mpl::deref<Begin>::type::base::symbol() + ' ';
+ str += mpl::deref<Begin>::type::symbol();
scale_symbol_string_impl<N - 1>::template apply<typename mpl::next<Begin>::type>::value(str);
}
};
@@ -305,7 +304,7 @@
{
static void value(std::string& str)
{
- str += mpl::deref<Begin>::type::base::name() + ' ';
+ str += mpl::deref<Begin>::type::name();
scale_name_string_impl<N - 1>::template apply<typename mpl::next<Begin>::type>::value(str);
}
};
@@ -368,8 +367,6 @@
template<class Char, class Traits, class Dimension, class System>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, const unit<Dimension, System>& u)
{
-// os << typename reduce_unit<unit<Dimension, System> >::type();
-// return(os);
if(units::get_format(os) == symbol)
{
os << symbol_string(u);
Modified: sandbox/units/boost/units/scale.hpp
==============================================================================
--- sandbox/units/boost/units/scale.hpp (original)
+++ sandbox/units/boost/units/scale.hpp 2008-05-26 13:18:02 EDT (Mon, 26 May 2008)
@@ -35,6 +35,8 @@
typedef Exponent exponent;
typedef double value_type;
static value_type value() { return(detail::static_rational_power<Exponent>(static_cast<double>(base))); }
+ static std::string name() { return ""; };
+ static std::string symbol() { return ""; };
};
template<long Base, class Exponent>
@@ -55,22 +57,34 @@
template<long Base>
const long scale<Base, static_rational<0> >::base;
+template<long Base,class Exponent>
+std::string symbol_string(const scale<Base,Exponent>&)
+{
+ return scale<Base,Exponent>::symbol();
+}
+
+template<long Base,class Exponent>
+std::string name_string(const scale<Base,Exponent>&)
+{
+ return scale<Base,Exponent>::name();
+}
+
#ifndef BOOST_UNITS_DOXYGEN
-#define BOOST_UNITS_SCALE_SPECIALIZATION(base_,exponent_,val,name_,symbol_) \
-template<> \
-struct scale<base_, exponent_ > \
-{ \
- static const long base = base_; \
- typedef exponent_ exponent; \
- typedef double value_type; \
- static value_type value() { return(val); } \
- static std::string name() { return(#name_); } \
- static std::string symbol() { return(#symbol_); } \
+#define BOOST_UNITS_SCALE_SPECIALIZATION(base_,exponent_,val_,name_,symbol_) \
+template<> \
+struct scale<base_, exponent_ > \
+{ \
+ static const long base = base_; \
+ typedef exponent_ exponent; \
+ typedef double value_type; \
+ static value_type value() { return(val_); } \
+ static std::string name() { return(#name_); } \
+ static std::string symbol() { return(#symbol_); } \
}
-#define BOOST_UNITS_SCALE_DEF(exponent,value,name_,symbol_) \
-BOOST_UNITS_SCALE_SPECIALIZATION(10,static_rational<exponent>,value, name_, symbol_)
+#define BOOST_UNITS_SCALE_DEF(exponent_,value_,name_,symbol_) \
+BOOST_UNITS_SCALE_SPECIALIZATION(10,static_rational<exponent_>,value_, name_, symbol_)
BOOST_UNITS_SCALE_DEF(-24, 1e-24 ,yocto, y);
BOOST_UNITS_SCALE_DEF(-21, 1e-21, zepto, z);
Modified: sandbox/units/libs/units/example/composite_output.cpp
==============================================================================
--- sandbox/units/libs/units/example/composite_output.cpp (original)
+++ sandbox/units/libs/units/example/composite_output.cpp 2008-05-26 13:18:02 EDT (Mon, 26 May 2008)
@@ -15,6 +15,7 @@
#include <boost/units/systems/si/capacitance.hpp>
#include <boost/units/systems/si/io.hpp>
+#include <boost/units/systems/si/prefixes.hpp>
#include <iostream>
#include <sstream>
@@ -51,6 +52,11 @@
using boost::units::cgs::second;
using boost::units::cgs::dyne;
+ scale<10,static_rational<-9> > s;
+
+ si::nano*si::farad;
+ 1.0*si::nano*si::farad;
+
std::cout << 2.0 * dyne << std::endl;
std::cout << symbol_format << 2.0 * dyne << std::endl;
@@ -67,14 +73,26 @@
std::cout << symbol_string(scale<10,static_rational<-9> >()) << std::endl;
std::cout << name_string(scale<10,static_rational<-9> >()) << std::endl;
-
- // should work...
-// std::cout << symbol_string(scale<10,static_rational<-9> >()*si::farad) << std::endl;
-// std::cout << name_string(scale<10,static_rational<-9> >()*si::farad) << std::endl;
+
+ // wrong! - should output "n" and "nano", respectively
+ std::cout << symbol_string(si::nano) << std::endl;
+ std::cout << name_string(si::nano) << std::endl;
std::cout << name_format << si::farad << std::endl;
std::cout << symbol_format << si::farad << std::endl;
std::cout << name_format << 1.0*si::farad << std::endl;
std::cout << symbol_format << 1.0*si::farad << std::endl;
+
+ // wrong! - should output "nF" and "nanofarad", respectively
+ std::cout << symbol_string(si::farad*si::nano) << std::endl;
+ std::cout << name_string(si::farad*si::nano) << std::endl;
+
+ // should work but doesn't
+// std::cout << symbol_string(si::nano*1.0*si::farad) << std::endl;
+// std::cout << name_string(1.0*si::nano*si::farad) << std::endl;
+
+ // wrong! - should output "n(cm g s^-1)" and "nano(centimeter gram second^-1)", respectively
+ std::cout << symbol_string(si::nano*gram*centimeter/second) << std::endl;
+ std::cout << name_string(si::nano*gram*centimeter/second) << std::endl;
}
Modified: sandbox/units/libs/units/example/kitchen_sink.cpp
==============================================================================
--- sandbox/units/libs/units/example/kitchen_sink.cpp (original)
+++ sandbox/units/libs/units/example/kitchen_sink.cpp 2008-05-26 13:18:02 EDT (Mon, 26 May 2008)
@@ -147,6 +147,7 @@
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/codata/physico-chemical_constants.hpp>
+#include <boost/units/systems/si/io.hpp>
#include "measurement.hpp"
@@ -421,7 +422,42 @@
<< quantity<power>(1.0*watt) << std::endl
<< quantity<pressure>(1.0*pascals) << std::endl
<< quantity<resistance>(1.0*ohm) << std::endl
- << quantity<torque>(1.0*newton_meters) << std::endl
+ << std::endl;
+
+ std::cout << symbol_format << quantity<capacitance>(1.0*farad) << std::endl
+ << symbol_format << quantity<catalytic_activity>(1.0*katal) << std::endl
+ << symbol_format << quantity<conductance>(1.0*siemen) << std::endl
+ << symbol_format << quantity<electric_charge>(1.0*coulomb) << std::endl
+ << symbol_format << quantity<electric_potential>(1.0*volt) << std::endl
+ << symbol_format << quantity<energy>(1.0*joule) << std::endl
+ << symbol_format << quantity<force>(1.0*newton) << std::endl
+ << symbol_format << quantity<frequency>(1.0*hertz) << std::endl
+ << symbol_format << quantity<illuminance>(1.0*lux) << std::endl
+ << symbol_format << quantity<inductance>(1.0*henry) << std::endl
+ << symbol_format << quantity<luminous_flux>(1.0*lumen) << std::endl
+ << symbol_format << quantity<magnetic_flux>(1.0*weber) << std::endl
+ << symbol_format << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
+ << symbol_format << quantity<power>(1.0*watt) << std::endl
+ << symbol_format << quantity<pressure>(1.0*pascals) << std::endl
+ << symbol_format << quantity<resistance>(1.0*ohm) << std::endl
+ << std::endl;
+
+ std::cout << name_format << quantity<capacitance>(1.0*farad) << std::endl
+ << name_format << quantity<catalytic_activity>(1.0*katal) << std::endl
+ << name_format << quantity<conductance>(1.0*siemen) << std::endl
+ << name_format << quantity<electric_charge>(1.0*coulomb) << std::endl
+ << name_format << quantity<electric_potential>(1.0*volt) << std::endl
+ << name_format << quantity<energy>(1.0*joule) << std::endl
+ << name_format << quantity<force>(1.0*newton) << std::endl
+ << name_format << quantity<frequency>(1.0*hertz) << std::endl
+ << name_format << quantity<illuminance>(1.0*lux) << std::endl
+ << name_format << quantity<inductance>(1.0*henry) << std::endl
+ << name_format << quantity<luminous_flux>(1.0*lumen) << std::endl
+ << name_format << quantity<magnetic_flux>(1.0*weber) << std::endl
+ << name_format << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
+ << name_format << quantity<power>(1.0*watt) << std::endl
+ << name_format << quantity<pressure>(1.0*pascals) << std::endl
+ << name_format << quantity<resistance>(1.0*ohm) << std::endl
<< std::endl;
sstream2 << "S1 : 2" << std::endl;
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