00001 ////////////////////////////////////////////////////////////////////////// 00002 // This material is provided "as is", with absolutely no warranty 00003 // expressed or implied. Any use is at your own risk. 00004 // 00005 // Permission to use or copy this software for any purpose is hereby 00006 // granted without fee, provided the above notices are retained on all 00007 // copies. Permission to modify the code and to distribute modified code 00008 // is granted, provided the above notices are retained, and a notice that 00009 // the code was modified is included with the above copyright notice. 00010 ////////////////////////////////////////////////////////////////////////// 00011 /** @file 00012 @brief Interface of the miscellaneous classes. 00013 00014 This file contains interface of the omni::misc::Timer class. 00015 00016 @author Sergey Polichnoy 00017 */ 00018 #ifndef __OMNI_MISC_HPP_ 00019 #define __OMNI_MISC_HPP_ 00020 00021 #include <omni/defs.hpp> 00022 00023 namespace omni 00024 { 00025 namespace misc 00026 { 00027 00028 ////////////////////////////////////////////////////////////////////////// 00029 /** @class Timer 00030 @brief %Timer object. 00031 00032 The Timer class is used for keeping time intervals. 00033 00034 It may be used to perform some periodic actions. 00035 For example to print simulation statistics each second: 00036 00037 @code 00038 Timer t(1000); 00039 00040 while (1) 00041 { 00042 // simulation... 00043 00044 if (t.elapsed()) 00045 { 00046 // print statistics... 00047 } 00048 } 00049 @endcode 00050 00051 One timer's tick is equal to one millisecond. 00052 00053 To change timer's interval the following code can be used: 00054 00055 @code 00056 Timer t; // default interval - 1 second 00057 t = Timer(2000); // 2 seconds 00058 t = Timer(500); // 1/2 second 00059 @endcode 00060 */ 00061 class Timer { 00062 public: 00063 typedef long tick_type; ///< @brief Tick type. 00064 00065 public: 00066 Timer(); 00067 explicit Timer(tick_type ms); 00068 00069 public: 00070 tick_type interval() const; 00071 bool elapsed() const; 00072 void force(); 00073 00074 private: 00075 mutable tick_type m_ref_point; 00076 tick_type m_interval; 00077 }; 00078 00079 } // misc namespace 00080 } // omni namespace 00081 00082 #endif // __OMNI_MISC_HPP_