simon-git: spigot (master): Simon Tatham

Commits to Tartarus hosted VCS tartarus-commits at lists.tartarus.org
Tue Mar 6 21:13:19 GMT 2018


TL;DR:
  4221f32 Lexical cleanups in bi_gmp.h.
  56128fc Generate formatter copy methods using a CRTP trick.

Repository:     https://git.tartarus.org/simon/spigot.git
On the web:     https://git.tartarus.org/?p=simon/spigot.git
Branch updated: master
Committer:      Simon Tatham <anakin at pobox.com>
Date:           2018-03-06 21:13:19

commit 4221f323bb26d646a802d5f73d11c2337f352121
web diff https://git.tartarus.org/?p=simon/spigot.git;a=commitdiff;h=4221f323bb26d646a802d5f73d11c2337f352121;hp=fe8a7a4637ffddc7835cdf03647c6732432cb09c
Author: Simon Tatham <anakin at pobox.com>
Date:   Tue Mar 6 18:39:00 2018 +0000

    Lexical cleanups in bi_gmp.h.
    
    When I moved all the method bodies out of the class definition, I
    accidentally left a lot of 'inline' keywords on the declarations which
    didn't need to be there. Also, I got GNU indent to help me reformat
    the relocated functions and didn't quite manage to clean up all the
    things it mis-spaced by (I think) mistaking C++ for C. Both fixed in
    this more or less cosmetic commit.

 bi_gmp.h | 101 ++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 51 insertions(+), 50 deletions(-)

commit 56128fc27e5b0d58982f23d1717f186b22423a1b
web diff https://git.tartarus.org/?p=simon/spigot.git;a=commitdiff;h=56128fc27e5b0d58982f23d1717f186b22423a1b;hp=4221f323bb26d646a802d5f73d11c2337f352121
Author: Simon Tatham <anakin at pobox.com>
Date:   Tue Mar 6 19:25:32 2018 +0000

    Generate formatter copy methods using a CRTP trick.
    
    Having got rid of one family of class-cloning methods in commit
    8476b5bea, I've just thought of a neat way to get rid of another.
    
    The descendants of Formatter in baseout.cpp have copy() methods which
    are similar enough to be auto-generated in principle, but you need to
    arrange for each class's implementation to cite that class's own type
    as the thing to allocate a new one of.
    
    Well, there is a way, using the 'Curiously Recurring Template Pattern'
    in which a template class is given a derived class of _itself_ as a
    parameter. The idea is that we make a templated class FormatterImpl<T>
    which derives from Formatter, and fills in the virtual copy() method
    using T as the type to construct. But what's T? To make this work, it
    has to be both the template parameter of FormatterImpl, _and_ its
    derived class. Fortunately, that's legal - you can write a declaration
    like 'class Foo : public Bar<Foo>' (where Bar is some template class)
    and the name 'Foo' comes into scope just quickly enough to make that
    work.
    
    So where each FooFormatter implementation class used to inherit
    directly from the abstract base class Formatter, now each inherits
    from its own instantiation of the new intermediate class, e.g.
    FormatterImpl<FooFormatter>, and _that_ inherits from Formatter in
    turn. Then the intermediate class fills in the copy() method - using
    its template parameter to know what type it's supposed to be making a
    new one of - while the ordinary derived class FooFormatter fills in
    the rest. It's slightly eye-twisting, but it does eliminate those
    boilerplate methods!
    
    Sadly, the remaining family of clone methods, in the main Spigot
    classes themselves, can't be removed so easily. Those ones really do
    need dedicated code to be written for each class - you need to know
    which set of the class's members to pass to the constructor for the
    clone, and whether any of them need cloning or copying in turn. There
    would be ways to move that work around based on this technique, but
    there's no way I can see to eliminate it completely.

 baseout.cpp | 50 ++++++++++++++++++--------------------------------
 1 file changed, 18 insertions(+), 32 deletions(-)



More information about the tartarus-commits mailing list