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#if defined(__clang__)
79#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
80#endif
81#endif
82 const char* p = strrchr (name, '/');
83 if (p != nullptr)
84 return p + 1;
85 else
86 return name;
87#if defined(__GNUC__)
88#pragma GCC diagnostic pop
89#endif
90 }

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 145 of file reflection-inlines.h.

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

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.0 by Doxygen 1.17.0.