Line data Source code
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 762835 : bool BitIsSet(IntType value, size_t bit) { 37 762835 : return (value & (1 << bit)) != 0; 38 : } 39 : 40 : template <typename IntType> 41 21570 : void SetBit(IntType &value, size_t bit) { 42 21570 : value |= (1 << bit); 43 21570 : } 44 : 45 : template <typename IntType> 46 21586 : void ClearBit(IntType &value, size_t bit) { 47 21586 : value &= ~(1 << bit); 48 21586 : } 49 : 50 : class ModuleInitializer { 51 : public: 52 1556 : ModuleInitializer(boost::function<void(void)> func) { 53 1556 : (func)(); 54 1556 : } 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 1470931 : int STLSortedCompare(InputIterator first1, InputIterator last1, 79 : InputIterator first2, InputIterator last2, 80 : CompareOp op) { 81 1470931 : InputIterator iter1 = first1; 82 1470931 : InputIterator iter2 = first2; 83 1803801 : while (iter1 != last1 && iter2 != last2) { 84 367750 : int result = op(*iter1, *iter2); 85 367750 : if (result != 0) { 86 34880 : return result; 87 : } 88 332870 : ++iter1; 89 332870 : ++iter2; 90 : } 91 1436051 : if (iter1 != last1) { 92 12557 : return 1; 93 : } 94 1423494 : if (iter2 != last2) { 95 2849 : return -1; 96 : } 97 1420645 : return 0; 98 : } 99 : 100 : template <typename Container> 101 4987621 : void STLDeleteValues(Container *container) { 102 4987621 : typename Container::iterator next; 103 13945204 : for (typename Container::iterator iter = container->begin(); 104 13945154 : iter != container->end(); iter = next) { 105 8957466 : next = iter; 106 8957466 : ++next; 107 8957462 : delete *iter; 108 : } 109 4987453 : container->clear(); 110 4987372 : } 111 : 112 : // Delete all the elements of a map. 113 : template <typename Container> 114 388443 : void STLDeleteElements(Container *container) { 115 388443 : typename Container::iterator next; 116 2359153 : for (typename Container::iterator iter = container->begin(); 117 2359153 : iter != container->end(); iter = next) { 118 1970710 : next = iter; 119 1970710 : ++next; 120 1970710 : delete iter->second; 121 : } 122 388443 : container->clear(); 123 388443 : } 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 : } 138 : ~custom_ptr() { 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> 166 : struct SmartPointerComparator { 167 0 : bool operator()( const SmartPointer<KeyType> lhs, 168 : const SmartPointer<KeyType> rhs) const { 169 0 : KeyType *left = lhs.get(); 170 0 : KeyType *right = rhs.get(); 171 0 : return (*left).IsLess(*right); 172 : } 173 : }; 174 : 175 : #endif /* UTIL_H_ */