00001 // $Id: monotime.cpp 1282 2006-06-09 09:46:49Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 00099 /* 00100 monotime.cpp 00101 Created by Phil: 8th, July 1993 00102 00103 Implements the MonotonicTime class 00104 This class stores samples of the ever-increasing system ticker (the "monotonic clock" in 00105 Archy speak) and stores them. The samples can then be manipulated by member functions. 00106 Where times are passed in and out of these functions they are alwasy given in 00107 milliseconds. 00108 00109 */ 00110 00111 /* 00112 //*/ 00113 00114 #include "camtypes.h" 00115 //#include "monotime.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00116 00117 DECLARE_SOURCE("$Revision: 1282 $"); 00118 00119 wxStopWatch MonotonicTime::s_stopwatch; 00120 00121 /******************************************************************************************** 00122 00123 > static void MonotonicTime::Init() 00124 00125 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00126 Created: 14/02/2006 00127 Inputs: None 00128 Outputs: None 00129 Returns: None 00130 Purpose: MonotonicTime initialiser 00131 Errors: None 00132 00133 ********************************************************************************************/ 00134 00135 void MonotonicTime::Init() 00136 { 00137 s_stopwatch.Start(); 00138 } 00139 00140 00141 00142 00143 /******************************************************************************************** 00144 00145 > MonotonicTime::MonotonicTime() 00146 00147 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00148 Created: 8/7/93 00149 Inputs: None 00150 Outputs: None 00151 Returns: None 00152 Purpose: MonotonicTime class constructor 00153 Create an instance of the time class by taking a sample of MonotonicTime. 00154 Errors: None 00155 00156 00157 ********************************************************************************************/ 00158 /* 00159 Technical notes: 00160 Calls Sample which in turn calls Windows 3.1 GetTickCount. 00161 00162 ********************************************************************************************/ 00163 00164 MonotonicTime::MonotonicTime() 00165 { 00166 Sample(); 00167 } 00168 00169 00170 00171 00172 /******************************************************************************************** 00173 00174 > MonotonicTime::MonotonicTime(MonotonicTime initialiser) 00175 00176 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00177 Created: 8/7/93 00178 Inputs: None 00179 Outputs: None 00180 Returns: None 00181 Purpose: MonotonicTime class constructor 00182 Create an instance of the time class by setting it to the specified time 00183 Errors: None 00184 00185 00186 ********************************************************************************************/ 00187 /* 00188 Technical notes: 00189 00190 ********************************************************************************************/ 00191 00192 MonotonicTime::MonotonicTime(UINT32 initialiser) 00193 { 00194 SampleTime = initialiser; 00195 } 00196 00197 00198 00199 00200 /******************************************************************************************** 00201 00202 > UINT32 MonotonicTime::Sample() 00203 00204 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00205 Created: 8/7/93 00206 Inputs: None 00207 Outputs: None 00208 Returns: The Sampled time 00209 Purpose: Take a sample of "now" in monotonic time 00210 Errors: None 00211 00212 00213 ********************************************************************************************/ 00214 /* 00215 Technical notes: 00216 Calls Windows 3.1 GetTickCount function. 00217 00218 ********************************************************************************************/ 00219 00220 UINT32 MonotonicTime::Sample() 00221 { 00222 #if defined(__WXMSW__) 00223 SampleTime = GetTickCount(); 00224 #elif defined(__WXGTK__) || defined(__WXMAC__) 00225 // SampleTime = count(); 00226 // SampleTime = GetPosixTickCount(); 00227 SampleTime = s_stopwatch.Time(); 00228 #else 00229 #pragma error( "Not impl'ed from this architechure" ); 00230 #endif 00231 00232 return SampleTime; 00233 } 00234 00235 00236 00237 00238 /******************************************************************************************** 00239 00240 > BOOL MonotonicTime::Elapsed(UINT32 interval) 00241 00242 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00243 Created: 8/7/93 00244 Inputs: None 00245 Outputs: None 00246 Returns: Flag saying whether interval has elapsed or not 00247 Purpose: Test whether the given time interval has elapsed since the monotonic time was 00248 last sampled. 00249 Errors: None 00250 00251 00252 ********************************************************************************************/ 00253 /* 00254 Technical notes: 00255 00256 ********************************************************************************************/ 00257 00258 BOOL MonotonicTime::Elapsed(UINT32 interval) 00259 { 00260 #if defined(__WXMSW__) 00261 return ( ( SampleTime + interval ) < GetTickCount() ); 00262 #elif defined(__WXGTK__) || defined(__WXMAC__) 00263 // return( ( SampleTime + ( interval * CLOCKS_PER_SEC / 1000 ) ) < (UINT32)clock() ); 00264 // return ( ( SampleTime + interval ) < GetPosixTickCount() ); 00265 return ((SampleTime +interval) < (UINT32)s_stopwatch.Time()); 00266 #else 00267 #pragma error( "Not impl'ed from this architechure" ); 00268 #endif 00269 } 00270 00271 00272 00273 00274 /******************************************************************************************** 00275 00276 > BOOL MonotonicTime::Elapsed(UINT32 interval, BOOL update) 00277 00278 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00279 Created: 8/7/93 00280 Inputs: None 00281 Outputs: None 00282 Returns: Flag saying whether interval has elapsed or not 00283 Purpose: Test whether the given time interval has elapsed since the monotonic time was 00284 last sampled. If the update flag is set then the sample time is reset to be 00285 the EXACT time when the interval was exceeded. This allows the interval to be 00286 regular and accurate in the long-term even though individual intervals will 00287 be subject to timing and sampling innaccuracies 00288 Errors: None 00289 00290 00291 ********************************************************************************************/ 00292 /* 00293 Technical notes: 00294 00295 ********************************************************************************************/ 00296 00297 BOOL MonotonicTime::Elapsed(UINT32 interval, BOOL update) 00298 { 00299 if ( Elapsed( interval ) ) 00300 { 00301 //#if defined(__WXGTK__) 00302 // interval = interval * CLOCKS_PER_SEC / 1000; 00303 //#endif 00304 00305 if ( update ) 00306 SampleTime += interval; 00307 00308 return TRUE; 00309 } 00310 return FALSE; 00311 } 00312 00313 00314 00315 00316 /******************************************************************************************** 00317 > UINT32 MonotonicTime::operator UINT32() 00318 00319 Author: Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> 00320 Created: 21/12/95 00321 Inputs: None 00322 Outputs: None 00323 Returns: The monotonic time expressed as a UINT32 00324 Purpose: Cast operator that allows comparisons of MonotonicTime objects to be made 00325 Errors: None 00326 ********************************************************************************************/ 00327 MonotonicTime::operator UINT32() 00328 { 00329 return SampleTime; 00330 } 00331 00332 00333 00334 00335 /******************************************************************************************** 00336 > UINT32 MonotonicTime::GetPosixTickCount() const 00337 00338 Author: Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00339 Created: 13/02/2006 00340 Inputs: None 00341 Outputs: None 00342 Returns: The monotonic time extracted from the Posix library expressed as a UINT32 00343 in milliseconds 00344 Purpose: Read high-quality monotonic timer value 00345 Errors: None 00346 ********************************************************************************************/ 00347 00348 //#if defined(__WXGTK__) || defined(__WXMAC__) 00350 //UINT32 MonotonicTime::GetPosixTickCount() const 00351 //{ 00352 // timespec ts; 00353 // clock_gettime(CLOCK_MONOTONIC, &ts); 00354 // return ts.tv_sec*1000+ts.tv_nsec/1000000; 00355 //} 00356 //#endif 00357 00358 00359 00360