-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.hpp
113 lines (101 loc) · 2.88 KB
/
util.hpp
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
#ifndef UTIL_HPP_
#define UTIL_HPP_
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <vector>
#include "platform.hpp"
#include "platform_api.hpp"
#ifdef PLATFORM_WINDOWS
#include <windows.h>
#endif
struct NonCopyable {
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};
struct CmdArgs {
using Path = std::filesystem::path;
Path configFile;
Path logFile;
static CmdArgs Parse(const std::vector<std::string>& args);
};
namespace _internal {
template <typename T>
void _log(std::ostream& os, T&& car) {
os << car << std::endl;
}
template <typename T, typename... Args>
void _log(std::ostream& os, T&& car, Args&&...cdr) {
os << car << ' ';
_log(os, std::forward<Args>(cdr)...);
}
} // namespace _internal
namespace Info {
template <typename... Args>
void Log(Args&&...args) {
::_internal::_log(std::cout, std::forward<Args>(args)...);
}
} // namespace Info
namespace Err {
template <typename... Args>
void Log(Args&&...args) {
std::stringstream ss;
::_internal::_log(ss, std::forward<Args>(args)...);
Dialog::messageBox(ss.str());
}
template <typename... Args>
[[noreturn]] void Exit(Args&&...args) {
Log(std::forward<Args>(args)...);
std::exit(1);
}
} // namespace Err
namespace String {
template <typename T>
inline std::u8string tou8(const std::basic_string<T>& str) {
static_assert(sizeof(T) == sizeof(char8_t), "Invalid conversion.");
return std::u8string(str.cbegin(), str.cend());
}
template <typename T>
inline std::u8string tou8(const std::basic_string_view<T> str) {
static_assert(sizeof(T) == sizeof(char8_t), "Invalid conversion.");
return std::u8string(str.cbegin(), str.cend());
}
#ifdef PLATFORM_WINDOWS
template <typename T>
inline std::basic_string<T> wideToMulti(const std::wstring_view wstr) {
static_assert(sizeof(T) == sizeof(char), "Invalid conversion.");
std::basic_string<T> str;
int size = WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, nullptr, 0, nullptr, nullptr);
str.resize(size - 1, '\0'); // "size" includes padding for '\0'
WideCharToMultiByte(
CP_ACP, 0, wstr.data(), -1, reinterpret_cast<char *>(str.data()), size, nullptr,
nullptr);
return str;
}
#endif
} // namespace String
namespace Enum {
template <typename T>
inline constexpr std::underlying_type_t<T> underlyCast(T v) {
return static_cast<decltype(underlyCast(v))>(v);
}
} // namespace Enum
namespace Path {
std::filesystem::path getWorkingDirectory();
std::filesystem::path makeAbsolute(
const std::filesystem::path& path,
const std::filesystem::path& cwd);
} // namespace Path
namespace Slog {
void Logger(
const char *tag,
uint32_t logLevel,
uint32_t logItem,
const char *message,
uint32_t linenr,
const char *filename,
void *user_data);
}
#endif // UTIL_HPP_