micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
type-traits.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-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * 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 be
9 * 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// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_TEST_PLUS_TYPE_TRAITS_H_
49#define MICRO_TEST_PLUS_TYPE_TRAITS_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57#include <string_view>
58#include <type_traits>
59
60#include "math.h"
61
62// ----------------------------------------------------------------------------
63
64#if defined(__GNUC__)
65#pragma GCC diagnostic push
66#if defined(__clang__)
67#pragma clang diagnostic ignored "-Wc++98-compat"
68#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
69#endif
70#endif
71
72// ============================================================================
73
75{
76 // --------------------------------------------------------------------------
77
102 namespace type_traits
103 {
122 template <class... Types>
123 struct list
124 {
125 };
126
148 template <class T, class... Extra>
149 struct identity
150 {
154 using type = T;
155 };
156
157#if defined(__DOXYGEN__)
158 // error: Detected potential recursive class relation between class
159 // micro_os_plus::micro_test_plus::type_traits::function_traits and base
160 // class micro_os_plus::micro_test_plus::type_traits::function_traits<
161 // decltype(&T::operator())>!
162 // https://github.com/doxygen/doxygen/issues/9915
163#else
184 template <class T>
185 struct function_traits : function_traits<decltype (&T::operator())>
186 {
187 };
188#endif
189
209 template <class R, class... Args_T>
210 struct function_traits<R (*) (Args_T...)>
211 {
215 using result_type = R;
216
220 using args = list<Args_T...>;
221 };
222
242 template <class R, class... Args_T>
243 struct function_traits<R (Args_T...)>
244 {
248 using result_type = R;
249
253 using args = list<Args_T...>;
254 };
255
276 template <class R, class T, class... Args_T>
277 struct function_traits<R (T::*) (Args_T...)>
278 {
282 using result_type = R;
283
287 using args = list<Args_T...>;
288 };
289
311 template <class R, class T, class... Args_T>
312 struct function_traits<R (T::*) (Args_T...) const>
313 {
317 using result_type = R;
318
322 using args = list<Args_T...>;
323 };
324
325 // ------------------------------------------------------------------------
326
343 struct op
344 {
345 };
346
347 // ------------------------------------------------------------------------
348 // Concepts.
349
362 template <class T>
363 concept container_like = requires (const T& t) {
364 t.begin ();
365 t.end ();
366 };
367
379 template <class T>
380 concept has_npos = requires { T::npos; };
381
393 template <class T>
394 concept has_value = requires (const T& t) { t.value; };
395
407 template <class T>
408 concept has_epsilon = requires (const T& t) { t.epsilon; };
409
422 template <class T>
423 concept is_floating_point = std::is_floating_point_v<T>;
424
436 template <class T>
437 concept is_op = std::is_base_of_v<type_traits::op, T>;
438
453 template <class Lhs_T, class Rhs_T>
455
469 template <class T>
470 concept checkable = is_op<T> or std::convertible_to<T, bool>;
471
485 template <class T>
486 concept printable = std::is_arithmetic_v<T>
487 or std::is_convertible_v<T, std::string_view>;
488
489 // ------------------------------------------------------------------------
490
508 template <class T>
510 {
514 using value_type = T;
515
521 constexpr explicit value_base_ (const T& v) noexcept;
522
528 [[nodiscard]] constexpr explicit
529 operator T () const noexcept;
530
538 [[nodiscard]] constexpr T
539 get (void) const noexcept;
540
545 };
546
565 template <auto N>
566 struct integral_constant : value_base_<decltype (N)>
567 {
571 static constexpr auto value = N;
572
576 constexpr integral_constant () noexcept;
577
583 [[nodiscard]] constexpr auto
584 operator- () const noexcept;
585 };
586
615 template <class T, auto N, auto D, auto Size, auto P = 1>
617 {
618 static_assert (P == 1 || P == -1,
619 "floating_point_constant: P must be +1 or -1");
620
624 using value_type = T;
625
632 static constexpr auto epsilon = T (1) / math::pow (T (10), Size - 1);
633
640 static constexpr auto value
641 = T (P) * (T (N) + (T (D) / math::pow (T (10), Size)));
642
648 [[nodiscard]] constexpr explicit
649 operator T () const noexcept;
650
658 [[nodiscard]] constexpr T
659 get (void) const noexcept;
660
666 [[nodiscard]] constexpr auto
667 operator- () const noexcept;
668 };
669
682 template <class T>
684 {
690 constexpr genuine_integral_value (const T& _value) noexcept;
691 };
692
706 template <class T>
707 struct value : value_base_<T>
708 {
714 constexpr value (const T& _value) noexcept;
715 };
716
739 template <class T>
740 requires is_floating_point<T>
741 struct value<T> : value_base_<T>
742 {
753 T epsilon = T{};
754
761 constexpr value (const T& _value, const T precision) noexcept;
762
768 constexpr value (const T& val);
769 };
770
771 // ------------------------------------------------------------------------
772 } // namespace type_traits
773
774 // --------------------------------------------------------------------------
775} // namespace micro_os_plus::micro_test_plus
776
777#if defined(__GNUC__)
778#pragma GCC diagnostic pop
779#endif
780
781// ----------------------------------------------------------------------------
782
783#endif // __cplusplus
784
785// ============================================================================
786// Templates & constexpr implementations.
787
789
790// ----------------------------------------------------------------------------
791
792#endif // MICRO_TEST_PLUS_TYPE_TRAITS_H_
793
794// ----------------------------------------------------------------------------
C++20 concept satisfied when at least one of two types derives from op.
C++20 concept satisfied when a type can be used as a test expression in expect() or assume().
C++20 concept satisfied when T provides both begin() and end() member functions.
C++20 concept satisfied when T provides an epsilon member.
C++20 concept satisfied when T provides a npos member.
C++20 concept satisfied when T provides a value member.
C++20 concept satisfied when T is a standard floating point type.
C++20 concept satisfied when a type derives from op.
C++20 concept satisfied when a type can be appended to the deferred reporter's output via operator<<.
C++ header file with declarations for the µTest++ mathematical utilities.
constexpr auto pow(const T base, const Exp_T exp) noexcept -> T
Generic exponentiation function to compute the power of a base raised to an exponent.
Type trait utilities and metaprogramming support for the µTest++ testing framework.
Primary namespace for the µTest++ testing framework.
Struct template representing a generic floating point constant with custom size and precision.
static constexpr auto epsilon
The epsilon value used for floating point comparisons.
constexpr T get(void) const noexcept
Getter for the compile-time constant value.
static constexpr auto value
The compile-time constant value.
constexpr genuine_integral_value(const T &_value) noexcept
Constructs a genuine_integral_value with the specified value.
Struct template for compile-time type identity.
static constexpr auto value
The compile-time constant value.
constexpr integral_constant() noexcept
Default constructor. Initialises the base with N.
Struct template representing a compile-time type list.
Empty base struct for all operator types.
T epsilon
The epsilon value used for floating-point comparisons.
constexpr value(const T &_value, const T precision) noexcept
Constructs a floating-point value with a specified precision.
constexpr value_base_(const T &v) noexcept
Constructs a value_base_ with the given value.
constexpr T get(void) const noexcept
Getter for the stored value.
constexpr value(const T &_value) noexcept
Constructs a value object with the specified value.
C++ header file with inline implementations for the µTest++ type trait utilities.