Skip to main content

type-traits.h File

C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support. More...

Included Headers

#include <string_view> #include <type_traits> #include "math.h" #include "inlines/type-traits-inlines.h"

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacetype_traits

Type trait utilities and metaprogramming support for the µTest++ testing framework. More...

Classes Index

structfloating_point_constant<T, N, D, Size, P>

Struct template representing a generic floating point constant with custom size and precision. More...

structfunction_traits<R(*)(Args_T...)>

Struct template specialisation for extracting function traits from function pointer types. More...

structfunction_traits<R(Args_T...)>

Struct template specialisation for extracting function traits from plain function types. More...

structfunction_traits<R(T::*)(Args_T...) const>

Struct template specialisation for extracting function traits from const member function types. More...

structfunction_traits<R(T::*)(Args_T...)>

Struct template specialisation for extracting function traits from non-const member function types. More...

structgenuine_integral_value<T>

Struct template representing a genuine integral value. More...

structidentity<T, Extra>

Struct template for compile-time type identity. More...

structintegral_constant<N>

Struct template representing a generic integral constant. More...

structlist<Types>

Struct template representing a compile-time type list. More...

structop

Empty base struct for all operator types. More...

structvalue<T>

Struct template representing a generic value, accessible via a getter. More...

structvalue<T>

Struct template representing a floating point value with precision control. More...

structvalue_base_<T>

Base struct template providing common storage and accessors for runtime value-wrapper types. More...

Concepts Index

conceptany_op

C++20 concept satisfied when at least one of two types derives from op. More...

conceptcheckable

C++20 concept satisfied when a type can be used as a test expression in expect() or assume(). More...

conceptcontainer_like

C++20 concept satisfied when T provides both begin() and end() member functions. More...

concepthas_epsilon

C++20 concept satisfied when T provides an epsilon member. More...

concepthas_npos

C++20 concept satisfied when T provides a npos member. More...

concepthas_value

C++20 concept satisfied when T provides a value member. More...

conceptis_floating_point

C++20 concept satisfied when T is a standard floating point type. More...

conceptis_op

C++20 concept satisfied when a type derives from op. More...

conceptprintable

C++20 concept satisfied when a type can be appended to the deferred reporter's output via operator<<. More...

Description

C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.

This header provides the declarations for the type trait utilities and metaprogramming constructs used within the µTest++ framework. It defines templates and variable traits for function traits, type lists, identity, value wrappers, compile-time checks for container and floating-point types, and type convertibility.

These utilities underpin advanced template programming, type deduction, and compile-time introspection, supporting the flexible and type-safe design of the framework. The provided traits and wrappers enable expressive and generic handling of types, values, and callable objects, facilitating robust and maintainable test code.

All definitions reside within the micro_os_plus::micro_test_plus::type_traits namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

All header files are organised within the include/micro-os-plus/micro-test-plus folder to maintain a structured and modular codebase.

This file is intended for internal use within the framework and should not be included directly by user code.

File Listing

The file content with the documentation metadata removed is:

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 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>
509 struct value_base_ : op
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
544 T value_{};
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>
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// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.