-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.h
148 lines (131 loc) · 3.6 KB
/
util.h
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
#ifndef PKGDEPDB_UTIL_H__
#define PKGDEPDB_UTIL_H__
namespace pkgdepdb {
class strref {
public:
static std::string empty;
const std::string &s_;
strref() : s_(empty) {}
strref(const std::string &s) : s_(s) {}
inline operator const std::string&() const;
inline bool operator< (const std::string &s) const;
inline bool operator<=(const std::string &s) const;
inline bool operator==(const std::string &s) const;
inline bool operator!=(const std::string &s) const;
inline bool operator>=(const std::string &s) const;
inline bool operator> (const std::string &s) const;
inline const std::string& operator*() const;
inline const std::string* operator->() const;
};
strref::operator const std::string&() const { return s_; }
bool strref::operator< (const std::string &s) const { return s_ < s; }
bool strref::operator<=(const std::string &s) const { return s_ <= s; }
bool strref::operator==(const std::string &s) const { return s_ == s; }
bool strref::operator!=(const std::string &s) const { return s_ != s; }
bool strref::operator>=(const std::string &s) const { return s_ >= s; }
bool strref::operator> (const std::string &s) const { return s_ > s; }
const std::string& strref::operator*() const { return s_; }
const std::string* strref::operator->() const { return &s_; }
template<typename T>
class rptr {
public:
T *ptr_;
constexpr rptr() : ptr_(nullptr) {}
rptr(T *t) : ptr_(t) {
if (ptr_) ptr_->refcount_++;
}
rptr(const rptr<T> &o) : ptr_(o.ptr_) {
if (ptr_) ptr_->refcount_++;
}
rptr(rptr<T> &&o) : ptr_(o.ptr_) {
o.ptr_ = 0;
}
~rptr() {
if (ptr_ && !--(ptr_->refcount_))
delete ptr_;
}
operator T*() const { return ptr_; }
T* get() const { return ptr_; }
bool operator!() const { return !ptr_; }
T& operator*() { return *ptr_; }
T* operator->() { return ptr_; }
const T* operator->() const { return ptr_; }
rptr<T>& operator=(T* o) {
if (o) o->refcount_++;
if (ptr_ && !--(ptr_->refcount_))
delete ptr_;
ptr_ = o;
return (*this);
}
rptr<T>& operator=(const rptr<T> &o) {
if (o.ptr_) o.ptr_->refcount_++;
if (ptr_ && !--(ptr_->refcount_))
delete ptr_;
ptr_ = o.ptr_;
return (*this);
}
rptr<T>& operator=(rptr<T> &&o) {
if (this == &o)
return (*this);
if (ptr_ && !--(ptr_->refcount_))
delete ptr_;
ptr_ = o.ptr_;
o.ptr_ = 0;
return (*this);
}
bool operator==(const T* t) const {
return ptr_ == t;
}
bool operator==(T* t) const {
return ptr_ == t;
}
inline T* release() {
if (!ptr_)
return nullptr;
auto p = ptr_;
ptr_->refcount_--;
ptr_ = nullptr;
return p;
}
};
class guard {
public:
bool on;
std::function<void()> fn;
guard(std::function<void()> f)
: on(true), fn(f) {}
~guard() {
if (on) fn();
}
void release() { on = false; }
};
template<class T, class... Args>
inline uniq<T> mk_unique(Args&&... args) {
return move(uniq<T>(new T(std::forward<Args>(args)...)));
}
template<class T, class... Args>
inline rptr<T> mk_rptr(Args&&... args) {
return move(rptr<T>(new T(std::forward<Args>(args)...)));
}
namespace util {
template<typename CONT, typename... Args>
inline bool any(const CONT &lst, const Args&... args) {
if (!lst.size())
return true;
for (auto &i : lst) {
if ((i)(args...))
return true;
}
return false;
}
template<typename CONT, typename... Args>
inline bool all(const CONT &lst, const Args&... args) {
for (auto &i : lst) {
if (!(*i)(args...))
return false;
}
return true;
}
} // ::pkgdepdb::util
} // ::pkgdepdb
#endif