Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dll and static variables in template method in non template class

I spent few days searching about my problem but couldn't find any working solution.

I have a class called ServiceEventHub which is in charge of dispatching events across my application. The code is inspired by what I found online to implement an event aggregator. The application is a plugin engine which dynamically loads different dll (plugins). This class is a service provided by the Engine which resides inside the application (.exe).

The problem is that the class relies on static variables to keep track of the different "event" emitted and registered. (events are simply structures defined in a common header file). And from what I underdstand, due to the fact that symbols are not exported by default on Windows, the static variables are not the same instances in the application and the dll. As you can imagine, the "event types" are not the same between the engine and the plugin and it doesn't behave as expected. It's my first time developing on Windows and I'm a bit lost.

Because some methods use template, I cannot move the implementation to a cpp file. I have tried the dllexport/dllimport method but again it doesn't work because the class uses templates. Also, in my case it's the app which exports and the dll which imports, not sure if it's supposed to work this way around.

I also looked at #pragma data_seg, but I don't know how to use it.. for the whole class? Just the 2 methods using statics?

Here is the full code:

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

Any help would be much appreciated.

like image 992
kashikai Avatar asked Jan 28 '19 16:01

kashikai


1 Answers

I managed to avoid using static counters by using template type info:

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

From what I can read online, the implementation should make sure that the returned value is unique to a type and that type1.hash_code == type2.hash_code means that type1 == type2.

Here is the code:

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)};
};
like image 74
kashikai Avatar answered Oct 24 '22 06:10

kashikai