Skip to content

Commit cd32e01

Browse files
committed
✨ Add chrono and string literals
C++14 introduces literals for string objects and chrono literals. This adds the literals introduced in C++14 to Backport Fixes #2
1 parent 7a954ec commit cd32e01

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ set(header_files
5656
"include/bpstd/optional.hpp"
5757
"include/bpstd/iterator.hpp"
5858
"include/bpstd/span.hpp"
59+
"include/bpstd/chrono.hpp"
60+
"include/bpstd/string.hpp"
5961
)
6062

6163
add_library(${PROJECT_NAME} INTERFACE)

include/bpstd/chrono.hpp

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*****************************************************************************
2+
* \file chrono.hpp
3+
*
4+
* \brief This header provides definitions from the C++ header <chrono>
5+
*****************************************************************************/
6+
7+
/*
8+
The MIT License (MIT)
9+
10+
Copyright (c) 2020 Matthew Rodusek All rights reserved.
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy
13+
of this software and associated documentation files (the "Software"), to deal
14+
in the Software without restriction, including without limitation the rights
15+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
copies of the Software, and to permit persons to whom the Software is
17+
furnished to do so, subject to the following conditions:
18+
19+
The above copyright notice and this permission notice shall be included in
20+
all copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
SOFTWARE.
29+
*/
30+
#ifndef BPSTD_CHRONO_HPP
31+
#define BPSTD_CHRONO_HPP
32+
33+
#include <chrono> // std::chrono::duration, std::chrono::system_clock, etc
34+
#include <cstdint> // std::int32_t
35+
36+
namespace bpstd {
37+
namespace chrono {
38+
39+
template <typename Rep, typename Period = std::ratio<1>>
40+
using duration = std::chrono::duration<Rep,Period>;
41+
42+
template <typename Clock, typename Duration = typename Clock::duration>
43+
using time_point = std::chrono::time_point<Clock, Duration>;
44+
45+
using nanoseconds = std::chrono::nanoseconds;
46+
using microseconds = std::chrono::microseconds;
47+
using milliseconds = std::chrono::milliseconds;
48+
using seconds = std::chrono::seconds;
49+
using minutes = std::chrono::minutes;
50+
using hours = std::chrono::hours;
51+
using days = std::chrono::duration<std::int32_t, std::ratio<86400>>;
52+
using weeks = std::chrono::duration<std::int32_t, std::ratio<604800>>;
53+
using months = std::chrono::duration<std::int32_t, std::ratio<2629746>>;
54+
using years = std::chrono::duration<std::int32_t, std::ratio<31556952>>;
55+
56+
using steady_clock = std::chrono::steady_clock;
57+
using system_clock = std::chrono::system_clock;
58+
59+
template <typename Duration>
60+
using sys_time = time_point<system_clock, Duration>;
61+
using sys_seconds = sys_time<seconds>;
62+
using sys_days = sys_time<days>;
63+
64+
65+
} // namespace chrono
66+
67+
//============================================================================
68+
// non-member functions
69+
//============================================================================
70+
71+
//----------------------------------------------------------------------------
72+
// Literals
73+
//----------------------------------------------------------------------------
74+
75+
inline namespace literals {
76+
inline namespace chrono_literals {
77+
78+
constexpr std::chrono::hours operator""_h(unsigned long long x);
79+
constexpr std::chrono::duration<double, std::ratio<3600,1>> operator""_h(long double x);
80+
81+
constexpr chrono::minutes operator""_min(unsigned long long x);
82+
constexpr chrono::duration<double, std::ratio<60>> operator""_min(long double x);
83+
84+
constexpr chrono::seconds operator""_s(unsigned long long x);
85+
constexpr chrono::duration<double> operator""_s(long double x);
86+
87+
constexpr chrono::milliseconds operator""_ms(unsigned long long x);
88+
constexpr chrono::duration<double, std::milli> operator""_ms(long double x);
89+
90+
constexpr chrono::microseconds operator""_us(unsigned long long x);
91+
constexpr chrono::duration<double, std::micro> operator""_us(long double x);
92+
93+
constexpr chrono::nanoseconds operator""_ns(unsigned long long x);
94+
constexpr chrono::duration<double, std::nano> operator""_ns(long double x);
95+
96+
} // inline namespace chrono_literals
97+
} // inline namespace chrono
98+
} // namespace bpstd
99+
100+
inline constexpr std::chrono::hours
101+
bpstd::literals::chrono_literals::operator""_h(unsigned long long x)
102+
{
103+
return chrono::hours{x};
104+
}
105+
106+
inline constexpr std::chrono::duration<double, std::ratio<3600,1>>
107+
bpstd::literals::chrono_literals::operator""_h(long double x)
108+
{
109+
return chrono::duration<double, std::ratio<3600,1>>{x};
110+
}
111+
112+
inline constexpr bpstd::chrono::minutes
113+
bpstd::literals::chrono_literals::operator""_min(unsigned long long x)
114+
{
115+
return chrono::minutes{x};
116+
}
117+
inline constexpr bpstd::chrono::duration<double, std::ratio<60>>
118+
bpstd::literals::chrono_literals::operator""_min(long double x)
119+
{
120+
return chrono::duration<double, std::ratio<60>>{x};
121+
}
122+
123+
inline constexpr bpstd::chrono::seconds
124+
bpstd::literals::chrono_literals::operator""_s(unsigned long long x)
125+
{
126+
return chrono::seconds{x};
127+
}
128+
129+
inline constexpr bpstd::chrono::duration<double>
130+
bpstd::literals::chrono_literals::operator""_s(long double x)
131+
{
132+
return chrono::duration<double>{x};
133+
}
134+
135+
inline constexpr bpstd::chrono::milliseconds
136+
bpstd::literals::chrono_literals::operator""_ms(unsigned long long x)
137+
{
138+
return chrono::milliseconds{x};
139+
}
140+
141+
inline constexpr bpstd::chrono::duration<double, std::milli>
142+
bpstd::literals::chrono_literals::operator""_ms(long double x)
143+
{
144+
return chrono::duration<double, std::milli>{x};
145+
}
146+
147+
inline constexpr bpstd::chrono::microseconds
148+
bpstd::literals::chrono_literals::operator ""_us(unsigned long long x)
149+
{
150+
return chrono::microseconds{x};
151+
}
152+
153+
inline constexpr bpstd::chrono::duration<double, std::micro>
154+
bpstd::literals::chrono_literals::operator""_us(long double x)
155+
{
156+
return chrono::duration<double, std::micro>{x};
157+
}
158+
159+
inline constexpr bpstd::chrono::nanoseconds
160+
bpstd::literals::chrono_literals::operator""_ns(unsigned long long x)
161+
{
162+
return chrono::nanoseconds{x};
163+
}
164+
165+
inline constexpr bpstd::chrono::duration<double, std::nano>
166+
bpstd::literals::chrono_literals::operator""_ns(long double x)
167+
{
168+
return chrono::duration<double, std::nano>{x};
169+
}
170+
171+
172+
#endif /* BPSTD_CHRONO_HPP */

