Skip to content
Snippets Groups Projects
Commit 462ab416 authored by Johannes Junggeburth's avatar Johannes Junggeburth :dog2:
Browse files

Merge branch 'TestIntrusivePtrVolII' into 'main'

GeoIntrusivePtr - Fix move semantics + add test

See merge request !278
parents d8a0a07a d33772c7
Branches
Tags
1 merge request!278GeoIntrusivePtr - Fix move semantics + add test
Pipeline #6884034 passed with warnings
......@@ -15,14 +15,14 @@ class GeoIntrusivePtr{
public:
template <typename GeoTypeGrp> friend class GeoIntrusivePtr;
GeoIntrusivePtr() noexcept = default;
GeoIntrusivePtr() noexcept = default;
// Standard constructor taking a bare pointer
GeoIntrusivePtr(GeoType* obj) noexcept:
m_ptr{obj} {
if (m_ptr) obj->ref();
}
/// Copy constructor
explicit GeoIntrusivePtr(const GeoIntrusivePtr& other) noexcept:
GeoIntrusivePtr(const GeoIntrusivePtr& other) noexcept:
GeoIntrusivePtr{other.get()} {}
/// Copy constructor for derived types
......@@ -32,16 +32,16 @@ class GeoIntrusivePtr{
GeoIntrusivePtr{other.get()} {}
/// Move constructor
explicit GeoIntrusivePtr(GeoIntrusivePtr&& other) noexcept:
m_ptr{other.m_ptr} {
other.m_ptr = nullptr;
GeoIntrusivePtr(GeoIntrusivePtr&& other) noexcept:
GeoIntrusivePtr{} {
move(std::move(other));
}
/// 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;
GeoIntrusivePtr{} {
move(std::move(other));
}
......@@ -62,24 +62,14 @@ class GeoIntrusivePtr{
return *this;
}
/// Move assignment operator
GeoIntrusivePtr& operator=(GeoIntrusivePtr&& other) {
if (m_ptr && m_ptr == other.get()) {
m_ptr->unref();
} else {
m_ptr = other.get();
}
other.m_ptr = nullptr;
GeoIntrusivePtr& operator=(GeoIntrusivePtr&& other) noexcept {
move(std::move(other));
return *this;
}
template <typename GeoTypeGrp,
typename = typename std::enable_if<!std::is_same<GeoType,GeoTypeGrp>::value, bool>>
GeoIntrusivePtr& operator=(GeoIntrusivePtr<GeoTypeGrp>&& other) {
if (m_ptr && m_ptr == other.get()) {
m_ptr->unref();
} else {
m_ptr = other.get();
}
other.m_ptr = nullptr;
GeoIntrusivePtr& operator=(GeoIntrusivePtr<GeoTypeGrp>&& other) noexcept {
move(std::move(other));
return *this;
}
/// Reset the pointer
......@@ -89,6 +79,14 @@ class GeoIntrusivePtr{
m_ptr = ptr;
if (m_ptr) m_ptr->ref();
}
template <class GeoTypeGrp>
void move(GeoIntrusivePtr<GeoTypeGrp>&& obj) {
if (m_ptr != obj.get()) {
if (m_ptr) m_ptr->unref();
m_ptr = obj.m_ptr;
}
obj.m_ptr = nullptr;
}
/// Destructor
~GeoIntrusivePtr() {
if (m_ptr) m_ptr->unref();
......
......@@ -2,6 +2,7 @@
#include <GeoModelKernel/GeoFullPhysVol.h>
#include <GeoModelKernel/GeoTransform.h>
#include <GeoModelKernel/GeoPhysVol.h>
#include <GeoModelKernel/GeoBox.h>
#include <iostream>
......@@ -65,6 +66,39 @@ int main(int argc, char *argv[]){
physVol4 = std::move(physVol3);
CHECKCOUNT(physVol4, 2);
}
GeoIntrusivePtr<GeoPhysVol> world{new GeoPhysVol(nullptr)};
world->add(new GeoTransform(GeoTrf::Translate3D(0.,0.,0 )));
{
GeoIntrusivePtr<GeoFullPhysVol> childVol{new GeoFullPhysVol(nullptr)};
world->add(childVol);
CHECKCOUNT(world, 1);
CHECKCOUNT(childVol, 2);
CHECKCOUNT(world, 1);
childVol->getAbsoluteTransform();
CHECKCOUNT(childVol, 2);
CHECKCOUNT(world, 1);
childVol->getX();
CHECKCOUNT(world, 1);
childVol->getDefX();
CHECKCOUNT(world, 1);
childVol->getDefAbsoluteTransform();
CHECKCOUNT(world, 1);
}
{
GeoIntrusivePtr<GeoPhysVol> childVol{new GeoPhysVol(nullptr)};
world->add(childVol);
CHECKCOUNT(world, 1);
CHECKCOUNT(childVol, 2);
CHECKCOUNT(world, 1);
childVol->getX();
CHECKCOUNT(world, 1);
childVol->getDefX();
CHECKCOUNT(world, 1);
}
CHECKCOUNT(world, 1);
return EXIT_SUCCESS;
}
#pragma clang diagnostic pop
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment