Scroom  0.14
Scroom::Bookkeeping::MapBase< K, V > Class Template Reference

#include <bookkeeping.hh>

Inheritance diagram for Scroom::Bookkeeping::MapBase< K, V >:
Inheritance graph
Collaboration diagram for Scroom::Bookkeeping::MapBase< K, V >:
Collaboration graph

Public Member Functions

Token reserve (const K &k)
 
Token reReserve (const K &k)
 
void remove (const K &k)
 
void remove (const K &k, const WeakToken &t)
 
Detail::LValue< V > at (const K &k)
 
void set (const K &k, const V &v)
 
get (const K &k)
 
std::list< K > keys () const
 
std::list< V > values () const
 
- Public Member Functions inherited from Scroom::Utils::Base
 Base ()=default
 
 Base (const Base &)=delete
 
 Base (Base &&)=delete
 
Baseoperator= (const Base &)=delete
 
Baseoperator= (Base &&)=delete
 
virtual ~Base ()=default
 
template<typename R >
boost::shared_ptr< R > shared_from_this ()
 
template<typename R >
boost::shared_ptr< R const > shared_from_this () const
 

Private Types

using MapType = typename std::map< K, boost::weak_ptr< Detail::ValueType< V > >>
 

Private Attributes

MapType map
 
boost::mutex mut
 

Member Typedef Documentation

◆ MapType

template<typename K , typename V >
using Scroom::Bookkeeping::MapBase< K, V >::MapType = typename std::map<K, boost::weak_ptr<Detail::ValueType<V> >>
private

Member Function Documentation

◆ at()

