dimanche 16 octobre 2022

Removing a callback function from a vector c++

I'm building a publish-subscribe class (called SystermInterface), which is responsible to receive updates from its instances, and publish them to subscribers.

Adding a subscriber callback function is trivial and has no issues, but removing it yields an error, because std::function<()> is not comparable in C++.

std::vector<std::function<void()> subs;
void subscribe(std::function<void()> f)
{
    subs.push_back(f);
}
void unsubscribe(std::function<void()> f)
{
    std::remove(subs.begin(), subs.end(), f);  // Error
}

I've came down to five solutions to this error:

  1. Registering the function using a weak_ptr, where the subscriber must keep the returned shared_ptr alive.
    Solution example at this link.
  2. Instead of registering at a vector, map the callback function by a custom key, unique per callback function.
    Solution example at this link
  3. Using vector of function pointers. Example
  4. Make the callback function comparable by utilizing the address.
  5. Use an interface class (parent class) to call a virtual function.
    In my design, all intended classes inherits a parent class called ServiceCore, So instead of registering a callback function, just register ServiceCore reference in the vector.

Given that the SystemInterface class has a field attribute per instance (ID) (Which is managed by ServiceCore, and supplied to SystemInterface by constructing a ServiceCore child instance).

To my perspective, the first solution is neat and would work, but it requires handling at subscribers, which is something I don't really prefer.

The second solution would make my implementation more complex, where my implementation looks as:

using namespace std;
enum INFO_SUB_IMPORTANCE : uint8_t
{
    INFO_SUB_PRIMARY,       // Only gets the important updates.
    INFO_SUB_COMPLEMENTARY, // Gets more.
    INFO_SUB_ALL            // Gets all updates
};

using CBF = function<void(string,string)>;
using INFO_SUBTREE = map<INFO_SUB_IMPORTANCE, vector<CBF>>;

using REQINF_SUBS   = map<string, INFO_SUBTREE>; // It's keyed by an iterator, explaining it goes out of the question scope.
using INFSRC_SUBS   = map<string, INFO_SUBTREE>;
using WILD_SUBS     = INFO_SUBTREE;

REQINF_SUBS infoSubrs;
INFSRC_SUBS sourceSubrs;
WILD_SUBS wildSubrs;

void subscribeInfo(string info, INFO_SUB_IMPORTANCE imp, CBF f) {
    infoSubrs[info][imp].push_back(f);
}
void subscribeSource(string source, INFO_SUB_IMPORTANCE imp, CBF f) { 
    sourceSubrs[source][imp].push_back(f);
}
void subscribeWild(INFO_SUB_IMPORTANCE imp, CBF f) {
    wildSubrs[imp].push_back(f);
}

The second solution would require INFO_SUBTREE to be an extended map, but can be keyed by an ID:

using KEY_T = uint32_t; // or string...
using INFO_SUBTREE = map<INFO_SUB_IMPORTANCE, map<KEY_T,CBF>>;

For the third solution, I'm not aware of the limitations given by using function pointers, and the consequences of the fourth solution.

The Fifth solution would eliminate the purpose of dealing with CBFs, but it'll be more complex at subscriber-side, where a subscriber is required to override the virtual function and so receives all updates at one place, in which further requires filteration of the message id and so direct the payload to the intended routines using multiple if/else blocks, which will increase by increasing subscriptions.

What I'm looking for is an advice for the best available option.

Aucun commentaire:

Enregistrer un commentaire