-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
158 lines (131 loc) · 3.94 KB
/
utils.hpp
File metadata and controls
158 lines (131 loc) · 3.94 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once
#include <boost/filesystem.hpp>
#include <chrono>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <sys/time.h>
#define __FUNC_NAME__ sandbox::details::func_name(__PRETTY_FUNCTION__)
#define __FILENAME__ boost::filesystem::path{__FILE__}.filename().string()
namespace sandbox {
namespace details {
inline std::string func_name(const std::string& prettyFunction)
{
auto end = prettyFunction.find('(');
auto result = prettyFunction.substr(0, end);
auto begin = result.rfind(' ');
result = result.substr(begin + 1);
end = result.rfind("::");
size_t last_begin = 0;
begin = result.find("::");
while (begin < end)
{
last_begin = begin + 2;
begin = result.find("::", begin + 2);
}
return result.substr(last_begin);
}
}
inline size_t get_filesize(const boost::filesystem::path& path)
{
std::ifstream file(path.string(), std::ios::binary | std::ios::ate | std::ios::in);
return static_cast<size_t>(file.tellg());
}
inline std::string read_file(const boost::filesystem::path& path)
{
std::ostringstream buf;
std::ifstream input(path.string());
buf << input.rdbuf();
return buf.str();
}
inline std::chrono::milliseconds::rep ms_since_epoch()
{
using namespace std::chrono;
return duration_cast<milliseconds>(high_resolution_clock::now().time_since_epoch()).count();
}
inline std::string get_current_datetime_str()
{
timeval curTime;
gettimeofday(&curTime, nullptr);
auto milli = static_cast<int>(curTime.tv_usec / 1000);
char buffer[10] = "";
strftime(buffer, sizeof(buffer), "%H:%M:%S", localtime(&curTime.tv_sec));
std::string milliStr;
if (milli < 10)
milliStr = "00" + std::to_string(milli);
else if (milli < 100)
milliStr = "0" + std::to_string(milli);
else
milliStr = std::to_string(milli);
return buffer + (':' + milliStr);
}
template <typename to_t, typename from_t>
to_t evil_cast(from_t&& f)
{
return *static_cast<to_t*>(reinterpret_cast<char*>(&f));
}
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
struct identity
{
template <typename U>
constexpr auto operator()(U&& v) const noexcept -> decltype(std::forward<U>(v))
{
return std::forward<U>(v);
}
};
static void benchmark_escape(void* p)
{
asm volatile("" : : "g"(p) : "memory");
}
static void benchmark_clobber()
{
asm volatile("" : : : "memory");
}
template <typename func_t, typename... Args>
std::chrono::milliseconds measure_execution_time(func_t&& func, Args&&... args)
{
auto start = std::chrono::high_resolution_clock::now();
func(std::forward<Args>(args)...);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
}
}
namespace std {
template <class E>
class hash
{
using sfinae = typename std::enable_if<std::is_enum<E>::value, E>::type;
using underlying_t = typename std::underlying_type<E>::type;
public:
size_t operator()(const E& e) const { return std::hash<underlying_t>()(static_cast<underlying_t>(e)); }
};
}
constexpr inline std::chrono::hours operator"" _h(unsigned long long i)
{
return std::chrono::hours{i};
}
constexpr inline std::chrono::minutes operator"" _min(unsigned long long i)
{
return std::chrono::minutes{i};
}
constexpr inline std::chrono::seconds operator"" _s(unsigned long long i)
{
return std::chrono::seconds{i};
}
constexpr inline std::chrono::milliseconds operator"" _ms(unsigned long long i)
{
return std::chrono::milliseconds{i};
}
constexpr inline std::chrono::microseconds operator"" _us(unsigned long long i)
{
return std::chrono::microseconds{i};
}
constexpr inline std::chrono::nanoseconds operator"" _ns(unsigned long long i)
{
return std::chrono::nanoseconds{i};
}