a std::set-ish container where entries are removed when ref. counts fall to zero
More...
template<typename T>
class weak_set< T >
a std::set-ish container where entries are removed when ref. counts fall to zero
A container of ref. counted (by shared_ptr) entries where an entry may be present zero or one times in the set.
Meant to be used in situations where an object must hold some weak references of entries which it can iterate.
Note that the insert() method replaces the reference pass to it 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 {
};
shared_ptr<Entry> build(const shared_ptr<Owner>& own) {
shared_ptr<Owner> N(new Entry);
N.O = own;
own.S.insert(N);
return N;
}
void example()
{
shared_ptr<Owner> O(new Owner);
shared_ptr<Entry> E(build(O));
assert(!O.S.empty());
E.reset();
assert(O.S.empty());
}
Definition at line 57 of file weakset.h.