Question

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.

Was it helpful?

Solution

I used reference counting and a templated entity_ptr type.

I created a entity_ptr_provider interface which defined the methods obtain(uuid) and release(uuid). An entity_ptr takes a templated argument of entity type (account, character etc), an entity of that type and an entity_ptr_provider.

On construction or copy of the entity_ptr type, it calls obtain on the entity_ptr_provider with the UUID of the entity so it can increment the references to it, when it's deconstructed, it calls release which allows the entity_ptr_provider to decrement the reference. When references reach 0, it's released from the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top