opensurgsim
LockedContainer.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
17 #define SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
18 
19 #include <boost/thread/mutex.hpp>
20 #include <boost/thread/locks.hpp>
21 
22 
23 namespace SurgSim
24 {
25 namespace Framework
26 {
27 
53 template <typename T>
55 {
56 public:
61  m_buffer(),
62  m_haveNewData(false)
63  {
64  }
65 
70  explicit LockedContainer(const T& initialValue) :
71  m_buffer(initialValue),
72  m_haveNewData(false)
73  {
74  }
75 
81  explicit LockedContainer(T&& initialValue) :
82  m_buffer(std::move(initialValue)),
83  m_haveNewData(false)
84  {
85  }
86 
87 
90  {
91  }
92 
98  void set(const T& value)
99  {
100  boost::lock_guard<boost::mutex> lock(m_mutex);
101  m_buffer = value;
102  m_haveNewData = true;
103  }
104 
110  void set(T&& value)
111  {
112  boost::lock_guard<boost::mutex> lock(m_mutex);
113  m_buffer = std::move(value);
114  m_haveNewData = true;
115  }
116 
119  void get(T* value) const
120  {
121  boost::lock_guard<boost::mutex> lock(m_mutex);
122  m_haveNewData = false;
123  *value = m_buffer;
124  }
125 
129  void take(T* value)
130  {
131  boost::lock_guard<boost::mutex> lock(m_mutex);
132  m_haveNewData = false;
133  *value = std::move(m_buffer);
134  }
135 
144  bool tryGetChanged(T* value) const
145  {
146  boost::lock_guard<boost::mutex> lock(m_mutex);
147  if (! m_haveNewData)
148  {
149  return false;
150  }
151  else
152  {
153  m_haveNewData = false;
154  *value = m_buffer;
155  return true;
156  }
157  }
158 
167  bool tryTakeChanged(T* value)
168  {
169  boost::lock_guard<boost::mutex> lock(m_mutex);
170  if (! m_haveNewData)
171  {
172  return false;
173  }
174  else
175  {
176  m_haveNewData = false;
177  *value = std::move(m_buffer);
178  return true;
179  }
180  }
181 
182 private:
186  LockedContainer& operator=(const LockedContainer&);
187 
188 
190  T m_buffer;
191 
193  mutable bool m_haveNewData;
194 
196  mutable boost::mutex m_mutex;
197 };
198 
199 }; // namespace Framework
200 }; // namespace SurgSim
201 
202 #endif // SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
~LockedContainer()
Destroy the container and the data it contains.
Definition: LockedContainer.h:89
Definition: MockObjects.h:47
bool tryGetChanged(T *value) const
Read (copy) the data from the container if it has been modified since the last access.
Definition: LockedContainer.h:144
LockedContainer(const T &initialValue)
Create the container and the data it contains.
Definition: LockedContainer.h:70
LockedContainer(T &&initialValue)
Create the container and the data it contains.
Definition: LockedContainer.h:81
void take(T *value)
Move the data out of the container.
Definition: LockedContainer.h:129
A simple thread-safe data container that can support multiple writers and readers.
Definition: LockedContainer.h:54
LockedContainer()
Create the container and the data it contains.
Definition: LockedContainer.h:60
bool tryTakeChanged(T *value)
Move the data out of the container if it has been modified since the last access. ...
Definition: LockedContainer.h:167