Skip to content

Commit 1a84b5f

Browse files
authored
Merge pull request #29 from Sherif-MoOo/std_libs
Internal core location helper enhance it to be a compile time evaluat…
2 parents 9587035 + 8ab7c63 commit 1a84b5f

File tree

1 file changed

+36
-42
lines changed
  • components/open-aa-std-adaptive-autosar-libs/include/ara/core/internal

1 file changed

+36
-42
lines changed
Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,79 @@
1-
/**********************************************************************************************************************
1+
/***********************************************************************************************************************
22
* PROJECT
3-
* -------------------------------------------------------------------------------------------------------------------
3+
* --------------------------------------------------------------------------------------------------------------------
44
* \verbatim
5-
* OpenAA: Open Source Adaptive AUTOSAR Project
5+
* OpenAA: Open Source Adaptive AUTOSAR Project (CXX_STANDARD 17)
66
* \endverbatim
7-
* -------------------------------------------------------------------------------------------------------------------
7+
* --------------------------------------------------------------------------------------------------------------------
88
* FILE DESCRIPTION
9-
* -------------------------------------------------------------------------------------------------------------------
9+
* --------------------------------------------------------------------------------------------------------------------
1010
* \file location_utils.h
1111
* \brief Internal utilities for compile-time extraction of filename/line information.
1212
*
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+
***********************************************************************************************************************/
1717

1818
#ifndef ARA_CORE_INTERNAL_LOCATION_UTILS_H_
1919
#define ARA_CORE_INTERNAL_LOCATION_UTILS_H_
2020

21-
#include <string_view> // Required for std::string_view
21+
#include <cstddef> // For std::size_t
22+
#include <string_view> // For std::string_view
2223

2324
namespace ara {
2425
namespace core {
2526
namespace internal {
2627

2728
/*!
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 '\\').
3133
*
3234
* \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.
3738
*/
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+
}
4546
}
47+
return path;
4648
}
4749

4850
} // namespace internal
4951
} // namespace core
5052
} // namespace ara
5153

5254
/*!
53-
* \brief Macro to stringify tokens.
55+
* \brief Helper macros to stringify tokens.
5456
*/
5557
#define ARA_CORE_LOC_STRINGIFY_(x) #x
5658
#define ARA_CORE_LOC_STRINGIFY(x) ARA_CORE_LOC_STRINGIFY_(x)
5759

5860
/*!
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__.
6062
*
61-
* \return A `std::string_view` pointing to the stripped filename.
63+
* \return A std::string_view pointing to the filename without directory paths.
6264
*/
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__))
6866

6967
/*!
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.
7169
*
7270
* \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.
7573
*
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.
7775
*/
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__)))
8477

8578
#endif // ARA_CORE_INTERNAL_LOCATION_UTILS_H_
79+

0 commit comments

Comments
 (0)