LockedContainer.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
#define SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
namespace SurgSim
{
namespace Framework
{
template <typename T>
class LockedContainer
{
public:
LockedContainer() :
m_buffer(),
m_haveNewData(false)
{
}
explicit LockedContainer(const T& initialValue) :
m_buffer(initialValue),
m_haveNewData(false)
{
}
explicit LockedContainer(T&& initialValue) :
m_buffer(std::move(initialValue)),
m_haveNewData(false)
{
}
~LockedContainer()
{
}
void set(const T& value)
{
boost::lock_guard<boost::mutex> lock(m_mutex);
m_buffer = value;
m_haveNewData = true;
}
void set(T&& value)
{
boost::lock_guard<boost::mutex> lock(m_mutex);
m_buffer = std::move(value);
m_haveNewData = true;
}
void get(T* value) const
{
boost::lock_guard<boost::mutex> lock(m_mutex);
m_haveNewData = false;
*value = m_buffer;
}
void take(T* value)
{
boost::lock_guard<boost::mutex> lock(m_mutex);
m_haveNewData = false;
*value = std::move(m_buffer);
}
bool tryGetChanged(T* value) const
{
boost::lock_guard<boost::mutex> lock(m_mutex);
if (! m_haveNewData)
{
return false;
}
else
{
m_haveNewData = false;
*value = m_buffer;
return true;
}
}
bool tryTakeChanged(T* value)
{
boost::lock_guard<boost::mutex> lock(m_mutex);
if (! m_haveNewData)
{
return false;
}
else
{
m_haveNewData = false;
*value = std::move(m_buffer);
return true;
}
}
private:
LockedContainer(const LockedContainer&);
LockedContainer& operator=(const LockedContainer&);
T m_buffer;
mutable bool m_haveNewData;
mutable boost::mutex m_mutex;
};
}; // namespace Framework
}; // namespace SurgSim
#endif // SURGSIM_FRAMEWORK_LOCKEDCONTAINER_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::LockedContainer::~LockedContainer
~LockedContainer()
Destroy the container and the data it contains.
Definition: LockedContainer.h:89
SurgSim::Framework::LockedContainer::m_haveNewData
bool m_haveNewData
True if there data that has been written, but not yet pulled in by get, take, etc.
Definition: LockedContainer.h:193
std
STL namespace.
SurgSim::Framework::LockedContainer::tryGetChanged
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
SurgSim::Framework::LockedContainer::LockedContainer
LockedContainer(const T &initialValue)
Create the container and the data it contains.
Definition: LockedContainer.h:70
SurgSim::Framework::LockedContainer::LockedContainer
LockedContainer(T &&initialValue)
Create the container and the data it contains.
Definition: LockedContainer.h:81
SurgSim::Framework::LockedContainer::operator=
LockedContainer & operator=(const LockedContainer &)
Prevent assignment.
SurgSim::Framework::LockedContainer::m_mutex
boost::mutex m_mutex
Mutex for synchronization of set() and get() calls.
Definition: LockedContainer.h:196
SurgSim::Framework::LockedContainer::take
void take(T *value)
Move the data out of the container.
Definition: LockedContainer.h:129
SurgSim::Framework::LockedContainer
A simple thread-safe data container that can support multiple writers and readers.
Definition: LockedContainer.h:54
SurgSim::Framework::LockedContainer::m_buffer
T m_buffer
Internal buffer.
Definition: LockedContainer.h:190
SurgSim::Framework::LockedContainer::LockedContainer
LockedContainer()
Create the container and the data it contains.
Definition: LockedContainer.h:60
SurgSim::Framework::LockedContainer::tryTakeChanged
bool tryTakeChanged(T *value)
Move the data out of the container if it has been modified since the last access. ...
Definition: LockedContainer.h:167