micro-test-plus 3.2.2
The µTest++ Testing Framework
Loading...
Searching...
No Matches
literals.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16#ifndef MICRO_TEST_PLUS_LITERALS_H_
17#define MICRO_TEST_PLUS_LITERALS_H_
18
19// ----------------------------------------------------------------------------
20
21#ifdef __cplusplus
22
23// ----------------------------------------------------------------------------
24
25#include "type-traits.h"
26#include "math.h"
27#include <cstdint>
28
29// ----------------------------------------------------------------------------
30
31#if defined(__GNUC__)
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Waggregate-return"
34#if defined(__clang__)
35#pragma clang diagnostic ignored "-Wc++98-compat"
36#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
37#endif
38#endif
39
41{
42 // --------------------------------------------------------------------------
43
48 namespace literals
49 {
54 template <char... Cs>
55 [[nodiscard]] constexpr auto
56 operator""_i ()
57 {
58 return type_traits::integral_constant<math::num<int, Cs...> ()>{};
59 }
60
65 template <char... Cs>
66 [[nodiscard]] constexpr auto
67 operator""_s ()
68 {
69 return type_traits::integral_constant<math::num<short, Cs...> ()>{};
70 }
71
76 template <char... Cs>
77 [[nodiscard]] constexpr auto
78 operator""_c ()
79 {
80 return type_traits::integral_constant<math::num<char, Cs...> ()>{};
81 }
82
87 template <char... Cs>
88 [[nodiscard]] constexpr auto
89 operator""_sc ()
90 {
92 math::num<signed char, Cs...> ()>{};
93 }
94
99 template <char... Cs>
100 [[nodiscard]] constexpr auto
101 operator""_l ()
102 {
103 return type_traits::integral_constant<math::num<long, Cs...> ()>{};
104 }
105
110 template <char... Cs>
111 [[nodiscard]] constexpr auto
112 operator""_ll ()
113 {
114 return type_traits::integral_constant<math::num<long long, Cs...> ()>{};
115 }
116
121 template <char... Cs>
122 [[nodiscard]] constexpr auto
123 operator""_u ()
124 {
125 return type_traits::integral_constant<math::num<unsigned, Cs...> ()>{};
126 }
127
132 template <char... Cs>
133 [[nodiscard]] constexpr auto
134 operator""_uc ()
135 {
137 math::num<unsigned char, Cs...> ()>{};
138 }
139
144 template <char... Cs>
145 [[nodiscard]] constexpr auto
146 operator""_us ()
147 {
149 math::num<unsigned short, Cs...> ()>{};
150 }
151
156 template <char... Cs>
157 [[nodiscard]] constexpr auto
158 operator""_ul ()
159 {
161 math::num<unsigned long, Cs...> ()>{};
162 }
163
168 template <char... Cs>
169 [[nodiscard]] constexpr auto
170 operator""_ull ()
171 {
173 math::num<unsigned long long, Cs...> ()>{};
174 }
175
180 template <char... Cs>
181 [[nodiscard]] constexpr auto
182 operator""_i8 ()
183 {
185 math::num<std::int8_t, Cs...> ()>{};
186 }
187
192 template <char... Cs>
193 [[nodiscard]] constexpr auto
194 operator""_i16 ()
195 {
197 math::num<std::int16_t, Cs...> ()>{};
198 }
199
204 template <char... Cs>
205 [[nodiscard]] constexpr auto
206 operator""_i32 ()
207 {
209 math::num<std::int32_t, Cs...> ()>{};
210 }
211
216 template <char... Cs>
217 [[nodiscard]] constexpr auto
218 operator""_i64 ()
219 {
221 math::num<std::int64_t, Cs...> ()>{};
222 }
223
228 template <char... Cs>
229 [[nodiscard]] constexpr auto
230 operator""_u8 ()
231 {
233 math::num<std::uint8_t, Cs...> ()>{};
234 }
235
240 template <char... Cs>
241 [[nodiscard]] constexpr auto
242 operator""_u16 ()
243 {
245 math::num<std::uint16_t, Cs...> ()>{};
246 }
247
252 template <char... Cs>
253 [[nodiscard]] constexpr auto
254 operator""_u32 ()
255 {
257 math::num<std::uint32_t, Cs...> ()>{};
258 }
259
264 template <char... Cs>
265 [[nodiscard]] constexpr auto
266 operator""_u64 ()
267 {
269 math::num<std::uint64_t, Cs...> ()>{};
270 }
271
276 template <char... Cs>
277 [[nodiscard]] constexpr auto
278 operator""_f ()
279 {
281 float, math::num<unsigned long, Cs...> (),
283 math::den_size<unsigned long, Cs...> ()>{};
284 }
285
290 template <char... Cs>
291 [[nodiscard]] constexpr auto
292 operator""_d ()
293 {
295 double, math::num<unsigned long, Cs...> (),
297 math::den_size<unsigned long, Cs...> ()>{};
298 }
299
304 template <char... Cs>
305 [[nodiscard]] constexpr auto
306 operator""_ld ()
307 {
309 long double, math::num<unsigned long long, Cs...> (),
311 math::den_size<unsigned long long, Cs...> ()>{};
312 }
313
318 constexpr auto
319 operator""_b (const char* name, decltype (sizeof ("")) size)
320 {
321 struct named : std::string_view, type_traits::op
322 {
323 using value_type = bool;
324 [[nodiscard]] constexpr
325 operator value_type () const
326 {
327 return true;
328 }
329
330 [[nodiscard]] constexpr auto
331 operator== (const named&) const
332 {
333 return true;
334 }
335
336 [[nodiscard]] constexpr auto
337 operator== (const bool other) const
338 {
339 return other;
340 }
341 };
342
343 return named{ { name, size }, {} };
344 }
345 } // namespace literals
346
347 // --------------------------------------------------------------------------
348
349#if defined(__GNUC__)
350#pragma GCC diagnostic push
351#if defined(__clang__)
352#pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync"
353#endif
354#endif
355
402
407 template <class T>
409 {
410 constexpr explicit _t (const T& t) : type_traits::value<T>{ t }
411 {
412 }
413 };
414
415#if defined(__GNUC__)
416#pragma GCC diagnostic pop
417#endif
418
423
424 // Wrappers that can be used to convert dynamic values to specific types
425 // that are recognised by the comparators.
426 // The syntax is similar to function calls, like `_i(expression)`, but the
427 // results have custom types expected by comparators.
474
478
488 template <class T>
490 {
492 constexpr explicit to_t (const T& t) : type_traits::value<T>{ t }
493 {
494 }
495 };
496
497 // --------------------------------------------------------------------------
498} // namespace micro_os_plus::micro_test_plus
499
500#if defined(__GNUC__)
501#pragma GCC diagnostic pop
502#endif
503
504// ----------------------------------------------------------------------------
505
506#endif // __cplusplus
507
508// ----------------------------------------------------------------------------
509
510#endif // MICRO_TEST_PLUS_LITERALS_H_
511
512// ----------------------------------------------------------------------------
type_traits::value< short > to_s
Definition literals.h:435
type_traits::value< std::int64_t > to_i64
Definition literals.h:459
type_traits::value< long double > to_ld
Definition literals.h:473
type_traits::value< double > to_d
Definition literals.h:471
type_traits::value< std::uint32_t > to_u32
Definition literals.h:465
type_traits::value< float > to_f
Definition literals.h:469
type_traits::value< unsigned long long > to_ull
Definition literals.h:451
type_traits::value< char > to_c
Definition literals.h:431
type_traits::value< std::uint64_t > to_u64
Definition literals.h:467
type_traits::value< bool > to_b
Definition literals.h:429
type_traits::value< std::uint8_t > to_u8
Definition literals.h:461
type_traits::value< unsigned char > to_uc
Definition literals.h:445
type_traits::value< std::int32_t > to_i32
Definition literals.h:457
type_traits::value< unsigned long > to_ul
Definition literals.h:449
type_traits::value< std::uint16_t > to_u16
Definition literals.h:463
type_traits::value< std::int8_t > to_i8
Definition literals.h:453
type_traits::value< long > to_l
Definition literals.h:439
type_traits::value< int > to_i
Definition literals.h:437
type_traits::value< long long > to_ll
Definition literals.h:441
type_traits::value< unsigned > to_u
Definition literals.h:443
type_traits::value< unsigned short > to_us
Definition literals.h:447
type_traits::value< signed char > to_sc
Definition literals.h:433
type_traits::value< std::int16_t > to_i16
Definition literals.h:455
User Defined Literals (UDL). Use them to suffix constants and obtain specific explicit types,...
Definition literals.h:49
constexpr auto den() -> T
Compute the decimals of a number represented as an array of characters.
Definition math.h:113
constexpr auto num() -> T
Compute the integral value of a number represented as an array of characters.
Definition math.h:87
constexpr auto den_size() -> T
Compute the number of decimal places of a number represented as an array of characters.
Definition math.h:135
Local type traits. Some may have standard equivalents, but better keep them locally.
Definition type-traits.h:46
type_traits::value< std::uint16_t > _u16
Definition literals.h:391
type_traits::value< unsigned long long > _ull
Definition literals.h:379
type_traits::value< short > _s
Definition literals.h:363
type_traits::value< int > _i
Definition literals.h:365
type_traits::value< unsigned char > _uc
Definition literals.h:373
type_traits::value< std::uint8_t > _u8
Definition literals.h:389
type_traits::value< std::int8_t > _i8
Definition literals.h:381
type_traits::value< std::int32_t > _i32
Definition literals.h:385
type_traits::value< unsigned short > _us
Definition literals.h:375
type_traits::value< double > _d
Definition literals.h:399
type_traits::value< long long > _ll
Definition literals.h:369
type_traits::value< unsigned > _u
Definition literals.h:371
type_traits::value< bool > _b
Definition literals.h:357
type_traits::value< long double > _ld
Definition literals.h:401
type_traits::value< unsigned long > _ul
Definition literals.h:377
type_traits::value< long > _l
Definition literals.h:367
type_traits::value< char > _c
Definition literals.h:359
type_traits::value< std::uint64_t > _u64
Definition literals.h:395
type_traits::value< signed char > _sc
Definition literals.h:361
type_traits::value< float > _f
Definition literals.h:397
type_traits::value< std::int64_t > _i64
Definition literals.h:387
type_traits::value< std::int16_t > _i16
Definition literals.h:383
type_traits::value< std::uint32_t > _u32
Definition literals.h:393
constexpr _t(const T &t)
Definition literals.h:410
constexpr to_t(const T &t)
Constructor.
Definition literals.h:492
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.