pva2pva 1.4.2
Loading...
Searching...
No Matches
weak_value_map< K, V, C > Class Template Reference

An associative map where a weak_ptr to the value is stored. More...

#include <weakmap.h>

Classes

class  element_proxy

Public Types

typedef K key_type
typedef V value_type
typedef std::tr1::shared_ptr< V > value_pointer
typedef std::tr1::weak_ptr< V > value_weak_pointer
typedef std::set< value_pointer > set_type
typedef epicsMutex mutex_type
typedef epicsGuard< epicsMutex > guard_type
typedef epicsGuardRelease< epicsMutex > release_type
typedef std::vector< std::pair< K, value_pointer > > lock_vector_type

Public Member Functions

 weak_value_map ()
 Construct a new empty set.
void swap (weak_value_map &O)
void clear ()
bool empty () const
size_t size () const
element_proxy operator[] (const K &k)
value_pointer find (const K &k) const
value_pointer insert (const K &k, value_pointer &v)
lock_vector_type lock_vector () const
epicsMutex & mutex () const

Detailed Description

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; // modifies 'N'
return N;
}
void example()
{
shared_ptr<Owner> O(new Owner);
shared_ptr<Entry> E(build(O, "test"));
assert(!O.M.empty());
assert(O.M["test"]==E);
E.reset(); // Entry is removed from the set and free'd
assert(O.M.empty());
}

Definition at line 59 of file weakmap.h.

Member Typedef Documentation

◆ guard_type

template<typename K, typename V, typename C = std::less<K>>
typedef epicsGuard<epicsMutex> weak_value_map< K, V, C >::guard_type

Definition at line 69 of file weakmap.h.

◆ key_type

template<typename K, typename V, typename C = std::less<K>>
typedef K weak_value_map< K, V, C >::key_type

Definition at line 62 of file weakmap.h.

◆ lock_vector_type

template<typename K, typename V, typename C = std::less<K>>
typedef std::vector<std::pair<K, value_pointer> > weak_value_map< K, V, C >::lock_vector_type

Definition at line 234 of file weakmap.h.

◆ mutex_type

template<typename K, typename V, typename C = std::less<K>>
typedef epicsMutex weak_value_map< K, V, C >::mutex_type

Definition at line 68 of file weakmap.h.

◆ release_type

template<typename K, typename V, typename C = std::less<K>>
typedef epicsGuardRelease<epicsMutex> weak_value_map< K, V, C >::release_type

Definition at line 70 of file weakmap.h.

◆ set_type

template<typename K, typename V, typename C = std::less<K>>
typedef std::set<value_pointer> weak_value_map< K, V, C >::set_type

Definition at line 66 of file weakmap.h.

◆ value_pointer

template<typename K, typename V, typename C = std::less<K>>
typedef std::tr1::shared_ptr<V> weak_value_map< K, V, C >::value_pointer

Definition at line 64 of file weakmap.h.

◆ value_type

template<typename K, typename V, typename C = std::less<K>>
typedef V weak_value_map< K, V, C >::value_type

Definition at line 63 of file weakmap.h.

◆ value_weak_pointer

template<typename K, typename V, typename C = std::less<K>>
typedef std::tr1::weak_ptr<V> weak_value_map< K, V, C >::value_weak_pointer

Definition at line 65 of file weakmap.h.

Constructor & Destructor Documentation

◆ weak_value_map()

template<typename K, typename V, typename C = std::less<K>>
weak_value_map< K, V, C >::weak_value_map ( )
inline

Construct a new empty set.

Definition at line 113 of file weakmap.h.

113:_data(new data) {}

Member Function Documentation

◆ clear()

template<typename K, typename V, typename C = std::less<K>>
void weak_value_map< K, V, C >::clear ( )
inline

Remove all (weak) entries from the set

Note
Thread safe

Definition at line 130 of file weakmap.h.

