如何在C++中模拟模板化的 std::函数

How emulate a templatised std::function in C++

本文关键字:std 函数 C++ 模拟      更新时间:2023-10-16

以下是我在C++程序中所做的事情的基本实例。我有一个听众列表,这些听众都是std::function的。我有一个概念DataType这意味着听众对哪种数据感兴趣。这里的想法与发布-订阅模式相同。对某些类型的数据感兴趣的方法应该能够使用AddListener将自己添加到侦听器列表中。添加了一些方法,并在需要时接收回调。

该程序工作正常!

#include <iostream>
#include <functional>
#include <vector>
#include <string>
enum class DataType {
Type_1,
Type_2
// and so on
};
typedef std::function<void(std::pair<DataType, std::string>)>   MyListenerType;
//template <typename T>
//typedef std::function<void(T>)>   MyListenerType;
// How can I emulate the above so that a method passing any kind of primitive data-type namely "int, bool, float or double" can be added into
// my vector of listners.

std::vector<MyListenerType>      my_data_listeners_1;
std::vector<MyListenerType>      my_data_listeners_2;
void ListenerMethod_Instance_1(std::pair<DataType, std::string> information) {
DataType data_type = information.first;
std::string message = information.second;
std::cout << "ListenerMethod_Instance_1 called with message " << message << "n";
}
void ListenerMethod_Instance_2(std::pair<DataType, std::string> information) {
DataType data_type = information.first;
std::string message = information.second;
std::cout << "ListenerMethod_Instance_2 called with message " << message << "n";
}
void AddListener (MyListenerType listener, DataType type_of_interest) {
if (DataType::Type_1 == type_of_interest) {
my_data_listeners_1.push_back(listener);
std::cout << "Added a method instance for DataType::Type_1" << "n";
}
else if (DataType::Type_2 == type_of_interest) {
my_data_listeners_2.push_back(listener);
std::cout << "Added a method instance for DataType::Type_2" << "n";
}
else {
std::cout << "Listener type not supported" << "n";
}
}
void CallAllListnersWhohaveSuscribed() {
if (!my_data_listeners_1.empty()) {
std::string send_message_1 = "some message 123";
std::pair <DataType, std::string> info_to_send_1 = std::make_pair (DataType::Type_1, send_message_1);
for(auto const  &listener : my_data_listeners_1) {
listener(info_to_send_1);
}
}
if (!my_data_listeners_2.empty()) {
std::string send_message_2 = "some message 456";
std::pair <DataType, std::string> info_to_send_2 = std::make_pair (DataType::Type_2, send_message_2);
for(auto const  &listener : my_data_listeners_2) {
listener(info_to_send_2);
}
}
}
int main() {
// Add ListenerMethod_Instance_1 for instance
DataType data_type_1 = DataType::Type_1;
auto listener_instance_1 = std::bind(ListenerMethod_Instance_1, std::placeholders::_1);
AddListener(listener_instance_1, data_type_1);
// Add ListenerMethod_Instance_2 for instance
DataType data_type_2 = DataType::Type_2;
auto listener_instance_2 = std::bind(ListenerMethod_Instance_2, std::placeholders::_1);
AddListener(listener_instance_2, data_type_2);
CallAllListnersWhohaveSuscribed();
return 0;
}

以下是程序的输出:

./stdFunctionTest
Added a method instance for DataType::Type_1
Added a method instance for DataType::Type_2
ListenerMethod_Instance_1 called with message some message 123
ListenerMethod_Instance_2 called with message some message 456

但这就是我想要修改和挣扎的方式需要注意的是,每个ListenerMethod_Instance_1ListenerMethod_Instance_2都必须解析这对,以获取我不想获取的信息。我想启用任何C++原始数据类型的方法,无论是"int、bool、float 还是 double",以便能够添加到侦听器向量中并接收回调。例如,以下方法应"可添加"到AddListener中。

void ListenerMethod_Instance_3(int integer_data) {
std::cout << "ListenerMethod_Instance_3 called with integer_data " << integer_data << "n";
}

在这里查看此链接看起来在某种程度上是可能的。但我正在努力使其适应我的用例。请指教。

那么,基本上如何使用std::function实现模板功能?

struct anything_view_t {
void* ptr=0;
template<class T, std::enable_if_t<!std::is_same<anything_view_t, std::decay_t<T>>{}, int> =0>
anything_view_t(T&&t):ptr(std::addressof(t)){}
anything_view_t()=default;
anything_view_t(anything_view_t const&)=default;
anything_view_t& operator=(anything_view_t const&)=default;
template<class T>
operator T() const { return *static_cast<T*>(ptr); }
};

这是一种非常不安全的类型擦除视图。

struct any_callbacks {
std::unordered_map<std::type_index, std::vector<std::function<void(anything_view_t)>>> table;
template<class T>
void add_callback( std::function<void(T)> f ){
table[typeid(T)].push_back(f);
}
template<class T>
void invoke_callbacks(T t) const {
auto it = table.find(typeid(T));
if (it==table.end()) return;
for(auto&&f:it->second)
f(t);
}
};

像上面这样的东西应该有效。 类型T必须完全匹配。 不支持引用。 代码没有编译,设计合理,可能有错别字。

这不会重构为基元类型。 你应该明确地通过T,不要依赖演绎,因为它是脆弱的。