/
common.hh
87 lines (74 loc) · 2.22 KB
/
common.hh
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
#ifndef COMMON_H
#define COMMON_H 1
#include <cassert>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <sys/time.h>
#include <memcached/engine.h>
#include "config.h"
#if defined(HAVE_MEMORY)
# include <memory>
#endif
#if defined(HAVE_TR1_MEMORY)
# include <tr1/memory>
#endif
#if defined(HAVE_BOOST_SHARED_PTR_HPP)
# include <boost/shared_ptr.hpp>
#endif
#if defined(SHARED_PTR_NAMESPACE)
using SHARED_PTR_NAMESPACE::shared_ptr;
#else
# error No shared pointer implementation found!
#endif
/* Linux' limits don't bring this in in c++ mode without doing weird
stuff. It's a known constant, so we'll just make it if we don't
have it. */
#ifndef UINT16_MAX
#define UINT16_MAX 65535
#endif /* UINT16_MAX */
// Stolen from http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
// A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
// Utility functions implemented in various modules.
extern EXTENSION_LOGGER_DESCRIPTOR *getLogger(void);
// Time handling functions
inline void advance_tv(struct timeval &tv, const double secs) {
double ip, fp;
fp = modf(secs, &ip);
int usec = static_cast<int>(fp * 1e6) + static_cast<int>(tv.tv_usec);
int quot = usec / 1000000;
int rem = usec % 1000000;
tv.tv_sec = static_cast<int>(ip) + tv.tv_sec + quot;
tv.tv_usec = rem;
}
inline bool less_tv(const struct timeval &tv1, const struct timeval &tv2) {
if (tv1.tv_sec == tv2.tv_sec) {
return tv1.tv_usec < tv2.tv_usec;
} else {
return tv1.tv_sec < tv2.tv_sec;
}
}
inline bool parseUint16(const char *in, uint16_t *out) {
assert(out != NULL);
errno = 0;
*out = 0;
char *endptr;
long num = strtol(in, &endptr, 10);
if (errno == ERANGE || num < 0 || num > UINT16_MAX) {
return false;
}
if (isspace(*endptr) || (*endptr == '\0' && endptr != in)) {
*out = static_cast<uint16_t>(num);
return true;
}
return false;
}
#endif /* COMMON_H */