delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
intrusive_ptr.hpp
1
8#pragma once
9
10#include <utility>
11
12namespace delpi {
13
21template <class T>
22class intrusive_ptr {
23 public:
24 typedef T element_type;
25
27 intrusive_ptr() : ptr_{nullptr} {}
28
39 explicit intrusive_ptr(T* p, const bool add_ref = true) : ptr_{p} {
40 if (ptr_ && add_ref) ptr_->AddRef();
41 }
42
53 intrusive_ptr(const intrusive_ptr& ip) : ptr_{ip.ptr_} {
54 if (ptr_) ptr_->AddRef();
55 }
56
60 intrusive_ptr(intrusive_ptr&& ip) noexcept : ptr_{ip.ptr_} { ip.ptr_ = nullptr; }
61
64 if (ptr_) ptr_->Release();
65 }
66
76 intrusive_ptr& operator=(const intrusive_ptr& ip) {
77 intrusive_ptr{ip}.swap(*this);
78 return *this;
79 }
80
86 intrusive_ptr& operator=(intrusive_ptr&& ip) noexcept {
87 // NOLINTNEXTLINE(runtime/explicit) per C++ standard concept definition
88 intrusive_ptr(static_cast<intrusive_ptr&&>(ip)).swap(*this);
89 return *this;
90 }
91
100 intrusive_ptr& operator=(T* ptr) {
101 intrusive_ptr{ptr}.swap(*this);
102 return *this;
103 }
104
106 T& operator*() const { return *ptr_; }
111 T* operator->() const { return ptr_; }
113 [[nodiscard]] T* get() const { return ptr_; }
114
116 void reset() { intrusive_ptr{}.swap(*this); }
117
123 void swap(intrusive_ptr& ip) noexcept { std::swap(ptr_, ip.ptr_); }
124
131 */
132 void attach(T* ptr) {
133 T* const temp_ptr = ptr_;
134 ptr_ = ptr;
135 if (temp_ptr) temp_ptr->Release();
136 }
137
144 */
145 T* detach() {
146 T* const pTemp = ptr_;
147 ptr_ = nullptr;
148 return pTemp;
149 }
150
151 typedef element_type* intrusive_ptr::* bool_;
164 operator bool_() const noexcept { return ptr_ ? &intrusive_ptr::ptr_ : nullptr; }
165 bool operator!() const { return ptr_ == nullptr; }
166
167 protected:
168 T* ptr_;
169};
170
171} // namespace delpi
Pointer to a generic object that supports intrusive reference counting.
T element_type
The type this intrusive_ptr points to.
intrusive_ptr & operator=(const intrusive_ptr &ip)
Copy assignment operator.
~intrusive_ptr()
Destruct the intrusive_ptr object, releasing the owned pointer.
T * detach()
Surrenders the reference held by an intrusive_ptr pointer.
T * ptr_
Raw pointer to the data.
void reset()
Release the owned pointer, decrementing the reference count and setting ptr_ to null.
intrusive_ptr(T *p, const bool add_ref=true)
Construct a new intrusive_ptr object wrapping the given pointer p.
intrusive_ptr & operator=(intrusive_ptr &&ip) noexcept
Move assignment operator.
void swap(intrusive_ptr &ip) noexcept
Exchanges the owned pointer between two intrusive_ptr objects.
void attach(T *ptr)
Sets the owned pointer to the given pointer ptr without incrementing the reference count.
intrusive_ptr(intrusive_ptr &&ip) noexcept
Construct a new intrusive_ptr object from another intrusive_ptr object using move semantics.
intrusive_ptr & operator=(T *ptr)
Copy assignment operator.
intrusive_ptr(const intrusive_ptr &ip)
Construct a new intrusive_ptr object from another intrusive_ptr object.
Global namespace for the delpi library.
LpRowSense operator!(const LpRowSense sense)
Invert the sense with delta == 0.