My program will create and delete a lot of objects (from a REST API). These objects will be referenced from multiple places. I'd like to have a "memory cache" and manage objects lifetime with reference counting so they can be released when they aren't used anymore.
All the objects inherit from a base class Ressource
.
The Cache
is mostly a std::map<_key_, std::shared_ptr<Ressource> >
Then I'm puzzled, how can the Cache
know when a Ressource
ref count is decremented? ie. A call to the std::shared_ptr destructor
or operator=
.
1/ I don't want to iterate over the std::map and check each ref.count().
2/ Can I reuse std::shared_ptr and implement a custom hook?
class RessourcePtr : public std::shared_ptr<Ressource>
...
3/ Should I implement my own ref count class? ex. https://stackoverflow.com/a/4910158/1058117
Thanks!