NamedData-inl.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2012-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_DATASTRUCTURES_NAMEDDATA_INL_H
#define SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H
#include <type_traits>
#include "SurgSim/DataStructures/NamedData.h"
#include "SurgSim/Framework/Assert.h"
namespace SurgSim
{
namespace DataStructures
{
template <typename T>
inline NamedData<T>::NamedData()
{
}
template <typename T>
inline NamedData<T>::NamedData(std::shared_ptr<const IndexDirectory> directory) :
m_directory(directory)
{
SURGSIM_ASSERT(isValid());
m_data.resize(m_directory->getNumEntries());
m_isDataValid.resize(m_directory->getNumEntries(), false);
}
template <typename T>
inline NamedData<T>::NamedData(const std::vector<std::string>& names) :
m_directory(std::make_shared<const IndexDirectory>(names))
{
SURGSIM_ASSERT(isValid());
m_data.resize(m_directory->getNumEntries());
m_isDataValid.resize(m_directory->getNumEntries(), false);
}
template <typename T>
inline NamedData<T>::NamedData(const NamedData& namedData) :
m_directory(namedData.m_directory),
m_data(namedData.m_data),
m_isDataValid(namedData.m_isDataValid)
{
SURGSIM_ASSERT(isValid());
}
template <typename T>
inline NamedData<T>& NamedData<T>::operator=(const NamedData& namedData)
{
SURGSIM_ASSERT(namedData.isValid()) <<
"Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
if (!isValid())
{
m_directory = namedData.m_directory;
}
else
{
SURGSIM_ASSERT(m_directory == namedData.m_directory) << "Incompatible NamedData contents in assignment!";
}
m_data = namedData.m_data;
m_isDataValid = namedData.m_isDataValid;
SURGSIM_ASSERT(isValid()) << "NamedData is not valid after assignment!";
SURGSIM_ASSERT(m_data.size() == m_directory->size() && m_isDataValid.size() == m_directory->size()) <<
"NamedData is not correctly sized after assignment!";
return *this;
}
template <typename T>
inline NamedData<T>::NamedData(NamedData&& namedData) :
m_directory(std::move(namedData.m_directory)),
m_data(std::move(namedData.m_data)),
m_isDataValid(std::move(namedData.m_isDataValid))
{
SURGSIM_ASSERT(isValid());
}
template <typename T>
inline NamedData<T>& NamedData<T>::operator=(NamedData&& namedData)
{
SURGSIM_ASSERT(namedData.isValid()) <<
"Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
if (!isValid())
{
m_directory = std::move(namedData.m_directory);
}
else
{
SURGSIM_ASSERT(m_directory == namedData.m_directory) << "Incompatible NamedData contents in assignment!";
}
m_data = std::move(namedData.m_data);
m_isDataValid = std::move(namedData.m_isDataValid);
SURGSIM_ASSERT(isValid()) << "NamedData is not valid after assignment!";
SURGSIM_ASSERT(m_data.size() == m_directory->size() && m_isDataValid.size() == m_directory->size()) <<
"NamedData is not correctly sized after assignment!";
return *this;
}
template <typename T>
inline bool NamedData<T>::isValid() const
{
return static_cast<bool>(m_directory);
}
template <typename T>
inline std::shared_ptr<const IndexDirectory> NamedData<T>::getDirectory() const
{
return m_directory;
}
template <typename T>
inline int NamedData<T>::getIndex(const std::string& name) const
{
if (! isValid())
{
return -1;
}
return m_directory->getIndex(name);
}
template <typename T>
inline std::string NamedData<T>::getName(int index) const
{
if (! isValid())
{
return "";
}
return m_directory->getName(index);
}
template <typename T>
inline bool NamedData<T>::hasEntry(int index) const
{
return ((index >= 0) && (index < static_cast<int>(m_data.size())));
}
template <typename T>
inline bool NamedData<T>::hasEntry(const std::string& name) const
{
if (! isValid())
{
return false;
}
return m_directory->hasEntry(name);
}
template <typename T>
inline bool NamedData<T>::hasData(int index) const
{
return hasEntry(index) && m_isDataValid[index];
}
template <typename T>
inline bool NamedData<T>::hasData(const std::string& name) const
{
int index = getIndex(name);
if (index < 0)
{
return false;
}
else
{
SURGSIM_ASSERT(hasEntry(index));
return m_isDataValid[index];
}
}
template <typename T>
inline bool NamedData<T>::get(int index, T* value) const
{
if (! hasData(index))
{
return false;
}
else
{
*value = m_data[index];
return true;
}
}
template <typename T>
inline bool NamedData<T>::get(const std::string& name, T* value) const
{
int index = getIndex(name);
if ((index < 0) || ! m_isDataValid[index])
{
return false;
}
else
{
SURGSIM_ASSERT(hasEntry(index));
*value = m_data[index];
return true;
}
}
template <typename T>
inline bool NamedData<T>::set(int index, const T& value)
{
if (! hasEntry(index))
{
return false;
}
else
{
m_data[index] = value;
m_isDataValid[index] = true;
return true;
}
}
template <typename T>
inline bool NamedData<T>::set(int index, T&& value)
{
if (! hasEntry(index))
{
return false;
}
else
{
m_data[index] = std::move(value);
m_isDataValid[index] = true;
return true;
}
}
template <typename T>
inline bool NamedData<T>::set(const std::string& name, const T& value)
{
int index = getIndex(name);
if (index < 0)
{
return false;
}
else
{
SURGSIM_ASSERT(set(index, value) == true)
<< "The directory returned an index larger than the number of entries in the stored data.";
return true;
}
}
template <typename T>
inline bool NamedData<T>::set(const std::string& name, T&& value)
{
int index = getIndex(name);
if (index < 0)
{
return false;
}
else
{
SURGSIM_ASSERT(set(index, std::move(value)) == true)
<< "The directory returned an index larger than the number of entries in the stored data.";
return true;
}
}
template <typename T>
inline bool NamedData<T>::reset(int index)
{
if (! hasEntry(index))
{
return false;
}
else
{
m_isDataValid[index] = false;
return true;
}
}
template <typename T>
inline bool NamedData<T>::reset(const std::string& name)
{
int index = getIndex(name);
if (index < 0)
{
return false;
}
else
{
SURGSIM_ASSERT(reset(index) == true)
<< "The directory returned an index larger than the number of entries in the stored data.";
return true;
}
}
template <typename T>
inline void NamedData<T>::resetAll()
{
m_isDataValid.assign(m_data.size(), false);
}
template <typename T>
inline size_t NamedData<T>::size() const
{
return m_data.size();
}
template <typename T>
inline int NamedData<T>::getNumEntries() const
{
return static_cast<int>(m_data.size());
}
template <typename T>
template <typename N>
inline void NamedData<T>::copy(const NamedData<N>& source, const NamedDataCopyMap& map)
{
static_assert(std::is_same<T, N>::value, "NamedData<T>::copy can only copy from another NamedData<T>.");
for (auto it = map.cbegin(); it != map.cend(); ++it)
{
T value;
if (source.get(it->first, &value))
{
set(it->second, value);
}
else
{
reset(it->second);
}
}
}
template <typename T>
void SurgSim::DataStructures::NamedData<T>::cacheIndex(const std::string& name, int* index) const
{
if (*index < 0)
{
*index = getIndex(name);
}
}
}; // namespace Input
}; // namespace SurgSim
#endif // SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::NamedData::cacheIndex
void cacheIndex(const std::string &name, int *index) const
Caches an entry&#39;s index if it is not already cached.
Definition: NamedData-inl.h:349
SurgSim::DataStructures::NamedData::size
size_t size() const
Check the number of existing entries.
Definition: NamedData-inl.h:318
SurgSim::DataStructures::NamedData
A templated dictionary in which data can be accessed by name or index, with immutable names & indices...
Definition: NamedData.h:102
SurgSim::DataStructures::NamedData::m_data
std::vector< T > m_data
The array of values.
Definition: NamedData.h:317
std
STL namespace.
NamedData.h
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::DataStructures::NamedDataCopyMap
std::unordered_map< int, int > NamedDataCopyMap
The type used for copying values between two NamedData objects that cannot assign to each other...
Definition: NamedData.h:32
SurgSim::DataStructures::NamedData::m_isDataValid
std::vector< bool > m_isDataValid
The array storing whether the data is currently valid.
Definition: NamedData.h:320
SurgSim::Math::isValid
bool isValid(float value)
Check if a float value is valid.
Definition: Valid-inl.h:98
SurgSim::DataStructures::NamedData::get
bool get(int index, T *value) const
Given an index, get the corresponding value.
Definition: NamedData-inl.h:190
SurgSim::DataStructures::IndexDirectory
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices...
Definition: IndexDirectory.h:32
SurgSim::DataStructures::NamedData::getIndex
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: NamedData-inl.h:133
Assert.h
The header that provides the assertion API.
SurgSim::DataStructures::NamedData::m_directory
std::shared_ptr< const IndexDirectory > m_directory
The mapping between names and indices.
Definition: NamedData.h:314
SurgSim::DataStructures::NamedData::NamedData
NamedData()
Create an empty object, with no associated names and indices yet.
Definition: NamedData-inl.h:30
SurgSim::DataStructures::NamedData::hasEntry
bool hasEntry(int index) const
Check whether the object contains an entry with the specified index.
Definition: NamedData-inl.h:153
SurgSim::DataStructures::NamedData::getName
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: NamedData-inl.h:143
SurgSim::DataStructures::NamedData::isValid
bool isValid() const
Check if the object has been initialized, which means it has a set of entries (i.e., names, indices, and the map between them).
Definition: NamedData-inl.h:121