Debian Codegraph @

inst/include/sourcetools/core/macros.h

raw
1 #ifndef SOURCETOOLS_CORE_MACROS_H
2 #define SOURCETOOLS_CORE_MACROS_H
3
4 #include <cstdio>
5
6 #include <string>
7 #include <iostream>
8
9 /* Utility */
10 #ifdef __GNUC__
11 # define LIKELY(x) __builtin_expect(!!(x), 1)
12 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
13 #else
14 # define LIKELY(x) x
15 # define UNLIKELY(x) x
16 #endif
17
18 #define SOURCE_TOOLS_CHECK_MASK(__SELF__, __MASK__) \
19 ((__MASK__ & __SELF__) == __MASK__)
20
21 #define SOURCE_TOOLS_LOWER_BITS(__VALUE__, __BITS__) \
22 (((1 << __BITS__) - 1) & __VALUE__)
23
24 #define SOURCE_TOOLS_PASTE(__X__, __Y__) __X__ ## __Y__
25 #define SOURCE_TOOLS_STRINGIFY(__X__) #__X__
26
27 /* Logging */
28 namespace sourcetools {
29 namespace debug {
30
31 inline std::string shortFilePath(const std::string& filePath)
32 {
33 std::string::size_type index = filePath.find_last_of("/");
34 if (index != std::string::npos)
35 return filePath.substr(index + 1);
36 return filePath;
37 }
38
39 inline std::string debugPosition(const char* filePath, int line)
40 {
41 static const int N = 1024;
42 char buffer[N];
43 std::string shortPath = shortFilePath(filePath);
44 if (shortPath.size() > N / 2)
45 shortPath = shortPath.substr(0, N / 2);
46 std::snprintf(buffer, N, "[%s:%4i]", shortPath.c_str(), line);
47 return buffer;
48 }
49
50 } // namespace debug
51 } // namespace sourcetools
52
53 // Flip on/off as necessary
54 #define SOURCE_TOOLS_ENABLE_DEBUG_LOGGING
55
56 #ifdef SOURCE_TOOLS_ENABLE_DEBUG_LOGGING
57
58 #include <iostream>
59
60 #define DEBUG(__X__) \
61 std::cerr << ::sourcetools::debug::debugPosition(__FILE__, __LINE__) \
62 << ": " << __X__ << ::std::endl;
63 #define DEBUG_BLOCK(x)
64
65 #else
66
67 #define DEBUG(x)
68 #define DEBUG_BLOCK(x) if (false)
69
70 #endif
71
72 #endif /* SOURCETOOLS_CORE_MACROS_H */