micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
reflection.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_REFLECTION_H_
18#define MICRO_TEST_PLUS_REFLECTION_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26#include <string_view>
27
28#if defined(__cpp_lib_source_location)
29#include <source_location>
30#endif
31
32// ----------------------------------------------------------------------------
33
34#if defined(__GNUC__)
35#pragma GCC diagnostic push
36#pragma GCC diagnostic ignored "-Wpadded"
37#pragma GCC diagnostic ignored "-Waggregate-return"
38#if defined(__clang__)
39#pragma clang diagnostic ignored "-Wc++98-compat"
40#pragma clang diagnostic ignored "-Wunknown-warning-option"
41#endif
42#endif
43
45{
46 // --------------------------------------------------------------------------
47
48 namespace reflection
49 {
50#if defined(__cpp_lib_source_location)
51 using source_location = std::source_location;
52#else
58 {
59 public:
60 [[nodiscard]] static constexpr auto
62#if (__has_builtin(__builtin_FILE) and __has_builtin(__builtin_LINE))
63 const char* file = __builtin_FILE (), int line = __builtin_LINE ()
64#else
65 const char* file = "unknown", int line = {}
66#endif
67 ) noexcept
68 {
69 source_location sl{};
70 sl.file_ = file;
71 sl.line_ = line;
72 return sl;
73 }
74
75 [[nodiscard]] constexpr auto
76 file_name () const noexcept
77 {
78 return file_;
79 }
80
81 [[nodiscard]] constexpr auto
82 line () const noexcept
83 {
84 return line_;
85 }
86
87 private:
88 const char* file_{ "unknown" };
89 int line_{};
90 };
91
92#endif
93
94 const char*
95 short_name (const char* name);
96
97 // TODO: update for the new namespaces.
98
102 template <class T>
103 [[nodiscard]] constexpr auto
104 type_name () -> std::string_view
105 {
106#if defined(__clang__)
107#pragma GCC diagnostic push
108#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
109 // printf("|%s|%zu|\n", __PRETTY_FUNCTION__, sizeof
110 // (__PRETTY_FUNCTION__)); printf("|%s|\n", &__PRETTY_FUNCTION__[78]);
111 return { &__PRETTY_FUNCTION__[78], sizeof (__PRETTY_FUNCTION__) - 80 };
112#pragma GCC diagnostic pop
113#elif defined(__GNUC__)
114 // printf("|%s|%zu|\n", __PRETTY_FUNCTION__, sizeof
115 // (__PRETTY_FUNCTION__)); printf("|%s|\n", &__PRETTY_FUNCTION__[93]);
116 return { &__PRETTY_FUNCTION__[93], sizeof (__PRETTY_FUNCTION__) - 144 };
117#else
118#error "Unsupported compiler"
119 return "Unsupported compiler";
120#endif
121 }
122 } // namespace reflection
123
124 // --------------------------------------------------------------------------
125} // namespace micro_os_plus::micro_test_plus
126
127#if defined(__GNUC__)
128#pragma GCC diagnostic pop
129#endif
130
131// ----------------------------------------------------------------------------
132
133#endif // __cplusplus
134
135// ----------------------------------------------------------------------------
136
137#endif // MICRO_TEST_PLUS_REFLECTION_H_
138
139// ----------------------------------------------------------------------------
Local implementation of the std::source_location.
Definition reflection.h:58
static constexpr auto current(const char *file="unknown", int line={}) noexcept
Definition reflection.h:61
constexpr auto type_name() -> std::string_view
Parse the PRETTY_FUNCTION macro to extract the type name.
Definition reflection.h:104
const char * short_name(const char *name)