C++: Fix duplicated shared_ptr creation.
This commit is contained in:
parent
3bc172a61f
commit
0d0170ae9e
|
@ -125,8 +125,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Base template for most classes which wrap a struct type from libsigrok. */
|
/* Base template for most classes which wrap a struct type from libsigrok. */
|
||||||
template <class Parent, typename Struct> class SR_API StructureWrapper :
|
template <class Parent, typename Struct> class SR_API StructureWrapper
|
||||||
public enable_shared_from_this<StructureWrapper<Parent, Struct> >
|
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
/* Parent object which owns this child object's underlying structure.
|
/* Parent object which owns this child object's underlying structure.
|
||||||
|
@ -144,24 +143,41 @@ protected:
|
||||||
references to both the parent and all its children are gone. */
|
references to both the parent and all its children are gone. */
|
||||||
shared_ptr<Parent> parent;
|
shared_ptr<Parent> parent;
|
||||||
|
|
||||||
|
/* Weak pointer for shared_from_this() implementation. */
|
||||||
|
weak_ptr<StructureWrapper<Parent, Struct> > weak_this;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
shared_ptr<StructureWrapper<Parent, Struct> >
|
/* Note, this implementation will create a new smart_ptr if none exists. */
|
||||||
get_shared_pointer(Parent *parent)
|
shared_ptr<StructureWrapper<Parent, Struct> > shared_from_this()
|
||||||
{
|
{
|
||||||
if (!parent)
|
shared_ptr<StructureWrapper<Parent, Struct> > shared;
|
||||||
throw Error(SR_ERR_BUG);
|
|
||||||
this->parent = static_pointer_cast<Parent>(parent->shared_from_this());
|
if (!(shared = weak_this.lock()))
|
||||||
return shared_ptr<StructureWrapper<Parent, Struct> >(
|
{
|
||||||
|
shared = shared_ptr<StructureWrapper<Parent, Struct> >(
|
||||||
this, reset_parent);
|
this, reset_parent);
|
||||||
|
weak_this = shared;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return shared;
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<StructureWrapper<Parent, Struct> >
|
shared_ptr<StructureWrapper<Parent, Struct> >
|
||||||
get_shared_pointer(shared_ptr<Parent> parent)
|
get_shared_pointer(shared_ptr<Parent> parent)
|
||||||
{
|
{
|
||||||
if (!parent)
|
if (!parent)
|
||||||
throw Error(SR_ERR_BUG);
|
throw Error(SR_ERR_BUG);
|
||||||
this->parent = parent;
|
this->parent = parent;
|
||||||
return shared_ptr<StructureWrapper<Parent, Struct> >(
|
return shared_from_this();
|
||||||
this, reset_parent);
|
}
|
||||||
|
|
||||||
|
shared_ptr<StructureWrapper<Parent, Struct> >
|
||||||
|
get_shared_pointer(Parent *parent)
|
||||||
|
{
|
||||||
|
if (!parent)
|
||||||
|
throw Error(SR_ERR_BUG);
|
||||||
|
return get_shared_pointer(static_pointer_cast<Parent>(
|
||||||
|
parent->shared_from_this()));
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
static void reset_parent(StructureWrapper<Parent, Struct> *object)
|
static void reset_parent(StructureWrapper<Parent, Struct> *object)
|
||||||
|
|
Loading…
Reference in New Issue