Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't easily describe this question so forgive me.

For context it has to do with GFX: GFX Forever: The Complete Guide to GFX for IoT[^]

I'm trying to reduce the code size for drawing operations.
Currently every *type* of draw target requires the generation of all new drawing code, so if you draw a line to a bitmap and a line to an lcd, there are two copies of much of the line drawing code. The same goes for drawing to large_bitmap objects instead of bitmap objects.

I'd like to be able to share some of this code, but what I need is a virtual base class that itself is probably a template (i know this creates issues but I may have to deal with it)

Essentially, if my draw targets inherit from a particular interface I want it to select the template instantiation for that particular virtual *base class* not the derived one.

For example:

C++
class Interface {
    virtual void foo() const = 0;
};

class Bar : public Interface {
    virtual void foo() const { std::cout << "Bar" << std::endl; }
};
class Baz : public Interface {
    virtual void foo() const { std::cout << "Baz" << std::endl; }
};

template<typename Target> static void do_foo(const Target* target) {
      target->foo();
}


and here

C++
int main() {
    Bar bar;
    Baz baz;
    do_foo(&bar); // prints "Bar"
    do_foo(&baz); // prints "Baz"
    do_foo<Interface>(&bar); // prints "Bar"
    do_foo<Interface>(&baz); // prints "Baz"
}


Here a total of 3 copies of do_foo<>() are instantiated, if I'm correct. There will be one for do_foo<Foo>(...), one for do_foo<Bar>(...) and one for do_foo<Interface>(...)

Here the only one that is reused is the instantiation using Interface, but I'd like *all* of them to use Interface, without explicitly specifying it.

I'm trying to think of way to do that, by hook or crook. I don't care if the code to make it work looks nasty as long as using it doesn't, if that makes sense.

What I have tried:

I haven't tried anything yet.

My first thought is to try to make non-template versions of the template methods to overload them with, but unless I miss my guess the compiler will prefer the most specific overload, which would be the templated one, and that's not what I want.

My other thought is to make non-template versions that take a pointer, while the template versions take a reference. It's kind of ... I don't like it for a number of reasons.

The other option is, I have a draw class that sources all of the template functions i want to recycle like this. I could make a draw_shared:: class that uses the virtual class. It's less than ideal as well.

So this is call for ideas.
Posted
Updated 18-Jun-21 16:50pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900