|
Boost-Commit : |
From: boost_at_[hidden]
Date: 2008-05-27 17:42:23
Author: matthiasschabel
Date: 2008-05-27 17:42:22 EDT (Tue, 27 May 2008)
New Revision: 45835
URL: http://svn.boost.org/trac/boost/changeset/45835
Log:
add a typename_format formatter that outputs the unit typename (somewhat simplified, demangled on GCC only)
Text files modified:
sandbox/units/boost/units/io.hpp | 65 ++++++++++++++++++++++++++++++---------
sandbox/units/boost/units/units_fwd.hpp | 2 +
sandbox/units/libs/units/example/kitchen_sink.cpp | 25 ++++++++++++++-
3 files changed, 74 insertions(+), 18 deletions(-)
Modified: sandbox/units/boost/units/io.hpp
==============================================================================
--- sandbox/units/boost/units/io.hpp (original)
+++ sandbox/units/boost/units/io.hpp 2008-05-27 17:42:22 EDT (Tue, 27 May 2008)
@@ -29,6 +29,7 @@
#include <boost/units/scale.hpp>
#include <boost/units/static_rational.hpp>
#include <boost/units/unit.hpp>
+#include <boost/units/detail/utility.hpp>
namespace boost {
@@ -99,9 +100,10 @@
enum format_mode
{
- raw,
- symbol,
- name
+ symbol_fmt = 0, // default - reduces unit names to known symbols for both base and derived units
+ name_fmt, // output full unit names for base and derived units
+ raw_fmt, // output only symbols for base units
+ typename_fmt // output demangled typenames
};
namespace detail {
@@ -149,21 +151,27 @@
ios.iword(detail::xalloc_key_holder<true>::value) = static_cast<long>(new_mode);
}
+inline std::ios_base& typename_format(std::ios_base& ios)
+{
+ (set_format)(ios, typename_fmt);
+ return(ios);
+}
+
inline std::ios_base& raw_format(std::ios_base& ios)
{
- (set_format)(ios, raw);
+ (set_format)(ios, raw_fmt);
return(ios);
}
inline std::ios_base& symbol_format(std::ios_base& ios)
{
- (set_format)(ios, symbol);
+ (set_format)(ios, symbol_fmt);
return(ios);
}
inline std::ios_base& name_format(std::ios_base& ios)
{
- (set_format)(ios, name);
+ (set_format)(ios, name_fmt);
return(ios);
}
@@ -388,11 +396,15 @@
typename mpl::begin<Scale>::type>::value(str);
std::string without_scale = symbol_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >());
- if(without_scale == boost::units::io_impl::symbol_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >())) {
+
+ if (without_scale == boost::units::io_impl::symbol_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >()))
+ {
str += "(";
str += without_scale;
str += ")";
- } else {
+ }
+ else
+ {
str += without_scale;
}
@@ -503,11 +515,15 @@
typename mpl::begin<Scale>::type>::value(str);
std::string without_scale = name_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >());
- if(without_scale == boost::units::io_impl::name_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >())) {
+
+ if (without_scale == boost::units::io_impl::name_string(unit<Dimension, heterogeneous_system<heterogeneous_system_impl<Units, Dimension, dimensionless_type> > >()))
+ {
str += "(";
str += without_scale;
str += ")";
- } else {
+ }
+ else
+ {
str += without_scale;
}
@@ -570,7 +586,8 @@
detail::name_string_impl<mpl::size<list<heterogeneous_system_dim<scaled_base_unit<Unit, UnitScale>, static_rational<1> >, dimensionless_type> >::value>::template apply<
typename mpl::begin<list<heterogeneous_system_dim<scaled_base_unit<Unit, UnitScale>, static_rational<1> >, dimensionless_type> >::type>::value(str);
- return(str);
+
+ return(str);
}
template<class Dimension,class System>
@@ -589,29 +606,45 @@
} // namespace io_impl
+template<class Dimension,class System>
+inline std::string
+typename_string(const unit<Dimension, System>&)
+{
+ return simplify_typename(typename reduce_unit< unit<Dimension,System> >::type());
+}
+
using io_impl::symbol_string;
using io_impl::name_string;
-/// Print an @c unit as a list of base units and exponents e.g "m s^-1"
+/// Print an @c unit as a list of base units and exponents
+///
+/// for @c symbol_format this gives e.g. "m s^-1" or "J"
+/// for @c name_format this gives e.g. "meter second^-1" or "joule"
+/// for @c raw_format this gives e.g. "m s^-1" or "meter kilogram^2 second^-2"
+/// for @c typename_format this gives the typename itself (currently demangled only on GCC)
template<class Char, class Traits, class Dimension, class System>
inline std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, const unit<Dimension, System>& u)
{
- if (units::get_format(os) == raw)
+ if (units::get_format(os) == typename_fmt)
+ {
+ os << typename_string(u);
+ }
+ else if (units::get_format(os) == raw_fmt)
{
// need to replace this with raw string
os << symbol_string(u);
}
- else if (units::get_format(os) == symbol)
+ else if (units::get_format(os) == symbol_fmt)
{
os << symbol_string(u);
}
- else if (units::get_format(os) == name)
+ else if (units::get_format(os) == name_fmt)
{
os << name_string(u);
}
else
{
- assert(!"The format mode must be either name or symbol");
+ assert(!"The format mode must be one of: typename_format, raw_format, name_format, symbol_format");
}
return(os);
Modified: sandbox/units/boost/units/units_fwd.hpp
==============================================================================
--- sandbox/units/boost/units/units_fwd.hpp (original)
+++ sandbox/units/boost/units/units_fwd.hpp 2008-05-27 17:42:22 EDT (Tue, 27 May 2008)
@@ -56,6 +56,8 @@
template<class T> std::string to_string(const T&);
template<class T> std::string name_string(const T&);
template<class T> std::string symbol_string(const T&);
+template<class T> std::string raw_string(const T&);
+template<class T> std::string typename_string(const T&);
} // namespace units
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-27 17:42:22 EDT (Tue, 27 May 2008)
@@ -466,7 +466,7 @@
//]
//[kitchen_sink_snippet_10
- std::cout << raw_format
+ std::cout << typename_format
<< quantity<capacitance>(1.0*farad) << std::endl
<< quantity<catalytic_activity>(1.0*katal) << std::endl
<< quantity<conductance>(1.0*siemen) << std::endl
@@ -487,7 +487,7 @@
//]
//[kitchen_sink_snippet_11
- std::cout << symbol_format
+ std::cout << raw_format
<< quantity<capacitance>(1.0*farad) << std::endl
<< quantity<catalytic_activity>(1.0*katal) << std::endl
<< quantity<conductance>(1.0*siemen) << std::endl
@@ -508,6 +508,27 @@
//]
//[kitchen_sink_snippet_12
+ std::cout << symbol_format
+ << quantity<capacitance>(1.0*farad) << std::endl
+ << quantity<catalytic_activity>(1.0*katal) << std::endl
+ << quantity<conductance>(1.0*siemen) << std::endl
+ << quantity<electric_charge>(1.0*coulomb) << std::endl
+ << quantity<electric_potential>(1.0*volt) << std::endl
+ << quantity<energy>(1.0*joule) << std::endl
+ << quantity<force>(1.0*newton) << std::endl
+ << quantity<frequency>(1.0*hertz) << std::endl
+ << quantity<illuminance>(1.0*lux) << std::endl
+ << quantity<inductance>(1.0*henry) << std::endl
+ << quantity<luminous_flux>(1.0*lumen) << std::endl
+ << quantity<magnetic_flux>(1.0*weber) << std::endl
+ << quantity<magnetic_flux_density>(1.0*tesla) << std::endl
+ << quantity<power>(1.0*watt) << std::endl
+ << quantity<pressure>(1.0*pascals) << std::endl
+ << quantity<resistance>(1.0*ohm) << std::endl
+ << std::endl;
+ //]
+
+ //[kitchen_sink_snippet_13
std::cout << name_format
<< quantity<capacitance>(1.0*farad) << std::endl
<< quantity<catalytic_activity>(1.0*katal) << 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