pva2pva  1.4.1
 All Classes Functions Variables Pages
Classes | Public Types | Public Member Functions | List of all members
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::map< K,
value_pointer, C > 
lock_map_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 operator[] (const K &k) const
 
value_pointer find (const K &k) const
 
value_pointer insert (const K &k, value_pointer &v)
 
lock_map_type lock_map () const
 Return an equivalent map with strong value references.
 
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 Function Documentation

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  }
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  }
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 215 of file weakmap.h.

216  {
217  value_pointer ret;
218  guard_type G(_data->mutex);
219  typename store_t::const_iterator it(_data->store.find(k));
220  if(it!=_data->store.end()) {
221  // may be nullptr if we race destruction
222  // as ref. count falls to zero before we can remove it
223  ret = it->second.lock();
224  }
225  return ret;
226  }
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 230 of file weakmap.h.

231  {
232  value_pointer ret;
233  guard_type G(_data->mutex);
234  typename store_t::const_iterator it = _data->store.find(k);
235  if(it!=_data->store.end())
236  ret = it->second.lock();
237  (*this)[k] = v;
238  return ret;
239  }
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 259 of file weakmap.h.

260  {
261  lock_vector_type ret;
262  guard_type G(_data->mutex);
263  ret.reserve(_data->store.size());
264  for(typename store_t::const_iterator it = _data->store.begin(),
265  end = _data->store.end(); it!=end; ++it)
266  {
267  value_pointer P(it->second.lock());
268  if(P) ret.push_back(std::make_pair(it->first, P));
269  }
270  return ret;
271  }
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 276 of file weakmap.h.

276  {
277  return _data->mutex;
278  }
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  }
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: