VariableReference.hpp

Go to the documentation of this file.
00001 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00002 //
00003 //        This file is part of E-Cell Simulation Environment package
00004 //
00005 //                Copyright (C) 1996-2002 Keio University
00006 //
00007 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //
00009 //
00010 // E-Cell is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 // 
00015 // E-Cell is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 // See the GNU General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU General Public
00021 // License along with E-Cell -- see the file COPYING.
00022 // If not, write to the Free Software Foundation, Inc.,
00023 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 // 
00025 //END_HEADER
00026 //
00027 // written by Koichi Takahashi <shafi@e-cell.org>,
00028 // E-Cell Project.
00029 //
00030 
00031 #ifndef __VARIABLEREFERENCE_HPP
00032 #define __VARIABLEREFERENCE_HPP
00033 
00034 #include "libecs.hpp"
00035 #include "Variable.hpp"
00036 
00037 namespace libecs
00038 {
00039 
00040   /** @addtogroup entities
00041    *@{
00042    */
00043 
00044 
00045   /** @file */
00046 
00047   class VariableReference
00048   {
00049 
00050   public:
00051 
00052     class CoefficientLess
00053     {
00054 
00055     public:
00056 
00057       CoefficientLess()
00058       {
00059         ; // do nothing
00060       }
00061 
00062       bool operator()( VariableReferenceCref aLhs, 
00063                        VariableReferenceCref aRhs ) const
00064       {
00065         return compare( aLhs.getCoefficient(), aRhs.getCoefficient() );
00066       }
00067 
00068       bool operator()( IntegerParam aLhs, 
00069                        VariableReferenceCref aRhs ) const
00070       {
00071         return compare( aLhs, aRhs.getCoefficient() );
00072       }
00073 
00074       bool operator()( VariableReferenceCref aLhs, 
00075                        IntegerParam aRhs ) const
00076       {
00077         return compare( aLhs.getCoefficient(), aRhs );
00078       }
00079 
00080     private:
00081 
00082       static const bool compare( IntegerParam aLhs, IntegerParam aRhs )
00083       {
00084         return std::less<Integer>()( aLhs, aRhs );
00085       }
00086 
00087     };
00088 
00089     class NameLess
00090     {
00091     public:
00092 
00093       NameLess()
00094       {
00095         ; // do nothing
00096       }
00097 
00098       bool operator()( VariableReferenceCref aLhs, 
00099                        VariableReferenceCref aRhs ) const
00100       {
00101         return compare( aLhs.getName(), aRhs.getName() );
00102       }
00103 
00104       bool operator()( StringCref aLhs, 
00105                        VariableReferenceCref aRhs ) const
00106       {
00107         return compare( aLhs, aRhs.getName() );
00108       }
00109 
00110       bool operator()( VariableReferenceCref aLhs, 
00111                        StringCref aRhs ) const
00112       {
00113         return compare( aLhs.getName(), aRhs );
00114       }
00115 
00116 
00117     private:
00118 
00119       static const bool compare( StringCref aLhs, StringCref aRhs )
00120       {
00121         const bool anIsLhsEllipsis( VariableReference::
00122                                     isEllipsisNameString( aLhs ) );
00123         const bool anIsRhsEllipsis( VariableReference::
00124                                     isEllipsisNameString( aRhs ) );
00125 
00126         // both are ellipses, or both are normal names.
00127         if( anIsLhsEllipsis == anIsLhsEllipsis )
00128           {
00129             return std::less<String>()( aLhs, aRhs );
00130           }
00131         else // always sort ellipses last
00132           {
00133             return anIsRhsEllipsis;
00134           }
00135       }
00136 
00137     };
00138 
00139 
00140     // compare coefficients first, and if equal, compare names.
00141     class Less
00142     {
00143     public:
00144 
00145       Less()
00146       {
00147         ; // do nothing
00148       }
00149 
00150       bool operator()( VariableReferenceCref aLhs, 
00151                        VariableReferenceCref aRhs ) const
00152       {
00153         static CoefficientLess aCoefficientLess;
00154         if( aCoefficientLess( aLhs, aRhs ) )
00155           {
00156             return true;
00157           }
00158         else if( aCoefficientLess( aRhs, aLhs ) )
00159           {
00160             return false;
00161           } 
00162         else // lhs.coeff == rhs.coeff
00163           {
00164             return NameLess()( aLhs, aRhs );
00165           }
00166       }
00167 
00168     };
00169 
00170 
00171   public:
00172 
00173     VariableReference()
00174       :
00175       theVariablePtr( NULLPTR ),
00176       theCoefficient( 0 ),
00177       theIsAccessor( true )
00178     {
00179       ; // do nothing
00180     }
00181 
00182     VariableReference( StringCref aName, 
00183                        VariablePtr aVariablePtr, 
00184                        IntegerParam aCoefficient,
00185                        const bool anIsAccessor = true )  
00186       : 
00187       theName( aName ),
00188       theVariablePtr( aVariablePtr ), 
00189       theCoefficient( aCoefficient ),
00190       theIsAccessor( anIsAccessor )
00191     {
00192       ; // do nothing
00193     }
00194 
00195     ~VariableReference() {}
00196 
00197     void setName( StringCref aName )
00198     {
00199       theName = aName;
00200     }
00201 
00202 
00203     // can there be unnamed VariableReferences?
00204     const String getName() const 
00205     { 
00206       return theName; 
00207     }
00208 
00209     void setVariable( VariablePtr const aVariablePtr )
00210     {
00211       theVariablePtr = aVariablePtr;
00212     }
00213 
00214     const VariablePtr getVariable() const 
00215     { 
00216       return theVariablePtr; 
00217     }
00218 
00219     void setCoefficient( IntegerParam aCoefficient )
00220     {
00221       theCoefficient = aCoefficient;
00222     }
00223 
00224     const Integer getCoefficient() const 
00225     { 
00226       return theCoefficient; 
00227     }
00228 
00229     const bool isMutator() const
00230     {
00231       if( theCoefficient == 0 )
00232         {
00233           return false;
00234         }
00235       else
00236         {
00237           return true;
00238         }
00239     }
00240 
00241     void setIsAccessor( const bool anIsAccessor )
00242     {
00243       theIsAccessor = anIsAccessor;
00244     }
00245 
00246     const bool isAccessor() const
00247     {
00248       return theIsAccessor;
00249     }
00250 
00251     void setValue( RealParam aValue ) const
00252     {
00253       theVariablePtr->setValue( aValue );
00254     }
00255 
00256     const Real getValue() const
00257     {
00258       return theVariablePtr->getValue();
00259     }
00260 
00261     /**
00262        Add a value to the variable according to the coefficient.
00263        
00264        Set a new value to the variable.  
00265        The new value is: old_value + ( aValue * theCoeffiencnt ).
00266 
00267        @param aValue a Real value to be added.
00268     */
00269 
00270     void addValue( RealParam aValue ) const
00271     {
00272       theVariablePtr->addValue( aValue * theCoefficient );
00273     }
00274 
00275     const Real getMolarConc() const
00276     {
00277       return theVariablePtr->getMolarConc();
00278     }
00279 
00280     const Real getNumberConc() const
00281     {
00282       return theVariablePtr->getNumberConc();
00283     }
00284 
00285     const Real getVelocity() const
00286     {
00287       return theVariablePtr->getVelocity();
00288     }
00289 
00290     const bool isFixed() const
00291     {
00292       return theVariablePtr->isFixed();
00293     }
00294 
00295     void setFixed( const bool aValue ) const
00296     {
00297       theVariablePtr->setFixed( aValue );
00298     }
00299 
00300     SystemPtr getSuperSystem() const
00301     {
00302       return theVariablePtr->getSuperSystem();
00303     }
00304 
00305     const bool isEllipsisName() const
00306     {
00307       return isEllipsisNameString( theName );
00308     }
00309 
00310     const Integer getEllipsisNumber() const;
00311 
00312     const bool isDefaultName() const
00313     {
00314       return isDefaultNameString( theName );
00315     }
00316 
00317     bool operator==( VariableReferenceCref rhs ) const
00318     {
00319       if( theName        == rhs.theName && 
00320           theVariablePtr == rhs.theVariablePtr &&
00321           theCoefficient == rhs.theCoefficient &&
00322           theIsAccessor  == rhs.theIsAccessor )
00323         {
00324           return true;
00325         }
00326       else
00327         {
00328           return false;
00329         }
00330     }
00331 
00332 
00333     static const bool isEllipsisNameString( StringCref aname );
00334     static const bool isDefaultNameString( StringCref aname );
00335 
00336 
00337   public:
00338 
00339     static const String ELLIPSIS_PREFIX;
00340     static const String DEFAULT_NAME;
00341 
00342   private:
00343 
00344     String      theName;
00345     VariablePtr theVariablePtr;
00346     Integer     theCoefficient;
00347     bool        theIsAccessor;
00348 
00349   };
00350 
00351   //@}
00352 
00353 } // namespace libecs
00354 
00355 
00356 #endif /* __VARIABLEREFERENCE_HPP */
00357 

Generated on Fri Aug 31 18:42:39 2007 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.5.3