forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.hpp
More file actions
113 lines (87 loc) · 2.87 KB
/
util.hpp
File metadata and controls
113 lines (87 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <source_location>
#include <string>
#include <string_view>
#include "nlohmann/json_fwd.hpp"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4459)
#endif
#include <nlohmann/json.hpp>
/// @brief JSON namespace used for settings and config parsing.
using NlohmannJsonParseError = nlohmann::json::parse_error;
/// @brief JSON namespace used for settings and config typing.
using NlohmannJsonTypeError = nlohmann::json::type_error;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
namespace ppc::util {
/**
* @brief Returns the unqualified name of the current function.
*
* @param loc Source location, defaults to the current function.
* @return Function name without namespaces or parameters.
*/
inline std::string FuncName(const std::source_location& loc = std::source_location::current()) {
std::string s{loc.function_name()};
if (auto p = s.find('('); p != std::string::npos) {
s.resize(p); // drop “(…)”
}
if (auto p = s.rfind("::"); p != std::string::npos) {
s.erase(0, p + 2); // drop namespaces
}
return s;
}
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);
int GetNumThreads();
template <typename T>
constexpr std::string_view GetNamespace() {
#if defined(__clang__) || defined(__GNUC__)
constexpr std::string_view kFunc = __PRETTY_FUNCTION__;
constexpr std::string_view kKey = "T = ";
auto start = kFunc.find(kKey);
if (start == std::string_view::npos) {
return {};
}
start += kKey.size();
auto end = kFunc.find_first_of(";]> ,", start);
if (end == std::string_view::npos) {
return {};
}
auto full_type = kFunc.substr(start, end - start);
auto ns_end = full_type.rfind("::");
if (ns_end == std::string_view::npos) {
return {};
}
return full_type.substr(0, ns_end);
#elif defined(_MSC_VER)
constexpr std::string_view kFunc = __FUNCSIG__;
constexpr std::string_view kKey = "GetNamespace<";
auto start = kFunc.find(kKey);
if (start == std::string_view::npos) return {};
start += kKey.size();
constexpr std::string_view prefixes[] = {"class ", "struct ", "enum ", "union "};
for (auto prefix : prefixes) {
if (kFunc.substr(start, prefix.size()) == prefix) {
start += prefix.size();
break;
}
}
auto end = kFunc.find('>', start);
if (end == std::string_view::npos) return {};
auto full_type = kFunc.substr(start, end - start);
auto ns_end = full_type.rfind("::");
if (ns_end == std::string_view::npos) return {};
return full_type.substr(0, ns_end);
#else
static_assert([] { return false; }(), "Unsupported compiler");
return {};
#endif
}
inline std::shared_ptr<nlohmann::json> InitJSONPtr() { return std::make_shared<nlohmann::json>(); }
bool IsUnderMpirun();
} // namespace ppc::util