16 #ifndef SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H 17 #define SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H 19 #include <type_traits> 21 #include "SurgSim/DataStructures/NamedData.h" 26 namespace DataStructures
36 m_directory(directory)
39 m_data.resize(m_directory->getNumEntries());
40 m_isDataValid.resize(m_directory->getNumEntries(),
false);
48 m_data.resize(m_directory->getNumEntries());
49 m_isDataValid.resize(m_directory->getNumEntries(),
false);
54 m_directory(namedData.m_directory),
55 m_data(namedData.m_data),
56 m_isDataValid(namedData.m_isDataValid)
65 "Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
69 m_directory = namedData.m_directory;
73 SURGSIM_ASSERT(m_directory == namedData.m_directory) <<
"Incompatible NamedData contents in assignment!";
76 m_data = namedData.m_data;
77 m_isDataValid = namedData.m_isDataValid;
79 SURGSIM_ASSERT(isValid()) <<
"NamedData is not valid after assignment!";
80 SURGSIM_ASSERT(m_data.size() == m_directory->size() && m_isDataValid.size() == m_directory->size()) <<
81 "NamedData is not correctly sized after assignment!";
88 m_directory(
std::move(namedData.m_directory)),
89 m_data(
std::move(namedData.m_data)),
90 m_isDataValid(
std::move(namedData.m_isDataValid))
99 "Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
103 m_directory = std::move(namedData.m_directory);
107 SURGSIM_ASSERT(m_directory == namedData.m_directory) <<
"Incompatible NamedData contents in assignment!";
110 m_data = std::move(namedData.m_data);
111 m_isDataValid = std::move(namedData.m_isDataValid);
113 SURGSIM_ASSERT(isValid()) <<
"NamedData is not valid after assignment!";
114 SURGSIM_ASSERT(m_data.size() == m_directory->size() && m_isDataValid.size() == m_directory->size()) <<
115 "NamedData is not correctly sized after assignment!";
120 template <
typename T>
123 return static_cast<bool>(m_directory);
126 template <
typename T>
132 template <
typename T>
142 template <
typename T>
149 return m_directory->
getName(index);
152 template <
typename T>
155 return ((index >= 0) && (index < static_cast<int>(m_data.size())));
158 template <
typename T>
168 template <
typename T>
171 return hasEntry(index) && m_isDataValid[index];
174 template <
typename T>
177 int index = getIndex(name);
185 return m_isDataValid[index];
189 template <
typename T>
192 if (! hasData(index))
198 *value = m_data[index];
203 template <
typename T>
206 int index = getIndex(name);
207 if ((index < 0) || ! m_isDataValid[index])
214 *value = m_data[index];
219 template <
typename T>
222 if (! hasEntry(index))
228 m_data[index] = value;
229 m_isDataValid[index] =
true;
234 template <
typename T>
237 if (! hasEntry(index))
243 m_data[index] = std::move(value);
244 m_isDataValid[index] =
true;
249 template <
typename T>
252 int index = getIndex(name);
260 <<
"The directory returned an index larger than the number of entries in the stored data.";
265 template <
typename T>
268 int index = getIndex(name);
276 <<
"The directory returned an index larger than the number of entries in the stored data.";
281 template <
typename T>
284 if (! hasEntry(index))
290 m_isDataValid[index] =
false;
295 template <
typename T>
298 int index = getIndex(name);
306 <<
"The directory returned an index larger than the number of entries in the stored data.";
311 template <
typename T>
314 m_isDataValid.assign(m_data.size(),
false);
317 template <
typename T>
320 return m_data.
size();
323 template <
typename T>
326 return static_cast<int>(m_data.size());
329 template <
typename T>
330 template <
typename N>
333 static_assert(std::is_same<T, N>::value,
"NamedData<T>::copy can only copy from another NamedData<T>.");
334 for (
auto it = map.cbegin(); it != map.cend(); ++it)
337 if (source.
get(it->first, &value))
339 set(it->second, value);
348 template <
typename T>
353 *index = getIndex(name);
360 #endif // SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
void cacheIndex(const std::string &name, int *index) const
Caches an entry's index if it is not already cached.
Definition: NamedData-inl.h:349
size_t size() const
Check the number of existing entries.
Definition: NamedData-inl.h:318
A templated dictionary in which data can be accessed by name or index, with immutable names & indices...
Definition: NamedData.h:102
Definition: MockObjects.h:47
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
bool get(int index, T *value) const
Given an index, get the corresponding value.
Definition: NamedData-inl.h:190
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices...
Definition: IndexDirectory.h:32
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: NamedData-inl.h:133
The header that provides the assertion API.
NamedData()
Create an empty object, with no associated names and indices yet.
Definition: NamedData-inl.h:30
bool hasEntry(int index) const
Check whether the object contains an entry with the specified index.
Definition: NamedData-inl.h:153
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: NamedData-inl.h:143
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