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

时间:2019-01-28 16:07:48

标签: c++ templates dynamic dll static

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

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

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

由于某些方法使用模板,因此无法将实现移至cpp文件。我尝试了dllexport / dllimport方法,但再次无法正常工作  因为该类使用模板。另外,在我的情况下,是导出的应用程序和导入的dll,不确定是否应该以这种方式工作。

我也看过#pragma data_seg,但是我不知道如何在整个课堂上使用它。只是两种使用静态方法?

这是完整的代码:

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)};
};

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

static size_t type()
{
    return typeid(EventType).hash_code();
}

从我可以在线阅读,实施应确保返回的值是唯一的类型和type1.hash_code == type2.hash_code意味着TYPE1 == 2型。

代码如下:

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)};
};