2013/6/13 Rutger ter Borg <rut...@terborg.net>

> On 2013-06-13 11:18, Dmitriy Igrishin wrote:
> >
> >     Who owns the contents? WStackedWidget, or WMenuItem, or?
> >
> >   WMenuItem.
> >
> >     Who owns the menu items? WMenu, or?
> >
> > WMenu.
> >
> >     Why doesn't WMenu have a setContentsStack?
> >
> >     Why doesn't WMenuItem have a setContents?
> >
> > I believe because it does not worth it.
> > Can you please explain why do we need these stuff?
> > What real example usage?
> >
> >
>
> Thanks for the reply. I'm creating classes the larger part of the widget
> tree as values, to minimize allocations. I'm assuming the ownership /
> memory model of Wt is the same as that of Qt.
>
> E.g., if you write
>
> WWidget parent_;
> WWidget child_( &parent );
>
> everything will be ok, because destruction and deregistration is done in
> reversed order.
>

> With WMenuItem and friends, stuff becomes a bit more confusing, because
> you need to pass its child widget, and, more importantly, its child
> widget needs to be heap allocated.
>
While it's possible, it seems to me, that this coding technique is just
asking for troubles. Wt has a pointer semantics and heavily oriented to
the abstract classes design. There are a lot of allocations under the hood
of Wt, so I don't believe in a benefit of stack-allocated objects (except of
WDialog and similar instances in case of blocking the thread awaitng for the
user response).


>
> E.g., if you write
>
> WStackedWidget stack_;
> WMenu menu_( &stack_, &some_parent );
> SomeContent contents_;
> WMenuItem some_item_( "my label", &contents_ );
>
> you will be in trouble, because contents_ will be deleted twice. If we
> were able to write
>

> WStackedWidget stack_;
> WMenu menu_( &stack_, &some_parent );
> WMenuItem some_item_( "my label" );
> SomeContent contents_;
> some_item_.setContents( contents_ );
>
> and assuming the same ownership model is in place, this wouldn't fail.
>
> Having said all this, I still don't understand the condition of the
> destruction in WMenuItem. Why doesn't this read
>
> ~WMenuItem() {
>     if ( contents_ ) {
>       delete contents_;
>     }
> }
>
Yes, because WMenuItem is owns contents. WMenuItem needs to be
an owner of contents while the content's ownership may be transferred to
the contents stack after the actual loading.
On the other hand, the content that was not reowned must be deleted
by the WMenuItem.

-- 
// Dmitriy.
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
witty-interest mailing list
witty-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to