Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

ReferencedVariable.h

Go to the documentation of this file.
00001 
00005 /* Copyright (C) 2004, 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 _ReferencedVariable_h
00025 #define _ReferencedVariable_h
00026 
00027 #include "Quantity/Quantity.h"
00028 #include "Quantity/Generic.h"
00029 
00030 namespace Quantities {
00031 
00033 //
00034 //  the ReferencedVariable classes
00035 //
00037 
00039 
00043 template<class PQ, class SU = typename PQ::DefaultUnit::Unit>
00044   class ReferencedVariable;
00045 
00047 
00050 template<class GT, class Head, class Tail, class DU, class SU, class ST>
00051   class ReferencedVariable<Quantity<GT, Loki::Typelist<Head, Tail>, 
00052                                                             DU, ST>, SU>
00053     : public 
00054           Variable<Quantity<GT, Loki::Typelist<Head, Tail>, DU, ST>, SU>
00055 
00056     {
00057       private:
00059         typedef 
00060           Variable<Quantity<GT, Loki::Typelist<Head, Tail>, DU, ST>, 
00061                                                                   SU> V;
00062 
00064         typedef Quantity<GT, Loki::Typelist<Head, Tail>, DU, ST> Q;
00065 
00067         ST reference_value;
00068 
00069       public:
00071 
00074         ReferencedVariable (void) 
00075           : V (ST(0.0)), reference_value (ST(0.0)) {}
00076 
00078 
00081         ReferencedVariable (const ST new_value) : V (new_value) {}
00082 
00084 
00087         ReferencedVariable (const ST new_value, const ST new_reference)
00088           : V (new_value - new_reference), 
00089                                       reference_value (new_reference) {}
00090 
00092 
00098         template<class NU>
00099           ReferencedVariable 
00100                 (const ST new_value, const ST new_reference, const NU &)
00101             {
00102               variable_value = V::Unit::Reverse 
00103                 (CheckAgainstAllUnits<NU, Loki::Typelist<Head, Tail> >
00104                            ::RET::Standard (new_value - new_reference));
00105               reference_value 
00106                       = V::Unit::Reverse (NU::Standard (new_reference));
00107             }
00108 
00110 
00120        template<template<class, class> class QR, class GTR, class HeadR,
00121                            class TailR, class DUR, class SUR, class STR>
00122          ReferencedVariable (const ST new_value, 
00123            const QR<Quantity<GTR, Loki::Typelist<HeadR, TailR>, 
00124                                         DUR, STR>, SUR> & new_reference)
00125            {
00126              typedef 
00127                QR<Quantity<GTR, Loki::Typelist<HeadR, TailR>, DUR, STR>,
00128                                                                  SUR> Q;
00129 cout << "in constructor" << endl;
00130              reference_value 
00131                = V::Unit::Reverse (CheckAgainstAllUnits<typename 
00132                  CheckSecondDimension<ReferencedVariable, Q>::RET::Unit,
00133                    typename Loki::TL::Append<Loki::Typelist<Head, Tail>,
00134                      Units::NonPrefixable<typename Q::Type, 
00135                        Units::GenericUnit, ST> >::Result>::RET::Standard
00136                                           (new_reference.value ()));
00137              variable_value = new_value - reference_value;
00138            }
00139 
00140 // other constructors?
00141 // e.g., with two units NVU, NRU for variable and reference values
00142 
00144 
00147         ReferencedVariable (const ReferencedVariable &new_variable)
00148           : V (new_variable.variable_value), 
00149                           reference_value (new_variable.reference_value)
00150             {
00151               namestring = new_variable.namestring;
00152               symbolstring = new_variable.symbolstring;
00153             };
00154 
00155 // conversion ?
00156 
00158 
00164         void reference (const ST new_reference)
00165           {variable_value 
00166             = variable_value + reference_value - new_reference; 
00167                                         reference_value = new_reference;
00168           }
00169 
00171 
00177         template<class NU>
00178           void reference (const ST new_reference, const NU &)
00179             {variable_value = variable_value + reference_value;
00180              reference_value = V::Unit::Reverse 
00181                (CheckAgainstAllUnits<NU, Loki::Typelist<Head, Tail> >
00182                                        ::RET::Standard (new_reference));
00183              variable_value -= reference_value;
00184             }
00185 
00187 
00190         ST reference (void) {return reference_value;}
00191 
00193 
00197         template<class NU>
00198           ST reference (const NU &) 
00199             {return CheckAgainstAllUnits<NU, Loki::Typelist<Head, Tail>
00200                  >::RET::Reverse (V::Unit::Standard (reference_value));}
00201 
00203 /*  assignment is to an already existing referenced variable, thus
00204     the value which is assigned to the existing object is referenced
00205     after the assignment is done to the reference value of the object
00206     assigned to; other explanations, see Variable.h.
00207 */
00208         template<template<class, class> class Q1, class GT1, 
00209           class Head1, class Tail1, class DU1, class SU1, class ST1>
00210             ReferencedVariable & operator=
00211               (const Q1<Quantity<GT1, Loki::Typelist<Head1, Tail1>, 
00212                                           DU1, ST1>, SU1> &new_variable)
00213              {
00214                void *this_pointer = this;
00215                const void *new_pointer = &new_variable;
00216 
00217                if (this_pointer != new_pointer)
00218                {
00219                  typedef Q1<Quantity<GT1, Loki::Typelist<Head1, Tail1>,
00220                                                       DU1, ST1>, SU1> Q;
00221                  variable_value = V::Unit::Reverse
00222                    (CheckAgainstAllUnits<typename
00223                      CheckSecondDimension<ReferencedVariable, Q>
00224                                                             ::RET::Unit,
00225                        typename Loki::TL::Append<
00226                          Loki::Typelist<Head, Tail>,
00227                            Units::NonPrefixable<typename Q::Type, 
00228                              Units::GenericUnit, ST> >::Result 
00229                                >::RET::Standard
00230                                  (new_variable.value ())) 
00231                                                       - reference_value;
00232                }
00233                return *this;
00234              }
00235 
00237 /*  assignment is to an already existing referenced variable, thus
00238     the value which is assigned to the existing object is referenced
00239     after the assignment is done to the reference value of the object
00240     assigned to; other explanations, see Variable.h
00241 */
00242         ReferencedVariable & operator=
00243                                 (const ReferencedVariable &new_variable)
00244         {
00245           if (this != &new_variable)
00246             {variable_value = 
00247                             new_variable.value () - reference_value;
00248             }
00249           return *this;
00250         }
00251 
00253 
00256         ST value (void) const 
00257                               {return variable_value + reference_value;}
00258 
00260 
00263         ST refvalue (void) const {return variable_value;}
00264 
00266 
00271         template<class NU>
00272           ST value (const NU &) const
00273             {return CheckAgainstAllUnits<NU,
00274               Loki::Typelist<Head, Tail> >::RET::Reverse
00275                                   (V::Unit::Standard (variable_value));}
00276 
00278 
00280         const std::string unitsymbol (void) const 
00281                                                  {return SU::Symbol ();}
00282 
00284 
00286         static std::string Unitsymbol (void) {return SU::Symbol ();}
00287 
00289 
00291         const std::string unitname (void) const {return SU::Name ();}
00292 
00294 
00296         static std::string Unitname (void) {return SU::Name ();}
00297 
00298     };
00299 
00300 }
00301 
00302 #endif /* _ReferencedVariable_h */

Generated on Sun Jan 15 13:58:00 2006 for Quantity by doxygen 1.3.6