mardi 18 novembre 2014

Reference counting in a collection


Vote count:

0




Let's have a collection of objects (say string is type of collection). I want each element of collection to have a reference count. So, on Add-Usage it should increment count for this given element.



coll.AddUsage("SomeElement"); // Type doesn't matter - but should increase count


On, Release-Usage, it should decrement the reference count for given element, and if count reaches 0, then it should remove the element from collection.


It is not important if AddUsage will allocate element (and set reference-count to 1), or would fail altogether (since element didn't exist). Important thing is RemoveUsage, which should remove given element (object) from collection.


I thought of using vector of a pair (or a custom struct), or using any kind of map/multimap. There exists no existing class in C++ library (may be out of thread-support library, one atomic classes, shared-pointer classes etc).



asked 2 mins ago

Ajay

6,587






Reference counting in a collection

Aucun commentaire:

Enregistrer un commentaire