9 #include <pv/sharedPtr.h>
10 #include <epicsMutex.h>
11 #include <epicsGuard.h>
58 template<
typename K,
typename V,
typename C = std::less<K> >
64 typedef std::tr1::shared_ptr<V> value_pointer;
65 typedef std::tr1::weak_ptr<V> value_weak_pointer;
66 typedef std::set<value_pointer> set_type;
68 typedef epicsMutex mutex_type;
69 typedef epicsGuard<epicsMutex> guard_type;
70 typedef epicsGuardRelease<epicsMutex> release_type;
72 typedef std::map<K, value_weak_pointer, C> store_t;
78 std::tr1::shared_ptr<data> _data;
81 std::tr1::weak_ptr<data> container;
83 value_pointer realself;
84 dtor(
const std::tr1::weak_ptr<data>& d,
86 const value_pointer& w)
87 :container(d), key(k), realself(w)
89 void operator()(value_type *)
93 std::tr1::shared_ptr<data> cont(container.lock());
95 guard_type G(cont->mutex);
96 cont->store.erase(key);
131 guard_type G(_data->mutex);
132 return _data->store.clear();
139 guard_type G(_data->mutex);
140 return _data->store.empty();
148 guard_type G(_data->mutex);
149 return _data->store.size();
169 throw std::invalid_argument(
"Only unique() references may be inserted");
170 value_pointer chainptr(v.get(), dtor(M._data, k, v));
171 M._data->store[k] = chainptr;
177 return *value_pointer(*
this);
181 return value_pointer(*this).get();
184 operator value_pointer()
const
186 value_pointer ret = M.
find(k);
188 throw std::runtime_error(
"Bad key");
191 bool operator==(
const value_pointer& v)
const
195 bool operator!=(
const value_pointer& v)
const
201 inline element_proxy operator[](
const K& k)
203 return element_proxy(*
this, k);
206 value_pointer operator[](
const K& k)
const
208 value_pointer ret =
find(k);
210 throw std::runtime_error(
"Bad key");
215 value_pointer
find(
const K& k)
const
218 guard_type G(_data->mutex);
219 typename store_t::const_iterator it(_data->store.find(k));
220 if(it!=_data->store.end()) {
223 ret = it->second.lock();
230 value_pointer
insert(
const K& k, value_pointer& v)
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();
241 typedef std::map<K, value_pointer, C> lock_map_type;
246 guard_type G(_data->mutex);
247 for(
typename store_t::const_iterator it = _data->store.begin(),
248 end = _data->store.end(); it!=end; ++it)
250 value_pointer P(it->second.lock);
251 if(P) ret[it->first] = P;
256 typedef std::vector<std::pair<K, value_pointer> > lock_vector_type;
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)
267 value_pointer P(it->second.lock());
268 if(P) ret.push_back(std::make_pair(it->first, P));
V * operator->() const
Support: map[k]->mem.
lock_vector_type lock_vector() const
void swap(weak_value_map &O)
weak_value_map()
Construct a new empty set.
epicsMutex & mutex() const
value_pointer find(const K &k) const
lock_map_type lock_map() const
Return an equivalent map with strong value references.
V & operator*() const
Support: *map[k].
value_pointer & operator=(value_pointer &v)
value_pointer insert(const K &k, value_pointer &v)
An associative map where a weak_ptr to the value is stored.