I'm trying to rewrite our Observer / Observable implementation to use std::shared_ptr/std::weak_ptr to get rid of some nasty race conditions currently present in the code.
Typically, the observers registers themselves when some condition is met or when they are constructing child objects like so:
// Used to be raw 'this' now child instead derives a weak_ptr and stores it
child->addObserver(shared_from_this())
And unregisters themselves in destructor like so:
child->removeObserver(this); // Not shared_from_this() since in destructor
In some situations this works fine, however in many instances the observer wants to register itself when in constructor. Since the shared_ptr hasn't been created yet we cannot call shared_from_this().
Since the weak_ptr is commonly recomended to implement the observer pattern in C++ I'm wondering what the idiomatic way to solve the above problem is.
Some thoughts:
- Let factory that creates the observer object register the observer. This leaks abstractions from the observer (why should the factory know who the child wants to observe?) and forces observer to expose internal objects that it may want to observe
- Add an init method that gets called by factory after constructor is complete, better than above but what is the semantic difference between a constructor and init anyway? What should be done where? Is it even RAII? Indeed, some languages even call their constructors init.
- Pass a lambda to constructor that takes another lambda that gets called after construction
- Some template magic maybe?
- Implement the observer pattern in some other way.
shared_ptr(why notunique_ptrand observer as raw pointer)? - Marek Rweak_ptr. You'll need a different design. I understand that you don't want a list of raw pointers to observers protected by a mutex? - curiousguy