TuioTime.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. TUIO C++ Library - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <[email protected]>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef INCLUDED_TUIOTIME_H
  18. #define INCLUDED_TUIOTIME_H
  19. #ifndef WIN32
  20. #include <pthread.h>
  21. #include <sys/time.h>
  22. #else
  23. #include <windows.h>
  24. #endif
  25. #define MSEC_SECOND 1000
  26. #define USEC_SECOND 1000000
  27. #define USEC_MILLISECOND 1000
  28. namespace TUIO {
  29. /**
  30. * The TuioTime class is a simple structure that is used to reprent the time that has elapsed since the session start.
  31. * The time is internally represented as seconds and fractions of microseconds which should be more than sufficient for gesture related timing requirements.
  32. * Therefore at the beginning of a typical TUIO session the static method initSession() will set the reference time for the session.
  33. * Another important static method getSessionTime will return a TuioTime object representing the time elapsed since the session start.
  34. * The class also provides various addtional convience method, which allow some simple time arithmetics.
  35. *
  36. * @author Martin Kaltenbrunner
  37. * @version 1.4
  38. */
  39. class TuioTime {
  40. private:
  41. long seconds, micro_seconds;
  42. static long start_seconds, start_micro_seconds;
  43. public:
  44. /**
  45. * The default constructor takes no arguments and sets
  46. * the Seconds and Microseconds attributes of the newly created TuioTime both to zero.
  47. */
  48. TuioTime () {
  49. seconds = 0;
  50. micro_seconds = 0;
  51. };
  52. /**
  53. * The destructor is doing nothing in particular.
  54. */
  55. ~TuioTime() {}
  56. /**
  57. * This constructor takes the provided time represented in total Milliseconds
  58. * and assigs this value to the newly created TuioTime.
  59. *
  60. * @param msec the total time in Millseconds
  61. */
  62. TuioTime (long msec) {
  63. seconds = msec/MSEC_SECOND;
  64. micro_seconds = USEC_MILLISECOND*(msec%MSEC_SECOND);
  65. };
  66. /**
  67. * This constructor takes the provided time represented in Seconds and Microseconds
  68. * and assigs these value to the newly created TuioTime.
  69. *
  70. * @param sec the total time in seconds
  71. * @param usec the microseconds time component
  72. */
  73. TuioTime (long sec, long usec) {
  74. seconds = sec;
  75. micro_seconds = usec;
  76. };
  77. /**
  78. * Sums the provided time value represented in total Microseconds to this TuioTime.
  79. *
  80. * @param us the total time to add in Microseconds
  81. * @return the sum of this TuioTime with the provided argument in microseconds
  82. */
  83. TuioTime operator+(long us) {
  84. long sec = seconds + us/USEC_SECOND;
  85. long usec = micro_seconds + us%USEC_SECOND;
  86. return TuioTime(sec,usec);
  87. };
  88. /**
  89. * Sums the provided TuioTime to the private Seconds and Microseconds attributes.
  90. *
  91. * @param ttime the TuioTime to add
  92. * @return the sum of this TuioTime with the provided TuioTime argument
  93. */
  94. TuioTime operator+(TuioTime ttime) {
  95. long sec = seconds + ttime.getSeconds();
  96. long usec = micro_seconds + ttime.getMicroseconds();
  97. sec += usec/USEC_SECOND;
  98. usec = usec%USEC_SECOND;
  99. return TuioTime(sec,usec);
  100. };
  101. /**
  102. * Subtracts the provided time represented in Microseconds from the private Seconds and Microseconds attributes.
  103. *
  104. * @param us the total time to subtract in Microseconds
  105. * @return the subtraction result of this TuioTime minus the provided time in Microseconds
  106. */
  107. TuioTime operator-(long us) {
  108. long sec = seconds - us/USEC_SECOND;
  109. long usec = micro_seconds - us%USEC_SECOND;
  110. if (usec<0) {
  111. usec += USEC_SECOND;
  112. sec--;
  113. }
  114. return TuioTime(sec,usec);
  115. };
  116. /**
  117. * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes.
  118. *
  119. * @param ttime the TuioTime to subtract
  120. * @return the subtraction result of this TuioTime minus the provided TuioTime
  121. */
  122. TuioTime operator-(TuioTime ttime) {
  123. long sec = seconds - ttime.getSeconds();
  124. long usec = micro_seconds - ttime.getMicroseconds();
  125. if (usec<0) {
  126. usec += USEC_SECOND;
  127. sec--;
  128. }
  129. return TuioTime(sec,usec);
  130. };
  131. /**
  132. * Assigns the provided TuioTime to the private Seconds and Microseconds attributes.
  133. *
  134. * @param ttime the TuioTime to assign
  135. */
  136. void operator=(TuioTime ttime) {
  137. seconds = ttime.getSeconds();
  138. micro_seconds = ttime.getMicroseconds();
  139. };
  140. /**
  141. * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes.
  142. *
  143. * @param ttime the TuioTime to compare
  144. * @return true if the two TuioTime have equal Seconds and Microseconds attributes
  145. */
  146. bool operator==(TuioTime ttime) {
  147. if ((seconds==(long)ttime.getSeconds()) && (micro_seconds==(long)ttime.getMicroseconds())) return true;
  148. else return false;
  149. };
  150. /**
  151. * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes.
  152. *
  153. * @param ttime the TuioTime to compare
  154. * @return true if the two TuioTime have differnt Seconds or Microseconds attributes
  155. */
  156. bool operator!=(TuioTime ttime) {
  157. if ((seconds!=(long)ttime.getSeconds()) || (micro_seconds!=(long)ttime.getMicroseconds())) return true;
  158. else return false;
  159. };
  160. /**
  161. * Resets the seconds and micro_seconds attributes to zero.
  162. */
  163. void reset() {
  164. seconds = 0;
  165. micro_seconds = 0;
  166. };
  167. /**
  168. * Returns the TuioTime Seconds component.
  169. * @return the TuioTime Seconds component
  170. */
  171. long getSeconds() {
  172. return seconds;
  173. };
  174. /**
  175. * Returns the TuioTime Microseconds component.
  176. * @return the TuioTime Microseconds component
  177. */
  178. long getMicroseconds() {
  179. return micro_seconds;
  180. };
  181. /**
  182. * Returns the total TuioTime in Milliseconds.
  183. * @return the total TuioTime in Milliseconds
  184. */
  185. long getTotalMilliseconds() {
  186. return seconds*MSEC_SECOND+micro_seconds/MSEC_SECOND;
  187. };
  188. /**
  189. * This static method globally resets the TUIO session time.
  190. */
  191. static void initSession();
  192. /**
  193. * Returns the present TuioTime representing the time since session start.
  194. * @return the present TuioTime representing the time since session start
  195. */
  196. static TuioTime getSessionTime();
  197. /**
  198. * Returns the absolut TuioTime representing the session start.
  199. * @return the absolut TuioTime representing the session start
  200. */
  201. static TuioTime getStartTime();
  202. /**
  203. * Returns the absolut TuioTime representing the current system time.
  204. * @return the absolut TuioTime representing the current system time
  205. */
  206. static TuioTime getSystemTime();
  207. };
  208. };
  209. #endif /* INCLUDED_TUIOTIME_H */