简体   繁体   中英

Create multiple function pointers for callbacks with different parameters in C++

I have a third party class Calculation with a function setCallback :

typedef void (*callback_function)(void);

class Calculation
{
public:
    void setCallback(callback_function pFunc);
};

and my function I want to use as callback

void callback(int id);

I want to create, say, four (number known at compile time) objects of Calculation and set the callback for each object. I could do something like

void callback0() { callback(0); }
void callback1() { callback(1); }
void callback2() { callback(2); }
void callback3() { callback(3); }

int main() {
  std::vector<Calculation> vec;

  for (int i = 0; i < 4; i++) {
    Calculation c = Calculation();
    vec.push_back(c);
  }

  vec[0].setCallback(callback0);
  vec[1].setCallback(callback1);
  vec[2].setCallback(callback2);
  vec[3].setCallback(callback3);

  return 0;
}

Question: How can I achieve this without duplicating and repeating code?

I was thinking of lamdas, like

  for (int i = 0; i < 4; i++) {
    Calculation c = Calculation();
    c.setCallback([i]() -> void {callback(i);});
    vec.push_back(c);
  }

but a lambda can only be converted to a function pointer if it does not capture .

Template function might help:

template <int N>
void callbackN() { callback(N); }

and then

vec[0].setCallback(callback<0>);
vec[1].setCallback(callback<1>);
vec[2].setCallback(callback<2>);
vec[3].setCallback(callback<3>);

Since the number of Calculation instances is known at compile-time, you set up something like this to automate things, but not sure if it's worth the effort:

template <size_t Size, typename Idx = std::make_index_sequence<Size>>
class CalcInterface {
    std::array<callback_function, Size> m_callbacks { makeCallbacks(Idx{}) };
    std::array<Calculation, Size> m_calcs;

protected:
    template <size_t N> 
    static inline callback_function makeCallback() {
        return []{ return callback(N); };
    }
    template <size_t... Seq>
    static inline auto makeCallbacks(std::index_sequence<Seq...>) {
        std::array<callback_function, Size> arr;
        ((arr[Seq] = makeCallback<Seq>()), ...);
        return arr;
    }

    static void callback(int id) { 
        std::cout << "Callback #" << id << " was called\n";
    }

public:
    CalcInterface() {
        for (size_t i=0; i<Size; ++i) {
            m_calcs[i].setCallback(m_callbacks[i]);
        }
    }

    void runAll() const {
        for (auto& calc: m_calcs) calc.run();
    }
};

Example here: https://godbolt.org/z/sWTzn59ce

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM