Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

UnitSystem.C

00001 #include <citrus/config.h>
00002 
00003 #include <citrus/UnitSystem.H>
00004 #include <citrus/DimensionGroup.H>
00005 
00006 UnitSystem* UnitSystem::theUnitSystems = 0 ;
00007 
00008 UnitSystem::UnitSystem()
00009 {
00010   m_name = 0 ;
00011   m_abbr = 0 ;
00012   m_desc = 0 ;
00013   m_next = 0 ;
00014 
00015   if ( theUnitSystems ) {
00016     UnitSystem* us = theUnitSystems ;
00017     while ( us->m_next )
00018       us = us->m_next ;
00019     us->m_next = this ;
00020   } else {
00021     theUnitSystems = this ;
00022   }
00023 }
00024 
00025 UnitSystem::~UnitSystem()
00026 {
00027   if ( m_name )
00028     delete [] m_name ;
00029   if ( m_abbr )
00030     delete [] m_abbr ;
00031   if ( m_desc )
00032     delete [] m_desc ;
00033 
00034   if ( theUnitSystems == this ) {
00035     theUnitSystems = m_next ;
00036   } else {
00037     UnitSystem* us = theUnitSystems ;
00038     while ( us->m_next != this )
00039       us = us->m_next ;
00040     us->m_next = m_next ;
00041   }
00042 }
00043 
00044 void UnitSystem::name( const char* name )
00045 {
00046   if ( !name )
00047     return ;
00048 
00049   if ( m_name )
00050     delete [] m_name ;
00051   m_name = new char[ strlen( name ) + 1 ] ;
00052   strcpy( m_name, name ) ;
00053 }
00054 
00055 const char* UnitSystem::name() const
00056 {
00057   return m_name ;
00058 }
00059 
00060 void UnitSystem::abbr( const char* abbr )
00061 {
00062   if ( !abbr )
00063     return ;
00064 
00065   if ( m_abbr )
00066     delete [] m_abbr ;
00067   m_abbr = new char[ strlen( abbr ) + 1 ] ;
00068   strcpy( m_abbr, abbr ) ;
00069 }
00070 
00071 
00072 const char* UnitSystem::abbr() const
00073 {
00074   return m_abbr ;
00075 }
00076 
00077 
00078 void UnitSystem::description( const char* desc )
00079 {
00080   if ( !desc )
00081     return ;
00082 
00083   if ( m_desc )
00084     delete [] m_desc ;
00085   m_desc = new char[ strlen( desc ) + 1 ] ;
00086   strcpy( m_desc, desc ) ;
00087 }
00088 
00089 const char* UnitSystem::description() const
00090 {
00091   return m_desc ;
00092 }
00093 
00094 bool UnitSystem::addSpec( const DimensionGroup* dg, const char* spec )
00095 {
00096   if ( ! dg )
00097     return false ;
00098 
00099   Quantity qty( spec ) ;
00100   if ( qty.group() == dg )
00101     m_specs[ dg ] = qty ;
00102   else
00103     return false ;
00104 
00105   return true ;
00106 }
00107 
00108 bool UnitSystem::getSpec( Quantity& result, const DimensionGroup* dg )
00109 {
00110   /* FIXME! This should eventually try harder: when a DimensionGroup* is 
00111    * not in m_specs, we should look at the DimensionPowers of dg and
00112    * see if each is represented in the UnitSystem (a well-defined system
00113    * will have a unit for each base dimension, so this should always be
00114    * satisfied). Then, we can compose a Quantity from those dimensions.
00115    */
00116   result = m_specs[ dg ] ;
00117   if ( result.group() != dg )
00118     return false ;
00119 
00120   return true ;
00121 }
00122 
00133 bool UnitSystem::convert( Quantity& result, const Quantity& source )
00134 {
00135   if ( ! getSpec( result, source.group() ) )
00136     return false ;
00137 
00138   ::convert( &result, &source ) ;
00139   return true ;
00140 }
00141 
Citrus C++ Reference Manual  20010520