This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
236 lines (188 loc) · 5.57 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2007 Daniel Erat <[email protected]>
// All rights reserved.
#ifndef __UTIL_H__
#define __UTIL_H__
#include <ctime>
#include <iostream>
#include <map>
#include <pcrecpp.h>
#include <string>
#include <sys/time.h>
#include <vector>
using namespace std;
#define LOG wham::Logger(__FILE__, __LINE__)
// FIXME: do something extra here
#define ERROR wham::Logger(__FILE__, __LINE__)
#define DEBUG wham::Logger(__FILE__, __LINE__)
#define CHECK(x) \
if (!(x)) { \
ERROR << "Assertion \"" #x "\" failed; exiting"; \
abort(); \
}
// FIXME: this is completely lame; it should print the values without
// evaluating the arguments a second time
#define CHECK_EQ(x, y) \
if ((x) != (y)) { \
ERROR << "Assertion " #x " == " #y " failed; exiting (\"" \
<< (x) << "\" != \"" << (y) << "\")"; \
abort(); \
}
#if 0
template<class A, class B>
inline void CHECK_EQ_func(const char& a_expr,
const char& b_expr,
const A& a,
const B& b) {
if (a != b) {
ERROR << "Assertion " << a_expr << " == " << b_expr << " failed; "
<< "exiting (\"" << a << "\" != \"" << b << "\")";
abort();
}
}
#define CHECK_EQ(a, b) CHECK_EQ_func(#a, #b, a, b);
#endif
#define DISALLOW_EVIL_CONSTRUCTORS(class_name) \
class_name(const class_name&); \
void operator=(const class_name&)
typedef unsigned int uint;
class UtilTestSuite;
namespace wham {
class Logger {
public:
Logger(const string& filename, int line_num);
~Logger();
Logger& operator<<(long v);
Logger& operator<<(unsigned long v);
Logger& operator<<(bool v);
Logger& operator<<(short v);
Logger& operator<<(unsigned short v);
Logger& operator<<(int v);
Logger& operator<<(unsigned int v);
Logger& operator<<(double v);
Logger& operator<<(float v);
Logger& operator<<(long double v);
Logger& operator<<(const void* v);
Logger& operator<<(ios_base& (*f)(ios_base&));
Logger& operator<<(const char* v);
Logger& operator<<(const string& v);
private:
// Has the input so far ended with a newline?
bool newline_seen_;
// Format flags at the time we were instatiated
ios_base::fmtflags orig_format_flags_;
DISALLOW_EVIL_CONSTRUCTORS(Logger);
}; // class Logger
// A reference-counted pointer class.
template<class T>
class ref_ptr {
public:
explicit ref_ptr(T* ptr=NULL)
: ptr_(ptr),
refs_(ptr ? new int(0) : NULL) {
add_ref();
}
ref_ptr(const ref_ptr<T>& o)
: ptr_(o.ptr_),
refs_(o.refs_) {
add_ref();
}
~ref_ptr() {
del_ref();
ptr_ = NULL;
refs_ = NULL;
}
ref_ptr<T>& operator=(const ref_ptr<T>& o) {
if (ptr_ != o.ptr_) {
del_ref();
ptr_ = o.ptr_;
refs_ = o.refs_;
add_ref();
}
return *this;
}
T* get() const {
return ptr_;
}
T &operator*() const {
return *ptr_;
}
T *operator->() const {
return ptr_;
}
void reset(T* ptr=NULL) {
del_ref();
ptr_ = ptr;
refs_ = ptr ? new int(0) : NULL;
add_ref();
}
void swap(ref_ptr<T>& other) {
T* tmp_ptr = ptr_;
ptr_ = other.ptr_;
other.ptr_ = tmp_ptr;
int* tmp_refs = refs_;
refs_ = other.refs_;
other.refs_ = tmp_refs;
}
// Release and return the pointer.
// This must be the only reference to it.
T* release() {
if (refs_ != NULL) {
CHECK_EQ(*refs_, 1);
delete refs_;
refs_ = NULL;
}
T* ptr = ptr_;
ptr_ = NULL;
return ptr;
}
bool operator==(const ref_ptr<T>& other) const {
return ptr_ == other.ptr_;
}
bool operator==(const T* ptr) const {
return ptr_ == ptr;
}
private:
friend class ::UtilTestSuite;
inline void add_ref() {
if (refs_ != NULL) (*refs_)++;
}
inline void del_ref() {
if (refs_ != NULL) {
(*refs_)--;
if (*refs_ == 0) {
delete ptr_;
delete refs_;
}
}
}
T* ptr_;
int* refs_;
};
template<class K, class V>
V FindWithDefault(const map<K, V>& the_map, const K& key, const V& def) {
// FIXME: It's dumb that I'm doing two lookups here. I'm too dense to
// figure out the syntax for declaring a const_iterator of the templated
// map class -- the obvious "map<K, V>::const_iterator it" yields
// "expected `;' before 'it'".
if (the_map.find(key) == the_map.end()) return def;
return the_map.find(key)->second;
}
// Get the number of seconds since the epoch.
double GetCurrentTime();
// Fill 'tv' with the time from 'time'.
void FillTimeval(double time, struct timeval* tv);
// Split a string on whitespace, saving the individual pieces to 'parts'.
void SplitString(const string& str, vector<string>* parts);
// Split a string on whitespace, returning the individual pieces as a new
// vector.
vector<string> SplitString(const string& str);
void SplitStringUsing(const string& str,
const string& delim,
vector<string>* parts);
void JoinString(const vector<string>& parts,
const string& delim,
string* output);
string JoinString(const vector<string>& parts, const string& delim);
string StringPrintf(const char* format, ...);
} // namespace wham
#endif // __UTIL_H__