File: time_units.hpp

package info (click to toggle)
supercollider 1%3A3.11.2%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 71,152 kB
  • sloc: cpp: 387,846; lisp: 80,328; ansic: 76,515; sh: 22,779; python: 7,932; makefile: 2,333; perl: 1,123; javascript: 915; java: 677; xml: 582; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (301 lines) | stat: -rwxr-xr-x 8,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *             Copyright Andrey Semashev 2013.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   time_units.hpp
 *
 * \brief  This header is the Boost.Sync library implementation, see the library documentation
 *         at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html.
 */

#ifndef BOOST_SYNC_DETAIL_TIME_UNITS_HPP_INCLUDED_
#define BOOST_SYNC_DETAIL_TIME_UNITS_HPP_INCLUDED_

#include <time.h>
#include <boost/cstdint.hpp>
#include <boost/sync/detail/config.hpp>

#if defined(BOOST_SYNC_DETAIL_PLATFORM_WINAPI)
#include <boost/detail/winapi/time.hpp>
#elif defined(BOOST_SYNC_DETAIL_PLATFORM_MACH)
#include <sys/time.h> // gettimeofday, timeval
#endif

#include <boost/sync/detail/header.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

namespace boost {

namespace sync {

namespace detail {

class system_duration
{
public:
    typedef int64_t native_type;

#if defined(BOOST_SYNC_DETAIL_PLATFORM_WINAPI)
    // The native duration is in milliseconds
    static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = 1000u;
#else
    // The native duration is in nanoseconds
    static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = 1000000000u;
#endif

private:
    native_type m_value;

public:
    BOOST_CONSTEXPR system_duration() BOOST_NOEXCEPT : m_value(0) {}
    explicit system_duration(native_type value) BOOST_NOEXCEPT : m_value(value) {}

    native_type get() const BOOST_NOEXCEPT { return m_value; }

    system_duration& operator+= (system_duration const& that) BOOST_NOEXCEPT
    {
        m_value += that.m_value;
        return *this;
    }
    system_duration& operator-= (system_duration const& that) BOOST_NOEXCEPT
    {
        m_value -= that.m_value;
        return *this;
    }
    system_duration operator- () const BOOST_NOEXCEPT
    {
        return system_duration(-m_value);
    }

    friend system_duration operator+ (system_duration left, system_duration const& right) BOOST_NOEXCEPT
    {
        left += right;
        return left;
    }
    friend system_duration operator- (system_duration left, system_duration const& right) BOOST_NOEXCEPT
    {
        left -= right;
        return left;
    }
};

#if defined(BOOST_SYNC_DETAIL_PLATFORM_WINAPI)

class system_time_point
{
public:
    typedef uint64_t native_type;
    // The native subsecond precision is 100 nanoseconds
    static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = 10000000u;

private:
    //! 100 nanosecond units since 1601-Jan-01 (i.e. equivalent to FILETIME)
    uint64_t m_value;

public:
    BOOST_CONSTEXPR system_time_point() BOOST_NOEXCEPT : m_value(0) {}
    explicit system_time_point(time_t t, unsigned int subsecond = 0) BOOST_NOEXCEPT :
        m_value(static_cast< uint64_t >(t) * subsecond_fraction + subsecond + 116444736000000000ull)
    {
    }

    //! Creates time point from duration since 1970-Jan-01
    explicit system_time_point(system_duration dur) BOOST_NOEXCEPT :
        m_value(dur.get() * 10000u + 116444736000000000ull)
    {
    }

    native_type const& get() const BOOST_NOEXCEPT { return m_value; }

    static system_time_point now() BOOST_NOEXCEPT
    {
        system_time_point res;
        boost::detail::winapi::GetSystemTimeAsFileTime(reinterpret_cast< boost::detail::winapi::FILETIME_* >(&res.m_value));
        return res;
    }

    system_time_point& operator+= (system_duration const& dur) BOOST_NOEXCEPT
    {
        m_value += dur.get() * 10000u;
        return *this;
    }
    system_time_point& operator-= (system_duration const& dur) BOOST_NOEXCEPT
    {
        m_value -= dur.get() * 10000u;
        return *this;
    }

    friend system_time_point operator+ (system_time_point left, system_duration const& right) BOOST_NOEXCEPT
    {
        left += right;
        return left;
    }
    friend system_time_point operator- (system_time_point left, system_duration const& right) BOOST_NOEXCEPT
    {
        left -= right;
        return left;
    }

