-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hpp
109 lines (97 loc) · 2.69 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
#ifndef UTIL_HPP
#define UTIL_HPP
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
std::vector<std::string> split_words(const std::string &line);
template<typename... Args>
std::string string_format(const std::string& format, Args... args)
{
size_t size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
auto buf = std::make_unique<char[]>(size);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
inline bool is_hex(const std::string &s)
{
return std::all_of(s.begin(), s.end(), ::isxdigit);
}
/**
* Checks if a bit in a value is set.
*
* This function checks if a bit inside a value is set or not.
* The \a y value specific the position of the bit, started at the
* LSB and count from \c 0.
*
* @param x The value to check
* @param y The position of the bit to check, started from the LSB
* @pre y < sizeof(T) * 8
* @return True if the bit is set, false else.
*/
template <typename T>
static inline bool HasBit(const T x, const uint8_t y)
{
return (x & (static_cast<T>(1U) << y)) != 0;
}
/**
* Set a bit in a variable.
*
* This function sets a bit in a variable. The variable is changed
* and the value is also returned. Parameter y defines the bit and
* starts at the LSB with 0.
*
* @param x The variable to set a bit
* @param y The bit position to set
* @pre y < sizeof(T) * 8
* @return The new value of the old value with the bit set
*/
template <typename T>
T SetBit(T &x, const uint8_t y)
{
return x = static_cast<T>(x | (static_cast<T>(1U) << y));
}
/**
* Clears a bit in a variable.
*
* This function clears a bit in a variable. The variable is
* changed and the value is also returned. Parameter y defines the bit
* to clear and starts at the LSB with 0.
*
* @param x The variable to clear the bit
* @param y The bit position to clear
* @pre y < sizeof(T) * 8
* @return The new value of the old value with the bit cleared
*/
template <typename T>
T ClrBit(T &x, const uint8_t y)
{
return x = static_cast<T>(x & ~(static_cast<T>(1U) << y));
}
enum log_level_t : size_t {
LOG_NOTHING,
LOG_INFO,
LOG_DEBUG,
LOG_DEBUG2
};
extern log_level_t GLOBAL_LOG_LEVEL;
template <typename T>
void log_r(T&& t)
{
std::cout << std::forward<T>(t) << std::endl;
}
template <typename T, typename... Args>
void log_r(T&& t, Args&&... obj)
{
std::cout << std::forward<T>(t);
log_r(std::forward<Args>(obj)...);
}
template <log_level_t level, typename... Args>
void log(Args&&... obj)
{
if (level > GLOBAL_LOG_LEVEL) return;
std::cout << '[' << level << "] ";
log_r(std::forward<Args>(obj)...);
}
#endif /* UTIL_HPP */