admin管理员组文章数量:1336321
I'm trying to just use the code from this article: ++-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?
I'm trying to just use the code from this article: http://blog.coldflake/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?
Share Improve this question asked Nov 20, 2024 at 6:32 fewaffewaf 1512 silver badges14 bronze badges 2 |2 Answers
Reset to default 1You 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./z/rzohE4Knj
If you need deferred calls with arguments, you may take a look on my library which provides the functionality that you need https://github/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
本文标签:
版权声明:本文标题:I'm trying to implement a delegate in c++, but I don't understand how to actually pass the delegate into a funct 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742376822a2463326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
std::function
instead of yourDelegate
. – wohlstad Commented Nov 20, 2024 at 7:11Delegate d
as an argument toprintString
you can make the parameterDelegate const & d
. But if you want to passd(42)
- it's the invokation ofd
, and it returnsvoid
so there's no point passing it anywhere. – wohlstad Commented Nov 20, 2024 at 7:19