template<typename K , typename V >
Detail::LValue< V > Scroom::Bookkeeping::MapBase< K, V >::at ( const K &  k)
inline
296  {
297  boost::mutex::scoped_lock const lock(mut);
298  auto i = map.find(k);
299 
300  if(map.end() != i)
301  {
302  typename Detail::ValueType<V>::Ptr const pv = i->second.lock();
303  if(pv)
304  {
305  return Detail::LValue<V>(pv);
306  }
307  }
308 
309  throw std::invalid_argument("Invalid key");
310  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ get()

template<typename K , typename V >
V Scroom::Bookkeeping::MapBase< K, V >::get ( const K &  k)
inline
333  {
334  boost::mutex::scoped_lock const lock(mut);
335  auto i = map.find(k);
336 
337  if(map.end() != i)
338  {
339  typename Detail::ValueType<V>::Ptr const pv = i->second.lock();
340  if(pv)
341  {
342  return pv->value;
343  }
344  }
345 
346  throw std::invalid_argument("Invalid key");
347  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ keys()

template<typename K , typename V >
std::list< K > Scroom::Bookkeeping::MapBase< K, V >::keys
inline
351  {
352  boost::mutex::scoped_lock const lock(mut);
353  std::list<K> result;
354  for(const typename MapType::value_type& el: map)
355  {
356  result.push_back(el.first);
357  }
358  return result;
359  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ remove() [1/2]

template<typename K , typename V >
void Scroom::Bookkeeping::MapBase< K, V >::remove ( const K &  k)
inline
284  {
285  boost::mutex::scoped_lock lock(mut);
286  typename MapType::iterator i = map.find(k);
287 
288  if(map.end() != i)
289  {
290  map.erase(i);
291  }
292  }

◆ remove() [2/2]

template<typename K , typename V >
void Scroom::Bookkeeping::MapBase< K, V >::remove ( const K &  k,
const WeakToken t 
)
inline
259  {
260  boost::mutex::scoped_lock const lock(mut);
261  auto i = map.find(k);
262 
263  if(map.end() != i)
264  {
265  typename Detail::ValueType<V>::Ptr const pv = i->second.lock();
266  if(pv)
267  {
268  Token const t(wt.lock());
269  Token const t_orig(pv->token.lock());
270  if(t == t_orig)
271  {
272  map.erase(i);
273  }
274  }
275  else
276  {
277  map.erase(i);
278  }
279  }
280  }

◆ reReserve()

template<typename K , typename V >
Token Scroom::Bookkeeping::MapBase< K, V >::reReserve ( const K &  k)
inline
229  {
230  boost::mutex::scoped_lock const lock(mut);
231  auto i = map.find(k);
232 
233  if(map.end() == i)
234  {
235  map[k] = typename Detail::ValueType<V>::WeakPtr();
236  i = map.find(k);
237  }
238 
239  typename Detail::ValueType<V>::Ptr pv = i->second.lock();
240  if(!pv)
241  {
243  i->second = pv;
244  }
245 
246  Token t(pv->token.lock());
247  if(!t)
248  {
249  t = Detail::MapTokenImpl<K, V>::create(shared_from_this<MapBase<K, V>>(), k);
250  t.add(pv);
251  pv->token = t;
252  }
253 
254  return t;
255  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ reserve()

template<typename K , typename V >
Token Scroom::Bookkeeping::MapBase< K, V >::reserve ( const K &  k)
inline
211  {
212  boost::mutex::scoped_lock const lock(mut);
213  if(map.end() != map.find(k))
214  {
215  throw std::invalid_argument("Key already exists");
216  }
217 
219  map[k] = pv;
220 
221  Token t = Detail::MapTokenImpl<K, V>::create(shared_from_this<MapBase<K, V>>(), k);
222  t.add(pv);
223  pv->token = t;
224  return t;
225  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ set()

template<typename K , typename V >
void Scroom::Bookkeeping::MapBase< K, V >::set ( const K &  k,
const V &  v 
)
inline
314  {
315  boost::mutex::scoped_lock const lock(mut);
316  auto i = map.find(k);
317 
318  if(map.end() != i)
319  {
320  typename Detail::ValueType<V>::Ptr const pv = i->second.lock();
321  if(pv)
322  {
323  pv->value = v;
324  return;
325  }
326  }
327 
328  throw std::invalid_argument("Invalid key");
329  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ values()

template<typename K , typename V >
std::list< V > Scroom::Bookkeeping::MapBase< K, V >::values
inline
363  {
364  boost::mutex::scoped_lock const lock(mut);
365  std::list<V> result;
366  for(const typename MapType::value_type& el: map)
367  {
368  typename Detail::ValueType<V>::Ptr const pv = el.second.lock();
369  if(pv)
370  {
371  result.push_back(pv->value);
372  }
373  }
374  return result;
375  }

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

Member Data Documentation

◆ map

template<typename K , typename V >
MapType Scroom::Bookkeeping::MapBase< K, V >::map
private

◆ mut

template<typename K , typename V >
boost::mutex Scroom::Bookkeeping::MapBase< K, V >::mut
mutableprivate

The documentation for this class was generated from the following files:
Scroom::Bookkeeping::Detail::ValueType::WeakPtr
boost::weak_ptr< ValueType< V > > WeakPtr
Definition: bookkeepingimpl.hh:121
Scroom::Utils::Base::shared_from_this
boost::shared_ptr< R > shared_from_this()
Definition: utilities.hh:109
Scroom::Bookkeeping::MapBase::map
MapType map
Definition: bookkeeping.hh:66
Scroom::Bookkeeping::Detail::ValueType::create
static Ptr create(V value)
Definition: bookkeepingimpl.hh:134
Scroom::Bookkeeping::Detail::ValueType::Ptr
boost::shared_ptr< ValueType< V > > Ptr
Definition: bookkeepingimpl.hh:120
Scroom::Bookkeeping::MapBase::mut
boost::mutex mut
Definition: bookkeeping.hh:67
Scroom::Bookkeeping::Detail::TokenImpl::create
static Scroom::Bookkeeping::Token create()
Definition: bookkeepingimpl.hh:65