I have an abstract base class called Curve. There are three classes that inherit from it:
– SingleCurve
– MultiCurve
– CurveShift, which “shifts” anything that derives from Curve (takes a boost::shared_ptr<Curve> in its constructor)
I have a repository in memory that keeps track of all the curves, let’s call it CurveStore. It is implemented as a singleton with a std::map<std::string, boost::shared_ptr<Curve> > inside of it.
My problem is with CurveShift. I want to use a boost::weak_ptr to reference the underlying Curve it is shifting. This way, should the underlying Curve go away, the CurveShift will not be able to get a lock() and I will know that the CurveShift is invalid. The problem is that in the naive implementation of CurveShift, where you try to get a lock() every time you access one of the member functions, it degrades performance significantly. Is there a standard way/”pattern” to avoid having to do a lock() in all the member functions?
The “pattern” would be “external/internal functions” (not an official name), where the external functions (public), lock the weak_ptr’s and the internal functions (private) take a shared_ptr& as parameter. This only helps if you have a lean interface that actually lets the object do something (not a getter/setter interface)