All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BufferedValue.h
Go to the documentation of this file.
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_DATASTRUCTURES_BUFFEREDVALUE_H
17 #define SURGSIM_DATASTRUCTURES_BUFFEREDVALUE_H
18 
19 #include <memory>
20 #include <utility>
21 #include <boost/thread.hpp>
23 
24 namespace SurgSim
25 {
26 namespace DataStructures
27 {
28 
32 template <class T>
34 {
35 public:
36 
37  // Default Constructor
38  BufferedValue();
39 
40  // Constructor
42  explicit BufferedValue(const T& value);
43 
46 
48  void publish();
49 
52  T& unsafeGet();
53 
56  std::shared_ptr<const T> safeGet() const;
57 
58 private:
59  typedef boost::shared_lock<boost::shared_mutex> SharedLock;
60  typedef boost::unique_lock<boost::shared_mutex> UniqueLock;
61 
64 
66  std::shared_ptr<const T> m_safeValue;
67 
69  mutable boost::shared_mutex m_mutex;
70 
71 };
72 
73 } // DataStructures
74 } // SurgSim
75 
77 
78 #endif
std::shared_ptr< const T > m_safeValue
The buffered value.
Definition: BufferedValue.h:66
~BufferedValue()
Destructor.
Definition: BufferedValue-inl.h:38
BufferedValue is a class to enable a representation of two values for one variable, where both values need to be accessible at the same time, one in a thread safe, single threaded context, the other in a thread unsafe context.
Definition: BufferedValue.h:33
T & unsafeGet()
Get the value.
Definition: BufferedValue-inl.h:50
std::shared_ptr< const T > safeGet() const
Get the buffered value.
Definition: BufferedValue-inl.h:56
boost::unique_lock< boost::shared_mutex > UniqueLock
Definition: BufferedValue.h:60
BufferedValue()
Definition: BufferedValue-inl.h:25
boost::shared_lock< boost::shared_mutex > SharedLock
Definition: BufferedValue.h:59
boost::shared_mutex m_mutex
The mutex used to lock for reading and writing.
Definition: BufferedValue.h:69
The header that provides the assertion API.
void publish()
Make the current value the one returned by calls to safeGet.
Definition: BufferedValue-inl.h:43
T m_value
The raw value.
Definition: BufferedValue.h:63