Skip to main content

reflection Namespace

Reflection utilities for the µTest++ testing framework. More...

Definition

namespace micro_os_plus::micro_test_plus::reflection { ... }

Classes Index

classsource_location

Local implementation of source location information for diagnostics. More...

Functions Index

const char *short_name (const char *name) noexcept

Extract a short type or function name from a fully qualified name. More...

template <class T>
constexpr std::string_viewtype_name (void)

Extract the type name from the __PRETTY_FUNCTION__ macro. More...

Description

Reflection utilities for the µTest++ testing framework.

The reflection namespace provides facilities for obtaining source location information and type names at compile time, thereby supporting advanced reporting and diagnostics within the µTest++ framework.

It includes a local implementation of source_location for environments lacking C++20 standard support, as well as utilities for extracting concise type names from compiler-specific macros such as __PRETTY_FUNCTION__.

All definitions within this namespace are intended to facilitate advanced reflection and reporting capabilities.

Functions

short_name()

const char * micro_os_plus::micro_test_plus::reflection::short_name (const char * name)
noexcept

Extract a short type or function name from a fully qualified name.

Parameters
name

The fully qualified name as a C-string.

Returns

A pointer to the short name within the input string.

This function extracts the short name from a given file path by locating the final folder separator ('/'). If a separator is found, it returns a pointer to the character immediately following it, effectively providing the file or folder name. If no separator is present, the original input string is returned. This utility is useful for reporting concise file or folder names in test output.

Definition at line 74 of file reflection.cpp.

74 short_name (const char* name) noexcept
75 {
76#if defined(__GNUC__)
77#pragma GCC diagnostic push
78
79#if defined(__clang__)
80#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
81#endif // defined(__clang__)
82#endif // defined(__GNUC__)
83
84 const char* p = strrchr (name, '/');
85 if (p != nullptr)
86 return p + 1;
87 else
88 return name;
89
90#if defined(__GNUC__)
91#pragma GCC diagnostic pop
92#endif // defined(__GNUC__)
93 }

Referenced by micro_os_plus::micro_test_plus::runner::abort, micro_os_plus::micro_test_plus::reporter_human::output_fail_prefix_ and micro_os_plus::micro_test_plus::reporter_tap::output_fail_suffix_.

type_name()

template <class T>
std::string_view micro_os_plus::micro_test_plus::reflection::type_name (void)
nodiscard constexpr

Extract the type name from the __PRETTY_FUNCTION__ macro.

This function template parses the compiler-specific __PRETTY_FUNCTION__ macro to extract a concise type name for the template parameter T.

Rather than relying on fixed character offsets (which are fragile across compiler versions and namespace changes), the implementation searches for well-known marker characters in the function signature string:

  • Clang formats the signature as "... [T = <typename>]", so the type name lies between the last '[' (skipping "[T = ") and the last ']'.
  • GCC formats the signature as "... [with T = <typename>]", so the type name lies between the last '=' (skipping the trailing space) and the last ']'.

This approach is resilient to namespace renaming, namespace nesting changes, and compiler format updates.

Template Parameters
T

The type whose name is to be extracted.

Parameters

None.

Returns

A std::string_view containing the extracted type name.

Definition at line 146 of file reflection-inlines.h.

146 type_name (void) -> std::string_view
147 {
148 const std::string_view sv = __PRETTY_FUNCTION__;
149#if defined(__clang__)
150 // Clang: "... [T = <typename>]"
151 // rfind('[') locates the opening bracket of "[T = ...]".
152 const auto start = sv.rfind ('[') + 5; // skip "[T = "
153 const auto end = sv.rfind (']');
154#elif defined(__GNUC__)
155 // GCC: "... [with T = <typename>]" or, on some versions,
156 // "... [with T = <typename>; std::string_view = ...]"
157 // Search for "T = " explicitly to avoid landing on a later '='.
158 const auto t_eq = sv.find ("T = ");
159 const auto start = t_eq + 4; // skip "T = "
160 const auto semi = sv.find (';', start);
161 const auto end
162 = (semi != std::string_view::npos) ? semi : sv.rfind (']');
163#else
164// Note: MSVC uses __FUNCSIG__ instead of __PRETTY_FUNCTION__.
165// MSVC is not a supported target for this framework.
166#error "Unsupported compiler"
167#endif // defined(__clang__) || defined(__GNUC__)
168 return sv.substr (start, end - start);
169 }

Referenced by micro_os_plus::micro_test_plus::detail::expression_formatter::operator<<.


The documentation for this namespace was generated from the following files:


Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.