C12Adapter Opensource C++ Interface
MTimer Class Reference

Timer with milliseconds resolution. More...

Inheritance diagram for MTimer:

Public Types

typedef Mint64 TimerIntegerType
 
typedef Muint64 TimerUnsignedType
 

Public Member Functions

 MTimer (TimerIntegerType expireInMilliseconds=0)
 Construct the timer and set its event to the given number of milliseconds. More...
 
 MTimer (const MTimer &other)
 Construct a copy of timer with the same timer event time.
 
virtual ~MTimer ()
 Destroy the timer.
 
MTimeroperator= (const MTimer &other)
 Assignment operator that makes the timer moment of the timer match the other timer start.
 
bool IsExpired () const
 Whether the timer moment has happened. More...
 
void ResetTimer ()
 Sets the timer event into the exact moment this call is made. More...
 
virtual unsigned GetEmbeddedSizeof () const
 Timer is an embedded object type, therefore return its size in bytes. More...
 
bool operator== (TimerIntegerType duration) const
 
bool operator!= (TimerIntegerType duration) const
 
bool operator>= (TimerIntegerType duration) const
 
bool operator<= (TimerIntegerType duration) const
 
bool operator> (TimerIntegerType duration) const
 
bool operator< (TimerIntegerType duration) const
 
int GetTimer () const
 
void SetTimer (int)
 
Mint64 GetTimer64 () const
 
void SetTimer64 (Mint64)
 
- Public Member Functions inherited from MObject
virtual ~MObject ()
 Object destructor.
 
virtual const MClassGetClass () const =0
 Get the final class of the object. More...
 
bool IsEmbeddedObject () const
 Tell if the object is of embedded kind. More...
 
SHOW_INTERNAL MVariant Call (const MStdString &name, const MVariant &params)
 Call the object service with parameters, given as variant. More...
 
MVariant Call0 (const MStdString &name)
 Call the object service with no parameters. More...
 
MVariant Call1 (const MStdString &name, const MVariant &p1)
 Call the object service with one parameter. More...
 
MVariant Call2 (const MStdString &name, const MVariant &p1, const MVariant &p2)
 Call the object service with two parameter. More...
 
MVariant Call3 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3)
 Call the object service with three parameter. More...
 
MVariant Call4 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4)
 Call the object service with four parameter. More...
 
MVariant Call5 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5)
 Call the object service with five parameter. More...
 
MVariant Call6 (const MStdString &name, const MVariant &p1, const MVariant &p2, const MVariant &p3, const MVariant &p4, const MVariant &p5, const MVariant &p6)
 Call the object service with six parameter. More...
 
virtual MVariant CallV (const MStdString &name, const MVariant::VariantVector &params)
 Call the object service with parameters, given as variant vector. More...
 
virtual bool IsPropertyPresent (const MStdString &name) const
 Tell if the property with the given name exists.
 
virtual bool IsServicePresent (const MStdString &name) const
 Tell if the service with the given name exists.
 
virtual MVariant GetProperty (const MStdString &name) const
 Get the property value using name of the property. More...
 
virtual void SetProperty (const MStdString &name, const MVariant &value)
 Set the property using name of the property, and value. More...
 
virtual MStdStringVector GetAllPropertyNames () const
 Return the list of publicly available properties, persistent or not. More...
 
virtual MStdStringVector GetAllPersistentPropertyNames () const
 Return the list of persistent properties. More...
 
virtual void SetPersistentPropertiesToDefault ()
 Set the persistent properties of the object to their default values. More...
 
virtual MVariant GetPersistentPropertyDefaultValue (const MStdString &name) const
 Get the default value of persistent property with the name given. More...
 
virtual void SetPersistentPropertyToDefault (const MStdString &name)
 Set the persistent property with the name given to default value. More...
 
virtual const char * GetType () const
 Get the name of the type for the object (could be the same as class name).
 
virtual void SetType (const MStdString &)
 Intentionally, it will set the name of the type for the object, but the service will not allow setting the name to anything other than the current name. More...
 
virtual void Validate ()
 Validate internal structures of the object. More...
 

Static Public Member Functions

static void Sleep (unsigned milliseconds)
 Sleep for the given number of milliseconds. More...
 
static TimerIntegerType SecondsToTimerMilliseconds (TimerIntegerType seconds)
 Convert seconds into milliseconds, respecting the internal representation of the timer. More...
 
