- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clock.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2013 Aldebaran Robotics. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the COPYING file.
5 */
6
7 #pragma once
8 #ifndef _QI_CLOCK_HXX_
9 #define _QI_CLOCK_HXX_
10
11 #include <boost/chrono/ceil.hpp>
12 #include <sstream>
13
14 namespace qi {
15
16 template <class Rep, class Period>
17 void sleepFor(const boost::chrono::duration<Rep, Period>& d)
18 {
19 sleepFor(boost::chrono::ceil<SteadyClock::duration>(d));
20 }
21
22 template <class Duration>
23 void sleepUntil(const boost::chrono::time_point<SteadyClock, Duration>& t)
24 {
25 sleepFor(t - SteadyClock::now());
26 }
27
28 template <class Duration>
29 void sleepUntil(const boost::chrono::time_point<Clock, Duration>& t)
30 {
31 sleepFor(t - Clock::now());
32 }
33
34 template <class Duration>
35 void sleepUntil(const boost::chrono::time_point<SystemClock, Duration>& t)
36 {
37 sleepFor(t - SystemClock::now());
38 }
39
40 template <class DurationTo, class TimePointFrom>
41 DurationTo durationSince(const TimePointFrom& t)
42 {
43 using ClockFrom = typename TimePointFrom::clock;
44 return boost::chrono::duration_cast<DurationTo>(ClockFrom::now() - t);
45 }
46
47 namespace detail
48 {
49 template <class Period>
50 void write_duration_unit_long_name(std::ostream &os);
51
52 template <>
53 inline void write_duration_unit_long_name<boost::nano>(std::ostream &os) {
54 static const char a[] = {'n', 'a', 'n', 'o', 's', 'e', 'c', 'o', 'n', 'd', 's'};
55 os.write(a, sizeof(a)/sizeof(char));
56 }
57
58 template <>
59 inline void write_duration_unit_long_name<boost::micro>(std::ostream &os) {
60 static const char a[] = {'m', 'i', 'c', 'r', 'o', 's', 'e', 'c', 'o', 'n', 'd', 's'};
61 os.write(a, sizeof(a)/sizeof(char));
62 }
63
64 template <>
65 inline void write_duration_unit_long_name<boost::milli>(std::ostream &os) {
66 static const char a[] = {'m', 'i', 'l', 'l', 'i', 's', 'e', 'c', 'o', 'n', 'd', 's'};
67 os.write(a, sizeof(a)/sizeof(char));
68 }
69
70 template <>
71 inline void write_duration_unit_long_name<boost::ratio<1, 1>>(std::ostream &os) {
72 static const char a[] = {'s', 'e', 'c', 'o', 'n', 'd', 's'};
73 os.write(a, sizeof(a)/sizeof(char));
74 }
75
76 template <>
77 inline void write_duration_unit_long_name<boost::ratio<60, 1>>(std::ostream &os) {
78 static const char a[] = {'m', 'i', 'n', 'u', 't', 'e', 's'};
79 os.write(a, sizeof(a)/sizeof(char));
80 }
81
82 template <>
83 inline void write_duration_unit_long_name<boost::ratio<3600, 1>>(std::ostream &os) {
84 static const char a[] = {'h', 'o', 'u', 'r', 's'};
85 os.write(a, sizeof(a)/sizeof(char));
86 }
87 }
88
89 template <class R, class P>
90 inline std::string to_string(const boost::chrono::duration<R, P> &d)
91 {
92 std::ostringstream os;
93 os << d.count() << ' ';
94 detail::write_duration_unit_long_name<P>(os);
95 return os.str();
96 }
97
98 template <class C, class D>
99 inline std::string to_string(const boost::chrono::time_point<C, D> &t)
100 {
101 std::ostringstream os;
102 os << t.time_since_epoch().count() << ' ';
103 detail::write_duration_unit_long_name<typename D::period>(os);
104 os << boost::chrono::clock_string<C, char>::since();
105 return os.str();
106 }
107 }
108
109 namespace boost
110 {
111 namespace chrono
112 {
113 // helpers for boost chrono io (v1)
114 template <class CharT>
115 struct clock_string<qi::SteadyClock, CharT>
116 {
117 static std::basic_string<CharT> name() {
118 static const CharT a[] = {'q', 'i', ':', ':', 'S', 't', 'e', 'a', 'd', 'y', 'C', 'l', 'o', 'c', 'k'};
119 return std::basic_string<CharT>(a, a + sizeof(a)/sizeof(a[0]));
120 }
121 static std::basic_string<CharT> since() {
122 static const CharT a[] = {' ', 's', 'i', 'n', 'c', 'e', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 's', 't', 'a', 'r', 't'};
123 return std::basic_string<CharT>(a, a + sizeof(a)/sizeof(a[0]));
124 }
125 };
126
127 template <class CharT>
128 struct clock_string<qi::Clock, CharT>
129 {
130 static std::basic_string<CharT> name() {
131 static const CharT a[] = {'q', 'i', ':', ':', 'C', 'l', 'o', 'c', 'k'};
132 return std::basic_string<CharT>(a, a + sizeof(a)/sizeof(a[0]));
133 }
134 static std::basic_string<CharT> since() {
135 return clock_string<boost::chrono::steady_clock, CharT>::since();
136 }
137 };
138
139 template <class CharT>
140 struct clock_string<qi::SystemClock, CharT>
141 {
142 static std::basic_string<CharT> name() {
143 static const CharT a[] = {'q', 'i', ':', ':', 'S', 'y', 's', 't', 'e', 'm', 'C', 'l', 'o', 'c', 'k'};
144 return std::basic_string<CharT>(a, a + sizeof(a)/sizeof(a[0]));
145 }
146 static std::basic_string<CharT> since() {
147 return clock_string<boost::chrono::system_clock, CharT>::since();
148 }
149 };
150 }
151 }
152
153 #endif // _QI_CLOCK_HXX_
qi::durationSince
DurationTo durationSince(const TimePointFrom &t)
}@
Definition: clock.hxx:41
qi::sleepFor
void sleepFor(const qi::Duration &d)
qi::to_string
std::string to_string(const DurationType< R, P > &d)
qi::Clock::now
static time_point now()
Returns a time_point representing the current value of the clock.
qi::detail::write_duration_unit_long_name
void write_duration_unit_long_name(std::ostream &os)
qi::SteadyClock::now
static time_point now()
Returns a time_point representing the current value of the clock.
qi::log::env::QI_DEFAULT_LOGHANDLER::name
char const *const name
qi::sleepUntil
void sleepUntil(const SteadyClockTimePoint &t)
Blocks the execution of the current thread until t has been reached.
qi::SystemClock::now
static time_point now()
Returns a time_point representing the current value of the clock.