PDA

View Full Version : when will be QSharedFromThis for QSharedPointer?



fsmoke
18th November 2012, 18:25
When this feature https://bugreports.qt-project.org/browse/QTBUG-7287 will be released in Qt?

amleto
18th November 2012, 21:01
How should we know?

ChrisW67
18th November 2012, 23:03
When this feature https://bugreports.qt-project.org/browse/QTBUG-7287 will be released in Qt?

If someone finishes the patch, including the documentation, gets it past the basic code review process and argues the case for having it in the first place then it may get into a future Qt. Since nobody has touched the bug since 2010 I don't think anyone's need to scratch this itch is strong enough.

fsmoke
19th November 2012, 06:06
If someone finishes the patch, including the documentation, gets it past the basic code review process and argues the case for having it in the first place then it may get into a future Qt. Since nobody has touched the bug since 2010 I don't think anyone's need to scratch this itch is strong enough.

But without this functional - Qt shared pointer mechanizm is no full - it's not useful!! I port small tree class from boost to Qt, some like this

class TreeNode
{
public:
void addChild(QSharedPointer<TreeNode> p);
private:
QWeakPointer _parent;
QVector<QSharedPointer<TreeNode>> _childs;
};

inside method addChild(boost realization) new child TreeNode field _parent was assigned to shared_from_this - in Qt realization i can't do this.
Besides TreeNode is parent class to other types and they can add child nodes themself. So I need to rewrite common architecture of my application - I don't want do this.

wysota
19th November 2012, 06:19
in Qt realization i can't do this.
So implement something with the same functionality. However I don't see why you can't get the same behaviour with just QSharedPointer and QWeakPointer. I don't see a need for any other pointers here.

Isn't it enough to just keep a weak pointer to itself in the object and promote it to strong ref if needed?

I have an alternative approach in mind if the weak pointer approach doesn't work.

fsmoke
19th November 2012, 06:49
So implement something with the same functionality. However I don't see why you can't get the same behaviour with just QSharedPointer and QWeakPointer. I don't see a need for any other pointers here.

Isn't it enough to just keep a weak pointer to itself in the object and promote it to strong ref if needed?

oh my god 8433

Simple realization of addChild


TreeNode::addChild(boost::shared_ptr<TreeNode> p)
{
if (!p._parent.expired())
throw std::runtime_error("TreeNode can't have multiple parents!");
p._parent = shared_from_this(). //!!!!!!!!!
_childs.push_back(p);
}

ChrisW67
19th November 2012, 10:26
But without this functional - Qt shared pointer mechanizm is no full - it's not useful!!
That's a bit like saying the C++ standard library is not useful because it does not have a "shared_from_this" mechanism either. Clearly that's why Boost implemented the functionality. You can use the Boost shared_ptr/weak and shared_from_this with a program that also uses Qt, so I'm not sure where the real problem is here.

fsmoke
19th November 2012, 11:57
That's a bit like saying the C++ standard library is not useful because it does not have a "shared_from_this" mechanism either.

Fun :)) C++ standard library have "shared_from_this" mechanism
http://en.cppreference.com/w/cpp/memory/enable_shared_from_this



Clearly that's why Boost implemented the functionality. You can use the Boost shared_ptr/weak and shared_from_this with a program that also uses Qt, so I'm not sure where the real problem is here.

Yes it was written in this way(boost + Qt) but I don't like code style mixing, so I rewrote it in clear Qt, because Qt no uses default c/c++ code style like in stl or
boost. It's difficult to support projects with multiple codestyles - it's very bad practice.

ChrisW67
19th November 2012, 21:31
Fun :)) C++ standard library have "shared_from_this" mechanism

... since C++11, and still not universally available. So ultimately you don't even need a third-party extension to C++ if you have a suitably equipped tool chain. Prior to that of course C++ was useless and nobody ever wrote anything of value in C++ ;)

wysota
20th November 2012, 00:43
oh my god 8433

Simple realization of addChild


TreeNode::addChild(boost::shared_ptr<TreeNode> p)
{
if (!p._parent.expired())
throw std::runtime_error("TreeNode can't have multiple parents!");
p._parent = shared_from_this(). //!!!!!!!!!
_childs.push_back(p);
}

If I want such constructions then I usually implement "explicit sharing" like approach. I have a public object that acts "like a pointer" and private object that holds the data. It is the private object that is shared and copying the public object only increases reference count of the private component. Then I don't need pointers to the public object because I can always copy it and destroying the last instance of the public object also destroys the private component.


class Object {
public:
Object() { d = new ObjectPrivate; }
Object(const Object &other) : d(other.d){ }
int x() const { return d->x; }
void setX(int x) { d->x = x; }
Object childAt(int i) const { return d->children.at(i); }
void addChild(Object o) { d->children.append(o); o.d->parent = *this; }
Object& operator=(const Object &other) { d = other.d; return *this;}
private:
QSharedPointer<ObjectPrivate> d;
};

class ObjectPrivate {
public:
int x;
Object parent;
QList<Object> children;
};

Building a tree from such object is trivial then. The construction is more "OOP-ish" than using pointers with the exception that creating a copy of the object doesn't actually make a copy of the data (which is ok in this case).

fsmoke
20th November 2012, 08:38
class Object {
public:
Object() { d = new ObjectPrivate; }
Object(const Object &other) : d(other.d){ }
int x() const { return d->x; }
void setX(int x) { d->x = x; }
Object childAt(int i) const { return d->children.at(i); }
void addChild(Object o) { d->children.append(o); o.d->parent = *this; }
Object& operator=(const Object &other) { d = other.d; return *this;}
private:
QSharedPointer<ObjectPrivate> d;
};

class ObjectPrivate {
public:
int x;
Object parent;
QList<Object> children;
};


It's very interesting realization - it's pimpl :)

But I see BUG in this code :)) - it seems recursive parent destruction - you must use QWeakPointer for parent.

wysota
20th November 2012, 22:18
It's very interesting realization - it's pimpl :)
Yes, it's a kind of pimpl (not completely because there is no "implementation" in the private component, just data). I call this construction a "pointer without pointers".


But I see BUG in this code :)) - it seems recursive parent destruction - you must use QWeakPointer for parent.
Could be, I didn't test the code before posting.