static unsigned GetTickCount ()
 
static Muint64 GetTickCount64 ()
 
static int SecondsToMilliseconds (int seconds)
 
- Static Public Member Functions inherited from MObject
static const MClassGetStaticClass ()
 Get the declared class of this particular object. More...
 
static bool IsClassPresent (const MStdString &name)
 Tells if the given class name is available. More...
 

Additional Inherited Members

- Static Public Attributes inherited from MObject
static const MClass s_class
 Class of MObject.
 
- Protected Member Functions inherited from MObject
 MObject ()
 Object constructor, protected as the class is abstract.
 
void DoSetPersistentPropertiesToDefault (const MClass *staticClass)
 Set the persistent properties to their default values for one object provided the class for that object. More...
 

Detailed Description

Timer with milliseconds resolution.

The timer has its timer moment, when its GetTimer call will return zero and when IsExpired will start to return true. When the class is created, and no expire moment is given, the timer expires immediately, and IsExpired starts to return true. This is still useful in case many events need to be traced by the same timer, and for such case, the property GetTimer can be used to look at the time elapsed since timer creation or reset. It is also acceptable and useful to set expire moment into the past, which is achieved by supplying negative times, as it can simplify program logic for cases when a certain event can take place already.

On Windows platform, in order to have a real 64-bit timer, one has to define _WIN32_WINNT to 0x0600 or above (Vista or later OS). When Windows XP has to be supported, 64-bit timers will roll over their 32-bit parts.

Preferred timer interface manipulates with 64-bit data types, while the 32-bit interface is kept for compatibility. The 32-bit interface should not be used for long durations, such as bigger than a week, as it overflows after about 25 days.

Timer is an embedded object, which is not necessary to destroy when it is created from Reflection interface.

Member Typedef Documentation

typedef Mint64 MTimer::TimerIntegerType

Timer integer type, the internal type used for comparison of timer events.

typedef Muint64 MTimer::TimerUnsignedType

Timer unsigned type, the internal type used for absolute values since start of some event in the past.

Constructor & Destructor Documentation

MTimer::MTimer ( TimerIntegerType  expireInMilliseconds = 0)
explicit

Construct the timer and set its event to the given number of milliseconds.

Parameters
expireInMillisecondsDistance in milliseconds between the exact moment of the creation of the object and the desired timer event. Negative value will mean the event has happened already in the past.

Member Function Documentation

virtual unsigned MTimer::GetEmbeddedSizeof ( ) const
virtual

Timer is an embedded object type, therefore return its size in bytes.

Returns
Size of MTimer in bytes.

Reimplemented from MObject.

static unsigned MTimer::GetTickCount ( )
inlinestatic

Get the number of milliseconds elapsed since some unspecified moment.

This is a static method, and the returned value is unrelated to the timer moment of any object. For Windows, the initial moment is the startup time. The 32-bit version of tick count overflows approximately every 49 days, then starts over again.

static Muint64 MTimer::GetTickCount64 ( )
inlinestatic

Get the number of milliseconds elapsed since some unspecified moment.

This is a static method, and the returned value is unrelated to the timer moment of any object. For Windows, the initial moment is the startup time. The 32-bit version of tick count overflows approximately every 49 days, then starts over again.

int MTimer::GetTimer ( ) const
inline

Return the distance in milliseconds between the current time and the timer event.

Negative distance means the timer event is in the future, IsExpired will be false in such case. Positive distance, IsExpired is true, will mean the timer event has happened in the past already.

It is worth mentioning that if the timer value is got right after the timer is assigned, the return value will be close to negated assignment value (milliseconds elapsed since assignment will explain the difference). Setting the timer property moves the zero position of the time scale that is used to get the timer value back.

32-bit integer variant of the interface roles over every 25 days, and therefore, should be used with care.

Returns
integer value, distance between the timer event and current moment.
See also
ResetTimer sets the timer event into the exact time of the call.
IsExpired checks whether the timer event has happened already.
Mint64 MTimer::GetTimer64 ( ) const
inline

Return the distance in milliseconds between the current time and the timer event.

Negative distance means the timer event is in the future, IsExpired will be false in such case. Positive distance, IsExpired is true, will mean the timer event has happened in the past already.

It is worth mentioning that if the timer value is got right after the timer is assigned, the return value will be close to negated assignment value (milliseconds elapsed since assignment will explain the difference). Setting the timer property moves the zero position of the time scale that is used to get the timer value back.

