time.h

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #ifndef SAMPA_TIME_H
00020 #define SAMPA_TIME_H
00021 
00022 #include <sampa/core/persistence.h>
00023 
00024 namespace Sampa {
00025 
00026 struct SAMPA_PERSISTENT_CLASS(Time) {
00027 public:
00028   enum Unit {  
00029     FS,   
00030     PS,   
00031     NS,   
00032     US,   
00033     MS,   
00034     SEC   
00035   };
00036 
00037   // constructors
00038   Time( DefaultCtrTag );
00039   Time( double, Unit );
00040   Time( double, bool scale );
00041   Time( unsigned long long, bool scale );
00042   Time( const Time& );
00043 
00044   // assignment operator
00045   Time& operator = ( const Time& );
00046 
00047   // conversion functions
00048   unsigned long long value() const;     
00049   double to_double() const;             
00050   unsigned long long to_default_time_unit() const;
00051  
00052   const std::string to_string() const;
00053 
00054   // relational operators
00055   bool operator == ( const Time& ) const;
00056   bool operator != ( const Time& ) const;
00057   bool operator <  ( const Time& ) const;
00058   bool operator <= ( const Time& ) const;
00059   bool operator >  ( const Time& ) const;
00060   bool operator >= ( const Time& ) const;
00061 
00062   // arithmetic operators
00063   Time& operator += ( const Time& );
00064   Time& operator -= ( const Time& );
00065   friend const Time operator + ( const Time&, const Time& );
00066   friend const Time operator - ( const Time&, const Time& );
00067   Time& operator *= ( double );
00068   Time& operator /= ( double );
00069   friend const Time operator * ( const Time&, double );
00070   friend const Time operator * ( double, const Time& );
00071   friend const Time operator / ( const Time&, double );
00072   friend double   operator / ( const Time&, const Time& );
00073 
00074   struct SAMPA_PERSISTENT_CLASS(Params) {
00075     unsigned long long m_default_time_unit; // in time resolution
00076     double             m_time_resolution; // in femto seconds
00077     bool               m_time_resolution_specified;
00078     bool               m_time_resolution_fixed;
00079     bool               m_default_time_unit_specified;
00080     Params(int);
00081     ~Params();
00082     SAMPA_PERSISTENT(Params);  
00083   };
00084 
00085   static Time ZERO;  
00086   static void set_params(Params*);
00087 private:
00088   static Params*     M_params;
00089   unsigned long long m_value;
00090 public:
00091   SAMPA_PERSISTENT(Time);
00092 };
00093 
00094 extern void set_time_resolution( double, Time::Unit );
00095 extern Time get_time_resolution();
00096 
00097 extern void set_default_time_unit( double, Time::Unit );
00098 extern Time get_default_time_unit();
00099 
00100 inline
00101 Time::Time( DefaultCtrTag )
00102 : m_value( 0 )
00103 {}
00104 
00105 inline
00106 Time::Time( const Time& t )
00107 : m_value( t.m_value )
00108 {}
00109 
00110 // assignment operator
00111 
00112 inline
00113 Time&
00114 Time::operator = ( const Time& t )
00115 {
00116   m_value = t.m_value;
00117   return *this;
00118 }
00119 
00120 
00121 // conversion functions
00122 
00123 inline
00124 unsigned long long 
00125 Time::value() const  // relative to the time resolution
00126 {
00127   return m_value;
00128 }
00129 
00130 inline
00131 double
00132 Time::to_double() const  // relative to the time resolution
00133 {
00134   return m_value;
00135 }
00136 
00137 inline
00138 unsigned long long
00139 Time::to_default_time_unit() const
00140 {
00141   return (  m_value / M_params->m_default_time_unit );
00142 }
00143 
00144 // relational operators
00145 
00146 inline
00147 bool
00148 Time::operator == ( const Time& t ) const
00149 {
00150   return ( m_value == t.m_value );
00151 }
00152 
00153 inline
00154 bool
00155 Time::operator != ( const Time& t ) const
00156 {
00157   return ( m_value != t.m_value );
00158 }
00159 
00160 inline
00161 bool
00162 Time::operator < ( const Time& t ) const
00163 {
00164   return ( m_value < t.m_value );
00165 }
00166 
00167 inline
00168 bool
00169 Time::operator <= ( const Time& t ) const
00170 {
00171   return ( m_value <= t.m_value );
00172 }
00173 
00174 inline
00175 bool
00176 Time::operator > ( const Time& t ) const
00177 {
00178   return ( m_value > t.m_value );
00179 }
00180 
00181 inline
00182 bool
00183 Time::operator >= ( const Time& t ) const
00184 {
00185   return ( m_value >= t.m_value );
00186 }
00187 
00188 
00189 // arithmetic operators
00190 inline
00191 Time&
00192 Time::operator += ( const Time& t )
00193 {
00194   m_value += t.m_value;
00195   return *this;
00196 }
00197 
00198 inline
00199 Time&
00200 Time::operator -= ( const Time& t )
00201 {
00202   m_value -= t.m_value;
00203   return *this;
00204 }
00205 
00206 
00207 inline
00208 const Time
00209 operator + ( const Time& t1, const Time& t2 )
00210 {
00211   return Time( t1 ) += t2;
00212 }
00213 
00214 inline
00215 const Time
00216 operator - ( const Time& t1, const Time& t2 )
00217 {
00218   return Time( t1 ) -= t2;
00219 }
00220 
00221 inline
00222 Time&
00223 Time::operator *= ( double d )
00224 {
00225   // linux bug workaround; don't change next two lines
00226   volatile double tmp = d * double(m_value) + 0.5;
00227   m_value = static_cast<unsigned long long>(tmp);
00228   return *this;
00229 }
00230 
00231 inline
00232 Time&
00233 Time::operator /= ( double d )
00234 {
00235   // linux bug workaround; don't change next two lines
00236   volatile double tmp = double(m_value) / d + 0.5;
00237   m_value = static_cast<unsigned long long>(tmp);
00238   return *this;
00239 }
00240 
00241 inline
00242 const Time
00243 operator * ( const Time& t, double d )
00244 {
00245   Time tmp( t );
00246   return tmp *= d;
00247 }
00248 
00249 inline
00250 const Time
00251 operator * ( double d, const Time& t )
00252 {
00253   Time tmp( t );
00254   return tmp *= d;
00255 }
00256 
00257 inline
00258 const Time
00259 operator / ( const Time& t, double d )
00260 {
00261   Time tmp( t );
00262   return tmp /= d;
00263 }
00264 
00265 inline
00266 double
00267 operator / ( const Time& t1, const Time& t2 )
00268 {
00269   return ( t1.to_double() / t2.to_double() );
00270 }
00271 
00272 } // namespace Sampa
00273 
00274 #endif

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3