Added additional operators to RestrictedInt and made
[senf.git] / senf / Scheduler / ClockService.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ClockService inline non-template implementation */
30
31 // Custom includes
32 #include <time.h>
33 #include <boost/date_time/posix_time/posix_time_types.hpp>
34 #include <senf/Utils/Exception.hh>
35 #include <senf/Utils/ClockTypeMacros.hh>
36
37 #define prefix_ inline
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::ClockService
42
43 prefix_ senf::ClockService::clock_type senf::ClockService::now()
44 {
45     struct timespec spec;
46     if (clock_gettime(CLOCK_MONOTONIC, &spec) < 0)
47         SENF_THROW_SYSTEM_EXCEPTION("clock_gettime()");
48     return SENF_INT2CLOCKTYPE(spec.tv_sec * 1000000000LL + spec.tv_nsec);
49 }
50
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // private members
53
54 namespace senf { namespace scheduler { ClockService::clock_type now(); } }
55
56 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
57 {
58     if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
59         restart_m();
60     boost::posix_time::time_duration delta (time - baseAbstime_);
61     return baseClock_ + clock_type( delta.ticks() )
62         * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
63 }
64
65 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type clock)
66 {
67     if (clock == 0)
68         return abstime_type();
69     if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
70         restart_m();
71 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
72     return baseAbstime_ + boost::posix_time::nanoseconds(clock-baseClock_);
73 #else
74     return baseAbstime_ + boost::posix_time::microseconds((clock.value() - baseClock_.value() + 500 )/1000);
75 #endif
76 }
77
78 prefix_ senf::ClockService::ClockService()
79 {
80     restart_m();
81 }
82
83 prefix_ void senf::ClockService::restart_m()
84 {
85     baseAbstime_ = boost::posix_time::microsec_clock::universal_time();
86     baseClock_ = now();
87 }
88
89 // public members
90
91 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type clock)
92 {
93     return instance().abstime_m(clock);
94 }
95
96 prefix_ senf::ClockService::reltime_type senf::ClockService::reltime(clock_type clock)
97 {
98 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
99     return boost::posix_time::nanoseconds(clock);
100 #else
101     return  boost::posix_time::microseconds((clock.value() + 500)/1000);
102 #endif
103 }
104
105 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type time)
106 {
107     return instance().clock_m(time);
108 }
109
110 prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time)
111 {
112     return clock( boost::posix_time::from_time_t(time) );
113 }
114
115 prefix_ senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type v)
116 {
117     return SENF_INT2CLOCKTYPE(v);
118 }
119
120 prefix_ senf::ClockService::clock_type senf::ClockService::microseconds(int64_type v)
121 {
122     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(nanoseconds(1000)));
123 }
124
125 prefix_ senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type v)
126 {
127     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(microseconds(1000)));
128 }
129
130 prefix_ senf::ClockService::clock_type senf::ClockService::seconds(int64_type v)
131 {
132     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(milliseconds(1000)));
133 }
134
135 prefix_ senf::ClockService::clock_type senf::ClockService::minutes(int64_type v)
136 {
137     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(seconds(60)));
138 }
139
140 prefix_ senf::ClockService::clock_type senf::ClockService::hours(int64_type v)
141 {
142     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(minutes(60)));
143 }
144
145 prefix_ senf::ClockService::clock_type senf::ClockService::days(int64_type v)
146 {
147     return SENF_INT2CLOCKTYPE(v * SENF_CLOCKTYPEVAL(hours(24)));
148 }
149
150 prefix_ senf::ClockService::int64_type senf::ClockService::in_nanoseconds(clock_type v)
151 {
152     return SENF_CLOCKTYPEVAL(v);
153 }
154
155 prefix_ senf::ClockService::int64_type senf::ClockService::in_microseconds(clock_type v)
156 {
157     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(nanoseconds(1000)));
158 }
159
160 prefix_ senf::ClockService::int64_type senf::ClockService::in_milliseconds(clock_type v)
161 {
162     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(microseconds(1000)));
163 }
164
165 prefix_ senf::ClockService::int64_type senf::ClockService::in_seconds(clock_type v)
166 {
167     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(milliseconds(1000)));
168 }
169
170 prefix_ senf::ClockService::int64_type senf::ClockService::in_minutes(clock_type v)
171 {
172     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(seconds(60)));
173 }
174
175 prefix_ senf::ClockService::int64_type senf::ClockService::in_hours(clock_type v)
176 {
177     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(minutes(60))) ;
178 }
179
180 prefix_ senf::ClockService::int64_type senf::ClockService::in_days(clock_type v)
181 {
182     return (SENF_CLOCKTYPEVAL(v) / SENF_CLOCKTYPEVAL(hours(24))) ;
183 }
184
185 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
186 {
187     return from_time_t(time.tv_sec) + microseconds(time.tv_usec);
188 }
189
190 prefix_ void senf::ClockService::restart()
191 {
192     instance().restart_m();
193 }
194
195 //-/////////////////////////////////////////////////////////////////////////////////////////////////
196 #undef prefix_
197
198 \f
199 // Local Variables:
200 // mode: c++
201 // fill-column: 100
202 // comment-column: 40
203 // c-file-style: "senf"
204 // indent-tabs-mode: nil
205 // ispell-local-dictionary: "american"
206 // compile-command: "scons -u test"
207 // End: