gpt4 book ai didi

c++ - 将 std::function 绑定(bind)到不同对象实例的相同函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:33 25 4
gpt4 key购买 nike

是否可以重新绑定(bind) std::function 以指向相同的函数但具有不同的对象实例?

如果我有一个对象,它有一个绑定(bind)到另一个函数的 std::function,但是如果那个对象被复制到另一个实例,我想将 std::function 重新绑定(bind)到那个新实例而不是旧实例。

#include "stdafx.h"
#include <iostream>
#include <functional>

class EventHandler
{
public:
int Num;
std::function<int()> OnEvent;

EventHandler (int inNum)
{
Num = inNum;
}

EventHandler (const EventHandler& other)
{
Num = other.Num;
OnEvent = other.OnEvent; //TODO: Need some way to redirect the std::function to the new instance rather than having the delegate point to the original object's handler.
}

int HandleEvent ()
{
return Num;
}
};

int main()
{
EventHandler a(4);
a.OnEvent = std::bind(&EventHandler::HandleEvent, a);
EventHandler b(a);
b.Num = 5;
//Uncommenting the line below is a manual way of redirecting event handler to the new instance.
//b.OnEvent = std::bind(&EventHandler::HandleEvent, b);

int aResult = a.OnEvent();
int bResult = b.OnEvent();

//This will print out 4 and 4 instead of 4 and 5 since b is still bound to a's event handler.
std::cout << "aResult=" << aResult << " bResult=" << bResult << '\n';

return 0;
}

我愿意使用 std::function 的包装器来存储附加信息。

最佳答案

下面的代码引入了一个binding_function<R(Args...)> ,它被称为 function<R()> , 并且参数可以在构造后随时重新绑定(bind)(假设它不是 nullptr )。

#include <functional>
#include <tuple>
#include <utility>
#include <memory>
#include <iostream>

template <typename T>
class binding_function;

template <typename R, typename... Args>
class binding_function<R(Args...)> : std::function<R()>
{
using base_function = std::function<R(Args...)>;
using binded_function = std::function<R()>;
base_function base;

public:
binding_function() = default;

template <typename BaseF, typename... TArgs>
binding_function(BaseF&& f, TArgs&&... args)
: base(std::forward<BaseF>(f)) {
rebind(std::forward<TArgs>(args)...);
}

template <typename... TArgs>
void rebind(TArgs&&... args)
{
static_cast<binded_function&>(*this) =
std::bind(base, std::forward<TArgs>(args)...);
}

using binded_function::operator();
};

class EventHandler
{
public:
// change type of OnEvent to binding_function
binding_function<int(EventHandler)> OnEvent;

// others remain the same
};

int main()
{
EventHandler a(4);

// first binding
a.OnEvent = {&EventHandler::HandleEvent, a};
EventHandler b(a);
b.Num = 5;
b.OnEvent.rebind(b); // rebinding

int aResult = a.OnEvent();
int bResult = b.OnEvent();

//This will print out 4 and 4 instead of 4 and 5 since b is still bound to a's event handler.
std::cout << "aResult=" << aResult << " bResult=" << bResult << '\n';

return 0;
}

关于c++ - 将 std::function 绑定(bind)到不同对象实例的相同函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37693605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com