|
Boost-Commit : |
From: steven_at_[hidden]
Date: 2008-05-24 13:21:54
Author: steven_watanabe
Date: 2008-05-24 13:21:54 EDT (Sat, 24 May 2008)
New Revision: 45732
URL: http://svn.boost.org/trac/boost/changeset/45732
Log:
Allow for printing unit names
Text files modified:
sandbox/units/boost/units/io.hpp | 68 ++++++++++++++++++++++++++++++++++++++-
sandbox/units/libs/units/test/test_scaled_unit.cpp | 6 +++
2 files changed, 72 insertions(+), 2 deletions(-)
Modified: sandbox/units/boost/units/io.hpp
==============================================================================
--- sandbox/units/boost/units/io.hpp (original)
+++ sandbox/units/boost/units/io.hpp 2008-05-24 13:21:54 EDT (Sat, 24 May 2008)
@@ -11,8 +11,10 @@
#ifndef BOOST_UNITS_IO_HPP
#define BOOST_UNITS_IO_HPP
+#include <cassert>
#include <string>
#include <iosfwd>
+#include <ios>
#include <boost/mpl/size.hpp>
#include <boost/mpl/begin.hpp>
@@ -77,6 +79,56 @@
}
};
+enum format_mode {
+ symbol,
+ name
+};
+
+namespace detail {
+
+template<bool>
+struct xalloc_key_holder {
+ static int value;
+ static bool initialized;
+};
+
+template<bool b>
+int xalloc_key_holder<b>::value = 0;
+template<bool b>
+bool xalloc_key_holder<b>::initialized = 0;
+
+struct xalloc_key_initializer_t {
+ xalloc_key_initializer_t() {
+ if(!xalloc_key_holder<true>::initialized) {
+ xalloc_key_holder<true>::value = std::ios_base::xalloc();
+ xalloc_key_holder<true>::initialized = true;
+ }
+ }
+};
+
+namespace {
+ xalloc_key_initializer_t xalloc_key_initializer;
+}
+
+}
+
+inline format_mode get_format(std::ios_base& ios) {
+ return(static_cast<format_mode>(ios.iword(detail::xalloc_key_holder<true>::value)));
+}
+inline void set_format(std::ios_base& ios, format_mode new_mode) {
+ ios.iword(detail::xalloc_key_holder<true>::value) = static_cast<long>(new_mode);
+}
+
+inline std::ios_base& symbol_format(std::ios_base& ios) {
+ (set_format)(ios, symbol);
+ return(ios);
+}
+
+inline std::ios_base& name_format(std::ios_base& ios) {
+ (set_format)(ios, name);
+ return(ios);
+}
+
namespace detail {
// This is needed so that std::string can be returned from
@@ -97,13 +149,25 @@
template<class T, class Os>
void print_base_unit(Os& os, const T&)
{
- os << (adapt_for_print)(base_unit_info<typename T::tag_type>::symbol()) << '^' << typename T::value_type();
+ if(units::get_format(os) == symbol) {
+ os << (adapt_for_print)(base_unit_info<typename T::tag_type>::symbol()) << '^' << typename T::value_type();
+ } else if(units::get_format(os) == name) {
+ os << (adapt_for_print)(base_unit_info<typename T::tag_type>::name()) << '^' << typename T::value_type();
+ } else {
+ assert(!"The format mode must be either name or symbol");
+ }
}
template<class Unit, class Os>
void print_base_unit(Os& os, const heterogeneous_system_dim<Unit, static_rational<1> >&)
{
- os << (adapt_for_print)(base_unit_info<Unit>::symbol());
+ if(units::get_format(os) == symbol) {
+ os << (adapt_for_print)(base_unit_info<Unit>::symbol());
+ } else if(units::get_format(os) == name) {
+ os << (adapt_for_print)(base_unit_info<Unit>::name());
+ } else {
+ assert(!"The format mode must be either name or symbol");
+ }
}
template<int N>
Modified: sandbox/units/libs/units/test/test_scaled_unit.cpp
==============================================================================
--- sandbox/units/libs/units/test/test_scaled_unit.cpp (original)
+++ sandbox/units/libs/units/test/test_scaled_unit.cpp 2008-05-24 13:21:54 EDT (Sat, 24 May 2008)
@@ -66,3 +66,9 @@
stream << si::nano * 12.5 * si::seconds;
BOOST_CHECK_EQUAL(stream.str(), "12.5 10^-9 s");
}
+
+BOOST_AUTO_TEST_CASE(test_output_name) {
+ std::stringstream stream;
+ stream << bu::name_format << si::nano * 12.5 * si::seconds;
+ BOOST_CHECK_EQUAL(stream.str(), "12.5 10^-9 second");
+}
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