c++ - 错误:无效使用非静态成员函数'int test::hotplug_callback(libusb_context*, libusb_device*, libusb_hotplug_event, void*)'
问题描述
我改编了 libusb hotplug 示例
* libusb example program for hotplug API
* Copyright © 2012-2013 Nathan Hjelm <hjelmn@mac.com>
(下面的代码)作为测试并将其放在一个类中,因为我的真实程序有同样的问题。我知道如果我将 static 放在它起作用的两个回调前面,但我不希望它们是静态的。我想从回调中访问我的实例变量。这个想法是用户插入一个USB设备,我们称之为usbXYZ。从回调中,我实例化类 usbXYZ 并放入 std::map - 用户将其删除,然后我将其从地图中删除。包含带有对象的 std::map 的类具有写入设备的“更高级别”方法。
请问如何使回调函数工作非静态?如果可能,请稍微解释一下,因为我不明白。谢谢。
#include <stdlib.h>
#include <stdio.h>
#include <thread>
#include "libusb-1.0/libusb.h"
class test{
public:
test() {
okGo();
}
private:
int done = 0;
libusb_device_handle *handle = NULL;
int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int rc;
(void)ctx;
(void)dev;
(void)event;
(void)user_data;
rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error getting device descriptor\n");
}
printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
if (handle) {
libusb_close (handle);
handle = NULL;
}
rc = libusb_open (dev, &handle);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error opening device\n");
}
done++;
return 0;
}
int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
(void)ctx;
(void)dev;
(void)event;
(void)user_data;
printf ("Device detached\n");
if (handle) {
libusb_close (handle);
handle = NULL;
}
done++;
return 0;
}
int okGo(){
libusb_hotplug_callback_handle hp[2];
int product_id, vendor_id, class_id;
int rc;
vendor_id = LIBUSB_HOTPLUG_MATCH_ANY;
product_id = LIBUSB_HOTPLUG_MATCH_ANY;
class_id = LIBUSB_HOTPLUG_MATCH_ANY;
rc = libusb_init (NULL);
if (rc < 0)
{
printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
return EXIT_FAILURE;
}
if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
printf ("Hotplug capabilites are not supported on this platform\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id, class_id, hotplug_callback, NULL, &hp[0]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 0\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error registering callback 1\n");
libusb_exit (NULL);
return EXIT_FAILURE;
}
while (done < 20) {
//rc = libusb_handle_events (NULL);
if (libusb_handle_events_completed(nullptr, nullptr) != LIBUSB_SUCCESS)
printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
}
if (handle) {
libusb_close (handle);
}
libusb_exit (NULL);
return EXIT_SUCCESS;
}
};
int main(int argc, char *argv[])
{
std::unique_ptr<test> testClass1 = std::make_unique<test>();
//just test
for (;;)
{
//main service loop
std::this_thread::sleep_for(std::chrono::microseconds(2000000));
}
}
解决方案
libusb 是一个 C 库,它只能接受非成员函数指针。非成员函数(或static
成员函数)指针和指向非静态成员函数的指针的区别在于,非静态成员函数需要一个对象才能被调用,而C对C++没有概念”对象”。
使用 libusb,您可以使用static
成员函数和用户数据指针(您现在传递一个空指针)来解决它。
例如
class test
{
// ...
static int LIBUSB_CALL static_hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
return reinterpret_cast<test*>(user_data)->hotplug_callback_detach(ctx, dev, event);
}
int hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event)
{
// Current code
}
// ...
int okGo()
{
// ...
rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_ENUMERATE, vendor_id,
product_id,class_id, static_hotplug_callback_detach, this, &hp[1]);
// ...
}
请注意,static
注册回调时使用了成员函数,它this
作为用户数据指针传递。
推荐阅读
- apache-kafka - 是否可以为 SingleStore Kafka 管道配置批量大小?
- python-sphinx - 无法通过 Confluence 服务器对 Sphinx 文档进行身份验证
- python - 在 python 字典中获取正确的列表值
- linux - 起搏器集群中的侦听器预言机
- java - 如何使用 Spring Data MongoDB Java API 编写“generatedDate: new Date()”?
- reactjs - 如何在下面附加验证错误消息在反应?
- java - 正则表达式来识别特定字符
- tuples - 如何在 CPLEX 上编辑关于元组的代码?
- python - 消失的确认信息pyqt5
- c++ - 从命令行通过 Visual Studio 调试器运行 C++ 程序