-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
40 lines (32 loc) · 1.03 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
#ifndef QUORA_UTIL_H
#define QUORA_UTIL_H
typedef int Value;
const int InvalidValue = -1;
// NetworkOrderInvalidValue is the network ordered representation
// of InvalidValue
const int ValueBitSize = 32;
const int MinValue = 0;
const int MaxValue = 2147483647;
const int NetworkOrderInvalidValue = htonl(InvalidValue);
// This method will check the return value of Unix-style APIs(where result < 0
// indicates failure otherwise success) and display corresponding error message
bool can_continue(int result, const char* operation);
size_t robust_read(int fd, void* buffer, size_t bufferSize);
size_t robust_write(int fd, void* buffer, size_t bufferSize);
template <class TData>
void to_host_order(TData* begin, TData* end) {
while (begin != end) {
*begin= ntohl(*begin);
++begin;
}
}
template <class TData>
void to_network_order(TData* begin, TData* end) {
while (begin != end) {
*begin= htonl(*begin);
++begin;
}
}
template <class T>
inline T naive_hash(const T& item) { return item; }
#endif