First, my actual question
I have a shared pointer in a map, I want to remove that shared pointer from the map when there are no other references besides the shared pointer in the map itself, so the target gets released. How can I do this? Alternatively, what's a better design to solve my problem?
Background below:
Out of nostalgia I've been writing a MUD engine in C++ using a MySQL backend. I'm at the point now of persisting entities (things like accounts, characters and so forth in this context). I'm having a bit of trouble figuring out the best way to implement this and decided to seek some advice.
My current persistence architecture looks like this, taking character for an example
Character entity -> Character storage -> Character persistence -> MySQL
Character entity refers to the character itself and is analogous to a model in MVC. It doesn't have anything but data and only knows about other entities.
Character storage is a container currently responsible for holding instances of the character entity in memory. When a request is made to find a character either by ID or name, it checks it's memory, implemented as a map of std::string containing UUIDs to a std::shared_ptr. If we have an instance already in memory, we pass back a shared pointer, if not, we ask the persistence layer for it, store it in the map and then pass a shared pointer back to it.
The persistence layer is abstract, there's a character_persistence interface which has a concrete implementation of mysql_character_persistence, so I could easily switch to different forms of persistence.