include/bpstd/string.hpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*****************************************************************************
2+
* \file string.hpp
3+
*
4+
* \brief This header provides definitions from the C++ header <string>
5+
*****************************************************************************/
6+
7+
/*
8+
The MIT License (MIT)
9+
10+
Copyright (c) 2020 Matthew Rodusek All rights reserved.
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy
13+
of this software and associated documentation files (the "Software"), to deal
14+
in the Software without restriction, including without limitation the rights
15+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
copies of the Software, and to permit persons to whom the Software is
17+
furnished to do so, subject to the following conditions:
18+
19+
The above copyright notice and this permission notice shall be included in
20+
all copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
SOFTWARE.
29+
*/
30+
#ifndef BPSTD_STRING_HPP
31+
#define BPSTD_STRING_HPP
32+
33+
#include <string>
34+
35+
namespace bpstd {
36+
37+
inline namespace literals {
38+
inline namespace string_literals {
39+
40+
std::string operator""_s(const char* s, std::size_t len);
41+
std::u16string operator""_s(const char16_t* s, std::size_t len);
42+
std::u32string operator""_s(const char32_t* s, std::size_t len);
43+
std::wstring operator""_s(const wchar_t* s, std::size_t len);
44+
45+
} // inline namespace string_literals
46+
} // inline namespace literals
47+
48+
} // namespace bpstd
49+
50+
inline std::string
51+
bpstd::literals::string_literals::operator""_s(const char* s, std::size_t len)
52+
{
53+
return std::string{s, len};
54+
}
55+
56+
inline std::u16string
57+
bpstd::literals::string_literals::operator""_s(const char16_t* s, std::size_t len)
58+
{
59+
return std::u16string{s, len};
60+
}
61+
62+
inline std::u32string
63+
bpstd::literals::string_literals::operator""_s(const char32_t* s, std::size_t len)
64+
{
65+
return std::u32string{s, len};
66+
}
67+
68+
inline std::wstring
69+
bpstd::literals::string_literals::operator""_s(const wchar_t* s, std::size_t len)
70+
{
71+
return std::wstring{s, len};
72+
}
73+
74+
75+
#endif /* BPSTD_STRING_HPP */

0 commit comments

Comments
 (0)