mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-25 06:38:13 -05:00
215 lines
7.4 KiB
C++
215 lines
7.4 KiB
C++
//##################################################################################################
|
|
//
|
|
// Custom Visualization Core library
|
|
// Copyright (C) 2011-2013 Ceetron AS
|
|
//
|
|
// This library may be used under the terms of either the GNU General Public License or
|
|
// the GNU Lesser General Public License as follows:
|
|
//
|
|
// GNU General Public License Usage
|
|
// This library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
//
|
|
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
|
// for more details.
|
|
//
|
|
// GNU Lesser General Public License Usage
|
|
// This library is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation; either version 2.1 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
//
|
|
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
|
// for more details.
|
|
//
|
|
//##################################################################################################
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
|
|
namespace caf
|
|
{
|
|
class PdmObjectHandle;
|
|
|
|
//==================================================================================================
|
|
/// Helper class for the PdmPointer class
|
|
/// The add and removing of references is put into a pure static class to
|
|
/// resolve circular include problems.
|
|
//
|
|
/// Overall idea of the referencing system:
|
|
/// The addressToObjectPointer is added to a std::set in the object pointed to.
|
|
/// when the object pointed to is deleted, its destructor sets the object pointers
|
|
/// it has addresses to to NULL
|
|
//==================================================================================================
|
|
|
|
class PdmPointerImpl
|
|
{
|
|
private:
|
|
template <class T>
|
|
friend class PdmPointer;
|
|
static void addReference( PdmObjectHandle** addressToObjectPointer );
|
|
static void removeReference( PdmObjectHandle** addressToObjectPointer );
|
|
};
|
|
|
|
//==================================================================================================
|
|
/// Guarded pointer class to point at PdmObjects
|
|
/// Use a PdmPointer<SomePdmObject> in the same way as a normal pointer.
|
|
/// The guarding sets the pointer to NULL if the object pointed to dies
|
|
///
|
|
/// NOTE: This is not reference counting. The user is responsible to delete the objects pointed to.
|
|
/// It _can_ be used together with the cvf::ref system if necessary (this is no recommendation)
|
|
//==================================================================================================
|
|
|
|
template <class T>
|
|
class PdmPointer
|
|
{
|
|
PdmObjectHandle* m_object;
|
|
|
|
public:
|
|
inline PdmPointer()
|
|
: m_object( nullptr )
|
|
{
|
|
}
|
|
inline PdmPointer( T* p )
|
|
: m_object( p )
|
|
{
|
|
PdmPointerImpl::addReference( &m_object );
|
|
}
|
|
inline PdmPointer( const PdmPointer<T>& p )
|
|
: m_object( p.m_object )
|
|
{
|
|
PdmPointerImpl::addReference( &m_object );
|
|
}
|
|
inline ~PdmPointer() { PdmPointerImpl::removeReference( &m_object ); }
|
|
|
|
T* p() const { return static_cast<T*>( const_cast<PdmObjectHandle*>( m_object ) ); }
|
|
bool isNull() const { return !m_object; }
|
|
bool notNull() const { return !isNull(); }
|
|
operator T*() const { return static_cast<T*>( const_cast<PdmObjectHandle*>( m_object ) ); }
|
|
T& operator*() const { return *static_cast<T*>( const_cast<PdmObjectHandle*>( m_object ) ); }
|
|
T* operator->() const { return static_cast<T*>( const_cast<PdmObjectHandle*>( m_object ) ); }
|
|
PdmPointer<T>& operator=( const PdmPointer<T>& p )
|
|
{
|
|
if ( this != &p ) PdmPointerImpl::removeReference( &m_object );
|
|
m_object = p.m_object;
|
|
PdmPointerImpl::addReference( &m_object );
|
|
return *this;
|
|
}
|
|
PdmPointer<T>& operator=( T* p )
|
|
{
|
|
if ( m_object != p ) PdmPointerImpl::removeReference( &m_object );
|
|
m_object = p;
|
|
PdmPointerImpl::addReference( &m_object );
|
|
return *this;
|
|
}
|
|
template <class S>
|
|
bool operator==( const PdmPointer<S>& rhs ) const
|
|
{
|
|
return m_object == rhs.rawPtr();
|
|
}
|
|
// Private methods used by PdmField<T*> and PdmPointersField<T*>. Do not use unless you mean it !
|
|
PdmObjectHandle* rawPtr() const { return m_object; }
|
|
void setRawPtr( PdmObjectHandle* p )
|
|
{
|
|
if ( m_object != p ) PdmPointerImpl::removeReference( &m_object );
|
|
m_object = p;
|
|
PdmPointerImpl::addReference( &m_object );
|
|
}
|
|
};
|
|
|
|
//==================================================================================================
|
|
/// Guarded const pointer to a PdmObject.
|
|
///
|
|
/// Same lifetime-guarding behavior as PdmPointer<T>, but exposes only const access to the target.
|
|
/// Use for non-owning read-only observers (text providers, renderers, part managers) that hold
|
|
/// a reference to a logically-const PdmObject. Construction from a const T* does not require
|
|
/// the caller to const_cast.
|
|
//==================================================================================================
|
|
|
|
template <class T>
|
|
class PdmConstPointer
|
|
{
|
|
PdmPointer<T> m_ptr;
|
|
|
|
public:
|
|
inline PdmConstPointer() = default;
|
|
inline PdmConstPointer( const T* p )
|
|
: m_ptr( const_cast<T*>( p ) )
|
|
{
|
|
}
|
|
inline PdmConstPointer( const PdmConstPointer<T>& p ) = default;
|
|
inline PdmConstPointer<T>& operator=( const PdmConstPointer<T>& p ) = default;
|
|
|
|
inline PdmConstPointer( const PdmPointer<T>& p )
|
|
: m_ptr( p )
|
|
{
|
|
}
|
|
|
|
const T* p() const { return m_ptr.p(); }
|
|
bool isNull() const { return m_ptr.isNull(); }
|
|
bool notNull() const { return m_ptr.notNull(); }
|
|
operator const T*() const { return m_ptr.p(); }
|
|
const T& operator*() const { return *m_ptr.p(); }
|
|
const T* operator->() const { return m_ptr.p(); }
|
|
|
|
PdmConstPointer<T>& operator=( const T* p )
|
|
{
|
|
m_ptr = const_cast<T*>( p );
|
|
return *this;
|
|
}
|
|
|
|
template <class S>
|
|
bool operator==( const PdmConstPointer<S>& rhs ) const
|
|
{
|
|
return m_ptr.rawPtr() == rhs.rawPtr();
|
|
}
|
|
|
|
PdmObjectHandle* rawPtr() const { return m_ptr.rawPtr(); }
|
|
};
|
|
|
|
} // End of namespace caf
|
|
|
|
#include <QMetaType>
|
|
Q_DECLARE_METATYPE( caf::PdmPointer<caf::PdmObjectHandle> );
|
|
|
|
#include "cafPdmFieldTraits.h"
|
|
|
|
#include <QVariant>
|
|
|
|
namespace caf
|
|
{
|
|
|
|
template <typename T>
|
|
QVariant pdmToVariant( const PdmPointer<T>& value )
|
|
{
|
|
return QVariant::fromValue( PdmPointer<PdmObjectHandle>( value.rawPtr() ) );
|
|
}
|
|
|
|
template <typename T>
|
|
void pdmFromVariant( const QVariant& v, PdmPointer<T>& out )
|
|
{
|
|
out.setRawPtr( v.value<PdmPointer<PdmObjectHandle>>().rawPtr() );
|
|
}
|
|
|
|
template <typename T>
|
|
struct PdmVariantEqualImpl<PdmPointer<T>>
|
|
{
|
|
static bool equal( const QVariant& a, const QVariant& b )
|
|
{
|
|
return a.value<PdmPointer<PdmObjectHandle>>() == b.value<PdmPointer<PdmObjectHandle>>();
|
|
}
|
|
};
|
|
|
|
} // namespace caf
|