summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/chrono
blob: aaa9646f2fc497bed56a01b0f73105127b29b480 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// <chrono> -*- C++ -*-

// Copyright (C) 2008, 2009 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/chrono
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_CHRONO
#define _GLIBCXX_CHRONO 1

#pragma GCC system_header

#ifndef __GXX_EXPERIMENTAL_CXX0X__
# include <c++0x_warning.h>
#else

#ifdef _GLIBCXX_INCLUDE_AS_TR1
#  error C++0x header cannot be included from TR1 header
#endif

#include <ratio>
#include <type_traits>
#include <limits>
#include <ctime>

#ifdef _GLIBCXX_USE_C99_STDINT_TR1

namespace std 
{
  /**
   * @defgroup chrono Time
   * @ingroup utilities
   *
   * Classes and functions for time.
   * @{
   */

  /** @namespace std::chrono
   *  @brief ISO C++ 0x entities sub namespace for time and date.
   */
  namespace chrono
  {
    template<typename _Rep, typename _Period = ratio<1>>
      struct duration;

    template<typename _Clock, typename _Duration = typename _Clock::duration>
      struct time_point;
  }

  // 20.8.2.3 specialization of common_type (for duration)
  template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
    struct common_type<chrono::duration<_Rep1, _Period1>,
                       chrono::duration<_Rep2, _Period2>>
    {
      typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
        ratio<__static_gcd<_Period1::num, _Period2::num>::value,
        (_Period1::den / __static_gcd<_Period1::den, _Period2::den>::value)
        * _Period2::den>> type;
    };
  
  // 20.8.2.3 specialization of common_type (for time_point)
  template<typename _Clock, typename _Duration1, typename _Duration2>
    struct common_type<chrono::time_point<_Clock, _Duration1>,
                       chrono::time_point<_Clock, _Duration2>>
    {
      typedef chrono::time_point<_Clock, 
        typename common_type<_Duration1, _Duration2>::type> type;
    };

  namespace chrono 
  {
    // Primary template for duration_cast impl.
    template<typename _ToDuration, typename _CF, typename _CR,
             bool _NumIsOne = false, bool _DenIsOne = false>
      struct __duration_cast_impl
      {
        template<typename _Rep, typename _Period>
          static _ToDuration __cast(const duration<_Rep, _Period>& __d)
          {
            return _ToDuration(static_cast<
              typename _ToDuration::rep>(static_cast<_CR>(__d.count())
              * static_cast<_CR>(_CF::num)
              / static_cast<_CR>(_CF::den)));
          }
      };

    template<typename _ToDuration, typename _CF, typename _CR>
      struct __duration_cast_impl<_ToDuration, _CF, _CR, true, true>
      {
        template<typename _Rep, typename _Period>
          static _ToDuration __cast(const duration<_Rep, _Period>& __d)
          {
            return _ToDuration(
              static_cast<typename _ToDuration::rep>(__d.count()));
          }
      };

    template<typename _ToDuration, typename _CF, typename _CR>
      struct __duration_cast_impl<_ToDuration, _CF, _CR, true, false>
      {
        template<typename _Rep, typename _Period>
          static _ToDuration __cast(const duration<_Rep, _Period>& __d)
          {
            return _ToDuration(static_cast<typename _ToDuration::rep>(
              static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); 
          }
      };

