Constant.h

Go to the documentation of this file.
00001 
00005 /* Copyright (C) 2002 - 2009, Bernd Speiser */
00006 /* This file is part of Quantity.
00007 
00008 Quantity is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 Quantity is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017   
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00021 02111-1307, USA.
00022 */
00023 
00024 #ifndef _Constant_h
00025 #define _Constant_h
00026 
00027 // Quantity includes
00028 #include "Quantity/Quantity.h"
00029 #include "Quantity/QuantityError.h"
00030 #include "Quantity/Variable.h"
00031 
00032 // BOOST includes
00033 #include <boost/serialization/base_object.hpp>
00034 
00035 namespace quantity {
00036 
00038 //
00039 //  the Constant classes
00040 //
00042 
00044 
00050 template<class BQ, class DQ = DerivedQuantity<typename BQ::QuantityType,
00051             typename BQ::DU, typename BQ::DDQ> > class Constant;
00052 
00054 
00064 template<typename QT, typename ST, typename SU, typename DQT>
00065   class Constant<Quantity<QT, ST>, DerivedQuantity<QT, SU, DQT> > : public Quantity<QT, ST>
00066     {
00067       public:
00069 
00071         typedef typename QuantityTraits<QT>::Dimension DIM;
00072 
00074 
00076       typedef typename QuantityTraits<QT>::UnitType UT;
00077 
00079 
00081       typedef typename QuantityTraits<QT>::UnitList UL;
00082 
00084 
00086       typedef typename QuantityTraits<QT>::DefaultUnit DU;
00087 
00089 
00095         typedef typename unit::CheckUnit<unit::Unit<UT>, SU>::Check StorageUnit;
00096 
00098 
00101         typedef Quantity<QT, ST> BQ; 
00102 
00104 
00106         typedef Constant<BQ, DerivedQuantity<QT, SU, DQT> > C;
00107 
00109 
00112         typedef Variable<BQ, DerivedQuantity<QT, SU, DQT> > V;
00113 
00114       private:
00116 
00120         ST _value;
00121 
00122 
00124 //  constructors for Constant
00126 
00127       public:
00129 
00135         Constant (void) : BQ (), _value (ST(0)) 
00136           {name (Name<QT, DQT>::exec ()); symbol (Symbol<QT, DQT>::exec ());}
00137 
00139 
00145         Constant (const ST value) : BQ (), _value (value)
00146           {name (Name<QT, DQT>::exec ()); symbol (Symbol<QT, DQT>::exec ());}
00147 
00149 
00157         Constant (const ST &value, const std::string &namestring, const std::string &symbolstring)
00158             : BQ (), _value (value)
00159           {BQ::name (namestring); BQ::symbol (symbolstring);}
00160 
00162 
00168         template<typename NU>
00169           Constant (const ST &value, const NU &)
00170             : BQ (), _value (Reverse<SU, ST>::VAL (Standardize<NU, ST>::VAL (value)))
00171             {name (Name<QT, DQT>::exec ()); symbol(Symbol<QT, DQT>::exec ());}
00172 
00174 
00182         template<typename NU>
00183           Constant (const ST &value, const NU &,
00184                     const std::string &namestring, const std::string &symbolstring)
00185             : BQ (), _value (Reverse<SU, ST>::VAL (Standardize<NU, ST>::VAL (value)))
00186             {BQ::name (namestring); BQ::symbol (symbolstring);}
00187 
00189 
00196         Constant (const ST value, const std::string &symbol) 
00197           : BQ ()
00198             {name (Name<QT, DQT>::exec ()); symbol(Symbol<QT, DQT>::exec ());
00199              ::unit::Unit<UT> *unitp = BQ::findBySymbol (symbol);
00200 
00201              _value = Reverse<StorageUnit, ST>::VAL 
00202                          (dynamic_standardize<UL>::VAL (value, *unitp));
00203 
00204              delete (unitp);
00205             }
00206 
00208 
00215         Constant (const ST new_value, const char *symbol) 
00216           : BQ ()
00217             {name (Name<QT, DQT>::exec ()); symbol(Symbol<QT, DQT>::exec ());
00218 
00219              ::unit::Unit<UT> *unitp = BQ::findBySymbol (std::string (symbol));
00220 
00221              _value = Reverse<StorageUnit, ST>::VAL 
00222                      (dynamic_standardize<UL>::VAL (new_value, *unitp));
00223 
00224              delete (unitp);
00225             }
00226 
00228 //  end constructors for Constant
00230 
00232 //  copy constructors for Constant
00234 
00236 
00241         template<template<typename, typename> class SM, typename SST>
00242           Constant (const SM<Quantity<QT, SST>, DQT> &quantity)
00243             : BQ (), _value (quantity.value ())
00244               {name (quantity.BQ::name ()); symbol (quantity.BQ::symbol ());}
00245 
00247 
00258         template<template<typename, typename> class SM,
00259                                            typename SST, typename SSU, typename SDQT>
00260           Constant (const SM<Quantity<QT, SST>, DerivedQuantity<QT, SSU, SDQT> > &quantity)
00261             : BQ (), _value (Reverse<StorageUnit, ST>::VAL
00262               (Standardize<SSU, SST>::VAL (quantity.value ())))
00263               {name (quantity.Quantity<QT, SST>::name ()); 
00264                   symbol (quantity.Quantity<QT, SST>::symbol ());}
00265 
00266 
00268 
00275         template<template<typename, typename> class SM,
00276                              typename SDIM, typename SST, typename SSU, typename SDQT>
00277           Constant (const SM<Quantity<GenericClass<SDIM>, SST>, 
00278                              DerivedQuantity<GenericClass<SDIM>, SSU, SDQT> > &quantity)
00279             : BQ (), _value (Reverse<StorageUnit, ST>::VAL
00280               (Standardize<typename
00281                  CheckSecondDimension<C,
00282                    SM<Quantity<GenericClass<SDIM>, SST>, 
00283                      DerivedQuantity<GenericClass<SDIM>, SSU, SDQT> > >
00284                                   ::RET::StorageUnit, SST>::VAL (quantity.value ())))
00285               {name (quantity.Quantity<GenericClass<SDIM>, SST>::name ()); 
00286                 symbol (quantity.Quantity<GenericClass<SDIM>, SST>::symbol ());}
00287 
00289 
00300         template<template<typename, typename> class SM,
00301                              typename SQT, typename SST, typename SSU, typename SDQT>
00302           Constant (const SM<Quantity<SQT, SST>, DerivedQuantity<SQT, SSU, SDQT> > &quantity)
00303             : BQ (), _value (Reverse<StorageUnit, ST>::VAL
00304               (Standardize<typename
00305                  CheckSecondDimension<typename BSUtilities::IF<BSUtilities::SameType<QT,
00306                    GenericClass<DIM> >::sameType, C, VariableError<true> >::RET,
00307                      SM<Quantity<SQT, SST>, DerivedQuantity<SQT, SSU, SDQT> > >
00308                        ::RET::StorageUnit, SST>::VAL (quantity.value ())))
00309               {name (quantity.Quantity<SQT, SST>::name ()); 
00310                  symbol (quantity.Quantity<SQT, SST>::symbol ());}
00311 
00313 
00318         Constant (const Dynamic<ST> &dynamic)
00319           {if (dynamic.isCommensurable (*this))
00320              _value = Reverse<StorageUnit, ST>::VAL (dynamic.value ());
00321            else
00322              {throw DimensionMismatch ();}
00323 
00324           }
00325 
00327 //  end copy constructors for Constant
00329 
00331 //  assignment operators for Constant
00333 
00334       private:
00335 
00337 
00340         template<template<typename, typename> class Q1, typename QT1,
00341                           typename ST1, typename SDQT>
00342           Constant & operator= (const Q1<Quantity<QT1, ST1>, SDQT> &);
00343 
00345 
00348         template<typename TQ> Constant & operator= (const TQ&);
00349 
00351 //  end assignment operators for Constant
00353 
00355 //  state reporting member functions for Constant
00357 
00358       public:
00360 
00362         ST value (void) const {return _value;}
00363 
00365 
00368         ST standard_value (void) const 
00369                      {return Standardize<StorageUnit, ST>::VAL (_value);}
00370 
00372 
00378           template<typename NU>
00379             Variable<Quantity<QT, ST>, DerivedQuantity<QT, NU, DQT> > value (const NU &) const
00380              {return Variable<Quantity<QT, ST>, DerivedQuantity<QT, NU, DQT> >
00381                 (Reverse<typename CheckAgainstAllUnits<NU, UL>::RET, ST>::VAL
00382                            (Standardize<StorageUnit, ST>::VAL (_value)));}
00383 
00386 //    The stored value is first standardized using the
00387 //    Standardize () function for the storage unit, and then dynamically
00388 //    recalculated into the unit found with findBySymbol. The allocated 
00389 //    object pointed to by unitp is deleted. If symbol is not found by
00390 //    findBySymbol, a UnitMismatch exception is thrown.
00391 //*/
00392 //        ST value (const std::string &symbol)
00393 //          {
00394 //            ST value;
00395 //            ::unit::Unit<UT> *unitp = BQ::findBySymbol (symbol);
00396 //
00397 //            value = dynamic_reverse<UL>::VAL
00398 //                    (Standardize<StorageUnit, ST>::VAL (_value), *unitp);
00399 //
00400 //            delete (unitp);
00401 //
00402 //            return value;
00403 //          }
00404 
00406 
00408         std::string unitsymbol (void) const {return StorageUnit::Symbol ();}
00409 
00411 
00413         static std::string Unitsymbol (void) {return StorageUnit::Symbol ();}
00414 
00416 
00418         std::string unitname (void) const {return StorageUnit::Name ();}
00419 
00421 
00423         static std::string Unitname (void) {return StorageUnit::Name ();}
00424 
00426 //  end state reporting member functions for Constant
00428 
00430 //  arithmetic operators for Constant
00432 
00434 
00437         typename GenerateVariable<C, BSUtilities::Rational<1> >::Mult
00438                                                      operator+ (void) const
00439            {typename GenerateVariable<C, BSUtilities::Rational<1> >::Mult
00440                                                         new_constant (*this);
00441              return new_constant;
00442            }
00443 
00445 
00448         typename GenerateVariable<C, BSUtilities::Rational<1> >::Mult
00449                                                     operator- (void) const
00450            {typename GenerateVariable<C, BSUtilities::Rational<1> >::Mult 
00451                                                           new_constant (*this);
00452              return new_constant *= -ST(1.0);
00453            }
00454 
00456 
00459         template <class Q>
00460           V operator+ (const Q &new_variable) const
00461             {V new_object = *this;
00462               return (new_object + new_variable);
00463             }
00464 
00466 
00469         template <class Q>
00470           V operator- (const Q &new_variable) const
00471             {V new_object = *this;
00472               return new_object -= new_variable;
00473             }
00474 
00476 
00478         V operator* (const ST factor) const
00479           {V new_object = *this;
00480             return new_object *= factor;
00481           }
00482 
00483 
00485           friend V operator*
00486             (const ST factor, const C &variable)
00487                          {return V (variable) *= factor;}
00488 
00489 
00491 
00493         V operator/ (const ST factor) const
00494           {V new_object = *this;
00495             return new_object /= factor;
00496           }
00497 
00499 
00504         template<class Q>
00505           typename GenerateVariable<Constant, Q>::Add operator*
00506                                                        (const Q &factor)
00507             {
00508               return typename GenerateVariable<Constant, Q>::Add
00509                          (standard_value () * factor.standard_value ());
00510             }
00511 
00513 
00518         template<class Q>
00519           typename GenerateVariable<Constant, Q>::Sub operator/
00520                                                        (const Q &factor)
00521             {
00522               return typename GenerateVariable<Constant, Q>::Sub
00523                          (standard_value () / factor.standard_value ());
00524             }
00525 
00527 
00530         friend
00531           typename GenerateVariable<Constant, Loki::NullType>::Inv 
00532                   operator/ (const ST numerator, const Constant &factor)
00533           {
00534             return typename 
00535               GenerateVariable<Constant, Loki::NullType>::Inv
00536                                  (numerator / factor.standard_value ());
00537           }
00538 
00540 //  end arithmetic operators for Constant
00542 
00544 //  comparison operators for Constant
00545 //  macro defined in Variable.h
00547 
00548 QUANTITY_COMPARISON_OPERATORS(Constant, operator==, ==)
00549 QUANTITY_COMPARISON_OPERATORS(Constant, operator!=, !=)
00550 QUANTITY_COMPARISON_OPERATORS(Constant, operator>, >)
00551 QUANTITY_COMPARISON_OPERATORS(Constant, operator<, <)
00552 QUANTITY_COMPARISON_OPERATORS(Constant, operator>=, >=)
00553 QUANTITY_COMPARISON_OPERATORS(Constant, operator<=, <=)
00554 
00556 //  end comparison operators for Constant
00558 
00560 
00567         template<long N, long D>
00568           typename GenerateVariable<Constant,
00569                                      BSUtilities::Rational<N, D> >::Mult
00570                          pow (const BSUtilities::Rational<N, D> &) const
00571           {return std::pow (standard_value (), 
00572                         static_cast<double>(N)/static_cast<double>(D));}
00573 
00575 
00582         template<int I>
00583           typename GenerateVariable<Constant,
00584               BSUtilities::Rational<I, long(1)> >::Mult
00585                            pow (const typename Loki::Int2Type<I>) const
00586           {return std::pow (standard_value (), I);}
00587 
00589 
00595     template<class T>
00596       Dynamic<ST> pow (const T &exp) const
00597                                 {return Dynamic<ST>::pow (*this, exp);}
00598 
00600 
00607         typename
00608           GenerateVariable<Constant, BSUtilities::Rational<1, 2>
00609                                                >::Mult sqrt (void) const
00610           {
00611             return typename
00612               GenerateVariable<Constant, BSUtilities::Rational<1, 2>
00613                                  >::Mult (std::sqrt(standard_value ()));
00614           }
00615 
00616 
00618 
00621       std::ostream & print_value (std::ostream &os) const
00622         {return os << _value << " " << StorageUnit::Symbol ();}
00623 
00625 
00627       void operator>> (std::string &str) const
00628           {str = BSUtilities::Conversion<Constant>::to_string (*this);}
00629 
00631 
00635       std::ostream & operator>> (std::ostream &os) const
00636                    {return this->print (os);}
00637 
00638   private:
00640       friend class boost::serialization::access;
00641 
00643 
00645       template<typename Archive>
00646         void serialize (Archive &ar, const unsigned int )
00647         {
00648           ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BQ);
00649           ar & BOOST_SERIALIZATION_NVP(_value);
00650         }
00651 
00652     };
00653 
00654 }
00655 
00656 #endif /* _Constant_h */

Generated on Mon Jul 27 15:55:44 2009 for Quantities by  doxygen 1.5.3