Skip to content
Snippets Groups Projects

Fix random pointer deletion

Merged Johannes Junggeburth requested to merge GeoIntrusiveUnitTest into main
8 files
+ 154
46
Compare changes
  • Side-by-side
  • Inline
Files
8
@@ -5,6 +5,7 @@
@@ -5,6 +5,7 @@
#define GEOMODELKERNEL_GeoIntrusivePtr_H
#define GEOMODELKERNEL_GeoIntrusivePtr_H
#include <GeoModelKernel/RCBase.h>
#include <GeoModelKernel/RCBase.h>
 
#include <type_traits>
#include <utility>
#include <utility>
@@ -14,42 +15,73 @@ class GeoIntrusivePtr{
@@ -14,42 +15,73 @@ class GeoIntrusivePtr{
public:
public:
template <typename GeoTypeGrp> friend class GeoIntrusivePtr;
template <typename GeoTypeGrp> friend class GeoIntrusivePtr;
GeoIntrusivePtr() = default;
explicit GeoIntrusivePtr() noexcept = default;
// Standard constructor taking a bare pointer
// Standard constructor taking a bare pointer
GeoIntrusivePtr(GeoType* obj):
GeoIntrusivePtr(GeoType* obj) noexcept:
m_ptr{obj} {
m_ptr{obj} {
if (m_ptr) obj->ref();
if (m_ptr) obj->ref();
}
}
/// Copy constructor
/// Copy constructor
template <class GeoTypeGrp>
GeoIntrusivePtr(const GeoIntrusivePtr& other) noexcept:
GeoIntrusivePtr(const GeoIntrusivePtr<GeoTypeGrp>& other):
GeoIntrusivePtr{other.get()} {}
GeoIntrusivePtr{other.get()} {}
 
 
/// Copy constructor for derived types
 
template <typename GeoTypeGrp,
 
typename = typename std::enable_if<!std::is_same<GeoType,GeoTypeGrp>::value, bool>>
 
GeoIntrusivePtr(const GeoIntrusivePtr<GeoTypeGrp>& other) noexcept:
 
GeoIntrusivePtr{other.get()} {}
 
 
/// Move constructor
 
explicit GeoIntrusivePtr(GeoIntrusivePtr&& other) noexcept:
 
m_ptr{other.m_ptr} {
 
other.m_ptr = nullptr;
 
}
 
/// Move constructor for derived types
 
template <typename GeoTypeGrp,
 
typename = typename std::enable_if<!std::is_same<GeoType,GeoTypeGrp>::value, bool>>
 
GeoIntrusivePtr(GeoIntrusivePtr<GeoTypeGrp>&& other) noexcept:
 
m_ptr{other.m_ptr} {
 
other.m_ptr = nullptr;
 
}
 
 
/// Assignment operator
/// Assignment operator
template <class GeoTypeGrp>
GeoIntrusivePtr& operator=(const GeoIntrusivePtr& other) noexcept {
GeoIntrusivePtr& operator=(const GeoIntrusivePtr<GeoTypeGrp>& other) {
reset(other.get());
if (m_ptr != other.get()) {
return *this;
if(m_ptr) m_ptr->unref();
}
 
GeoIntrusivePtr& operator=(GeoType* other) noexcept {
 
reset(other);
 
return *this;
 
}
 
/// Move assignment operator
 
GeoIntrusivePtr& operator=(GeoIntrusivePtr&& other) {
 
if (m_ptr && m_ptr == other.get()) {
 
m_ptr->unref();
 
} else {
m_ptr = other.get();
m_ptr = other.get();
if(m_ptr) m_ptr->ref();
}
}
 
other.m_ptr = nullptr;
return *this;
return *this;
}
}
/// Move constructor
template <typename GeoTypeGrp,
template <class GeoTypeGrp>
typename = typename std::enable_if<!std::is_same<GeoType,GeoTypeGrp>::value, bool>>
GeoIntrusivePtr(GeoIntrusivePtr<GeoTypeGrp>&& other):
GeoIntrusivePtr& operator=(GeoIntrusivePtr<GeoTypeGrp>&& other) {
m_ptr{std::move(other.m_ptr)} { other.m_ptr = nullptr;}
if (m_ptr && m_ptr == other.get()) {
/// Move assignment operator
template <class GeoTypeGrp>
GeoIntrusivePtr& operator=(GeoIntrusivePtr<GeoTypeGrp>&& other) {
if (m_ptr != other.get()) {
if(m_ptr) m_ptr->unref();
m_ptr = std::move(other.m_ptr);
} else if (m_ptr) {
m_ptr->unref();
m_ptr->unref();
 
} else {
 
m_ptr = other.get();
}
}
other.m_ptr = nullptr;
other.m_ptr = nullptr;
return *this;
return *this;
}
}
 
/// Reset the pointer
 
void reset(GeoType* ptr = nullptr) {
 
if (m_ptr == ptr) return;
 
if (m_ptr) m_ptr->unref();
 
m_ptr = ptr;
 
if (m_ptr) m_ptr->ref();
 
}
/// Destructor
/// Destructor
~GeoIntrusivePtr() {
~GeoIntrusivePtr() {
if (m_ptr) m_ptr->unref();
if (m_ptr) m_ptr->unref();
@@ -69,10 +101,10 @@ class GeoIntrusivePtr{
@@ -69,10 +101,10 @@ class GeoIntrusivePtr{
/// Invalidity operator
/// Invalidity operator
bool operator!() const { return !m_ptr; }
bool operator!() const { return !m_ptr; }
/// Comparison operator
/// Comparison operator
template <class GeoTypeGrp>
// template <class GeoTypeGrp>
bool operator==(const GeoIntrusivePtr<GeoTypeGrp>& other) const {
// bool operator==(const GeoIntrusivePtr<GeoTypeGrp>& other) const {
return m_ptr == other.m_ptr;
// return m_ptr == other.m_ptr;
}
// }
bool operator==(GeoType* other) const {
bool operator==(GeoType* other) const {
return m_ptr == other;
return m_ptr == other;
}
}
@@ -84,4 +116,4 @@ class GeoIntrusivePtr{
@@ -84,4 +116,4 @@ class GeoIntrusivePtr{
GeoType* m_ptr{nullptr};
GeoType* m_ptr{nullptr};
};
};
#endif
#endif
 
\ No newline at end of file
Loading