template<typename K, typename V, typename C = std::less<K>>
class weak_value_map< K, V, C >
An associative map where a weak_ptr to the value is stored.
Acts like std::map<K, weak_ptr<V> > where entries are automatically removed when no longer referenced.
Meant to be used in situations where an object must hold some weak references of entries which it can iterate.
Note that insert() and operator[] w/ assignment replaces the reference pass in with a "wrapped" reference which removes from the set then releases the original ref. The reference passed in must be unique() or std::invalid_argument is thrown. While this can't be enforced, no weak_ptr to this object should exist.
A reference loop will exist if the object owning the weak_set also holds strong references to entries in this set.
- Note
- With the exception of swap() all methods are thread-safe
- Warning
- Use caution when storing types deriving from enabled_shared_from_this<> As the implict weak reference they contain will not be wrapped.
struct Owner;
struct Entry {
shared_ptr<Owner> O;
};
struct Owner {
weak_map<string, Entry> M;
};
shared_ptr<Entry> build(const shared_ptr<Owner>& own, const std::string& k) {
shared_ptr<Owner> N(new Entry);
N.O = own;
own.M[k] = N;
return N;
}
void example()
{
shared_ptr<Owner> O(new Owner);
shared_ptr<Entry> E(build(O, "test"));
assert(O.M["test"]==E);
E.reset();
}
Definition at line 59 of file weakmap.h.