Cytnx v0.7.4
Loading...
Searching...
No Matches
intrusive_ptr_base.hpp
Go to the documentation of this file.
1#ifndef _H_intrusive_ptr_base_
2#define _H_intrusive_ptr_base_
3
4#include <ostream>
5#include <cassert>
6#include <boost/smart_ptr/intrusive_ptr.hpp>
7#include <boost/checked_delete.hpp>
8#include <boost/detail/atomic_count.hpp>
9
10namespace cytnx {
12 template<class T>
13 class intrusive_ptr_base {
14 public:
15
17 intrusive_ptr_base() : ref_count(0) {
18 // pass
19 };
20
22 intrusive_ptr_base(intrusive_ptr_base<T> const &) : ref_count(0) {
23 // pass
24 }
25
26
27 // copy assignment
28 intrusive_ptr_base &operator=(intrusive_ptr_base const &rhs) {
29 // not allowed.
30 return *this;
31 }
32
33 // hook boost::intrusive_ptr add
34 friend void intrusive_ptr_add_ref(intrusive_ptr_base<T> const *s) {
35 // add ref
36 //std::cout << "add" << std::endl;
37 assert(s->ref_count >= 0);
38 assert(s != 0);
39 ++s->ref_count;
40 }
41
42
43 // hook boost::intrusive_ptr release
44 friend void intrusive_ptr_release(intrusive_ptr_base<T> const *s) {
45 //release ref
46 //std::cout << "release" << std::endl;
47 assert(s->ref_count > 0);
48 assert(s != 0);
49 if (--s->ref_count == 0)
50 boost::checked_delete(static_cast<T const *>(s));
51 }
52
53 boost::intrusive_ptr<T> self() {
54 return boost::intrusive_ptr<T>((T * )
55 this);
56 }
57
58 boost::intrusive_ptr<const T> self() const {
59 return boost::intrusive_ptr<const T>((T const*)this);
60 }
61
62 int refcount() const {
63 return ref_count;
64 }
65
66 private:
67 // should be modifiable within the class.
68 mutable boost::detail::atomic_count ref_count;
69
70 };
72}//cytnx
73
74#endif
Definition Accessor.hpp:12