fewaf
fewaf

Reputation: 151

I'm trying to implement a delegate in c++, but I don't understand how to actually pass the delegate into a function

I'm trying to just use the code from this article: http://blog.coldflake.com/posts/C++-delegates-on-steroids/

So the delegate is this:

class Delegate
{
    typedef void (*Type)(void* callee, int);
public:
    Delegate(void* callee, Type function)
        : fpCallee(callee)
        , fpCallbackFunction(function) {}
    template <class T, void (T::*TMethod)(int)>
    static Delegate from_function(T* callee)
    {
        Delegate d(callee, &methodCaller<T, TMethod>);
        return d;
    }
    void operator()(int x) const
    {
        return (*fpCallbackFunction)(fpCallee, x);
    }
private:
    void* fpCallee;
    Type fpCallbackFunction;
    template <class T, void (T::*TMethod)(int)>
    static void methodCaller(void* callee, int x)
    {
        T* p = static_cast<T*>(callee);
        return (p->*TMethod)(x);
    }
};

And a class with the function I want to use is this:

class A
{
public:
    void foo(int x)
    {
        printf("foo called with x=%d\n", x);
    }
    void bar(int x) {}
};

And he shows how to use it here:

int main()
{
    A a;
    Delegate d = Delegate::from_function<A, &A::foo>(&a);
    d(42);
}

This works nicely, but the idea is to be able to actually pass this delegate into a function without any dependencies.

So if I have a new function that takes a string and prints it...

void printString(const std::string& str) {
    std::cout << str << std::endl;
}

How do I modify the parameters passed into this printString function so that it can take this delegate? What is the parameter type for the new delegate "d" that I made?

Something like...

void printString(const std::string& str, auto d) {
        std::cout << str << std::endl;
        // and then I can do something with d here
    }

So that I can then make main() this:

int main()
    {
        A a;
        Delegate d = Delegate::from_function<A, &A::foo>(&a);
        d(42);
        printString("foo", d(42));    <- note I don't know if I include the (42) here
    }

So the question is, what do I put as the parameters in printString so that I can pass d into it?

Upvotes: 0

Views: 68

Answers (2)

alexb
alexb

Reputation: 322

If you need deferred calls with arguments, you may take a look on my library which provides the functionality that you need https://github.com/alexbsys/cpp-delegates.git It is small and simple, only several hpp files, you can just take some code from it. The aim that caller should not know anything about arguments or return types, but can still perform the call. Example:

#include <delegates/delegates.hpp>
#include <memory>
#include <iostream>
#include <string>

using namespace std;
using namespace delegates;

class Printer {
public:
  void PrintInt(int val) { cout << val << endl; }
  void PrintString(string s) { cout << s << endl; }
  void PrintIntConst(int val) const { cout << "const " << val << endl; }
};

void main() {
  auto printer = make_shared<Printer>();
  list<shared_ptr<IDelegate> > calls;

  // parameters initial values can be set immediately when delegate created
  calls.push_back(factory::make_shared(printer, &Printer::PrintInt, 42));
  calls.push_back(factory::make_shared(printer, &Printer::PrintString, string("Hello")));
  calls.push_back(factory::make_shared(printer, &Printer::PrintIntConst, 1234));

  // ...or set up later from other place
  auto delegate1 = factory::make_shared(printer, &Printer::PrintInt);
  delegate1->args()->set<int>(0, 1234); // set parameter #0 to 1234
  calls.push_back(delegate1);
  
  auto delegate2 = factory::make_shared(printer, &Printer::PrintString);
  delegate2->args()->set<string>(0, "TEST"); // set parameter #0 to string value
  calls.push_back(delegate2);

  // lambda: result type 'void', args: 'int', 'const std::string&'
  auto delegate3 = factory::make_shared<void, int, const std::string&>([](int a, const std::string& s) { 
    std::cout << "delegate called, a=" << a << ", s=" << s << std::endl; 
  });

  delegate3->args()->set<int>(0, 5432);
  delegate3->args()->set<std::string>(1, "TestLambda");

  calls.push_back(delegate3);

  // call without know anything about parameters
  for (auto& d : calls) {
    d->call();
  }
}

Result:

42
Hello
const 1234
1234
TEST
delegate called, a=5432, s=TestLambda

Upvotes: 0

Alan Birtles
Alan Birtles

Reputation: 36488

You can't easily pass the Delegate with an included parameter to another function.

Note that with c++11 lambdas or std::bind/std::function the Delegate class isn't necessary:

int main()
    {
        A a;
        printString("foo", std::bind(&A::foo, a, 42));
        printString("foo", std::bind_front(&A::foo, a, 42));
        printString("foo", [&]{ a.foo(42); });
    }

If you do still want to use the Delegate class then these techniques still apply:

int main()
    {
        A a;
        Delegate d = Delegate::from_function<A, &A::foo>(&a);
        auto delegateOp = &Delegate::operator();
        printString("foo", std::bind(delegateOp, d, 42));
        printString("foo", std::bind_front(delegateOp, d, 42));
        printString("foo", [&]{ d(42); });
    }

https://godbolt.org/z/rzohE4Knj

Upvotes: 1

Related Questions