C:/nobackup/private/physical_svn/trunk/include/physical/detail/format_impl.hpp

Go to the documentation of this file.
00001 
00002 /*=============================================================================
00003     physical quantities / units / constants
00004     Copyright (c) 2006, 2007 Martin Schulz
00005     http://physical.sourceforge.net
00006   
00007     This is private code by Martin Schulz.
00008   
00009     Use, modification and distribution is subject to the Boost Software
00010     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00011     http://www.boost.org/LICENSE_1_0.txt)
00012 =============================================================================*/
00013 
00014 #ifndef PHYSICAL_FORMAT_IMPL
00015 #define PHYSICAL_FORMAT_IMPL
00016 
00017 #include "physical/format.hpp"
00018 
00019 #include "physical/formatspec.hpp"
00020 #include <sstream>
00021 
00022 namespace physical {
00023 
00024         namespace format {
00025 
00026                 namespace detail {
00027 
00028          template<class UnitSystem>
00029                         std::ostream& single_dimension(std::ostream& os, 
00030                                 enum UnitSystem::baseunit bu, const std::string& symbol, bool& first, DynamicPhysical<UnitSystem> q) {
00031                                 int e = q.exponents.exponents[bu];
00032                                 if(e) {
00033                                         if (!first) os << "*";
00034                                         os << symbol;
00035                                         if (e!=1) os << '^' << e;
00036                                         first=false;
00037                                 };
00038                                 return os;
00039                         };
00040 
00041          template<class UnitSystem>
00042                         std::ostream& automatic_dimension(std::ostream& os, DynamicPhysical<UnitSystem> q) {
00043                                 assert(q.exponents.exponents.size() == UnitSystem::number_baseunits);
00044                                 bool first=true;
00045                                 os << "[";
00046                                 single_dimension(os, UnitSystem::meter,         "m",    first, q);
00047                                 single_dimension(os, UnitSystem::kilogram,      "kg",   first, q);
00048                                 single_dimension(os, UnitSystem::second,        "s",    first, q);
00049                                 single_dimension(os, UnitSystem::ampere,        "A",    first, q);
00050                                 single_dimension(os, UnitSystem::kelvin,        "K",    first, q);
00051                                 single_dimension(os, UnitSystem::mol,           "mol",  first, q);
00052                                 single_dimension(os, UnitSystem::candela,       "cd",   first, q);
00053                                 if (first) os << "1";
00054                                 os << "]";
00055                                 return os;
00056                         };
00057 
00058          template<class UnitSystem>
00059                         std::ostream& automatic_quantity(std::ostream& os, DynamicPhysical<UnitSystem> q) {
00060                                 os << q.numerical_value;
00061                                 return automatic_dimension(os, q);
00062                         };
00063 
00064          template<class UnitSystem>
00065                         std::string automatic_dimension_string(DynamicPhysical<UnitSystem> q) {
00066                                 std::ostringstream os;
00067                                 automatic_dimension(os, q);
00068                                 return os.str();
00069                         };
00070 
00071          template<class UnitSystem>
00072                         std::ostream& format_to_spec(std::ostream& os, DynamicPhysical<UnitSystem> quantity, FormatSpec<UnitSystem> spec){
00073                                 
00074                                 if (spec.unit.exponents != quantity.exponents) {
00075                                         // Use automatically generated unit string.
00076                                         // Or better throw IncompatibleUnitException?
00077                                         os << quantity.numerical_value << "[";
00078                                         detail::automatic_dimension(os, quantity);
00079                                         os << "]";
00080 
00081                                 } else {
00082                                         // the (hopefully) usual case
00083 
00084                                         const DynamicPhysical<UnitSystem> &a = quantity;        
00085                                         const FormatSpec<UnitSystem>::unit_type &u = spec.unit;
00086                                         const DynamicPhysical<UnitSystem> x = a/u;      
00087                                         
00088                                         os.precision(spec.precision);
00089                                         os << x.numerical_value << "[" << spec.unitname <<"]";
00090                                 };
00091                                 return os;
00092                         }
00093 
00094 
00095 
00096                 } // namespace detail
00097         
00098         }; // namespace format
00099 
00100 }; // namespace physical
00101 
00102 #endif // PHYSICAL_FORMAT_IMPL

Generated on Mon Apr 2 22:25:03 2007 for physical_svn by  doxygen 1.5.1-p1
hosted on SourceForge.net Logo