LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - util.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 42 47 89.4 %
Date: 2026-09-28 02:13:17 Functions: 58 87 66.7 %
Legend: Lines: hit not hit

          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      761350 : bool BitIsSet(IntType value, size_t bit) {
      37      761350 :     return (value & (1 << bit)) != 0;
      38             : }
      39             : 
      40             : template <typename IntType>
      41       21594 : void SetBit(IntType &value, size_t bit) {
      42       21594 :     value |= (1 << bit);
      43       21594 : }
      44             : 
      45             : template <typename IntType>
      46       21607 : void ClearBit(IntType &value, size_t bit) {
      47       21607 :     value &= ~(1 << bit);
      48       21607 : }
      49             : 
      50             : class ModuleInitializer {
      51             : public:
      52          10 :     ModuleInitializer(boost::function<void(void)> func) {
      53          10 :         (func)();
      54          10 :     }
      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        3426 : int STLSortedCompare(InputIterator first1, InputIterator last1,
      79             :                      InputIterator first2, InputIterator last2,
      80             :                      CompareOp op) {
      81        3426 :     InputIterator iter1 = first1;
      82        3426 :     InputIterator iter2 = first2;
      83       15280 :     while (iter1 != last1 && iter2 != last2) {
      84       11878 :         int result = op(*iter1, *iter2);
      85       11878 :         if (result != 0) {
      86          24 :             return result;
      87             :         }
      88       11854 :         ++iter1;
      89       11854 :         ++iter2;
      90             :     }
      91        3402 :     if (iter1 != last1) {
      92         620 :         return 1;
      93             :     }
      94        2782 :     if (iter2 != last2) {
      95           0 :         return -1;
      96             :     }
      97        2782 :     return 0;
      98             : }
      99             : 
     100             : template <typename Container>
     101      479202 : void STLDeleteValues(Container *container) {
     102      479202 :     typename Container::iterator next;
     103     1177924 :     for (typename Container::iterator iter = container->begin();
     104     1177916 :          iter != container->end(); iter = next) {
     105      698723 :         next = iter;
     106      698723 :         ++next;
     107      698722 :         delete *iter;
     108             :     }
     109      479188 :     container->clear();
     110      479178 : }
     111             : 
     112             : // Delete all the elements of a map.
     113             : template <typename Container>
     114        5971 : void STLDeleteElements(Container *container) {
     115        5971 :     typename Container::iterator next;
     116       98581 :     for (typename Container::iterator iter = container->begin();
     117       98581 :          iter != container->end(); iter = next) {
     118       92610 :         next = iter;
     119       92610 :         ++next;
     120       92610 :         delete iter->second;
     121             :     }
     122        5971 :     container->clear();
     123        5971 : }
     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_ */

Generated by: LCOV version 1.14