foobar2000 SDK  2015-08-03
syncd_storage.h
Go to the documentation of this file.
1 namespace pfc {
2 // Read/write lock guarded object store for safe concurrent access
3 template<typename t_object>
4 class syncd_storage {
5 private:
7 public:
9  template<typename t_source>
10  syncd_storage(const t_source & p_source) : m_object(p_source) {}
11  template<typename t_source>
12  void set(t_source const & p_in) {
13  inWriteSync(m_sync);
14  m_object = p_in;
15  }
16  template<typename t_destination>
17  void get(t_destination & p_out) const {
18  inReadSync(m_sync);
19  p_out = m_object;
20  }
21  t_object get() const {
22  inReadSync(m_sync);
23  return m_object;
24  }
25  template<typename t_source>
26  const t_self & operator=(t_source const & p_source) {set(p_source); return *this;}
27 private:
28  mutable ::pfc::readWriteLock m_sync;
29  t_object m_object;
30 };
31 
32 // Read/write lock guarded object store for safe concurrent access
33 // With 'has changed since last read' flag
34 template<typename t_object>
36 private:
38 public:
39  syncd_storage_flagged() : m_changed_flag(false) {}
40  template<typename t_source>
41  syncd_storage_flagged(const t_source & p_source, bool initChanged = false) : m_changed_flag(initChanged), m_object(p_source) {}
42  void set_changed(bool p_flag = true) {
43  inWriteSync(m_sync);
44  m_changed_flag = p_flag;
45  }
46  template<typename t_source>
47  void set(t_source const & p_in) {
48  inWriteSync(m_sync);
49  m_object = p_in;
50  m_changed_flag = true;
51  }
52  bool has_changed() const {
53  inReadSync(m_sync);
54  return m_changed_flag;
55  }
56  t_object peek() const {inReadSync(m_sync); return m_object;}
57  template<typename t_destination>
58  bool get_if_changed(t_destination & p_out) {
59  inReadSync(m_sync);
60  if (m_changed_flag) {
61  p_out = m_object;
62  m_changed_flag = false;
63  return true;
64  } else {
65  return false;
66  }
67  }
68  t_object get() {
69  inReadSync(m_sync);
70  m_changed_flag = false;
71  return m_object;
72  }
73  t_object get( bool & bHasChanged ) {
74  inReadSync(m_sync);
75  bHasChanged = m_changed_flag;
76  m_changed_flag = false;
77  return m_object;
78  }
79  template<typename t_destination>
80  void get(t_destination & p_out) {
81  inReadSync(m_sync);
82  p_out = m_object;
83  m_changed_flag = false;
84  }
85  template<typename t_source>
86  const t_self & operator=(t_source const & p_source) {set(p_source); return *this;}
87 private:
88  mutable volatile bool m_changed_flag;
89  mutable ::pfc::readWriteLock m_sync;
90  t_object m_object;
91 };
92 
93 }
bool get_if_changed(t_destination &p_out)
Definition: syncd_storage.h:58
mutable::pfc::readWriteLock m_sync
Definition: syncd_storage.h:89
void set_changed(bool p_flag=true)
Definition: syncd_storage.h:42
syncd_storage< t_object > t_self
Definition: syncd_storage.h:6
syncd_storage_flagged< t_object > t_self
Definition: syncd_storage.h:37
volatile bool m_changed_flag
Definition: syncd_storage.h:88
const t_self & operator=(t_source const &p_source)
Definition: syncd_storage.h:26
const t_self & operator=(t_source const &p_source)
Definition: syncd_storage.h:86
syncd_storage_flagged(const t_source &p_source, bool initChanged=false)
Definition: syncd_storage.h:41
syncd_storage(const t_source &p_source)
Definition: syncd_storage.h:10
mutable::pfc::readWriteLock m_sync
Definition: syncd_storage.h:28
t_object peek() const
Definition: syncd_storage.h:56