micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
literals.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_LITERALS_H_
18#define MICRO_TEST_PLUS_LITERALS_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26#include "type-traits.h"
27#include "math.h"
28#include <cstdint>
29
30// ----------------------------------------------------------------------------
31
32#if defined(__GNUC__)
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Waggregate-return"
35#if defined(__clang__)
36#pragma clang diagnostic ignored "-Wc++98-compat"
37#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
38#endif
39#endif
40
42{
43 // --------------------------------------------------------------------------
44
49 namespace literals
50 {
55 template <char... Cs>
56 [[nodiscard]] constexpr auto
57 operator""_i ()
58 {
59 return type_traits::integral_constant<math::num<int, Cs...> ()>{};
60 }
61
66 template <char... Cs>
67 [[nodiscard]] constexpr auto
68 operator""_s ()
69 {
70 return type_traits::integral_constant<math::num<short, Cs...> ()>{};
71 }
72
77 template <char... Cs>
78 [[nodiscard]] constexpr auto
79 operator""_c ()
80 {
81 return type_traits::integral_constant<math::num<char, Cs...> ()>{};
82 }
83
88 template <char... Cs>
89 [[nodiscard]] constexpr auto
90 operator""_sc ()
91 {
93 math::num<signed char, Cs...> ()>{};
94 }
95
100 template <char... Cs>
101 [[nodiscard]] constexpr auto
102 operator""_l ()
103 {
104 return type_traits::integral_constant<math::num<long, Cs...> ()>{};
105 }
106
111 template <char... Cs>
112 [[nodiscard]] constexpr auto
113 operator""_ll ()
114 {
115 return type_traits::integral_constant<math::num<long long, Cs...> ()>{};
116 }
117
122 template <char... Cs>
123 [[nodiscard]] constexpr auto
124 operator""_u ()
125 {
126 return type_traits::integral_constant<math::num<unsigned, Cs...> ()>{};
127 }
128
133 template <char... Cs>
134 [[nodiscard]] constexpr auto
135 operator""_uc ()
136 {
138 math::num<unsigned char, Cs...> ()>{};
139 }
140
145 template <char... Cs>
146 [[nodiscard]] constexpr auto
147 operator""_us ()
148 {
150 math::num<unsigned short, Cs...> ()>{};
151 }
152
157 template <char... Cs>
158 [[nodiscard]] constexpr auto
159 operator""_ul ()
160 {
162 math::num<unsigned long, Cs...> ()>{};
163 }
164
169 template <char... Cs>
170 [[nodiscard]] constexpr auto
171 operator""_ull ()
172 {
174 math::num<unsigned long long, Cs...> ()>{};
175 }
176
181 template <char... Cs>
182 [[nodiscard]] constexpr auto
183 operator""_i8 ()
184 {
186 math::num<std::int8_t, Cs...> ()>{};
187 }
188
193 template <char... Cs>
194 [[nodiscard]] constexpr auto
195 operator""_i16 ()
196 {
198 math::num<std::int16_t, Cs...> ()>{};
199 }
200
205 template <char... Cs>
206 [[nodiscard]] constexpr auto
207 operator""_i32 ()
208 {
210 math::num<std::int32_t, Cs...> ()>{};
211 }
212
217 template <char... Cs>
218 [[nodiscard]] constexpr auto
219 operator""_i64 ()
220 {
222 math::num<std::int64_t, Cs...> ()>{};
223 }
224
229 template <char... Cs>
230 [[nodiscard]] constexpr auto
231 operator""_u8 ()
232 {
234 math::num<std::uint8_t, Cs...> ()>{};
235 }
236
241 template <char... Cs>
242 [[nodiscard]] constexpr auto
243 operator""_u16 ()
244 {
246 math::num<std::uint16_t, Cs...> ()>{};
247 }
248
253 template <char... Cs>
254 [[nodiscard]] constexpr auto
255 operator""_u32 ()
256 {
258 math::num<std::uint32_t, Cs...> ()>{};
259 }
260
265 template <char... Cs>
266 [[nodiscard]] constexpr auto
267 operator""_u64 ()
268 {
270 math::num<std::uint64_t, Cs...> ()>{};
271 }
272
277 template <char... Cs>
278 [[nodiscard]] constexpr auto
279 operator""_f ()
280 {
282 float, math::num<unsigned long, Cs...> (),
283 math::den<unsigned long, Cs...> (),
284 math::den_size<unsigned long, Cs...> ()>{};
285 }
286
291 template <char... Cs>
292 [[nodiscard]] constexpr auto
293 operator""_d ()
294 {
296 double, math::num<unsigned long, Cs...> (),
297 math::den<unsigned long, Cs...> (),
298 math::den_size<unsigned long, Cs...> ()>{};
299 }
300
305 template <char... Cs>
306 [[nodiscard]] constexpr auto
307 operator""_ld ()
308 {
310 long double, math::num<unsigned long long, Cs...> (),
311 math::den<unsigned long long, Cs...> (),
312 math::den_size<unsigned long long, Cs...> ()>{};
313 }
314
319 constexpr auto
320 operator""_b (const char* name, decltype (sizeof ("")) size)
321 {
322 struct named : std::string_view, type_traits::op
323 {
324 using value_type = bool;
325 [[nodiscard]] constexpr
326 operator value_type () const
327 {
328 return true;
329 }
330
331 [[nodiscard]] constexpr auto
332 operator== (const named&) const
333 {
334 return true;
335 }
336
337 [[nodiscard]] constexpr auto
338 operator== (const bool other) const
339 {
340 return other;
341 }
342 };
343
344 return named{ { name, size }, {} };
345 }
346 } // namespace literals
347
348 // --------------------------------------------------------------------------
349
350#if defined(__GNUC__)
351#pragma GCC diagnostic push
352#if defined(__clang__)
353#pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync"
354#endif
355#endif
356
403
408 template <class T>
410 {
411 constexpr explicit _t (const T& t) : type_traits::value<T>{ t }
412 {
413 }
414 };
415
416#if defined(__GNUC__)
417#pragma GCC diagnostic pop
418#endif
419
425 // Wrappers that can be used to convert dynamic values to specific types
426 // that are recognised by the comparators.
427 // The syntax is similar to function calls, like `_i(expression)`, but the
428 // results have custom types expected by comparators.
475
489 template <class T>
491 {
493 constexpr explicit to_t (const T& t) : type_traits::value<T>{ t }
494 {
495 }
496 };
497
498 // --------------------------------------------------------------------------
499} // namespace micro_os_plus::micro_test_plus
500
501#if defined(__GNUC__)
502#pragma GCC diagnostic pop
503#endif
504
505// ----------------------------------------------------------------------------
506
507#endif // __cplusplus
508
509// ----------------------------------------------------------------------------
510
511#endif // MICRO_TEST_PLUS_LITERALS_H_
512
513// ----------------------------------------------------------------------------
constexpr auto num() -> T
Compute the integral value of a number represented as an array of characters.
Definition math.h:88
constexpr auto den_size() -> T
Compute the number of decimal places of a number represented as an array of characters.
Definition math.h:136
type_traits::value< std::uint16_t > _u16
Definition literals.h:392
type_traits::value< unsigned long long > _ull
Definition literals.h:380
type_traits::value< short > _s
Definition literals.h:364
type_traits::value< int > _i
Definition literals.h:366
type_traits::value< unsigned char > _uc
Definition literals.h:374
type_traits::value< std::uint8_t > _u8
Definition literals.h:390
type_traits::value< std::int8_t > _i8
Definition literals.h:382
type_traits::value< std::int32_t > _i32
Definition literals.h:386
type_traits::value< unsigned short > _us
Definition literals.h:376
type_traits::value< double > _d
Definition literals.h:400
type_traits::value< long long > _ll
Definition literals.h:370
type_traits::value< unsigned > _u
Definition literals.h:372
type_traits::value< bool > _b
Definition literals.h:358
type_traits::value< long double > _ld
Definition literals.h:402
type_traits::value< unsigned long > _ul
Definition literals.h:378
type_traits::value< long > _l
Definition literals.h:368
type_traits::value< char > _c
Definition literals.h:360
type_traits::value< std::uint64_t > _u64
Definition literals.h:396
type_traits::value< signed char > _sc
Definition literals.h:362
type_traits::value< float > _f
Definition literals.h:398
type_traits::value< std::int64_t > _i64
Definition literals.h:388
type_traits::value< std::int16_t > _i16
Definition literals.h:384
type_traits::value< std::uint32_t > _u32
Definition literals.h:394
constexpr _t(const T &t)
Definition literals.h:411
Template for wrapping any other type.
Definition literals.h:491
constexpr to_t(const T &t)
Constructor.
Definition literals.h:493
A generic floating point constant, with custom size and precision. It has a getter and a '-' operator...
A generic integral constant. It has a getter and a '-' operator to return the negative value.
Empty base class of all operators.
Class defining a generic value, accessible via a getter.