[PATCH] Qt: Implement trip merging logic

Thiago Macieira thiago at macieira.org
Thu Jun 27 08:40:49 PDT 2013


On quarta-feira, 26 de junho de 2013 19.59.28, Linus Torvalds wrote:
> On Wed, Jun 26, 2013 at 7:54 PM, Thiago Macieira <thiago at macieira.org> 
wrote:
> > If you make it a member function, it needs to be mentioned in the header.
> 
> I was afraid of that. Annoying.
> 
> I realized I could just make it a "normal" function like your
> suggested approach, but then you don't get the private namespace and
> you have to mention "this" explicitly. So I was hoping there was some
> "best of both worlds" model that would allow for local helper
> functions like this.

There is, but it starts to get ugly. So this depends on how ugly you want to 
go.

=== Option 1 ===

In Qt's own code, we have a policy of having no private functions at all, and 
very, very few private members. Instead, there's a full private class which is 
a friend. So you'd have:

class DiveListViewPrivate;
class DiveListView
{
    [...]
private:
    friend class DiveListViewPrivate;
    DiveListViewPrivate *d;
};

This technique is known as "d pointer" or "private implementation" (pimpl, see 
http://en.wikipedia.org/wiki/Pimpl ), and it's the technique whereby Qt 
maintains binary compatibility for years on end.

You'd then have:

void DiveListViewPrivate::merge_trip(const QModelIndex &a, int offset)
{
    [...]
    // the "q" pointer points from the private to the public
    q->reload(currentLayout, false);
}

void DiveListView::mergeTripAbove()
{
    d->mergeTrip(d->contextMenuIndex, -1)
}

=== Option 2 ===

If you want the actual namespace of the DiveListView class and you're willing 
to accept a little more ugliness, you can do this with few actual changes:

class DiveListViewPrivate;
class DiveListView
{
    [... keep everything as is, just add ...]
    friend class DiveListViewPrivate;
    // no member
};

In the .cpp:
void DiveListViewPrivate::merge_trip(const QModelIndex &a, int offset)
{
    [... code as you wrote ...]
}

void DiveListView::mergeTripAbove()
{
    static_cast<DiveListViewPrivate *>(this)->mergeTrip(contextMenuIndex, -1);
    // or with C-style casts:
    ((DiveListViewPrivate *)this)->mergeTrip(contextMenuIndex, -1);
}

Alternatively, instead of friending in the public class, you could change 
private to protected.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20130627/da5f246e/attachment.sig>


More information about the subsurface mailing list