1
- /* *********************************************************************************************************************
1
+ /* **********************************************************************************************************************
2
2
* PROJECT
3
- * -------------------------------------------------------------------------------------------------------------------
3
+ * --------------------------------------------------------------------------------------------------------------------
4
4
* \verbatim
5
- * OpenAA: Open Source Adaptive AUTOSAR Project
5
+ * OpenAA: Open Source Adaptive AUTOSAR Project (CXX_STANDARD 17)
6
6
* \endverbatim
7
- * -------------------------------------------------------------------------------------------------------------------
7
+ * --------------------------------------------------------------------------------------------------------------------
8
8
* FILE DESCRIPTION
9
- * -------------------------------------------------------------------------------------------------------------------
9
+ * --------------------------------------------------------------------------------------------------------------------
10
10
* \file location_utils.h
11
11
* \brief Internal utilities for compile-time extraction of filename/line information.
12
12
*
13
- * \details This file defines helper functions and macros for path-stripping and location capturing. They are used
14
- * internally within the Adaptive AUTOSAR environment to support consistent violation logging, etc .
15
- *
16
- *********************************************************************************************************************/
13
+ * \details This file defines helper functions and macros for path-stripping and location capturing.
14
+ * It provides a fully constexpr implementation that strips directory paths from __FILE__ at compile time .
15
+ * \note This helper could be replace with std::source_location if you're using (CXX_STANDARD 20)
16
+ ********************************************************************************************************************** * /
17
17
18
18
#ifndef ARA_CORE_INTERNAL_LOCATION_UTILS_H_
19
19
#define ARA_CORE_INTERNAL_LOCATION_UTILS_H_
20
20
21
- #include < string_view> // Required for std::string_view
21
+ #include < cstddef> // For std::size_t
22
+ #include < string_view> // For std::string_view
22
23
23
24
namespace ara {
24
25
namespace core {
25
26
namespace internal {
26
27
27
28
/* !
28
- * \brief A constexpr function to strip directory paths from a string (like __FILE__).
29
- * \param fullPath A std::string_view representing the full path, typically __FILE__.
30
- * \return A std::string_view pointing to the filename part after the last slash or backslash.
29
+ * \brief A constexpr function template to strip directory paths from a string literal (e.g. __FILE__).
30
+ * \tparam N The size of the string literal (including the null terminator).
31
+ * \param fullPath A reference to a string literal representing the full path.
32
+ * \return A std::string_view pointing to the filename part (after the last '/' or '\\').
31
33
*
32
34
* \details
33
- * - This handles both forward slash '/' and backslash '\\' to cover various platforms.
34
- * - Example:
35
- * std::string_view result = CutLeadingPath("C:\\MyProject\\source\\file.cpp");
36
- * // result now points to "file.cpp"
35
+ * - Iterates backwards over the string literal until a path separator is found.
36
+ * - If no separator is found, the entire string is returned.
37
+ * - Because it accepts a string literal reference, the operation is fully evaluated at compile time.
37
38
*/
38
- constexpr auto CutLeadingPath ( std::string_view fullPath) noexcept -> std::string_view
39
- {
40
- std::size_t pos = fullPath. find_last_of ( " / \\ " );
41
- if (pos == std::string_view::npos ) {
42
- return fullPath;
43
- } else {
44
- return fullPath. substr (pos + 1 );
39
+ template < std::size_t N>
40
+ constexpr std::string_view StripFilePath ( const char (&fullPath)[N]) noexcept {
41
+ std::string_view path{ fullPath, N - 1 }; // Exclude the null terminator
42
+ for ( std::size_t i = path. size (); i > 0 ; --i ) {
43
+ if (path[i - 1 ] == ' / ' || path[i - 1 ] == ' \\ ' ) {
44
+ return path. substr (i);
45
+ }
45
46
}
47
+ return path;
46
48
}
47
49
48
50
} // namespace internal
49
51
} // namespace core
50
52
} // namespace ara
51
53
52
54
/* !
53
- * \brief Macro to stringify tokens.
55
+ * \brief Helper macros to stringify tokens.
54
56
*/
55
57
#define ARA_CORE_LOC_STRINGIFY_ (x ) #x
56
58
#define ARA_CORE_LOC_STRINGIFY (x ) ARA_CORE_LOC_STRINGIFY_(x)
57
59
58
60
/* !
59
- * \brief Provides a path-stripped version of __FILE__ at compile time using lambda capture .
61
+ * \brief Provides a compile-time evaluated, path-stripped version of __FILE__.
60
62
*
61
- * \return A ` std::string_view` pointing to the stripped filename.
63
+ * \return A std::string_view pointing to the filename without directory paths .
62
64
*/
63
- #define ARA_CORE_INTERNAL_FILE \
64
- []() -> std::string_view { \
65
- constexpr std::string_view stripped = ::ara::core::internal::CutLeadingPath (__FILE__); \
66
- return stripped; \
67
- }()
65
+ #define ARA_CORE_INTERNAL_FILE (::ara::core::internal::StripFilePath(__FILE__))
68
66
69
67
/* !
70
- * \brief Provides a path-stripped version of __FILE__ plus line number at compile time .
68
+ * \brief Provides a compile-time evaluated, path-stripped version of __FILE__ with line number.
71
69
*
72
70
* \details
73
- * - Combines __FILE__ ":" __LINE__ and then strips the path .
74
- * - The final string looks like "file.cpp:123" if the path was "C:\xyz\file.cpp" .
71
+ * - Uses compile-time literal concatenation to form a string of the format "file.cpp:123" .
72
+ * - The StripFilePath function removes any leading directory information .
75
73
*
76
- * \return A ` std::string_view` containing the stripped filename and line number.
74
+ * \return A std::string_view containing the stripped filename and line number.
77
75
*/
78
- #define ARA_CORE_INTERNAL_FILELINE \
79
- []() -> std::string_view { \
80
- static constexpr char path_and_line[] = __FILE__ " :" ARA_CORE_LOC_STRINGIFY (__LINE__); \
81
- static constexpr std::string_view sv = ::ara::core::internal::CutLeadingPath (path_and_line); \
82
- return sv; \
83
- }()
76
+ #define ARA_CORE_INTERNAL_FILELINE (::ara::core::internal::StripFilePath(__FILE__ " :" ARA_CORE_LOC_STRINGIFY(__LINE__)))
84
77
85
78
#endif // ARA_CORE_INTERNAL_LOCATION_UTILS_H_
79
+
0 commit comments