UniqueConstant.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 _UniqueConstant_h
00025 #define _UniqueConstant_h
00026 
00027 // Quantity includes
00028 #include "Quantity/Quantity.h"
00029 #include "Quantity/QuantityError.h"
00030 #include "Quantity/Variable.h"
00031 
00032 // Loki includes
00033 #include "loki/Singleton.h"
00034 
00035 // BOOST includes
00036 #include <boost/serialization/base_object.hpp>
00037 
00038 namespace quantity {
00039 
00041 //
00042 //  the UniqueConstant classes
00043 //
00045 
00047 
00053 template<typename BQ, typename DQ = DerivedQuantity<typename BQ::QuantityType,
00054             typename BQ::DU, typename BQ::DDQ> > class UniqueConstant;
00055 
00057 
00066 template<typename QT, typename ST, typename SU, typename DQT>
00067   class UniqueConstant<Quantity<QT, ST>, DerivedQuantity<QT, SU, DQT> > : public Quantity<QT, ST>
00068     {
00069       public:
00071 
00073         typedef typename QuantityTraits<QT>::Dimension DIM;
00074 
00076 
00078       typedef typename QuantityTraits<QT>::UnitType UT;
00079 
00081 
00083       typedef typename QuantityTraits<QT>::UnitList UL;
00084 
00086 
00088       typedef typename QuantityTraits<QT>::DefaultUnit DU;
00089 
00091 
00097         typedef typename unit::CheckUnit<unit::Unit<UT>, SU>::Check StorageUnit;
00098 
00100 
00103         typedef Quantity<QT, ST> BQ; 
00104 
00106 
00108         typedef UniqueConstant<BQ, DerivedQuantity<QT, SU, DQT> > UC;
00109 
00111 
00114         typedef Variable<BQ, DerivedQuantity<QT, SU, DQT> > V;
00115 
00117 
00120         friend class Loki::CreateUsingNew<UC>;
00121 
00122       private:
00124 
00128         static const ST default_value_;
00129 
00131 
00134         ST _value;
00135 
00137 //  constructors for UniqueConstant
00139 
00140       private:
00142 
00149         UniqueConstant (void) : BQ (), _value (default_value_) 
00150           {BQ::name (Name<QT, DQT>::exec ()); BQ::symbol (Symbol<QT, DQT>::exec ());}
00151 
00152       private:
00154 
00157         UniqueConstant (const UniqueConstant &)
00158           : BQ (), _value (default_value_) {}
00159 
00161 //  end constructors for UniqueConstant
00163 
00165 //  destructor for UniqueConstant
00167 
00169 
00171       private:
00172         ~UniqueConstant (void) {}
00173 
00175 //  end destructor for UniqueConstant
00177 
00179 //  other private special member functions for UniqueConstant
00181 
00183 
00185       private:
00186         UniqueConstant & operator= (const UniqueConstant &);
00187 
00189 
00191       private:
00192         void operator& ();
00193 
00195 //  end other private special member functions for UniqueConstant
00197 
00199 //  state reporting member functions for UniqueConstant
00201 
00202       public:
00204 
00206         ST value (void) const {return _value;}
00207 
00209 
00212         ST standard_value (void) const 
00213             {return Standardize<StorageUnit, ST>::VAL (_value);}
00214 
00216 
00222           template<typename NU>
00223             Variable<Quantity<QT, ST>, DerivedQuantity<QT, NU, DQT> > value (const NU &) const
00224              {return Variable<Quantity<QT, ST>, DerivedQuantity<QT, NU, DQT> >
00225                 (Reverse<typename CheckAgainstAllUnits<NU, UL>::RET, ST>::VAL
00226                            (Standardize<StorageUnit, ST>::VAL (_value)));}
00227 
00230 //    The stored value is first standardized using the
00231 //    Standardize () function for storage unit, and then dynamically
00232 //    recalculated into the unit found with findBySymbol. 
00233 //    The allocated object pointed to by unitp is deleted. If symbol is 
00234 //    not found by findBySymbol, a UnitMismatch exception is thrown.
00235 //*/
00236 //        ST value (const std::string &symbol)
00237 //          {
00238 //            ST value;
00239 //            ::unit::Unit<UT> *unitp = BQ::findBySymbol (symbol);
00240 //
00241 //            value = dynamic_reverse<UL>::VAL
00242 //                    (Standardize<StorageUnit, ST>::VAL (_value), *unitp);
00243 //
00244 //            delete (unitp);
00245 //
00246 //            return value;
00247 //          }
00248 
00249     protected:
00251 
00255       void name (const std::string &namestring) {BQ::name (namestring);}
00256 
00258 
00262       void symbol (const std::string &symbolstring) {BQ::symbol (symbolstring);}
00263 
00264     public:
00266 
00268         std::string unitsymbol (void) const {return StorageUnit::Symbol ();}
00269 
00271 
00273         static std::string Unitsymbol (void) {return StorageUnit::Symbol ();}
00274 
00276 
00278         std::string unitname (void) const {return StorageUnit::Name ();}
00279 
00281 
00283         static std::string Unitname (void) {return StorageUnit::Name ();}
00284 
00286 
00288         std::string name (void) const {return BQ::name ();}
00289 
00291 
00293         std::string symbol (void) const {return BQ::symbol ();}
00294 
00296 //  end state reporting member functions for UniqueConstant
00298 
00300 //  arithmetic operators for UniqueConstant
00302 
00304 
00307         typename GenerateVariable<UC, BSUtilities::Rational<1> >::Mult 
00308                                                                operator+ (void) const
00309           {typename GenerateVariable<UC, BSUtilities::Rational<1> >::Mult new_constant (*this);
00310              return new_constant;
00311            }
00312 
00314 
00317         typename GenerateVariable<UC, BSUtilities::Rational<1> >::Mult operator- (void) const
00318            {typename GenerateVariable<UC, BSUtilities::Rational<1> >::Mult new_constant (*this);
00319              return new_constant *= -ST(1.0);
00320            }
00321 
00323 
00325         template <class Q>
00326           V operator+ (const Q &new_variable) const
00327             {V new_object = *this;
00328               return new_object += new_variable;
00329             }
00330 
00332 
00335         template <class Q>
00336           V operator- (const Q &new_variable) const
00337             {V new_object = *this;
00338               return new_object -= new_variable;
00339             }
00340 
00342 
00344         V operator* (const ST factor) const
00345           {V new_object = *this;
00346             return new_object *= factor;
00347           }
00348 
00349 // maybe no longer necessary, see Variable
00351         friend V operator*
00352             (const ST factor, const UC &variable)
00353                          {return V (variable) *= factor;}
00354 
00355 
00357 
00359         V operator/ (const ST factor) const
00360           {V new_object = *this;
00361             return new_object /= factor;
00362           }
00363 
00365 
00370         template<class Q>
00371           typename GenerateVariable<UniqueConstant, Q>::Add operator*
00372                                                        (const Q &factor)
00373             {
00374               return typename 
00375                 GenerateVariable<UniqueConstant, Q>::Add
00376                          (standard_value () * factor.standard_value ());
00377             }
00378 
00380 
00385         template<class Q>
00386           typename GenerateVariable<UniqueConstant, Q>::Sub 
00387                                              operator/ (const Q &factor)
00388             {
00389               return typename 
00390                 GenerateVariable<UniqueConstant, Q>::Sub
00391                          (standard_value () / factor.standard_value ());
00392             }
00393 
00395 
00398         friend 
00399           typename GenerateVariable<UniqueConstant, Loki::NullType>::Inv
00400             operator/ (const ST numerator, const UniqueConstant &factor)
00401           {
00402             return typename 
00403               GenerateVariable<UniqueConstant, Loki::NullType>::Inv
00404                                  (numerator / factor.standard_value ());
00405           }
00406 
00408 //  end arithmetic operators for UniqueConstant
00410 
00412 //  comparison operators for UniqueConstant
00413 //  macro defined in Variable.h
00415 
00416 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator==, ==)
00417 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator!=, !=)
00418 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator>, >)
00419 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator<, <)
00420 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator>=, >=)
00421 QUANTITY_COMPARISON_OPERATORS(UniqueConstant, operator<=, <=)
00422 
00424 //  end comparison operators for UniqueConstant
00426 
00428 
00434         template<long N, long D> 
00435           typename GenerateVariable<UniqueConstant,
00436                                      BSUtilities::Rational<N, D> >::Mult
00437                          pow (const BSUtilities::Rational<N, D> &) const
00438           {return std::pow (standard_value (), 
00439                         static_cast<double>(N)/static_cast<double>(D));}
00440 
00442 
00448         template<int I>
00449           typename GenerateVariable<UniqueConstant,
00450               BSUtilities::Rational<I, long(1)> >::Mult 
00451                             pow (const typename Loki::Int2Type<I>) const
00452           {return std::pow (standard_value (), I);}
00453 
00455 
00461     template<class T>
00462       Dynamic<ST> pow (const T &exp) const
00463                                 {return Dynamic<ST>::pow (*this, exp);}
00464 
00466 
00473         typename
00474           GenerateVariable<UniqueConstant, BSUtilities::Rational<1, 2>
00475                                                >::Mult sqrt (void) const
00476           {
00477             return typename
00478               GenerateVariable<UniqueConstant, BSUtilities::Rational<1, 2>
00479                                  >::Mult (std::sqrt(standard_value ()));
00480           }
00481 
00483 
00486      std::ostream & print_value (std::ostream &os) const
00487         {return os << _value << " " << StorageUnit::Symbol ();}
00488 
00490 
00492       void operator>> (std::string &str) const
00493         {str = BSUtilities::Conversion<UniqueConstant>::to_string 
00494                                                                (*this);}
00496 
00500       std::ostream & operator>> (std::ostream &os) const
00501                    {return this->print (os);}
00502 
00503   private:
00505      friend class boost::serialization::access;
00506 
00508 
00510       template<typename Archive>
00511         void serialize (Archive &ar, const unsigned int )
00512         {
00513           ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(BQ);
00514         }
00515 
00516     };
00517 
00518 }
00519 
00520 #endif /* _UniqueConstant_h */

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