32-bit integer variant of the interface roles over every 25 days, and therefore, should be used with care.

Returns
integer value, distance between the timer event and current moment.
See also
ResetTimer sets the timer event into the exact time of the call.
IsExpired checks whether the timer event has happened already.
bool MTimer::IsExpired ( ) const

Whether the timer moment has happened.

True if the timer moment matches current moment, or it is in the past. When the timer object was created without parameters, or it was reset, this method returns true until the timer is set into a milliseconds moment in the future.

bool MTimer::operator!= ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
bool MTimer::operator< ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
bool MTimer::operator<= ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
bool MTimer::operator== ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
bool MTimer::operator> ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
bool MTimer::operator>= ( TimerIntegerType  duration) const
inline

Time comparison operator that accepts milliseconds.

Parameters
durationMilliseconds elapsed since timer creation or reset.
void MTimer::ResetTimer ( )
inline

Sets the timer event into the exact moment this call is made.

IsExpired will start to return true after this call, and GetTimer will be counting milliseconds elapsed since that moment.

static int MTimer::SecondsToMilliseconds ( int  seconds)
inlinestatic

Convert seconds into milliseconds, where both are integers of 32-bit size.

A very large value of seconds might result in the number of milliseconds overflowing the int size. When the number of milliseconds is larger than what fits into int, the returned number of milliseconds is the maximum 32-bit positive signed number (and it will be smaller than the seconds requested multiplied by 1000). In either case, such a large value effectively represents eternity.

Parameters
secondsSeconds to convert.
Returns
Result number of milliseconds. To prevent overflow, if seconds multiplied by 1000 is larger than the maximum int value (0x7FFFFFFF), the returned value is 0x7FFFFFFF milliseconds.
static TimerIntegerType MTimer::SecondsToTimerMilliseconds ( TimerIntegerType  seconds)
inlinestatic

Convert seconds into milliseconds, respecting the internal representation of the timer.

A very large value of seconds might result in the number of milliseconds overflowing the TimerIntegerType size, which could be a 32-bit or 64-bit integer, depending on the operating system and build options. When the number of milliseconds overflows, the returned number of milliseconds is the maximum 32-bit or 64-bit positive signed number (and will be smaller than the seconds requested multiplied by 1000). In either case, such a large value effectively represents eternity.

Parameters
secondsSeconds to convert.
Returns
Result number of milliseconds. To prevent overflow, if seconds multiplied by 1000 is larger than the maximum TimerIntegerType value, the returned value is the maximum TimerIntegerType value.
See also
SecondsToMilliseconds - handles 32-bit integers in all compilation modes
void MTimer::SetTimer ( int  )

Return the distance in milliseconds between the current time and the timer event.

Negative distance means the timer event is in the future, IsExpired will be false in such case. Positive distance, IsExpired is true, will mean the timer event has happened in the past already.

It is worth mentioning that if the timer value is got right after the timer is assigned, the return value will be close to negated assignment value (milliseconds elapsed since assignment will explain the difference). Setting the timer property moves the zero position of the time scale that is used to get the timer value back.

32-bit integer variant of the interface roles over every 25 days, and therefore, should be used with care.

Returns
integer value, distance between the timer event and current moment.
See also
ResetTimer sets the timer event into the exact time of the call.
IsExpired checks whether the timer event has happened already.
void MTimer::SetTimer64 ( Mint64  )

Return the distance in milliseconds between the current time and the timer event.

Negative distance means the timer event is in the future, IsExpired will be false in such case. Positive distance, IsExpired is true, will mean the timer event has happened in the past already.

It is worth mentioning that if the timer value is got right after the timer is assigned, the return value will be close to negated assignment value (milliseconds elapsed since assignment will explain the difference). Setting the timer property moves the zero position of the time scale that is used to get the timer value back.

32-bit integer variant of the interface roles over every 25 days, and therefore, should be used with care.

Returns
integer value, distance between the timer event and current moment.
See also
ResetTimer sets the timer event into the exact time of the call.
IsExpired checks whether the timer event has happened already.
static void MTimer::Sleep ( unsigned  milliseconds)
static

Sleep for the given number of milliseconds.

The precision is not guaranteed, but the delay will not be less than the number of milliseconds specified.

Parameters
millisecondsTime to wait in 1/1000 seconds increments.