MongoDB  2.4.4-pre-
value.h
1 /* @file value.h
2  concurrency helpers DiagStr, Guarded
3 */
4 
21 #pragma once
22 
23 #include "spin_lock.h"
24 
25 namespace mongo {
26 
38  template <typename T, SimpleMutex& BY>
39  class Guarded {
40  T _val;
41  public:
42  T& ref(const SimpleMutex::scoped_lock& lk) {
43  dassert( &lk.m() == &BY );
44  return _val;
45  }
46  };
47 
48  // todo: rename this to ThreadSafeString or something
52  class DiagStr {
53  mutable SpinLock m;
54  string _s;
55  public:
56  DiagStr(const DiagStr& r) : _s(r.get()) { }
57  DiagStr(const string& r) : _s(r) { }
58  DiagStr() { }
59  bool empty() const {
60  scoped_spinlock lk(m);
61  return _s.empty();
62  }
63  string get() const {
64  scoped_spinlock lk(m);
65  return _s;
66  }
67  void set(const char *s) {
68  scoped_spinlock lk(m);
69  _s = s;
70  }
71  void set(const string& s) {
72  scoped_spinlock lk(m);
73  _s = s;
74  }
75  operator string() const { return get(); }
76  void operator=(const string& s) { set(s); }
77  void operator=(const DiagStr& rhs) {
78  set( rhs.get() );
79  }
80 
81  // == is not defined. use get() == ... instead. done this way so one thinks about if composing multiple operations
82  bool operator==(const string& s) const;
83  };
84 
85 }