130 {
131 guard_type G(_data->mutex);
132 return _data->store.clear();
133 }
An associative map where a weak_ptr to the value is stored.
Definition weakmap.h:60

◆ empty()

template<typename K, typename V, typename C = std::less<K>>
bool weak_value_map< K, V, C >::empty ( ) const
inline

Test if set is empty at this moment

Note
Thread safe
Warning
see size()

Definition at line 138 of file weakmap.h.

138 {
139 guard_type G(_data->mutex);
140 return _data->store.empty();
141 }

◆ find()

template<typename K, typename V, typename C = std::less<K>>
value_pointer weak_value_map< K, V, C >::find ( const K & k) const
inline

Lookup key 'k'

Returns
a strong reference or nullptr if 'k' is not present

Definition at line 208 of file weakmap.h.

209 {
210 value_pointer ret;
211 guard_type G(_data->mutex);
212 typename store_t::const_iterator it(_data->store.find(k));
213 if(it!=_data->store.end()) {
214 // may be nullptr if we race destruction
215 // as ref. count falls to zero before we can remove it
216 ret = it->second.lock();
217 }
218 return ret;
219 }

◆ insert()

template<typename K, typename V, typename C = std::less<K>>
value_pointer weak_value_map< K, V, C >::insert ( const K & k,
value_pointer & v )
inline

Insert or replace

Returns
previous value of key k (may be nullptr).

Definition at line 223 of file weakmap.h.

224 {
225 value_pointer ret;
226 guard_type G(_data->mutex);
227 typename store_t::const_iterator it = _data->store.find(k);
228 if(it!=_data->store.end())
229 ret = it->second.lock();
230 (*this)[k] = v;
231 return ret;
232 }
value_pointer find(const K &k) const
Definition weakmap.h:208

◆ lock_vector()

template<typename K, typename V, typename C = std::less<K>>
lock_vector_type weak_value_map< K, V, C >::lock_vector ( ) const
inline

Return a vector of pairs of keys and strong value references. useful for iteration

Definition at line 237 of file weakmap.h.

238 {
239 lock_vector_type ret;
240 guard_type G(_data->mutex);
241 ret.reserve(_data->store.size());
242 for(typename store_t::const_iterator it = _data->store.begin(),
243 end = _data->store.end(); it!=end; ++it)
244 {
245 value_pointer P(it->second.lock());
246 if(P) ret.push_back(std::make_pair(it->first, P));
247 }
248 return ret;
249 }

◆ mutex()

template<typename K, typename V, typename C = std::less<K>>
epicsMutex & weak_value_map< K, V, C >::mutex ( ) const
inline

Access to the weak_set internal lock for use with batch operations.

Warning
Use caution when swap()ing while holding this lock!

Definition at line 254 of file weakmap.h.

254 {
255 return _data->mutex;
256 }

◆ operator[]()

template<typename K, typename V, typename C = std::less<K>>
element_proxy weak_value_map< K, V, C >::operator[] ( const K & k)
inline

Definition at line 201 of file weakmap.h.

202 {
203 return element_proxy(*this, k);
204 }

◆ size()

template<typename K, typename V, typename C = std::less<K>>
size_t weak_value_map< K, V, C >::size ( ) const
inline

number of entries in the set at this moment

Note
Thread safe
Warning
May be momentarily inaccurate (larger) due to dead refs. which have not yet been removed.

Definition at line 147 of file weakmap.h.

147 {
148 guard_type G(_data->mutex);
149 return _data->store.size();
150 }

◆ swap()

template<typename K, typename V, typename C = std::less<K>>
void weak_value_map< K, V, C >::swap ( weak_value_map< K, V, C > & O)
inline

exchange the two sets.

Warning
Not thread safe (exchanges mutexes as well)

Definition at line 124 of file weakmap.h.

124 {
125 _data.swap(O._data);
126 }

The documentation for this class was generated from the following file: