zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Timer_HQ.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
49 #ifndef ZENI_TIMER_HQ_H
50 #define ZENI_TIMER_HQ_H
51 
52 #include <Zeni/Chronometer.h>
53 #include <Zeni/Error.h>
54 #include <Zeni/Singleton.h>
55 
56 #ifdef _WINDOWS
57 #include <Windows.h>
58 #else
59 #include <sys/time.h>
60 #endif
61 
62 namespace Zeni {
63 
66 #ifdef _WINDOWS
67  typedef LONGLONG HQ_Tick_Type;
68 #else
69  typedef long double HQ_Tick_Type;
70 
71  timespec subtract(const timespec &lhs, const timespec &rhs);
72  long double to_seconds(const timespec &ticks);
73  long double to_useconds(const timespec &ticks);
74 #ifdef _MACOSX
75  timespec orwl_gettime(void);
76 #endif
77 #endif
78 
79  class ZENI_DLL Time_HQ {
80  public:
82  typedef long double Second_Type;
83 
84  Time_HQ();
85  Time_HQ(const HQ_Tick_Type &ticks);
86  Time_HQ(const HQ_Tick_Type &ticks, const HQ_Tick_Type &ticks_per_second);
87  inline Time_HQ & operator=(const HQ_Tick_Type &ticks);
88 
89  // Accessors
90  // Time passed since last updated
91  inline HQ_Tick_Type get_ticks_passed() const;
92  inline long double get_seconds_passed() const;
93  // From a specific time
94  inline HQ_Tick_Type get_ticks_since(const Time_HQ &time) const;
95  inline long double get_seconds_since(const Time_HQ &time) const;
96 
97  // Modifiers
98  inline void update();
99 
100  // Comparisons
101  inline bool operator<(const Time_HQ &rhs) const;
102 
103  private:
104  HQ_Tick_Type m_ticks;
105  HQ_Tick_Type m_ticks_per_second;
106  };
107 
108  class ZENI_DLL Timer_HQ;
109 
110 #ifdef _WINDOWS
111  ZENI_EXT template class ZENI_DLL Singleton<Timer_HQ>;
112  ZENI_EXT template class ZENI_DLL Chronometer<Time_HQ>;
113 #endif
114 
115  class ZENI_DLL Timer_HQ : public Singleton<Timer_HQ> {
116  friend class Singleton<Timer_HQ>;
117 
118  static Timer_HQ * create();
119 
120  Timer_HQ();
121 
122  // Undefined
123  Timer_HQ(const Timer_HQ &);
124  Timer_HQ & operator=(const Timer_HQ &);
125 
126  public:
127  // Accessors
128  inline HQ_Tick_Type get_ticks();
129  inline HQ_Tick_Type get_ticks_per_second();
130  inline long double get_seconds();
131  inline Time_HQ get_time();
132 
133  private:
134  inline void update();
135 
136 #ifdef _WINDOWS
137  HQ_Tick_Type m_ticks;
138 #else
139  timespec m_ticks;
140 #endif
141  HQ_Tick_Type m_ticks_per_second;
142  };
143 
144  ZENI_DLL Timer_HQ & get_Timer_HQ();
145 
146  struct ZENI_DLL Timer_HQ_Init_Failure : public Error {
147  Timer_HQ_Init_Failure() : Error("Zeni Timer_HQ Failed to Initialize Correctly") {}
148  };
149 }
150 
151 #endif
A High Quality Timer Singleton.
Definition: Timer_HQ.h:115
A Snapshot of the Timer_HQ.
Definition: Timer_HQ.h:79
bool operator<(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:273
long double to_seconds(const timespec &ticks)
Definition: Timer_HQ.cpp:50
long double HQ_Tick_Type
Definition: Timer_HQ.h:69
long double Second_Type
Definition: Timer_HQ.h:82
Timer_HQ & get_Timer_HQ()
Get access to the singleton.
Definition: Timer_HQ.cpp:123
static long get_time(void)
Definition: test_bbox.c:16
HQ_Tick_Type Tick_Type
Definition: Timer_HQ.h:81
long double to_useconds(const timespec &ticks)
Definition: Timer_HQ.cpp:54
A stoppable running timer.
Definition: Chronometer.h:40
timespec subtract(const timespec &lhs, const timespec &rhs)
Definition: Timer_HQ.cpp:35
The Error Class.
Definition: Error.h:52