    template<typename _ToDuration, typename _CF, typename _CR>
      struct __duration_cast_impl<_ToDuration, _CF, _CR, false, true>
      {
        template<typename _Rep, typename _Period>
          static _ToDuration __cast(const duration<_Rep, _Period>& __d)
          {
            return _ToDuration(static_cast<typename _ToDuration::rep>(
              static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
          }
      };

    /// duration_cast
    template<typename _ToDuration, typename _Rep, typename _Period>
      inline _ToDuration
      duration_cast(const duration<_Rep, _Period>& __d)
      {
        typedef typename
          ratio_divide<_Period, typename _ToDuration::period>::type __cf;
        typedef typename
          common_type<typename _ToDuration::rep, _Rep, intmax_t>::type __cr;

        return __duration_cast_impl<_ToDuration, __cf, __cr,
          __cf::num == 1, __cf::den == 1>::__cast(__d);
      }

    /// treat_as_floating_point
    template<typename _Rep>
      struct treat_as_floating_point 
      : is_floating_point<_Rep>
      { };

    /// duration_values
    template<typename _Rep>
      struct duration_values
      {
        static const _Rep
        zero()
        { return _Rep(0); }
        
        static const _Rep
        max()
        { return numeric_limits<_Rep>::max(); }
        
        static const _Rep
        min()
        { return numeric_limits<_Rep>::min(); }
      };

    template<typename _Tp>
      struct __is_duration
      : std::false_type
      { };

    template<typename _Rep, typename _Period>
      struct __is_duration<duration<_Rep, _Period>>
      : std::true_type
      { };

    template<typename T>
      struct __is_ratio
      : std::false_type
      { };

    template<intmax_t _Num, intmax_t _Den>
      struct __is_ratio<ratio<_Num, _Den>>
      : std::true_type
      { };

    /// duration
    template<typename _Rep, typename _Period>
      struct duration
      {
	static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
	static_assert(__is_ratio<_Period>::value, 
		      "period must be a specialization of ratio");
        static_assert(_Period::num > 0, "period must be positive");
        
        typedef _Rep    rep;
        typedef _Period period;
        
        // 20.8.3.1 construction / copy / destroy
	duration() = default;

        template<typename _Rep2>
          explicit duration(_Rep2 const& __rep)
          : __r(static_cast<rep>(__rep))
          {
            static_assert(is_convertible<_Rep2,rep>::value 
			  && (treat_as_floating_point<rep>::value 
			      || !treat_as_floating_point<_Rep2>::value),
	      "cannot construct integral duration with floating point type");
          }

        template<typename _Rep2, typename _Period2>
          duration(const duration<_Rep2, _Period2>& __d)
          : __r(duration_cast<duration>(__d).count())
          {
            static_assert(treat_as_floating_point<rep>::value == true 
              || ratio_divide<_Period2, period>::type::den == 1, 
              "the resulting duration is not exactly representable");
          }

	~duration() = default;
	duration(const duration&) = default;
	duration& operator=(const duration&) = default;

        // 20.8.3.2 observer
        rep
        count() const
        { return __r; }

        // 20.8.3.3 arithmetic
        duration
        operator+() const 
        { return *this; }

        duration
        operator-() const 
        { return duration(-__r); }

        duration&
        operator++() 
        {
          ++__r;
          return *this;
        }

        duration
        operator++(int) 
        { return duration(__r++); }

        duration&
        operator--() 
        { 
          --__r;
          return *this;
        }

        duration
        operator--(int) 
        { return duration(__r--); }
        
        duration&
        operator+=(const duration& __d)
        {
          __r += __d.count();
          return *this;
        }

        duration&
        operator-=(const duration& __d)
        {
          __r -= __d.count();
          return *this;
        }

        duration&
        operator*=(const rep& __rhs)
        {
          __r *= __rhs;
          return *this;
        }

        duration&
        operator/=(const rep& __rhs)
        { 
          __r /= __rhs;
          return *this;
        }

        // 20.8.3.4 special values
        // TODO: These should be constexprs.
        static const duration
        zero()
        { return duration(duration_values<rep>::zero()); }

        static const duration
        min()
        { return duration(duration_values<rep>::min()); }
      
        static const duration
        max()
        { return duration(duration_values<rep>::max()); }
   
      private:    
        rep __r;
      };

    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline typename common_type<duration<_Rep1, _Period1>, 
                                  duration<_Rep2, _Period2>>::type
      operator+(const duration<_Rep1, _Period1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      {
        typedef typename common_type<duration<_Rep1, _Period1>, 
                                     duration<_Rep2, _Period2>>::type __ct;
        return __ct(__lhs) += __rhs;
      }

    template<typename _Rep1, typename _Period1, 
             typename _Rep2, typename _Period2>
      inline typename common_type<duration<_Rep1, _Period1>, 
                                  duration<_Rep2, _Period2>>::type
      operator-(const duration<_Rep1, _Period1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      {
        typedef typename common_type<duration<_Rep1, _Period1>,
                                     duration<_Rep2, _Period2>>::type __ct;
        return __ct(__lhs) -= __rhs;
      }

    template<typename _Rep1, typename _Period, typename _Rep2>
      inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
      operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
      {
        typedef typename common_type<_Rep1, _Rep2>::type __cr;
        return duration<__cr, _Period>(__d) *= __s;
      }

    template<typename _Rep1, typename _Period, typename _Rep2>
      inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
      operator*(const _Rep2& __s, const duration<_Rep1, _Period>& __d)
      { return __d * __s; }

    template<typename _Tp, typename _Up, typename _Ep = void>
      struct __division_impl;
  
    template<typename _Rep1, typename _Period, typename _Rep2>
      struct __division_impl<duration<_Rep1, _Period>, _Rep2, 
        typename enable_if<!__is_duration<_Rep2>::value>::type>
      {
        typedef typename common_type<_Rep1, _Rep2>::type __cr;
        typedef 
          duration<typename common_type<_Rep1, _Rep2>::type, _Period> __rt;

        static __rt
        __divide(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
        { return duration<__cr, _Period>(__d) /= __s; }
      };

    template<typename _Rep1, typename _Period1, 
             typename _Rep2, typename _Period2>
      struct __division_impl<duration<_Rep1, _Period1>, 
                             duration<_Rep2, _Period2>>
      {
        typedef typename common_type<duration<_Rep1, _Period1>, 
                                     duration<_Rep2, _Period2>>::type __ct;
        typedef typename common_type<_Rep1, _Rep2>::type __rt;

        static __rt
        __divide(const duration<_Rep1, _Period1>& __lhs, 
                 const duration<_Rep2, _Period2>& __rhs)
        { return __ct(__lhs).count() / __ct(__rhs).count(); }
      };
  
    template<typename _Rep, typename _Period, typename _Up>
      inline typename __division_impl<duration<_Rep, _Period>, _Up>::__rt
      operator/(const duration<_Rep, _Period>& __d, const _Up& __u)
      {
        return 
          __division_impl<duration<_Rep, _Period>, _Up>::__divide(__d, __u);
      }
 
    // comparisons
    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline bool
      operator==(const duration<_Rep1, _Period1>& __lhs, 
                 const duration<_Rep2, _Period2>& __rhs)
      {
        typedef typename common_type<duration<_Rep1, _Period1>, 
                                     duration<_Rep2, _Period2>>::type __ct;
        return __ct(__lhs).count() == __ct(__rhs).count();
      }

    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline bool
      operator<(const duration<_Rep1, _Period1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      {
        typedef typename common_type<duration<_Rep1, _Period1>, 
                                     duration<_Rep2, _Period2>>::type __ct;
        return __ct(__lhs).count() < __ct(__rhs).count();
      }

    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline bool
      operator!=(const duration<_Rep1, _Period1>& __lhs, 
                 const duration<_Rep2, _Period2>& __rhs)
      { return !(__lhs == __rhs); }

    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline bool
      operator<=(const duration<_Rep1, _Period1>& __lhs, 
                 const duration<_Rep2, _Period2>& __rhs)
      { return !(__rhs < __lhs); }

    template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
      inline bool 
      operator>(const duration<_Rep1, _Period1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      { return __rhs < __lhs; }

    template<typename _Rep1, typename _Period1, 
             typename _Rep2, typename _Period2>
      inline bool
      operator>=(const duration<_Rep1, _Period1>& __lhs, 
                 const duration<_Rep2, _Period2>& __rhs)
      { return !(__lhs < __rhs); }

    /// nanoseconds
    typedef duration<int64_t,        nano> nanoseconds;

    /// microseconds
    typedef duration<int64_t,       micro> microseconds;

    /// milliseconds
    typedef duration<int64_t,       milli> milliseconds;
    
    /// seconds
    typedef duration<int64_t             > seconds;

    /// minutes
    typedef duration<int,     ratio<  60>> minutes;

    /// hours
    typedef duration<int,     ratio<3600>> hours;

    /// time_point
    template<typename _Clock, typename _Duration>
      struct time_point
      {
	typedef _Clock                    clock;
	typedef _Duration                 duration;
	typedef typename duration::rep    rep;
	typedef typename duration::period period;

	time_point() : __d(duration::zero())
	{ }

	explicit time_point(const duration& __dur) 
	: __d(duration::zero() + __dur)
	{ }

	// conversions
	template<typename _Duration2>
	  time_point(const time_point<clock, _Duration2>& __t)
	  : __d(__t.time_since_epoch())
	  { }

	// observer
	duration
	time_since_epoch() const
	{ return __d; }
	
	// arithmetic
	time_point&
	operator+=(const duration& __dur)
	{
	  __d += __dur;
	  return *this;
	}
	
	time_point&
	operator-=(const duration& __dur)
	{
	  __d -= __dur;
	  return *this;
	}
	
	// special values
	// TODO: These should be constexprs.
	static const time_point
	min()
	{ return time_point(duration::min()); }
	
	static const time_point
	max()
	{ return time_point(duration::max()); }
	
      private:
	duration __d;
      };
  
    /// time_point_cast
    template<typename _ToDuration, typename _Clock, typename _Duration>
      inline time_point<_Clock, _ToDuration> 
      time_point_cast(const time_point<_Clock, _Duration>& __t)
      {
        return time_point<_Clock, _ToDuration>(
          duration_cast<_ToDuration>(__t.time_since_epoch()));  
      }

    template<typename _Clock, typename _Duration1,
             typename _Rep2, typename _Period2>
      inline time_point<_Clock, 
        typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
      operator+(const time_point<_Clock, _Duration1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      {
        typedef time_point<_Clock, 
          typename common_type<_Duration1, 
                               duration<_Rep2, _Period2>>::type> __ct;
        return __ct(__lhs) += __rhs;
      }

    template<typename _Rep1, typename _Period1,
             typename _Clock, typename _Duration2>
      inline time_point<_Clock, 
        typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
      operator+(const duration<_Rep1, _Period1>& __lhs, 
                const time_point<_Clock, _Duration2>& __rhs)
      { return __rhs + __lhs; }

    template<typename _Clock, typename _Duration1,
             typename _Rep2, typename _Period2>
      inline time_point<_Clock, 
        typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
      operator-(const time_point<_Clock, _Duration1>& __lhs, 
                const duration<_Rep2, _Period2>& __rhs)
      { return __lhs + (-__rhs); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline typename common_type<_Duration1, _Duration2>::type
      operator-(const time_point<_Clock, _Duration1>& __lhs, 
                const time_point<_Clock, _Duration2>& __rhs)
      { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator==(const time_point<_Clock, _Duration1>& __lhs,
                 const time_point<_Clock, _Duration2>& __rhs)
      { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator!=(const time_point<_Clock, _Duration1>& __lhs,
                 const time_point<_Clock, _Duration2>& __rhs)
      { return !(__lhs == __rhs); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator<(const time_point<_Clock, _Duration1>& __lhs,
                const time_point<_Clock, _Duration2>& __rhs)
      { return  __lhs.time_since_epoch() < __rhs.time_since_epoch(); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator<=(const time_point<_Clock, _Duration1>& __lhs,
                 const time_point<_Clock, _Duration2>& __rhs)
      { return !(__rhs < __lhs); }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator>(const time_point<_Clock, _Duration1>& __lhs,
                const time_point<_Clock, _Duration2>& __rhs)
      { return __rhs < __lhs; }

    template<typename _Clock, typename _Duration1, typename _Duration2>
      inline bool
      operator>=(const time_point<_Clock, _Duration1>& __lhs,
                 const time_point<_Clock, _Duration2>& __rhs)
      { return !(__lhs < __rhs); }

    /// system_clock
    struct system_clock
    {
#ifdef _GLIBCXX_USE_CLOCK_REALTIME
      typedef chrono::nanoseconds     duration;      
#elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
      typedef chrono::microseconds    duration;      
#else
      typedef chrono::seconds         duration;      
#endif

      typedef duration::rep    rep;
      typedef duration::period period;
      typedef chrono::time_point<system_clock, duration> time_point;

      static const bool is_monotonic = false;

      static time_point
      now();   

      // Map to C API
      static std::time_t
      to_time_t(const time_point& __t)
      {
        return std::time_t(
          duration_cast<chrono::seconds>(__t.time_since_epoch()).count());
      }

      static time_point
      from_time_t(std::time_t __t)
      { 
        return time_point_cast<system_clock::duration>(
          chrono::time_point<system_clock, chrono::seconds>(
            chrono::seconds(__t)));
      }

      // TODO: requires constexpr
      /*  
      static_assert(
        system_clock::duration::min() < 
        system_clock::duration::zero(), 
        "a clock's minimum duration cannot be less than its epoch");
      */
    };

#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
    /// monotonic_clock
    struct monotonic_clock
    {
      typedef chrono::nanoseconds duration;
      typedef duration::rep       rep;
      typedef duration::period    period;
      typedef chrono::time_point<monotonic_clock, duration> time_point;

      static const bool is_monotonic = true;

      static time_point
      now();
    };
#else
    typedef system_clock monotonic_clock;
#endif

    typedef system_clock high_resolution_clock;
  } // namespace chrono

  // @} group chrono
} // namespace std

#endif //_GLIBCXX_USE_C99_STDINT_TR1

#endif //__GXX_EXPERIMENTAL_CXX0X__

#endif //_GLIBCXX_CHRONO