RefCount.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section -- Refcount allocator */
10 
11 #ifndef SQUID_REFCOUNT_H_
12 #define SQUID_REFCOUNT_H_
13 
14 // reference counting requires the Lock API on base classes
15 #include "base/Lock.h"
16 
17 #include <iostream>
18 
25 template <class C>
26 class RefCount
27 {
28 
29 public:
30  RefCount () : p_ (NULL) {}
31 
32  RefCount (C const *p) : p_(p) { reference (*this); }
33 
35  dereference();
36  }
37 
38  RefCount (const RefCount &p) : p_(p.p_) {
39  reference (p);
40  }
41 
42  RefCount (RefCount &&p) : p_(std::move(p.p_)) {
43  p.p_=NULL;
44  }
45 
47  // DO NOT CHANGE THE ORDER HERE!!!
48  // This preserves semantics on self assignment
49  C const *newP_ = p.p_;
50  reference(p);
51  dereference(newP_);
52  return *this;
53  }
54 
56  if (this != &p) {
57  dereference(p.p_);
58  p.p_ = NULL;
59  }
60  return *this;
61  }
62 
63  explicit operator bool() const { return p_; }
64 
65  bool operator !() const { return !p_; }
66 
67  C * operator-> () const {return const_cast<C *>(p_); }
68 
69  C & operator * () const {
70  assert(p_);
71  return *const_cast<C *>(p_);
72  }
73 
74  C * getRaw() const { return const_cast<C *>(p_); }
75 
76  bool operator == (const RefCount& p) const {
77  return p.p_ == p_;
78  }
79 
80  bool operator != (const RefCount &p) const {
81  return p.p_ != p_;
82  }
83 
84 private:
85  void dereference(C const *newP = NULL) {
86  /* Setting p_ first is important:
87  * we may be freed ourselves as a result of
88  * delete p_;
89  */
90  C const (*tempP_) (p_);
91  p_ = newP;
92 
93  if (tempP_ && tempP_->unlock() == 0)
94  delete tempP_;
95  }
96 
97  void reference (const RefCount& p) {
98  if (p.p_)
99  p.p_->lock();
100  }
101 
102  C const *p_;
103 
104 };
105 
106 template <class C>
107 inline std::ostream &operator <<(std::ostream &os, const RefCount<C> &p)
108 {
109  if (p != NULL)
110  return os << p.getRaw() << '*' << p->LockCount();
111  else
112  return os << "NULL";
113 }
114 
115 #endif /* SQUID_REFCOUNT_H_ */
116 
C * operator->() const
Definition: RefCount.h:67
#define assert(EX)
Definition: assert.h:17
char * p
Definition: membanger.c:43
RefCount(RefCount &&p)
Definition: RefCount.h:42
C & operator*() const
Definition: RefCount.h:69
void dereference(C const *newP=NULL)
Definition: RefCount.h:85
RefCount(C const *p)
Definition: RefCount.h:32
C const * p_
Definition: RefCount.h:102
void reference(const RefCount &p)
Definition: RefCount.h:97
~RefCount()
Definition: RefCount.h:34
bool operator==(const RefCount &p) const
Definition: RefCount.h:76
RefCount()
Definition: RefCount.h:30
RefCount & operator=(const RefCount &p)
Definition: RefCount.h:46
RefCount(const RefCount &p)
Definition: RefCount.h:38
C * getRaw() const
Definition: RefCount.h:74
#define NULL
Definition: types.h:166
bool operator!=(const RefCount &p) const
Definition: RefCount.h:80
bool operator!() const
Definition: RefCount.h:65

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors