MongoDB  2.7.0
mapsf.h
1 #pragma once
2 
31 #include "mongo/platform/unordered_map.h"
32 
33 namespace mongo {
34 
53  template< class M >
54  struct mapsf : boost::noncopyable {
55  SimpleMutex m;
56  M val;
57  friend struct ref;
58  public:
59 
60  typedef typename M::const_iterator const_iterator;
61  typedef typename M::key_type key_type;
62  typedef typename M::mapped_type mapped_type;
63 
64  mapsf() : m("mapsf") { }
65  void swap(M& rhs) {
67  val.swap(rhs);
68  }
69  bool empty() {
71  return val.empty();
72  }
73  // safe as we pass by value:
74  mapped_type get(key_type k) {
76  const_iterator i = val.find(k);
77  if( i == val.end() )
78  return mapped_type();
79  return i->second;
80  }
81  // think about deadlocks when using ref. the other methods
82  // above will always be safe as they are "leaf" operations.
83  struct ref {
85  public:
86  M &r;
87  ref(mapsf &m) : lk(m.m), r(m.val) { }
88  mapped_type& operator[](const key_type& k) { return r[k]; }
89  };
90  };
91 
92 }
Thread safe map.
Definition: mapsf.h:54
The concept with SimpleMutex is that it is a basic lock/unlock with no special functionality (such as...
Definition: mutex.h:158
Definition: mapsf.h:83
Definition: mutex.h:171