gpt4 book ai didi

c++ - Boost::signals2 传递无效数据

转载 作者:太空狗 更新时间:2023-10-29 21:19:48 28 4
gpt4 key购买 nike

我从事 C/C++ 开发已有大约 20 年了,但模板一直是我的弱点。随着模板编程在 C++11 和 C++14 标准中变得越来越有用和复杂,我决定尝试一个练习来学习。我取得了一定的成功,但我遇到了一个问题。我有以下类(class):

namespace Events {
// Place your new EventManager events here
static const uint32_t StatsData = 0;
static const uint32_t StatsRequest = 1;
static const uint32_t StatsReply = 2;
static const uint32_t ApplianceStatsRequest = 3;
static const uint32_t ApplianceStatsReply = 4;
static const uint32_t NullEvent = 5;
};

class EventManager {
public:
static EventManager *instance() {
if (Instance)
return Instance;

return new EventManager();
};

static void destroy() {
delete Instance;
Instance = nullptr;
}

template<typename T>
bool consume_event(uint32_t event, std::function<T> func) {
if (_event_map.find(event) == _event_map.end())
// Create the signal, in true RAII style
_event_map[event] = new boost::signals2::signal<T>();

boost::any_cast<boost::signals2::signal<T> *>(_event_map[event])->connect(func);

return true;
}

void emit(uint32_t event) {
if (_event_map.find(event) == _event_map.end())
return;

try {
boost::signals2::signal<void()> *sig =
boost::any_cast<boost::signals2::signal<void()> *>(_event_map[event]);

(*sig)();
}
catch (boost::bad_any_cast &e) {
SYSLOG(ERROR) << "Caught instance of boost::bad_any_cast: " << e.what();
abort();
}
}

template<typename... Args>
void emit(uint32_t event, Args... args) {
if (_event_map.find(event) == _event_map.end())
return;

try {
boost::signals2::signal<void(Args...)> *sig =
boost::any_cast<boost::signals2::signal<void(Args...)> *>(_event_map[event]);
(*sig)(args...);
}
catch (boost::bad_any_cast &e) {
SYSLOG(ERROR) << "Caught instance of boost::bad_any_cast: " << e.what();
abort();
}
}

private:
EventManager() { Instance = this; };
~EventManager() { Instance = nullptr; };

static EventManager *Instance;
std::map<uint32_t, boost::any> _event_map;
};

这段代码可能会进入一个大型框架,该框架加载多个模块,这些模块是 linux 上的动态库。这个想法是让给定的模块能够调用:

consume_event<ParamTypes><EventNumber, SomeCallack)

回调可以是带有签名 void(ParamTypes) 的函数,或者是带有签名 void(ParamTypes) 的函数上 std::bind() 的结果。

然后另一个模块将能够调用:

emit<ParamTypes>(EventNumber, ParamValues) 

并且每个调用了 consume_event 的模块都会使用 ParamValues 调用它的处理程序。

这似乎在几乎所有情况下都有效,除非我传递对自定义类的引用,如下所示:

std::cout << "Sending stats data with ref: " << std::hex << ip_entry.second <<  std::endl;
emit<ip_stats_t &>(Events::StatsData, *ip_entry.second);

在这种情况下,连接到信号的函数接收到 0xa,并在尝试将其视为 ip_stats_t & 时立即崩溃。

输出是:

Sending stats data with ref: 0x7fbbc4177d50 <- This is the output of the line seen above
ips addr: 0xa << this is from the function that gets called by the signal.

更新:我刚刚注意到它在通过引用传递任何变量时做同样的事情,而不仅仅是上面的自定义类。

此外,请注意没有 SSCCE在这个问题中,因为任何 SSCCE 变量都有效。直到将工作代码放入上述框架中后,问题才会出现。

Update2:这里真正的问题是,如何才能让这个设计变得更好。这个不仅不能正常工作,而且在语法上,它很糟糕。它丑陋、不优雅,而且真的,除了它做了我想要它做的事情并增加了我对模板的理解之外,它没有任何好处。

更新 3:我现在 100% 确认这与我传递的数据类型无关。如果我通过引用传递 any 变量,槽总是接收 0xa 作为引用地址。这包括 std::strings,甚至是 int。如果我按值传递任何变量,该值的复制构造函数最终会收到 0xa 作为要复制的值的引用。只有在从模块 A 中创建的信号调用模块 B 中的插槽时才会发生这种情况。我错过了什么?

有什么想法吗?谢谢!

最佳答案

已更新 从那以后,我提出了一个似乎更接近您想要实现的目标的演示:

