Cytnx 1.1.2.dev761+ge28145acd
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
15 // The boost::intrusive_ptr hooks, found by argument-dependent lookup. Deduction through the base
16 // class gives the derived type T, so intrusive_ptr_release() deletes the complete object.
17 //
18 // They are namespace-scope function templates on purpose. Defined as friends inside the class
19 // template, they break nvcc's cudafe++ as soon as a constexpr destructor calls them, which
20 // boost::intrusive_ptr's destructor is since Boost 1.89 (#1124).
21 template <class T>
22 void intrusive_ptr_add_ref(const intrusive_ptr_base<T> *s);
23 template <class T>
24 void intrusive_ptr_release(const intrusive_ptr_base<T> *s);
25
26 template <class T>
27 class intrusive_ptr_base {
28 public:
30 intrusive_ptr_base()
31 : ref_count(0){
32 // pass
33 };
34
36 intrusive_ptr_base(intrusive_ptr_base<T> const &) : ref_count(0) {
37 // pass
38 }
39
40 // copy assignment
41 intrusive_ptr_base &operator=(intrusive_ptr_base const &rhs) {
42 // not allowed.
43 return *this;
44 }
45
46 boost::intrusive_ptr<T> self() { return boost::intrusive_ptr<T>((T *)this); }
47
48 boost::intrusive_ptr<const T> self() const {
49 return boost::intrusive_ptr<const T>((T const *)this);
50 }
51
52 int refcount() const { return ref_count; }
53
54 private:
55 template <class U>
56 friend void intrusive_ptr_add_ref(const intrusive_ptr_base<U> *s);
57 template <class U>
58 friend void intrusive_ptr_release(const intrusive_ptr_base<U> *s);
59
60 // should be modifiable within the class.
61 mutable boost::detail::atomic_count ref_count;
62 };
63
64 template <class T>
65 void intrusive_ptr_add_ref(const intrusive_ptr_base<T> *s) {
66 assert(s != nullptr);
67 assert(s->ref_count >= 0);
68 ++s->ref_count;
69 }
70
71 template <class T>
72 void intrusive_ptr_release(const intrusive_ptr_base<T> *s) {
73 assert(s != nullptr);
74 assert(s->ref_count > 0);
75 if (--s->ref_count == 0) boost::checked_delete(static_cast<T const *>(s));
76 }
78} // namespace cytnx
79
80#endif // CYTNX_INTRUSIVE_PTR_BASE_H_
Definition Accessor.hpp:12