非模板类中模板方法中的DLL和静态变量

kas*_*kai 6 c++ dll static templates dynamic

我花了几天时间搜索我的问题,但找不到任何可行的解决方案.

我有一个名为ServiceEventHub的类,它负责在我的应用程序中调度事件.代码的灵感来自我在网上发现的实现事件聚合器的内容.该应用程序是一个插件引擎,动态加载不同的DLL(插件).此类是引擎提供的服务,它位于应用程序(.exe)中.

问题是该类依赖于静态变量来跟踪发出和注册的不同"事件".(事件只是在公共头文件中定义的结构).从我的观点来看,由于在Windows上默认情况下不导出符号这一事实,静态变量与应用程序和dll中的实例不同.可以想象,引擎和插件之间的"事件类型"并不相同,并且它的行为不符合预期.这是我第一次在Windows上开发,我有点迷失了.

因为有些方法使用模板,所以我无法将实现移动到cpp文件.我已经尝试了dllexport/dllimport方法,但它再次无效,因为该类使用模板.另外,在我的情况下,它是导出的应用程序和导入的DLL,不确定它是否应该以这种方式工作.

我也看了#pragma data_seg,但我不知道如何使用它..对于全班?只使用静态的2种方法?

这是完整的代码:

class ServiceEventHub
{
public:

template <class EventType>
using Slot = std::function<void(const EventType&)>;

ServiceEventHub()
{

}

template <typename EventType>
void subscribe(Slot<EventType> callable)
{
    LOG_FUNC_ENTER();

    std::lock_guard<std::recursive_mutex> lock(m_mutex);
    size_t type = Event<EventType>::type();

    if (type >= m_subscribers.size())
    {
        m_subscribers.resize(type + 1);
    }

    m_subscribers[type].push_back(CallbackWrapper<EventType>(callable));
}

template <typename EventType>
void emit(EventType&& event)
{
    LOG_FUNC_ENTER(typeid(EventType).name());

    // Critical section starts
    std::lock_guard<std::recursive_mutex> lock(m_mutex);
    size_t type = Event<EventType>::type();

    if (type >= m_subscribers.size())
    {
        return;
    }

    Event<EventType> eventWrapper(std::forward<EventType>(event));
    for (auto& receiver : m_subscribers[type])
    {
        m_ioService.post(boost::bind(receiver, eventWrapper));
    }

    // Critical section ends
}

private:

struct BaseEvent
{
    virtual ~BaseEvent() {}
protected:
    static size_t getNextType()
    {
        static size_t s_typeCount{ 0 };
        return s_typeCount++;
    }
};

template <typename EventType>
struct Event : BaseEvent
{
    static size_t type()
    {
        static size_t s_type = BaseEvent::getNextType();
        return s_type;
    }
    Event(EventType&& event)
        : event_(std::forward<EventType>(event))
    {
    }
    EventType event_;
};

template <typename EventType>
struct CallbackWrapper
{
    CallbackWrapper(Slot<EventType> callable)
        : m_callable(callable)
    {
    }

    void operator()(const BaseEvent& event)
    {
        m_callable(static_cast<const Event<EventType>&>(event).event_);
    }

    Slot<EventType> m_callable;
};

void workerThread(boost::asio::io_service* ioService)
{
    LOG_FUNC_ENTER();

    ioService->run();
}

std::vector<std::vector<Slot<BaseEvent> > > m_subscribers = {};
std::recursive_mutex                        m_mutex;
boost::asio::io_service                     m_ioService{};
boost::asio::io_service::work               m_ioWork{m_ioService};
std::thread                                 m_thread{boost::bind(&ServiceEventHub::workerThread, this, &m_ioService)};
};
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

kas*_*kai 1

我设法通过使用模板类型信息来避免使用静态计数器:

static size_t type()
{
    return typeid(EventType).hash_code();
}
Run Code Online (Sandbox Code Playgroud)

从我在线阅读的内容来看,实现应该确保返回的值对于类型来说是唯一的,并且 type1.hash_code == type2.hash_code 意味着 type1 == type2。

这是代码:

class ServiceEventHub
{
public:

template <class EventType>
using Slot = std::function<void(const EventType&)>;

template <typename EventType>
void subscribe(Slot<EventType> callable)
{
    LOG_FUNC_ENTER();

    size_t type = Event<EventType>::type();

    // Critical section starts
    std::lock_guard<std::recursive_mutex> lock(m_mutex);
    auto search = m_subscribers.find(type);

    if (search != m_subscribers.cend())
    {
        search->second.push_back(CallbackWrapper<EventType>(callable));
    }
    else
    {
        m_subscribers[type] = { CallbackWrapper<EventType>(callable) };
    }
    // Critical section ends
}

template <typename EventType>
void emit(EventType&& event)
{
    LOG_FUNC_ENTER(typeid(EventType).name());

    size_t type = Event<EventType>::type();

    // Critical section starts
    std::lock_guard<std::recursive_mutex> lock(m_mutex);
    auto typeCallbacks = m_subscribers.find(type);

    if (typeCallbacks == m_subscribers.cend())
    {
        return;
    }

    Event<EventType> eventWrapper(std::forward<EventType>(event));

    for (auto& receiver : typeCallbacks->second)
    {
        m_ioService.post(boost::bind(receiver, eventWrapper));
    }
    // Critical section ends
}

private:

struct BaseEvent
{
    virtual ~BaseEvent() {}
};

template <typename EventType>
struct Event : BaseEvent
{
    static size_t type()
    {
        return typeid(EventType).hash_code();
    }
    Event(EventType&& event)
        : event_(std::forward<EventType>(event))
    {
    }
    EventType event_;
};

template <typename EventType>
struct CallbackWrapper
{
    CallbackWrapper(Slot<EventType> callable)
        : m_callable(callable)
    {
    }

    void operator()(const BaseEvent& event)
    {
        m_callable(static_cast<const Event<EventType>&>(event).event_);
    }

    Slot<EventType> m_callable;
};

void workerThread(boost::asio::io_service* ioService)
{
    LOG_FUNC_ENTER();

    ioService->run();
}

std::map<size_t, std::vector<Slot<BaseEvent> > >    m_subscribers = {};
std::recursive_mutex                                m_mutex;
boost::asio::io_service                             m_ioService{};
boost::asio::io_service::work                       m_ioWork{m_ioService};
std::thread                                          
m_thread{boost::bind(&ServiceEventHub::workerThread, this, &m_ioService)};
};
Run Code Online (Sandbox Code Playgroud)