0e43d4da848438619d14846e8900728318e5008f
[synfig.git] / ETL / tags / stable / ETL / _clock_system.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Proc Clock Description Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL__CLOCK_SYSTEM_H
28 #define __ETL__CLOCK_SYSTEM_H
29
30 /* === H E A D E R S ======================================================= */
31
32 #ifndef _WIN32
33 # include <time.h>
34 # define __sys_clock    ::clock
35 # define __sys_time     ::time
36 #else
37 # ifdef __GNUG__
38 #  include <time.h>
39 #  define __sys_clock   ::clock
40 #  define __sys_time    ::time
41 # else
42 typedef int clock_t;
43 typedef int time_t;
44 extern clock_t _clock();
45 extern time_t _time(time_t *);
46 #  define CLOCKS_PER_SEC 1000
47 #  define __sys_clock   _clock
48 #  define __sys_time    _time
49 # endif
50 #endif
51
52 /* === M A C R O S ========================================================= */
53
54 /* === T Y P E D E F S ===================================================== */
55
56 /* === C L A S S E S & S T R U C T S ======================================= */
57
58 _ETL_BEGIN_NAMESPACE
59
60 class clock_desc_sys_clock
61 {
62 public:
63         typedef float value_type;
64
65         inline static bool realtime()
66         { return false; }
67
68         inline static bool proctime()
69         { return true; }
70
71         inline static value_type
72         one_second()
73         { return 1.0f; }
74
75         inline static value_type precision()
76         { return one_second()/(value_type)CLOCKS_PER_SEC; }
77
78         inline static const char *description()
79         { return "ANSI C clock()"; };
80
81 protected:
82         typedef clock_t timestamp;
83
84         static void
85         get_current_time(timestamp &time)
86         { time=__sys_clock(); }
87
88         static timestamp
89         get_current_time()
90         { return __sys_clock(); }
91
92         static value_type
93         timestamp_to_seconds(const timestamp &x)
94         { return precision()*x; }
95
96         static timestamp
97         seconds_to_timestamp(const value_type &x)
98         { return (timestamp)(x*(value_type)CLOCKS_PER_SEC+0.5); }
99
100 };
101
102 class clock_desc_sys_time
103 {
104 public:
105         typedef float value_type;
106
107         inline static bool realtime()
108         { return true; }
109
110         inline static bool proctime()
111         { return false; }
112
113         inline static value_type
114         one_second()
115         { return 1.0f; }
116
117         inline static value_type precision()
118         { return one_second(); }
119
120         inline static const char *description()
121         { return "ANSI C time()"; };
122
123 protected:
124         typedef time_t timestamp;
125
126         static void
127         get_current_time(timestamp &time)
128         { __sys_time(&time); }
129
130         static timestamp
131         get_current_time()
132         { return __sys_time(NULL); }
133
134         static value_type
135         timestamp_to_seconds(const timestamp &x)
136         { return (value_type)x; }
137
138         static timestamp
139         seconds_to_timestamp(const value_type &x)
140         { return (timestamp)(x+(value_type)0.5f); }
141 };
142
143 _ETL_END_NAMESPACE
144
145 /* === E N D =============================================================== */
146
147 #endif