palacaze / sigslot

A simple C++14 signal-slots implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exclude groups from notifications?

kzantow opened this issue · comments

Hi, first of all thank you so much for this library - it is excellent!

I have a use case that requires usually notifying all observers but in one specific case skipping a set of them. With the groups feature, I found a trivial way to do this is just have an overloaded invocation function that takes a group id and only notifies groups with gid >= what I provide, e.g.:

template <typename... U>
void operator()(group_id gid, U && ...a) {
    if (m_block) {
        return;
    }

    // Reference to the slots to execute them out of the lock
    // a copy may occur if another thread writes to it.
    cow_copy_type<list_type, Lockable> ref = slots_reference();

    for (const auto &group : detail::cow_read(ref)) {
        if (group.gid >= gid) {
            for (const auto &s : group.slts) {
                s->operator()(a...);
            }
        }
    }
}

This makes it easy for me to add some observers with gid == -1, for example. Is this a use case you might consider supporting with this or perhaps a more appropriately named function?

Thanks!

I may be wrong, but could it point towards a design problem?

The observer pattern is usually used to decouple interfaces. I may be mislead, but it seems to me that by assigning meaning to the group numbers and performing selective invocation of slots, some sort of coupling emerges. The signal emitter chooses which slots should be invoked.

Why not create 2 signals with close but different semantics instead of only one? One would always be fired, the other one only within the right conditions. That way observers could connect to the appropriate one.