micro-test-plus 6.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::reflection Namespace Reference

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

Classes

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

Functions

const char * short_name (const char *name) noexcept
 Deprecated extraction of the file name from a path.
template<class T>
constexpr auto type_name (void) -> std::string_view
 Extracts the type name from the __PRETTY_FUNCTION__ identifier.

Detailed Description

The reflection namespace provides facilities for obtaining source location information and type names at compile time, for the reports of the checks.

It includes a local implementation of source_location for standard libraries that do not yet provide std::source_location, as well as a utility for extracting concise type names from the compiler-specific __PRETTY_FUNCTION__ identifier.

Function Documentation

◆ type_name()

template<class T>
auto micro_os_plus::micro_test_plus::reflection::type_name ( void ) -> std::string_view
nodiscardconstexpr

This function template parses the compiler-specific __PRETTY_FUNCTION__ identifier 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 the marker that introduces the template argument in the function signature string:

  • Clang formats the signature as "... [T = <typename>]", so the type name lies between the first "[T = " and the last ']'. The marker is searched for as a whole, since the type name may itself contain brackets (e.g. int[3]).
  • GCC formats the signature as "... [with T = <typename>]" or, on some versions, "... [with T = <typename>; ...]", so the type name starts after the first "[with T = " and ends at the following ';' or, if there is none, at the last ']'.

If the marker is not found, or the compiler is not supported (e.g. MSVC, which uses __FUNCSIG__ instead of __PRETTY_FUNCTION__), the function returns "unknown" rather than failing to compile, since the name is used only to improve the test output.

The expected layouts are verified at compile time by the static_assert checks that follow the definition.

The name is spelled as the compiler prints it, and is used to identify the expected exception type in the throws<E> failure messages. Only clang and GCC are supported; with other compilers the function returns "unknown".

Template Parameters
TThe type whose name is to be extracted.
Parameters
None.
Returns
A std::string_view containing the extracted type name, or "unknown" if it cannot be determined.

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

150 {
151#if defined(__clang__)
152 // Clang: "... [T = <typename>]"
153 const std::string_view sv = __PRETTY_FUNCTION__;
154 constexpr std::string_view marker{ "[T = " };
155 const auto pos = sv.find (marker);
156 const auto end = sv.rfind (']');
157#elif defined(__GNUC__)
158 // GCC: "... [with T = <typename>]" or, on some versions,
159 // "... [with T = <typename>; std::string_view = ...]"
160 const std::string_view sv = __PRETTY_FUNCTION__;
161 constexpr std::string_view marker{ "[with T = " };
162 const auto pos = sv.find (marker);
163 const auto semi = sv.find (';', pos);
164 const auto end
165 = (semi != std::string_view::npos) ? semi : sv.rfind (']');
166#else
167 // Unsupported compiler; no portable signature string is available.
168 const std::string_view sv{};
169 constexpr std::string_view marker{};
170 const auto pos = std::string_view::npos;
171 const auto end = std::string_view::npos;
172#endif // defined(__clang__) || defined(__GNUC__)
173 if (pos == std::string_view::npos || end == std::string_view::npos
174 || end < pos + marker.size ())
175 {
176 return "unknown";
177 }
178 const auto start = pos + marker.size ();
179 return sv.substr (start, end - start);
180 }

References type_name().

Referenced by micro_os_plus::micro_test_plus::detail::expression_formatter::operator<<(), and type_name().