OpenSDN source code
intrusive_ptr_back_ref.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef SRC_BASE_INTRUSIVE_PTR_BACK_REF_H_
7 #define SRC_BASE_INTRUSIVE_PTR_BACK_REF_H_
8 
9 #include <boost/intrusive_ptr.hpp>
10 
11 // IntrusivePtrRef Class is an extension to boost::intrusive_ptr
12 // to provide additional functionality of making back reference
13 // pointer available with callbacks intrusive_ptr_add_back_ref
14 // and intrusive_ptr_del_back_ref, using which application using
15 // IntrusivePtrRef can keep track of pointers who holds references
16 // to the object
17 
18 // referrer, pair of base pointer and member pointer
19 typedef std::pair<void*, void*> IntrusiveReferrer;
20 
21 template <class D>
22 class IntrusivePtrRef : public boost::intrusive_ptr<D> {
23 public:
24  IntrusivePtrRef() : boost::intrusive_ptr<D>(), referrer_(NULL) {
25  }
26 
27  IntrusivePtrRef(D *data) : boost::intrusive_ptr<D>(data), referrer_(NULL) {
28  if (data) {
30  }
31  }
32 
33  IntrusivePtrRef(D *data, void *referrer) : boost::intrusive_ptr<D>(data),
34  referrer_(referrer) {
35  if (data) {
36  intrusive_ptr_add_back_ref(IntrusiveReferrer(referrer, this), data);
37  }
38  }
39 
40  IntrusivePtrRef(IntrusivePtrRef const &rhs, void *referrer)
41  : boost::intrusive_ptr<D>(rhs), referrer_(referrer) {
42  if (this->get()) {
44  this->get());
45  }
46  }
47 
48  virtual ~IntrusivePtrRef() {
49  if (this->get()) {
51  }
52  }
53 
55  IntrusivePtrRef(rhs, this).swap(*this);
56  return *this;
57  }
58 
60  IntrusivePtrRef(rhs, this).swap(*this);
61  return *this;
62  }
63 
64  void reset() {
65  IntrusivePtrRef(NULL, this).swap(*this);
66  }
67 
68  void reset(D *rhs) {
69  IntrusivePtrRef(rhs, this).swap(*this);
70  }
71 
72  void swap(IntrusivePtrRef &rhs) {
73  // Trigger Base class swap to swap values
74  boost::intrusive_ptr<D>::swap(rhs);
75 
76  // swap the referrer for this and rhs
77  D *tmp = this->get();
78  if (tmp) {
79  // change referrer for new value of IntrusivePtrRef
82  }
83  tmp = rhs.get();
84  if (tmp) {
85  // change referrer for new value of rhs
88  }
89  }
90 
91 private:
92  void *referrer_;
93 };
94 
95 #endif // SRC_BASE_INTRUSIVE_PTR_BACK_REF_H_
void intrusive_ptr_add_back_ref(const IntrusiveReferrer ref, const Interface *p)
void intrusive_ptr_del_back_ref(const IntrusiveReferrer ref, const Interface *p)
void swap(IntrusivePtrRef &rhs)
IntrusivePtrRef & operator=(D *rhs)
IntrusivePtrRef(D *data, void *referrer)
IntrusivePtrRef(IntrusivePtrRef const &rhs, void *referrer)
IntrusivePtrRef & operator=(IntrusivePtrRef const &rhs)
std::pair< void *, void * > IntrusiveReferrer