Realistic 3D camera system
3D camera system components
chrono_time_traits.hpp
Go to the documentation of this file.
1 //
2 // detail/chrono_time_traits.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP
12 #define ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/cstdint.hpp"
19 
21 
22 namespace asio {
23 namespace detail {
24 
25 // Helper template to compute the greatest common divisor.
26 template <int64_t v1, int64_t v2>
27 struct gcd { enum { value = gcd<v2, v1 % v2>::value }; };
28 
29 template <int64_t v1>
30 struct gcd<v1, 0> { enum { value = v1 }; };
31 
32 // Adapts std::chrono clocks for use with a deadline timer.
33 template <typename Clock, typename WaitTraits>
35 {
36  // The clock type.
37  typedef Clock clock_type;
38 
39  // The duration type of the clock.
40  typedef typename clock_type::duration duration_type;
41 
42  // The time point type of the clock.
43  typedef typename clock_type::time_point time_type;
44 
45  // The period of the clock.
46  typedef typename duration_type::period period_type;
47 
48  // Get the current time.
49  static time_type now()
50  {
51  return clock_type::now();
52  }
53 
54  // Add a duration to a time.
55  static time_type add(const time_type& t, const duration_type& d)
56  {
57  const time_type epoch;
58  if (t >= epoch)
59  {
60  if ((time_type::max)() - t < d)
61  return (time_type::max)();
62  }
63  else // t < epoch
64  {
65  if (-(t - (time_type::min)()) > d)
66  return (time_type::min)();
67  }
68 
69  return t + d;
70  }
71 
72  // Subtract one time from another.
73  static duration_type subtract(const time_type& t1, const time_type& t2)
74  {
75  const time_type epoch;
76  if (t1 >= epoch)
77  {
78  if (t2 >= epoch)
79  {
80  return t1 - t2;
81  }
82  else if (t2 == (time_type::min)())
83  {
84  return (duration_type::max)();
85  }
86  else if ((time_type::max)() - t1 < epoch - t2)
87  {
88  return (duration_type::max)();
89  }
90  else
91  {
92  return t1 - t2;
93  }
94  }
95  else // t1 < epoch
96  {
97  if (t2 < epoch)
98  {
99  return t1 - t2;
100  }
101  else if (t1 == (time_type::min)())
102  {
103  return (duration_type::min)();
104  }
105  else if ((time_type::max)() - t2 < epoch - t1)
106  {
107  return (duration_type::min)();
108  }
109  else
110  {
111  return -(t2 - t1);
112  }
113  }
114  }
115 
116  // Test whether one time is less than another.
117  static bool less_than(const time_type& t1, const time_type& t2)
118  {
119  return t1 < t2;
120  }
121 
122  // Implement just enough of the posix_time::time_duration interface to supply
123  // what the timer_queue requires.
125  {
126  public:
127  explicit posix_time_duration(const duration_type& d)
128  : d_(d)
129  {
130  }
131 
132  int64_t ticks() const
133  {
134  return d_.count();
135  }
136 
137  int64_t total_seconds() const
138  {
139  return duration_cast<1, 1>();
140  }
141 
142  int64_t total_milliseconds() const
143  {
144  return duration_cast<1, 1000>();
145  }
146 
147  int64_t total_microseconds() const
148  {
149  return duration_cast<1, 1000000>();
150  }
151 
152  private:
153  template <int64_t Num, int64_t Den>
154  int64_t duration_cast() const
155  {
156  const int64_t num1 = period_type::num / gcd<period_type::num, Num>::value;
157  const int64_t num2 = Num / gcd<period_type::num, Num>::value;
158 
159  const int64_t den1 = period_type::den / gcd<period_type::den, Den>::value;
160  const int64_t den2 = Den / gcd<period_type::den, Den>::value;
161 
162  const int64_t num = num1 * den2;
163  const int64_t den = num2 * den1;
164 
165  if (num == 1 && den == 1)
166  return ticks();
167  else if (num != 1 && den == 1)
168  return ticks() * num;
169  else if (num == 1 && period_type::den != 1)
170  return ticks() / den;
171  else
172  return ticks() * num / den;
173  }
174 
175  duration_type d_;
176  };
177 
178  // Convert to POSIX duration type.
179  static posix_time_duration to_posix_duration(const duration_type& d)
180  {
181  return posix_time_duration(WaitTraits::to_wait_duration(d));
182  }
183 };
184 
185 } // namespace detail
186 } // namespace asio
187 
189 
190 #endif // ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP
static bool less_than(const time_type &t1, const time_type &t2)
static duration_type subtract(const time_type &t1, const time_type &t2)
static time_type add(const time_type &t, const duration_type &d)
static posix_time_duration to_posix_duration(const duration_type &d)