    friend system_duration operator- (system_time_point const& left, system_time_point const& right) BOOST_NOEXCEPT
    {
        return system_duration(static_cast< system_duration::native_type >(left.m_value - right.m_value) / 10000u);
    }
};

#else

class system_time_point
{
public:
    typedef struct ::timespec native_type;
    // The native subsecond precision is nanoseconds
    static BOOST_CONSTEXPR_OR_CONST uint64_t subsecond_fraction = system_duration::subsecond_fraction;

private:
    //! Time, since 1970-Jan-01
    native_type m_value;

public:
    BOOST_CONSTEXPR system_time_point() BOOST_NOEXCEPT : m_value() {}
    explicit system_time_point(time_t t, unsigned int subsecond = 0) BOOST_NOEXCEPT
    {
        m_value.tv_sec = t;
        m_value.tv_nsec = subsecond;
    }

    //! Creates time point from duration since 1970-Jan-01
    explicit system_time_point(system_duration dur) BOOST_NOEXCEPT
    {
        m_value.tv_sec = static_cast< ::time_t >(dur.get() / subsecond_fraction);
        m_value.tv_nsec = dur.get() % subsecond_fraction;
    }

    native_type const& get() const BOOST_NOEXCEPT { return m_value; }

    static system_time_point now() BOOST_NOEXCEPT
    {
#if defined(BOOST_HAS_CLOCK_GETTIME)
        system_time_point t;
        ::clock_gettime(CLOCK_REALTIME, &t.m_value);
        return t;
#elif defined(BOOST_SYNC_DETAIL_PLATFORM_MACH)
        ::timeval tv;
        ::gettimeofday(&tv, 0);
        return system_time_point(tv.tv_sec, tv.tv_usec * (subsecond_fraction / 1000000u));
#else
        return system_time_point(::time(0));
#endif
    }

    system_time_point& operator+= (system_duration const& dur) BOOST_NOEXCEPT
    {
        int64_t nsec = static_cast< int64_t >(m_value.tv_nsec) + dur.get();
        int64_t tv_nsec = nsec % system_duration::subsecond_fraction;
        if (tv_nsec < 0)
        {
            tv_nsec += subsecond_fraction;
            --m_value.tv_sec;
        }
        m_value.tv_nsec = static_cast< long >(tv_nsec);
        m_value.tv_sec += nsec / system_duration::subsecond_fraction;

        return *this;
    }
    system_time_point& operator-= (system_duration const& dur) BOOST_NOEXCEPT
    {
        return operator+= (-dur);
    }

    friend system_time_point operator+ (system_time_point left, system_duration const& right) BOOST_NOEXCEPT
    {
        left += right;
        return left;
    }
    friend system_time_point operator- (system_time_point left, system_duration const& right) BOOST_NOEXCEPT
    {
        left -= right;
        return left;
    }

    friend system_duration operator- (system_time_point const& left, system_time_point const& right) BOOST_NOEXCEPT
    {
        int64_t seconds = static_cast< int64_t >(left.m_value.tv_sec) - static_cast< int64_t >(right.m_value.tv_sec);
        int64_t nseconds = static_cast< int64_t >(left.m_value.tv_nsec) - static_cast< int64_t >(right.m_value.tv_nsec);
        return system_duration(seconds * system_duration::subsecond_fraction + nseconds);
    }
};

#endif

template< typename TimePoint >
class chrono_time_point :
    public TimePoint
{
public:
    typedef TimePoint time_point;
    typedef typename time_point::clock clock;
    typedef typename time_point::duration duration;

private:
    time_point m_value;

public:
    BOOST_DEFAULTED_FUNCTION(chrono_time_point(), : m_value() {})
    BOOST_DEFAULTED_FUNCTION(chrono_time_point(chrono_time_point const& that), : m_value(that.m_value) {})

    explicit chrono_time_point(time_point const& that) : m_value(that)
    {
    }

    template< typename T >
    explicit chrono_time_point(T const& arg) : m_value(arg)
    {
    }

    time_point const& get() const { return m_value; }

    static chrono_time_point now() { return chrono_time_point(clock::now()); }

    chrono_time_point& operator+= (duration const& dur)
    {
        m_value += dur;
        return *this;
    }
    chrono_time_point& operator-= (duration const& dur)
    {
        m_value -= dur;
        return *this;
    }

    friend chrono_time_point operator+ (chrono_time_point left, duration const& right)
    {
        left.m_value += right;
        return left;
    }
    friend chrono_time_point operator- (chrono_time_point left, duration const& right)
    {
        left.m_value -= right;
        return left;
    }

    friend duration operator- (chrono_time_point const& left, chrono_time_point const& right)
    {
        return left.m_value - right.m_value;
    }
};

} // namespace detail

} // namespace sync

} // namespace boost

#include <boost/sync/detail/footer.hpp>

#endif // BOOST_SYNC_DETAIL_TIME_UNITS_HPP_INCLUDED_