kfmfe04
kfmfe04

Reputation: 15337

how to refactor this class template to minimize boiler-plate code?

I've recently started using boost mirror for ORM reflection so I can minimize the amount of DB-related boiler-plate code.

One kind of pattern I've run into is listed below.

Since Bar's code is going to look identical even for Baz, I was wondering, is it possible to collapse this code any further?

It would be nice if the getValues() interface would look the same, but the identical implementation could also live in Foo instead.

#include <iostream>

template< typename T >
struct Foo
{
    static std::ostream& GetValues_Worker( std::ostream& os, T const& t )
    {
        // do boost mirror reflection stuff here
        return os;
    }
};

struct Bar : 
    public Foo<Bar>
{
    // REFACTOR IF POSSIBLE:  this code will always look the same - even for Baz
    std::ostream& 
    getValues( std::ostream& os ) const
    {
        return GetValues_Worker( os, *this );
    }
};

struct Baz : 
    public Foo<Baz>
{
    // REFACTOR IF POSSIBLE:  looks the same as Bar
    std::ostream& 
    getValues( std::ostream& os ) const
    {
        return GetValues_Worker( os, *this );
    }
};

int main( int argc, char* argv[] )
{
    Bar b;
    std::cerr << b.getValues( std::cerr ) << std::endl;
}

ANSWER

It turns out that ecatmur's answer below works in most cases. In my specific situation, I adapted his solution to my real code, it worked in 2 out of 4 cases. In the two cases where it failed to work, it was a bit beyond the scope of the Mickey-Mouse example that I gave above. The closest explanation that I could find in SO which explains the compile-time errors I was getting is probably this post. The crux of the problem appeared to be related to what was happening inside my Worker code. In the two failed cases, I was doing output streaming of the subclasses' members based on what I was getting back from runtime reflection results in boost mirror. I think this is turned out to be a case of non-deducible context. I still don't understand why the solution in those two failed cases works exactly (why using a visitor in the form of a virtual method gets around the issue). Anyhow, I stumbled upon that approach and was trying to reduce the code some more (in those 4 cases), but in two of them, I couldn't really reduce that code any more without running into the non-deducible context issue.

Upvotes: 1

Views: 364

Answers (1)

ecatmur
ecatmur

Reputation: 157484

Bar::getValues can be moved to Foo<Bar> using a CRTP downcast:

template< typename T >
struct Foo
{
    static std::ostream& GetValues_Worker( std::ostream& os, T const& t )
    {
        // do boost mirror reflection stuff here
        return os;
    }

    std::ostream& getValues( std::ostream& os ) const
    {
        return GetValues_Worker( os, *static_cast<T const *>(this) );
    }
};

At this point you might as well combine the two methods:

template< typename T >
struct Foo
{
    std::ostream& getValues( std::ostream& os ) const
    {
        T const &t = *static_cast<T const *>(this);
        // do boost mirror reflection stuff here
        return os;
    }
};

Upvotes: 4

Related Questions