-2

following this question , I am trying to avoid copy-pasting some code related to calling all of the same-named methods of the mixins of the class BaseSensor.

in sensor.hpp

struct EdgeSensor //a mixin
{
    void update(){}
    void printStats() {}
};

struct TrendSensor //another mixin
{
    void update(){}
    void printStats() {}
};

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
    void update() /*{ what goes in here??? }*/
    void printStats() /*{ what goes in here??? }*/
};

in sensor.t.hpp

template<typename ... SensorType>
void BaseSensor<SensorType...>::update()
{
    int arr[] = { (SensorType::update(), 0)..., 0 };
    (void)arr;
}

template<typename ... SensorType>
void BaseSensor<SensorType...>::printStats()
{
    int arr[] = { (SensorType::printStats(), 0)..., 0 };
    (void)arr;
}

in main.cpp

int main(int , const char **) 
{
    {
        BaseSensor<EdgeSensor,TrendSensor> ets;
        ets.update();
        ets.printStats();
    }
    {
        BaseSensor<EdgeSensor> ets;
        ets.update();
        ets.printStats();
    }
}

The above code executes the update() of all the mixins in turn, before going on to execute all the printStats() from all the mixins as well.

I wonder if it is somehow possible to avoid duplicating the implementation of BaseSensor::update() and BaseSensor::printStats() and create a generic (template) function that accepts the name of the target function to execute across all the mixins:

For example, I could create a method runAll()

template<typename ... SensorType>
class BaseSensor : public SensorType ... //to my BaseSensor class
{
    void update() /*{ what goes in here??? }*/
    void printStats() /*{ what goes in here??? }*/

    template<typename FnName>
    void runAll(FnName f)
    {
        int arr[] = { (SensorType::f(), 0)..., 0 };
        (void)arr;
    }
};

How would I call it then from BaseSensor::update() and BaseSensor::printStats(). I have attempted to use

void update() { runAll<update>(); }
void printStats() { runAll<printStats>(); }

but this does not work (did not expect it to). The problem with passing function name as a function argument (which I see is many other questions such as here is that I do not know how to point to various ::update() functions from BaseSensor::update(). for example

void update() { runAll<update>( update() ); }

is also not correct.

Is it possible to avoid copying in this case? Can this be done in a one-liner so as to avoid alot of copying using c++11 (i.e. without using generic lambdas as is done here)? How would the template parameters look like if I where to move a working runAll() into file "sensor.t.hpp" ?

Thank you.

Community
  • 1
  • 1
nass
  • 1,453
  • 1
  • 23
  • 38

2 Answers2

1

You can still write the (simplified version of) generic lambda manually:

void update() {
    execute([](auto &t) { t.update(); });
}

becomes so

void update() {
    struct {
        template <typename T>
        void operator () (T& t) const { t.update(); }
    } updater;
    execute(updater);
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • hi there, thank you for the information . This still seems "long" for copy-pasting it. Ideally, I could get the struct "out" somehow and provide a "variable" name for the `t.update()` function call ? thank you – nass Sep 18 '16 at 16:15
  • This is quite tricky when you are to introduce the _second_ function indeed. – skypjack Sep 18 '16 at 16:26
1

As long as the functions to be called are two, you can use a dedicated structure and rely on overloading to solve it.
It follows a minimal, working example:

#include<iostream>

struct Executor {
    template<typename T>
    static void execute(int, T &t) {
        t.update();
    }

    template<typename T>
    static void execute(char, T &t) {
        t.printStats();
    }
};

struct EdgeSensor
{
    void update() { std::cout << "EdgeSensor::update" << std::endl; }
    void printStats() { std::cout << "EdgeSensor::printStats" << std::endl; }
};

struct TrendSensor
{
    void update() { std::cout << "TrendSensor::update" << std::endl; }
    void printStats() { std::cout << "TrendSensor::printStats" << std::endl; }
};

template<typename ... SensorType>
class BaseSensor : public SensorType ...
{
    template<typename T>
    void execute() {
        int arr[] = { (Executor::execute(T{}, static_cast<SensorType&>(*this)), 0)..., 0 };
        (void)arr;
    }

public:
    void update() {
        execute<int>();
    }

    void printStats() {
        execute<char>();
    }
};

int main() {
    BaseSensor<EdgeSensor,TrendSensor> ets;
    ets.update();
    ets.printStats();
}

In case you have more than two functions to be called, I guess the choice trick applies well here.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • hm still there are parts (in the struct) that need to be replicated for every other similar function. It appears the only Truly generic way are the generic lambdas... – nass Sep 18 '16 at 16:22
  • 2
    I would use some enum/tag for dispatching instead of uninformative `char`/`int` – Jarod42 Sep 18 '16 at 16:23
  • @Jarod42 I would do that too. From my point of view, the `int`/`char` trick is more concise for an example, that's all. – skypjack Sep 18 '16 at 16:24