Cytnx v1.0.0
Loading...
Searching...
No Matches
intrusive_ptr_base.hpp
Go to the documentation of this file.
1#ifndef CYTNX_INTRUSIVE_PTR_BASE_H_
2#define CYTNX_INTRUSIVE_PTR_BASE_H_
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:
16 intrusive_ptr_base()
17 : ref_count(0){
18 // pass
19 };
20
22 intrusive_ptr_base(intrusive_ptr_base<T> const &) : ref_count(0) {
23 // pass
24 }
25
26 // copy assignment
27 intrusive_ptr_base &operator=(intrusive_ptr_base const &rhs) {
28 // not allowed.
29 return *this;
30 }
31
32 // hook boost::intrusive_ptr add
33 friend void intrusive_ptr_add_ref(T *s) {
34 // add ref
35 assert(s != nullptr);
36 auto &base = static_cast<const intrusive_ptr_base<T> &>(*s);
37 assert(base.ref_count >= 0);
38 ++base.ref_count;
39 }
40 // hook boost::intrusive_ptr release
41 friend void intrusive_ptr_release(T *s) {
42 // release ref
43 assert(s != nullptr);
44 auto &base = static_cast<const intrusive_ptr_base<T> &>(*s);
45 assert(base.ref_count > 0);
46 if (--base.ref_count == 0) boost::checked_delete(static_cast<T const *>(s));
47 }
48
49 boost::intrusive_ptr<T> self() { return boost::intrusive_ptr<T>((T *)this); }
50
51 boost::intrusive_ptr<const T> self() const {
52 return boost::intrusive_ptr<const T>((T const *)this);
53 }
54
55 int refcount() const { return ref_count; }
56
57 private:
58 // should be modifiable within the class.
59 mutable boost::detail::atomic_count ref_count;
60 };
62} // namespace cytnx
63
64#endif // CYTNX_INTRUSIVE_PTR_BASE_H_
Definition Accessor.hpp:12