I'm wondering if there is a better way todo this.
Originally I had a a class sender
and one receiver
sender would have a std::function
object which it would call.
struct sender
{
std::function<void()> callback;
};
struct receiver
{
void callback_func()
};
The trouble is that both these objects need to be movable. Which causes issue with the callback.
I got around this by making a tertiary object observer_callback
class observing_callback
{
public:
// gubbins to make moveable etc.
std::function<void()> callback;
std::function<void()> un_register_callback;
};
Now I can make sender
and receiver
moveable quite easily. The only issue I have with this now is that its a shared_ptr.
class sender {
public:
template<typename T>
std::shared_ptr<observing_callback> register_cb(T *instance, void (T::*cb_method)())
// .. other stuff
};
I tend to avoid shared_ptrs when possible because it can confuse ownership, because the method is public I cant be sure who owns this object and thus somebody could use the code not as intended easily. Making it private friend isn't an option.
Is there a nicer way to archive this? Have I missed something easy?
Aucun commentaire:
Enregistrer un commentaire