Mistake on this page?
Report an issue in GitHub or email us
mbed_chrono.h
1 
2 /*
3  * Copyright (c) 2015-2019, ARM Limited, All Rights Reserved
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef __MBED_CHRONO_H__
20 #define __MBED_CHRONO_H__
21 
22 #include "mbed_toolchain.h"
23 #include <cstdint>
24 #include <cassert>
25 #include <ratio>
26 #include <chrono>
27 
28 /** \addtogroup platform-public-api */
29 /** @{*/
30 
31 /**
32  * \defgroup platform_chrono chrono utilities
33  *
34  * Additions and variations of std::chrono
35  *
36  * - unsigned 32-bit variants of standard signed 64-bit duration types
37  * - centiseconds and deciseconds
38  * @{
39  */
40 namespace mbed {
41 
42 /* Extensions declared in mbed::chrono, following pattern of std::chrono */
43 namespace chrono {
44 
45 /* Add deciseconds and centiseconds - may be
46  * useful to use lower precision when not messing with templating.
47  */
48 using deciseconds = std::chrono::duration<long long, std::deci>;
49 using centiseconds = std::chrono::duration<long long, std::centi>;
50 
51 /** 32-bit microsecond duration type
52  *
53  * Standard std::chrono::microseconds is signed 64-bit. For some purposes
54  * it's more efficient to process small times as 32-bit. And when doing so,
55  * as we likely need to worry about wrapping, use of an unsigned
56  * value to process modulo 2**32 is appropriate.
57  */
58 using microseconds_u32 = std::chrono::duration<std::uint32_t, std::micro>;
59 
60 /** 32-bit millisecond duration type
61  *
62  * Standard std::chrono::milliseconds is signed 64-bit. For some purposes
63  * it's more efficient to process times as 32-bit. And when doing so,
64  * as we likely need to worry about wrapping, use of an unsigned
65  * value to process modulo 2**32 is appropriate.
66  */
67 using milliseconds_u32 = std::chrono::duration<std::uint32_t, std::milli>;
68 
69 } // namespace chrono
70 
71 inline namespace literals {
72 
73 inline namespace chrono_literals {
74 
75 /** User-defined literal for deciseconds (1/10 of a second)
76  *
77  * Useful in case we might change kernel tick frequency to be slower - with tick frequency 1000Hz, it is
78  * possible to assign 500ms to a KernelClock::duration, but that would fail at slower rates.
79  *
80  * Example use:
81  *
82  * using namespace mbed::chrono_literals;
83  *
84  * ThisThread::sleep_for(5_ds);
85  */
86 constexpr chrono::deciseconds operator "" _ds(unsigned long long x)
87 {
88  chrono::deciseconds::rep val = static_cast<chrono::deciseconds::rep>(x);
89  assert(val >= 0 && static_cast<unsigned long long>(val) == x);
90  return chrono::deciseconds(val);
91 }
92 
93 /** User-defined literal for centiseconds (1/100 of a second)
94  *
95  * Useful in case we might change kernel tick frequency to be slower - with tick frequency 1000Hz, it is
96  * possible to assign 500ms to a KernelClock::duration, but that would fail at slower rates.
97  *
98  * Example use:
99  *
100  * using namespace mbed::chrono_literals;
101  *
102  * ThisThread::sleep_for(1_cs);
103  */
104 constexpr chrono::centiseconds operator "" _cs(unsigned long long x)
105 {
106  chrono::centiseconds::rep val = static_cast<chrono::centiseconds::rep>(x);
107  assert(val >= 0 && static_cast<unsigned long long>(val) == x);
108  return chrono::centiseconds(val);
109 }
110 
111 } // inline namespace chrono_literals
112 
113 } // inline namespace literals
114 
115 namespace chrono {
116 
117 using namespace chrono_literals;
118 
119 } // namespace chrono
120 
121 } // namespace mbed
122 
123 /**@}*/
124 
125 /**@}*/
126 
127 #endif // __MBED_CHRONO_H__
128 
Definition: ATHandler.h:46
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.