-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.h
More file actions
72 lines (59 loc) · 1.49 KB
/
Copy pathutils.h
File metadata and controls
72 lines (59 loc) · 1.49 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
#ifndef UTILS_H
#define UTILS_H
#include <llvm/ADT/APSInt.h>
#include <string>
#include <type_traits>
struct IntegerValue
{
bool isSigned = false;
union
{
uint64_t uintValue;
int64_t intValue;
};
uint64_t AsUnsigned() const {return uintValue;}
int64_t AsSigned() const {return intValue;}
template<typename T>
auto GetAs() -> std::enable_if_t<std::is_signed<T>::value, T>
{
return static_cast<T>(intValue);
}
template<typename T>
auto GetAs() -> std::enable_if_t<std::is_unsigned<T>::value, T>
{
return static_cast<T>(uintValue);
}
};
inline IntegerValue ConvertAPSInt(llvm::APSInt intValue)
{
IntegerValue result;
result.isSigned = intValue.isSigned();
result.intValue = intValue.getExtValue();
return result;
}
inline IntegerValue ConvertAPInt(llvm::APInt intValue)
{
IntegerValue result;
result.isSigned = false;
result.uintValue = intValue.getZExtValue();
return result;
}
template<typename Seq, typename Fn>
void WriteSeq(std::ostream& os, Seq&& seq, std::string delim, Fn&& ftor)
{
bool isFirst = true;
for (auto& i : seq)
{
if (isFirst)
isFirst = false;
else
os << delim;
ftor(os, i);
}
}
template<typename Seq>
void WriteSeq(std::ostream& os, Seq&& seq, std::string delim = ", ")
{
WriteSeq(os, std::forward<Seq>(seq), std::move(delim), [](auto&& os, auto&& i) {os << i;});
}
#endif // UTILS_H