-
Notifications
You must be signed in to change notification settings - Fork 390
/
util.h
165 lines (147 loc) · 3.93 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
/*
* This header file contains C++ language helpers used by every class
* in the system. DO NOT add anything here that can be thought of as a
* library or that requires the inclusion on header files.
*/
#ifndef __UTIL_H__
#define __UTIL_H__
#include <boost/function.hpp>
#define DISALLOW_COPY_AND_ASSIGN(_Class) \
_Class(const _Class &); \
_Class& operator=(const _Class &)
#ifdef NDEBUG
#define CHECK_INVARIANT(Cond) \
do { \
if (!(Cond)) { \
LOG(WARN, "Invariant failed: " ## Cond); \
return false; \
} \
} while (0)
#else
#define CHECK_INVARIANT(Cond) \
do { \
assert(Cond); \
} while (0)
#endif
template <typename IntType>
bool BitIsSet(IntType value, size_t bit) {
return (value & (1 << bit));
}
template <typename IntType>
void SetBit(IntType &value, size_t bit) {
value |= (1 << bit);
}
template <typename IntType>
void ClearBit(IntType &value, size_t bit) {
value &= ~(1 << bit);
}
class ModuleInitializer {
public:
ModuleInitializer(boost::function<void(void)> func) {
(func)();
}
};
// This two levels of indirection is necessary because otherwise preprocessor
// will not expand __LINE__ after ## operator
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define MODULE_INITIALIZER(Func) \
static ModuleInitializer TOKENPASTE2(init_, __LINE__)(Func);
#define BOOL_KEY_COMPARE(x, y) \
do { \
if ((x) < (y)) return true; \
if ((y) < (x)) return false; \
} while (0)
#define KEY_COMPARE(x, y) \
do { \
if ((x) < (y)) return -1; \
if ((y) < (x)) return 1; \
} while(0);
// Compare sorted vectors of pointers.
template <class InputIterator, class CompareOp>
int STLSortedCompare(InputIterator first1, InputIterator last1,
InputIterator first2, InputIterator last2,
CompareOp op) {
InputIterator iter1 = first1;
InputIterator iter2 = first2;
while (iter1 != last1 && iter2 != last2) {
int result = op(*iter1, *iter2);
if (result != 0) {
return result;
}
++iter1;
++iter2;
}
if (iter1 != last1) {
return 1;
}
if (iter2 != last2) {
return -1;
}
return 0;
}
template <typename Container>
void STLDeleteValues(Container *container) {
typename Container::iterator next;
for (typename Container::iterator iter = container->begin();
iter != container->end(); iter = next) {
next = iter;
++next;
delete *iter;
}
container->clear();
}
// Delete all the elements of a map.
template <typename Container>
void STLDeleteElements(Container *container) {
typename Container::iterator next;
for (typename Container::iterator iter = container->begin();
iter != container->end(); iter = next) {
next = iter;
++next;
delete iter->second;
}
container->clear();
}
// Check if key exist in collection
template <typename Collection, typename T>
bool STLKeyExists(const Collection &col, const T &key) {
return col.find(key) != col.end();
}
template <typename T>
class custom_ptr {
public:
custom_ptr(boost::function<void(T *)> deleter, T *ptr = 0)
: deleter_(deleter), ptr_(ptr) {
assert(deleter_ != NULL);
}
~custom_ptr() {
if (ptr_ != NULL) {
(deleter_)(ptr_);
}
}
T *get() const {
return ptr_;
}
T *operator->() const {
return ptr_;
}
void reset(T *ptr = 0) {
if (ptr_ != NULL) {
(deleter_)(ptr_);
}
ptr_ = ptr;
}
T *release() {
T *ptr = ptr_;
ptr_ = NULL;
return ptr;
}
private:
boost::function<void(T *)> deleter_;
T *ptr_;
};
#endif /* UTIL_H_ */