On Mon, Sep 14, 2009 at 7:41 PM, Vjekoslav Brajkovic
Hello! :)
I'm writing a write-through cache unit which returns const shared_ptr<T>. After the data has been modified and once share_ptr goes out of scope, I would like the shared_ptr destructor to call a handler which would commit the modified value to some other location -- a DHT for instance. Is there a way to supply a callback function to shared_ptr destructor? Any help would be greatly appreciated.
Please let me know if the question is too ambiguous.
If you read the docs, you notice it can take a destructor argument as well, on the synopsis page you can see it has some constructors that take it: http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm. Copying a good section from that page: Custom deallocators allow a factory function returning a shared_ptr to insulate the user from its memory allocation strategy. Since the deallocator is not part of the type, changing the allocation strategy does not break source or binary compatibility, and does not require a client recompilation. For example, a "no-op" deallocator is useful when returning a shared_ptr to a statically allocated object, and other variations allow a shared_ptr to be used as a wrapper for another smart pointer, easing interoperability. The support for custom deallocators does not impose significant overhead. Other shared_ptr features still require a deallocator to be kept. The requirement that the copy constructor of D does not throw comes from the pass by value. If the copy constructor throws, the pointer is leaked. Removing the requirement requires a pass by (const) reference.