C:/nobackup/private/physical_svn/trunk/include/physical/detail/dynamicexponents.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_DYNAMICEXPONENTS
00015 #define PHYSICAL_DYNAMICEXPONENTS
00016 
00017 #include "physical/config.hpp"
00018 
00019 #include <algorithm>
00020 #include <functional>
00021 #include <cassert>
00022 #include <vector>
00023 
00024 #include <boost/mpl/for_each.hpp>
00025 
00026 namespace physical {
00027 
00028         namespace detail {
00029 
00030                 template<class UnitSystem>
00031                 struct DynamicExponents {
00032                         std::vector<int> exponents;
00033                         
00034                         DynamicExponents()
00035                                 : exponents(UnitSystem::number_baseunits,0)
00036                         {};
00037                         
00038                         DynamicExponents(const std::vector<int>& v)
00039                                 : exponents(v)
00040                         {};
00041 
00042 
00043                         struct append_to {
00044                                 std::vector<int>&vec;
00045                                 append_to(std::vector<int>&v):vec(v) {};
00046                                 void operator()(int t) { vec.push_back(t); };
00047                         };
00048 
00049                         // conversion from static to dynamic exponents
00050                         template<class Exponents>
00051                         DynamicExponents(Exponents)
00052                         {
00053                                 boost::mpl::for_each<typename Exponents::dimensions>(append_to(exponents));
00054                         };
00055                 };
00056 
00057                 template<class UnitSystem>
00058                 inline DynamicExponents<UnitSystem> 
00059                 operator+(DynamicExponents<UnitSystem> exponents1, DynamicExponents<UnitSystem> exponents2){
00060                         std::vector<int> v(exponents1.exponents.size());
00061                         std::transform(exponents1.exponents.begin(), exponents1.exponents.end(), 
00062                                            exponents2.exponents.begin(), v.begin(), std::plus<int>());
00063                         return DynamicExponents<UnitSystem>(v);
00064                 };
00065 
00066 
00067                 template<class UnitSystem>
00068                 inline DynamicExponents<UnitSystem> 
00069                 operator-(DynamicExponents<UnitSystem> exponents1, DynamicExponents<UnitSystem> exponents2){
00070                         std::vector<int> v(exponents1.exponents.size());
00071                         std::transform(exponents1.exponents.begin(), exponents1.exponents.end(), 
00072                                            exponents2.exponents.begin(), v.begin(), std::minus<int>());
00073                         return DynamicExponents<UnitSystem>(v);
00074                 };
00075 
00076 
00077 
00078                 template<class UnitSystem>
00079                 inline bool operator==(DynamicExponents<UnitSystem> a, DynamicExponents<UnitSystem> b) {
00080                         assert(a.exponents.size() == UnitSystem::number_baseunits);
00081                         assert(b.exponents.size() == UnitSystem::number_baseunits);
00082          if (a.exponents.size()!=b.exponents.size()) return false;
00083          return std::equal(a.exponents.begin(), a.exponents.end(), b.exponents.begin());
00084                 };
00085 
00086 
00087 
00088                 template<class UnitSystem>
00089                 inline bool operator!=(DynamicExponents<UnitSystem> a, DynamicExponents<UnitSystem> b) {
00090          return !operator==(a, b);
00091                 };
00092 
00093         }; // namespace detail
00094 
00095 }; // namespace physical
00096 
00097 
00098 #endif

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