OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 /*
6  * This header file contains C++ language helpers used by every class
7  * in the system. DO NOT add anything here that can be thought of as a
8  * library or that requires the inclusion on header files.
9  */
10 #ifndef __UTIL_H__
11 #define __UTIL_H__
12 
13 #include <boost/function.hpp>
14 
15 #define DISALLOW_COPY_AND_ASSIGN(_Class) \
16  _Class(const _Class &); \
17  _Class& operator=(const _Class &)
18 
19 
20 #ifdef NDEBUG
21 #define CHECK_INVARIANT(Cond) \
22  do { \
23  if (!(Cond)) { \
24  LOG(WARN, "Invariant failed: " ## Cond); \
25  return false; \
26  } \
27  } while (0)
28 #else
29 #define CHECK_INVARIANT(Cond) \
30  do { \
31  assert(Cond); \
32  } while (0)
33 #endif
34 
35 template <typename IntType>
36 bool BitIsSet(IntType value, size_t bit) {
37  return (value & (1 << bit)) != 0;
38 }
39 
40 template <typename IntType>
41 void SetBit(IntType &value, size_t bit) {
42  value |= (1 << bit);
43 }
44 
45 template <typename IntType>
46 void ClearBit(IntType &value, size_t bit) {
47  value &= ~(1 << bit);
48 }
49 
51 public:
52  ModuleInitializer(boost::function<void(void)> func) {
53  (func)();
54  }
55 };
56 
57 // This two levels of indirection is necessary because otherwise preprocessor
58 // will not expand __LINE__ after ## operator
59 #define TOKENPASTE(x, y) x ## y
60 #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
61 #define MODULE_INITIALIZER(Func) \
62 static ModuleInitializer TOKENPASTE2(init_, __LINE__)(Func);
63 
64 #define BOOL_KEY_COMPARE(x, y) \
65  do { \
66  if ((x) < (y)) return true; \
67  if ((y) < (x)) return false; \
68  } while (false)
69 
70 #define KEY_COMPARE(x, y) \
71  do { \
72  if ((x) < (y)) return -1; \
73  if ((y) < (x)) return 1; \
74  } while (false)
75 
76 // Compare sorted vectors of pointers.
77 template <class InputIterator, class CompareOp>
78 int STLSortedCompare(InputIterator first1, InputIterator last1,
79  InputIterator first2, InputIterator last2,
80  CompareOp op) {
81  InputIterator iter1 = first1;
82  InputIterator iter2 = first2;
83  while (iter1 != last1 && iter2 != last2) {
84  int result = op(*iter1, *iter2);
85  if (result != 0) {
86  return result;
87  }
88  ++iter1;
89  ++iter2;
90  }
91  if (iter1 != last1) {
92  return 1;
93  }
94  if (iter2 != last2) {
95  return -1;
96  }
97  return 0;
98 }
99 
100 template <typename Container>
101 void STLDeleteValues(Container *container) {
102  typename Container::iterator next;
103  for (typename Container::iterator iter = container->begin();
104  iter != container->end(); iter = next) {
105  next = iter;
106  ++next;
107  delete *iter;
108  }
109  container->clear();
110 }
111 
112 // Delete all the elements of a map.
113 template <typename Container>
114 void STLDeleteElements(Container *container) {
115  typename Container::iterator next;
116  for (typename Container::iterator iter = container->begin();
117  iter != container->end(); iter = next) {
118  next = iter;
119  ++next;
120  delete iter->second;
121  }
122  container->clear();
123 }
124 
125 // Check if key exist in collection
126 template <typename Collection, typename T>
127 bool STLKeyExists(const Collection &col, const T &key) {
128  return col.find(key) != col.end();
129 }
130 
131 template <typename T>
132 class custom_ptr {
133 public:
134  custom_ptr(boost::function<void(T *)> deleter, T *ptr = 0)
135  : deleter_(deleter), ptr_(ptr) {
136  assert(deleter_ != NULL);
137  }
139  if (ptr_ != NULL) {
140  (deleter_)(ptr_);
141  }
142  }
143  T *get() const {
144  return ptr_;
145  }
146  T *operator->() const {
147  return ptr_;
148  }
149  void reset(T *ptr = 0) {
150  if (ptr_ != NULL) {
151  (deleter_)(ptr_);
152  }
153  ptr_ = ptr;
154  }
155  T *release() {
156  T *ptr = ptr_;
157  ptr_ = NULL;
158  return ptr;
159  }
160 private:
161  boost::function<void(T *)> deleter_;
162  T *ptr_;
163 };
164 
165 template <class KeyType, template <class> class SmartPointer>
167  bool operator()( const SmartPointer<KeyType> lhs,
168  const SmartPointer<KeyType> rhs) const {
169  KeyType *left = lhs.get();
170  KeyType *right = rhs.get();
171  return (*left).IsLess(*right);
172  }
173 };
174 
175 #endif /* UTIL_H_ */
void STLDeleteValues(Container *container)
Definition: util.h:101
ModuleInitializer(boost::function< void(void)> func)
Definition: util.h:52
bool STLKeyExists(const Collection &col, const T &key)
Definition: util.h:127
T * operator->() const
Definition: util.h:146
void reset(T *ptr=0)
Definition: util.h:149
T * ptr_
Definition: util.h:162
custom_ptr(boost::function< void(T *)> deleter, T *ptr=0)
Definition: util.h:134
void SetBit(IntType &value, size_t bit)
Definition: util.h:41
void STLDeleteElements(Container *container)
Definition: util.h:114
boost::function< void(T *)> deleter_
Definition: util.h:161
int STLSortedCompare(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2, CompareOp op)
Definition: util.h:78
bool operator()(const SmartPointer< KeyType > lhs, const SmartPointer< KeyType > rhs) const
Definition: util.h:167
~custom_ptr()
Definition: util.h:138
T * release()
Definition: util.h:155
void ClearBit(IntType &value, size_t bit)
Definition: util.h:46
bool BitIsSet(IntType value, size_t bit)
Definition: util.h:36