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

ReferencedVariable.h

Go to the documentation of this file.
00001 
00005 /* Copyright (C) 2004 - 2005, 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 quantity {
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 BT, class DIM, class UL, class DU, class SU, class ST>
00051   class ReferencedVariable<Quantity<DIM, BT, UL, DU, ST>, SU>
00052                     : public Variable<Quantity<DIM, BT, UL, DU, ST>, SU>
00053 
00054     {
00055       private:
00057         typedef Variable<Quantity<DIM, BT, UL, DU, ST>, SU> V;
00058 
00060         typedef Quantity<DIM, BT, UL, DU, ST> Q;
00061 
00063         ST reference_value;
00064 
00065       public:
00067 
00070         ReferencedVariable (void) 
00071           : V (ST(0.0)), reference_value (ST(0.0)) {}
00072 
00074 
00077         ReferencedVariable (const ST new_value) : V (new_value) {}
00078 
00080 
00083         ReferencedVariable (const ST new_value, const ST new_reference)
00084           : V (new_value - new_reference), 
00085                                       reference_value (new_reference) {}
00086 
00088 
00094         template<class NU>
00095           ReferencedVariable 
00096                 (const ST new_value, const ST new_reference, const NU &)
00097             {
00098               variable_value = Reverse<typename V::Unit>::VAL
00099                 (Standardize<typename
00100                   CheckAgainstAllUnits<NU, UL>
00101                                ::RET>::VAL (new_value - new_reference));
00102               reference_value 
00103                 = Reverse<typename V::Unit>::VAL (Standardize<NU>::VAL
00104                                                        (new_reference));
00105             }
00106 
00108 
00118        template<template<class, class> class QR, class BTR, class DIMR,
00119                            class ULR, class DUR, class SUR, class STR>
00120          ReferencedVariable (const ST new_value, 
00121            const QR<Quantity<DIMR, BTR, ULR, DUR, STR>, SUR> 
00122                                                         & new_reference)
00123            {
00124              typedef 
00125                QR<Quantity<DIMR, BTR, ULR, DUR, STR>, SUR> Q;
00126              reference_value 
00127                = Reverse<typename V::Unit>::VAL 
00128                  (Standardize<typename CheckAgainstAllUnits<typename 
00129                    CheckSecondDimension<ReferencedVariable, Q>
00130                                                             ::RET::Unit,
00131                      typename 
00132                        Loki::TL::Append<UL, unit::NonPrefixable<BTR, 
00133                          unit::GenericUnit> >::Result>::RET>::VAL
00134                                               (new_reference.value ()));
00135              variable_value = new_value - reference_value;
00136            }
00137 
00138 // other constructors?
00139 // e.g., with two units NVU, NRU for variable and reference values
00140 
00142 
00145         ReferencedVariable (const ReferencedVariable &new_variable)
00146           : V (new_variable.variable_value), 
00147                           reference_value (new_variable.reference_value)
00148             {
00149               namestring = new_variable.namestring;
00150               symbolstring = new_variable.symbolstring;
00151             };
00152 
00153 // conversion ?
00154 
00156 
00162         void reference (const ST new_reference)
00163           {variable_value 
00164             = variable_value + reference_value - new_reference; 
00165                                         reference_value = new_reference;
00166           }
00167 
00169 
00175         template<class NU>
00176           void reference (const ST new_reference, const NU &)
00177             {variable_value = variable_value + reference_value;
00178              reference_value = Reverse<typename V::Unit>::VAL
00179                (Standardize<typename
00180                  CheckAgainstAllUnits<NU, UL>::RET>::VAL 
00181                                                        (new_reference));
00182              variable_value -= reference_value;
00183             }
00184 
00186 
00189         ST reference (void) {return reference_value;}
00190 
00192 
00196         template<class NU>
00197           ST reference (const NU &) 
00198             {return Reverse<typename
00199               CheckAgainstAllUnits<NU, UL>::RET>::VAL 
00200                 (Standardize<typename V::Unit>::VAL (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 BT1, class DIM1,
00209                              class UL1, class DU1, class SU1, class ST1>
00210             ReferencedVariable & operator=
00211               (const Q1<Quantity<DIM1, BT1, UL1, DU1, ST1>, SU1> 
00212                                                           &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<DIM1, BT1, UL1, DU1, ST1>, SU1> Q;
00220                  variable_value = Reverse<typename V::Unit>::VAL
00221                    (Standardize<typename CheckAgainstAllUnits<typename
00222                        CheckSecondDimension<ReferencedVariable, Q>
00223                                                             ::RET::Unit,
00224                          typename Loki::TL::Append<UL,
00225                              unit::NonPrefixable<BT1, 
00226                                unit::GenericUnit> >::Result>::RET>::VAL
00227                                  (new_variable.value ())) 
00228                                                       - reference_value;
00229                }
00230                return *this;
00231              }
00232 
00234 /*  assignment is to an already existing referenced variable, thus
00235     the value which is assigned to the existing object is referenced
00236     after the assignment is done to the reference value of the object
00237     assigned to; other explanations, see Variable.h
00238 */
00239         ReferencedVariable & operator=
00240                                 (const ReferencedVariable &new_variable)
00241         {
00242           if (this != &new_variable)
00243             {variable_value = 
00244                             new_variable.value () - reference_value;
00245             }
00246           return *this;
00247         }
00248 
00250 
00253         ST value (void) const 
00254                               {return variable_value + reference_value;}
00255 
00257 
00260         ST refvalue (void) const {return variable_value;}
00261 
00263 
00268         template<class NU>
00269           ST value (const NU &) const
00270             {return 
00271               Reverse<typename CheckAgainstAllUnits<NU, UL>::RET>::VAL
00272                  (Standardize<typename V::Unit>::VAL (variable_value));}
00273 
00275 
00277         const std::string unitsymbol (void) const 
00278                                                  {return SU::Symbol ();}
00279 
00281 
00283         static std::string Unitsymbol (void) {return SU::Symbol ();}
00284 
00286 
00288         const std::string unitname (void) const {return SU::Name ();}
00289 
00291 
00293         static std::string Unitname (void) {return SU::Name ();}
00294 
00295     };
00296 
00297 }
00298 
00299 #endif /* _ReferencedVariable_h */

Generated on Sun Jan 15 14:05:20 2006 for Quantity by doxygen 1.3.6