Name for smart pointer / coding pattern

Recently, I often came across situations when I needed something similar to this data structure.

Limitations : C ++ 03 standard.


 +-----+--------------+               +----+
 |node0| NodeDataRef ->-------------->|data|
 +-----+--------------+               +----+
 +-----+--------------+                ^^ ^
 |node1| NodeDataRef ->----------------+| |
 +-----+--------------+                 | |
 +-----+--------------+                 | |
 |node2| NodeDataRef ->-----------------+ |
 +-----+--------------+                   |
                                          |
 +-----+--------------+                   |
 |root | RootDataRef ->-------------------+
 +-----+--------------+              
  • There are several classes Nodewhere each Nodecontains a NodeDataRef"reference" (think shared_ptr) to the same instance of " Data" (class, structure, independently dynamically distributed).
  • There is also a " Root" or "master" node / class that contains a link RootDataRef(this time, I think weak_ptr) to the same " Data".
  • Node , Data RootDataRef 0/NULL. NodeDataRef shared_ptr<Data> RootDataRef weak_ptr<Data>
  • root node , NodeDataRef s. NodeDataRef, , NULL/0, RootDataRef 0/NULL.

.. weak_ptr<Data>, shared_ptr<Data>.

  • / ?
  • Boost Qt 4? ( "" ).
+5
5

/ ?

, , .

, Boost Qt 4? ( "" )

, . , . , , , .

, , , - , .

UPDATE:

"" , "", . , , , - ( ).

, , - , - .

+4

shared_ptr<T> weak_ptr<T> T = scoped_ptr<Data>. , Data ).

shared_ptr make_shared<unique_ptr<Data>>(new Data(...));

weak_ptr , root.lock().reset()

template <typename T>
struct RootHandle : public boost::weak_ptr< boost::scoped_ptr<T> >
{
    typedef boost::weak_ptr< boost::scoped_ptr<T> > weak_type;
    typedef boost::shared_ptr< boost::scoped_ptr<T> > strong_type;

    RootHandle() {}
    RootHandle(const weak_type& impl)
        :weak_type(impl) {}


    T* get() const
    {
        strong_type x = lock();
        return (x) ? x->get() : 0;
    }

    void reset()
    {
        strong_type x = lock();
        if (x)
            x->reset();
    }
};

template <typename T>
struct NormalHandle : public boost::shared_ptr< boost::scoped_ptr<T> >
{
public:
    typedef boost::shared_ptr< boost::scoped_ptr<T> > strong_type;

    NormalHandle() {}
    NormalHandle(const strong_type& impl)
        :strong_type(impl) {}

    T* get() const
    {
        boost::scoped_ptr<T>* ppx = strong_type::get();
        return (0 != ppx) ? ppx->get() : 0;
    }   
};

NormalHandles :

NormalHandle<Data> handle1(boost::make_shared< boost::scoped_ptr<Data> >(new Data(4, 3, "abc")));

0

, , () , , - NodeDataRef ( ).

- Data. RootDataRef , - . , NodeDataRef, , . root, , NodeDataRef .

0

( ) , shared_ptr:
shared_ptr < shared_ptr < →
, , reset shared_ptr ( !)
, shared_ptr.

0

Qt- (?), , .

, RootDataRef QSharedPointer . NodeDataRef, RootDataRef , QSharedPointer:: toWeakRef. , NodeDataRef s, RootDataRef (, ?) .

, , QWeakPointer:: toStrongRef, , .


Edit: Alternatively, do you really need a pointer? If this is a case of shared data and not an actual shared object, you can consider the Qt implicit exchange scheme in QSharedDataPointer and especially QExplicitlySharedDataPointer .

0
source

All Articles