Line data Source code
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 2616 : IntrusivePtrRef() : boost::intrusive_ptr<D>(), referrer_(NULL) { 25 2616 : } 26 : 27 1099 : IntrusivePtrRef(D *data) : boost::intrusive_ptr<D>(data), referrer_(NULL) { 28 1099 : if (data) { 29 1 : intrusive_ptr_add_back_ref(IntrusiveReferrer(referrer_, this), data); 30 : } 31 1099 : } 32 : 33 9034 : IntrusivePtrRef(D *data, void *referrer) : boost::intrusive_ptr<D>(data), 34 9034 : referrer_(referrer) { 35 9034 : if (data) { 36 655 : intrusive_ptr_add_back_ref(IntrusiveReferrer(referrer, this), data); 37 : } 38 9034 : } 39 : 40 63 : IntrusivePtrRef(IntrusivePtrRef const &rhs, void *referrer) 41 63 : : boost::intrusive_ptr<D>(rhs), referrer_(referrer) { 42 63 : if (this->get()) { 43 0 : intrusive_ptr_add_back_ref(IntrusiveReferrer(referrer, this), 44 0 : this->get()); 45 : } 46 63 : } 47 : 48 13869 : virtual ~IntrusivePtrRef() { 49 13869 : if (this->get()) { 50 1572 : intrusive_ptr_del_back_ref(IntrusiveReferrer(referrer_, this), this->get()); 51 : } 52 27738 : } 53 : 54 48 : IntrusivePtrRef & operator=(IntrusivePtrRef const &rhs) { 55 48 : IntrusivePtrRef(rhs, this).swap(*this); 56 48 : return *this; 57 : } 58 : 59 334 : IntrusivePtrRef & operator=(D *rhs) { 60 334 : IntrusivePtrRef(rhs, this).swap(*this); 61 334 : return *this; 62 : } 63 : 64 0 : void reset() { 65 0 : IntrusivePtrRef(NULL, this).swap(*this); 66 0 : } 67 : 68 12 : void reset(D *rhs) { 69 12 : IntrusivePtrRef(rhs, this).swap(*this); 70 12 : } 71 : 72 394 : void swap(IntrusivePtrRef &rhs) { 73 : // Trigger Base class swap to swap values 74 394 : boost::intrusive_ptr<D>::swap(rhs); 75 : 76 : // swap the referrer for this and rhs 77 394 : D *tmp = this->get(); 78 394 : if (tmp) { 79 : // change referrer for new value of IntrusivePtrRef 80 121 : intrusive_ptr_add_back_ref(IntrusiveReferrer(referrer_, this), tmp); 81 121 : intrusive_ptr_del_back_ref(IntrusiveReferrer(rhs.referrer_, &rhs), tmp); 82 : } 83 394 : tmp = rhs.get(); 84 394 : if (tmp) { 85 : // change referrer for new value of rhs 86 209 : intrusive_ptr_add_back_ref(IntrusiveReferrer(rhs.referrer_, &rhs), tmp); 87 209 : intrusive_ptr_del_back_ref(IntrusiveReferrer(referrer_, this), tmp); 88 : } 89 394 : } 90 : 91 : private: 92 : void *referrer_; 93 : }; 94 : 95 : #endif // SRC_BASE_INTRUSIVE_PTR_BACK_REF_H_