@lk75 For fun, here's an approach that abstracts the event mechanism in a fairly extensible way, while

  • not being overly complicated
  • not requiring calling signature to be repeated all over the place (it's in Traits now)
  • not leaking signals by using true RAII style (SCNR). No more use of new or delete!

See it Live On Coliru.

Note how I simplified the singleton and turned both consume_event and emit into one-liners now:

    static EventManager& instance() {
static EventManager instance;
return instance;
};

template <EventId event, typename F>
bool consume_event(F&& func) {
get_slot<event>().connect(std::forward<F>(func));
return true;
}

template <EventId event, typename... Args>
void emit(Args&&... args) {
get_slot<event>()(std::forward<Args>(args)...);
}

完整代码

供引用:

Live On Coliru

#include <boost/any.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2/signal.hpp>
#include <iostream>
#include <memory>
#include <string>

struct ip_stats_t {
std::string canary;
};

enum class EventId : uint32_t {
// Place your new EventManager events here
StatsData = 0,
StatsRequest = 1,
StatsReply = 2,
ApplianceStatsRequest = 3,
ApplianceStatsReply = 4,
NullEvent = 5, // Not implemented
};

namespace Events {

template <EventId> struct Traits;

template <> struct Traits<EventId::StatsData> { using signal_type = boost::signals2::signal<void(int)>; } ;
template <> struct Traits<EventId::StatsRequest> { using signal_type = boost::signals2::signal<void(bool, bool)>; } ;
template <> struct Traits<EventId::StatsReply> { using signal_type = boost::signals2::signal<void(std::string)>; } ;
template <> struct Traits<EventId::ApplianceStatsRequest> { using signal_type = boost::signals2::signal<void(double, ip_stats_t&)>; } ;
//template <> struct Traits<EventId::NullEvent> { using signal_type = boost::signals2::signal<void()>; } ;

template <> struct Traits<EventId::ApplianceStatsReply> : Traits<EventId::ApplianceStatsRequest> { };
}

class EventManager {
public:
static EventManager& instance() {
static EventManager instance;
return instance;
};

template <EventId event, typename F>
bool consume_event(F&& func) {
get_slot<event>().connect(std::forward<F>(func));
return true;
}

template <EventId event, typename... Args>
void emit(Args&&... args) {
get_slot<event>()(std::forward<Args>(args)...);
}

private:
template <EventId event, typename Slot = typename Events::Traits<event>::signal_type, typename SlotPtr = boost::shared_ptr<Slot> >
Slot& get_slot() {
try {
if (_event_map.find(event) == _event_map.end())
_event_map.emplace(event, boost::make_shared<Slot>());

return *boost::any_cast<SlotPtr>(_event_map[event]);
}
catch (boost::bad_any_cast const &e) {
std::cerr << "Caught instance of boost::bad_any_cast: " << e.what() << " on event #" << static_cast<uint32_t>(event) << "\n";
abort();
}
}

EventManager() = default;
std::map<EventId, boost::any> _event_map;
};

int main() {
auto& emgr = EventManager::instance();

emgr.consume_event<EventId::ApplianceStatsRequest>([](double d, ip_stats_t& v) {
std::cout << "d: " << d << ", v.canary: " << v.canary << "\n";
});
emgr.consume_event<EventId::ApplianceStatsRequest>([](double d, ip_stats_t& v) {
std::cout << "And you can register more than one\n";
});


ip_stats_t v { "This is statically checked" };
emgr.emit<EventId::ApplianceStatsRequest>(3.142f, v);

emgr.emit<EventId::StatsData>(42); // no connection, but works
emgr.consume_event<EventId::StatsData>([](int) { std::cout << "Now it's connected\n"; });
emgr.emit<EventId::StatsData>(42); // now with connection!

#if 0
emgr.emit<EventId::ApplianceStatsRequest>(); // error: no match for call to ‘(boost::signals2::signal<void(double, ip_stats_t&)>) ()’
emgr.consume_event<EventId::NullEvent>([]{}); // use of incomplete type Traits<NullEvent>
#endif
}

旧答案:

您似乎在可变转发方面遇到了麻烦:

    (*sig)(std::forward<Args>(args)...);

此外,转发只有在通过“通用引用”获取参数时才真正有意义:

template<typename... Args>
void emit(uint32_t event, Args&&... args) { // NOTE!!

但是,您不依赖参数类型推导来获取实际值类别(右值与左值)。而且,这是对的(因为编译器可能永远不会获得“正确”的确切参数类型来匹配存储的信号(使 any_cast 失败充其量,或者充其量调用 Undefined Behaviour。)

所以在这种情况下,您应该放弃整个转发业务:

template<typename... Args> using Sig = boost::signals2::signal<void(Args...)>;

template<typename... Args>
void emit(uint32_t event, Args... args) {
if (_event_map.find(event) == _event_map.end())
return;

try {
Sig<Args...> *sig = boost::any_cast<Sig<Args...> *>(_event_map[event]);

(*sig)(args...);
}
catch (boost::bad_any_cast &e) {
std::cerr << "Caught instance of boost::bad_any_cast: " << e.what();
abort();
}
}

完整演示程序: Live On Coliru

#include <boost/any.hpp>
#include <boost/signals2/signal.hpp>
#include <iostream>
#include <string>

struct ip_stats_t {
std::string canary;
};

template<typename... Args> using Sig = boost::signals2::signal<void(Args...)>;
std::map<uint32_t, boost::any> _event_map;

template<typename... Args>
void emit(uint32_t event, Args&&... args) {
if (_event_map.find(event) == _event_map.end())
return;

try {
Sig<Args...> *sig = boost::any_cast<Sig<Args...> *>(_event_map[event]);

(*sig)(std::forward<Args>(args)...);
}
catch (boost::bad_any_cast &e) {
std::cerr << "Caught instance of boost::bad_any_cast: " << e.what();
abort();
}
}

int main()
{
Sig<int, double> sig1;
Sig<ip_stats_t&> sig2;

sig1.connect([](int i, double d) { std::cout << "Sig1 handler: i = " << i << ", d = " << d << "\n"; });
sig2.connect([](ip_stats_t& v) { std::cout << "Sig2 handler: canary = " << v.canary << "\n"; });

_event_map[1] = &sig1;
_event_map[2] = &sig2;

emit<int, double>(1, 42, 3.14);

ip_stats_t instance { "Hello world" }, *ptr = &instance;

emit<ip_stats_t&>(2, *ptr);
}

关于c++ - Boost::signals2 传递无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26346357/

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