|
Boost-Commit : |
From: boost_at_[hidden]
Date: 2008-05-25 21:00:14
Author: matthiasschabel
Date: 2008-05-25 21:00:14 EDT (Sun, 25 May 2008)
New Revision: 45758
URL: http://svn.boost.org/trac/boost/changeset/45758
Log:
preliminary stuff for name_string/symbol_string
Text files modified:
sandbox/units/boost/units/io.hpp | 82 +++++++++++++++++++++++++++++++++------
sandbox/units/boost/units/units_fwd.hpp | 12 ++++-
sandbox/units/libs/units/example/composite_output.cpp | 50 ++++++++++++------------
3 files changed, 102 insertions(+), 42 deletions(-)
Modified: sandbox/units/boost/units/io.hpp
==============================================================================
--- sandbox/units/boost/units/io.hpp (original)
+++ sandbox/units/boost/units/io.hpp 2008-05-25 21:00:14 EDT (Sun, 25 May 2008)
@@ -15,6 +15,7 @@
#include <string>
#include <iosfwd>
#include <ios>
+#include <sstream>
#include <boost/mpl/size.hpp>
#include <boost/mpl/begin.hpp>
@@ -22,6 +23,7 @@
#include <boost/mpl/deref.hpp>
#include <boost/serialization/nvp.hpp>
+#include <boost/units/units_fwd.hpp>
#include <boost/units/heterogeneous_system.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/static_rational.hpp>
@@ -72,6 +74,7 @@
{
return(BaseUnit::name());
}
+
/// The symbol for the base unit (Returns BaseUnit::symbol() by default)
static std::string symbol()
{
@@ -79,7 +82,8 @@
}
};
-enum format_mode {
+enum format_mode
+{
symbol,
name
};
@@ -87,19 +91,24 @@
namespace detail {
template<bool>
-struct xalloc_key_holder {
+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) {
+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;
}
@@ -107,32 +116,53 @@
};
namespace {
- xalloc_key_initializer_t xalloc_key_initializer;
-}
+
+xalloc_key_initializer_t xalloc_key_initializer;
-}
+} // namespace
-inline format_mode get_format(std::ios_base& ios) {
+} // namespace detail
+
+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) {
+
+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) {
+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) {
+inline std::ios_base& name_format(std::ios_base& ios)
+{
(set_format)(ios, name);
return(ios);
}
+// by default, return result of static symbol() method for class
+template<class T>
+inline std::string symbol_string(const T&)
+{
+ return T::symbol();
+}
+
+// by default, return result of static name() method for class
+template<class T>
+inline std::string name_string(const T&)
+{
+ return T::name();
+}
+
namespace detail {
// This is needed so that std::string can be returned from
-// the base unit functions and wtill allow the operators
+// the base unit functions and will allow the operators
// to work for any std::basic_ostream
template<class T>
const T& adapt_for_print(const T& t)
@@ -227,7 +257,7 @@
struct print_scale_impl<0> {
template<class Begin, class Os>
struct apply {
- static void value(Os&) {}
+ static void value(Os&) { }
};
};
@@ -263,6 +293,30 @@
return(os);
}
+// quick and dirty solution - should replace guts with implementation in operator<< above to directly create string
+template<class Dimension,class System>
+inline std::string
+symbol_string(const unit<Dimension,System>& u)
+{
+ std::stringstream sstr;
+
+ sstr << u;
+
+ return sstr.str();
+}
+
+// quick and dirty solution - should replace guts with implementation in operator<< above to directly create string
+template<class Dimension,class System>
+inline std::string
+name_string(const unit<Dimension,System>& u)
+{
+ std::stringstream sstr;
+
+ sstr << name_format << u;
+
+ return sstr.str();
+}
+
} // namespace units
} // namespace boost
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-25 21:00:14 EDT (Sun, 25 May 2008)
@@ -8,14 +8,16 @@
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
-#ifndef BOOST_UNITS_UNITSFWD_HPP
-#define BOOST_UNITS_UNITSFWD_HPP
+#ifndef BOOST_UNITS_UNITS_FWD_HPP
+#define BOOST_UNITS_UNITS_FWD_HPP
/// \file
/// Forward declarations of library components.
#ifndef BOOST_UNITS_DOXYGEN
+#include <string>
+
namespace boost {
namespace units {
@@ -51,10 +53,14 @@
template<class From,class To> struct conversion_helper;
+// generate string representation of unit using base_unit names or symbols, respectively
+template<class T> std::string name_string(const T&);
+template<class T> std::string symbol_string(const T&);
+
} // namespace units
} // namespace boost
#endif
-#endif // BOOST_UNITS_UNITSFWD_HPP
+#endif // BOOST_UNITS_UNITS_FWD_HPP
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-25 21:00:14 EDT (Sun, 25 May 2008)
@@ -11,6 +11,8 @@
#include <boost/units/quantity.hpp>
#include <boost/units/systems/cgs.hpp>
#include <boost/units/io.hpp>
+#include <boost/units/scale.hpp>
+
#include <iostream>
#include <sstream>
@@ -26,30 +28,16 @@
//]
-template<class T>
-std::string symbol_string(const T& x)
+template<>
+std::string name_string(const cgs::force&)
{
- std::stringstream sstr;
-
- sstr << symbol_format << x;
-
- return sstr.str();
-}
-
-template<class T>
-std::string name_string(const T& x)
-{
- std::stringstream sstr;
-
- sstr << name_format << x;
-
- return sstr.str();
+ return "dyne";
}
-template<class Y>
-std::string name_string(const quantity<cgs::force,Y>& x)
+template<>
+std::string symbol_string(const cgs::force&)
{
- return "dyne";
+ return "goombah";
}
}
@@ -58,11 +46,23 @@
int main()
{
- std::cout << 2.0 * boost::units::cgs::dyne << std::endl;
+ using namespace boost::units;
+ using boost::units::cgs::centimeter;
+ using boost::units::cgs::gram;
+ using boost::units::cgs::second;
+ using boost::units::cgs::dyne;
+
+ std::cout << 2.0 * dyne << std::endl;
- std::cout << boost::units::symbol_format << 2.0 * boost::units::cgs::dyne << std::endl;
- std::cout << boost::units::name_format << 2.0 * boost::units::cgs::dyne << std::endl;
+ std::cout << symbol_format << 2.0 * dyne << std::endl;
+ std::cout << name_format << 2.0 * dyne << std::endl;
+
+ std::cout << symbol_string(dyne) << std::endl;
+ std::cout << name_string(dyne) << std::endl;
+
+ std::cout << symbol_string(gram*centimeter/(second*second)) << std::endl;
+ std::cout << name_string(gram*centimeter/(second*second)) << std::endl;
- std::cout << boost::units::symbol_string(2.0*boost::units::cgs::dyne) << std::endl;
- std::cout << boost::units::name_string(2.0*boost::units::cgs::dyne) << std::endl;
+ std::cout << symbol_string(scale<10,static_rational<-9> >()) << std::endl;
+ std::cout << name_string(scale<10,static_rational<-9> >()) << 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