107class PyObjectStorage:
public Cascade
111 typedef PyObjectStorage<T, Cascade> TSelf;
114 typedef T& TReference;
116 TStorage& storage() {
return storage_; }
117 const TStorage& storage()
const {
return storage_; }
121 PyObjectStorage(): Cascade(), storage_(defaultStorage()) {}
122 explicit PyObjectStorage(T* pointee): Cascade(), storage_(impl::fixObjectType(pointee)) {}
123 PyObjectStorage(
const PyObjectStorage& other): Cascade(other), storage_(other.storage_) {}
124 PyObjectStorage(PyObjectStorage&& other)
noexcept:
125 Cascade(std::forward<Cascade>(other)),
126 storage_(other.storage_)
128 other.storage_ =
nullptr;
130 template <
typename U> PyObjectStorage(
const PyObjectStorage<U, Cascade>& other):
131 Cascade(other), storage_(other.storage()) {}
132 TPointer pointer()
const {
return storage_; }
133 void dispose() { storage_ = 0; }
134 bool isNull()
const {
return !storage_; }
135 void swap(TSelf& other) { Cascade::swap(other); std::swap(storage_, other.storage_); }
136 static TStorage defaultStorage() {
return 0; }
137 template <
typename U>
const PyObjectStorage<U, Cascade> staticCast()
const
139 return PyObjectStorage<U, Cascade>(
static_cast<U*
>(storage_), *
this);
141 template <
typename U>
const PyObjectStorage<U, Cascade> dynamicCast()
const
143 if (U* p =
dynamic_cast<U*
>(storage_))
145 return PyObjectStorage<U, Cascade>(p, *
this);
147 return PyObjectStorage<U, Cascade>();
149 template <
typename U>
const PyObjectStorage<U, Cascade> constCast()
const
151 return PyObjectStorage<U, Cascade>(
const_cast<U*
>(storage_), *
this);
154 template <
typename U,
typename C>
friend class PyObjectStorage;
155 PyObjectStorage(T* pointee,
const Cascade& cascade): Cascade(cascade), storage_(pointee) {}
166class LASS_PYTHON_DLL PyObjectCounter
172 template <
typename T>
void init(T* ) {}
173 template <
typename T>
void dispose(T* ) {}
174 template <
typename T>
void increment(T* pointee)
176 if (Py_IsInitialized())
186 template <
typename T>
void increment(
const T* pointee)
188 increment(
const_cast<T*
>(pointee));
190 template <
typename T>
bool decrement(T* pointee)
192 LASS_ASSERT(pointee);
194 if (Py_IsInitialized())
197 r = pointee->ob_refcnt <=1;
202 r = pointee->ob_refcnt <=1;
203 if (--pointee->ob_refcnt == 0)
205 if constexpr (std::is_convertible_v<T*, PyObjectPlus*>)
212 PyTypeObject* type = Py_TYPE(pointee);
213 destructor dealloc = type->tp_dealloc;
220 template <
typename T>
bool decrement(
const T* pointee)
222 return decrement(
const_cast<T*
>(pointee));
224 template <
typename T> TCount count(T* pointee)
const
226 LASS_ASSERT(pointee);
227 return pointee->ob_refcnt;
229 void swap(PyObjectCounter& ) {}