NamedData-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2012-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_NAMEDDATA_INL_H
17 #define SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H
18 
19 #include <type_traits>
20 
23 
24 namespace SurgSim
25 {
26 namespace DataStructures
27 {
28 
29 template <typename T>
31 {
32 }
33 
34 template <typename T>
35 inline NamedData<T>::NamedData(std::shared_ptr<const IndexDirectory> directory) :
36  m_directory(directory)
37 {
39  m_data.resize(m_directory->getNumEntries());
40  m_isDataValid.resize(m_directory->getNumEntries(), false);
41 }
42 
43 template <typename T>
44 inline NamedData<T>::NamedData(const std::vector<std::string>& names) :
45  m_directory(std::make_shared<const IndexDirectory>(names))
46 {
48  m_data.resize(m_directory->getNumEntries());
49  m_isDataValid.resize(m_directory->getNumEntries(), false);
50 }
51 
52 template <typename T>
53 inline NamedData<T>::NamedData(const NamedData& namedData) :
54  m_directory(namedData.m_directory),
55  m_data(namedData.m_data),
56  m_isDataValid(namedData.m_isDataValid)
57 {
59 }
60 
61 template <typename T>
63 {
64  SURGSIM_ASSERT(namedData.isValid()) <<
65  "Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
66 
67  if (!isValid())
68  {
69  m_directory = namedData.m_directory;
70  }
71  else
72  {
73  SURGSIM_ASSERT(m_directory == namedData.m_directory) << "Incompatible NamedData contents in assignment!";
74  }
75 
76  m_data = namedData.m_data;
77  m_isDataValid = namedData.m_isDataValid;
78 
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!";
82 
83  return *this;
84 }
85 
86 template <typename T>
87 inline NamedData<T>::NamedData(NamedData&& namedData) :
88  m_directory(std::move(namedData.m_directory)),
89  m_data(std::move(namedData.m_data)),
90  m_isDataValid(std::move(namedData.m_isDataValid))
91 {
93 }
94 
95 template <typename T>
97 {
98  SURGSIM_ASSERT(namedData.isValid()) <<
99  "Cannot use an invalid (empty) NamedData on the right-hand side of an assignment!";
100 
101  if (!isValid())
102  {
103  m_directory = std::move(namedData.m_directory);
104  }
105  else
106  {
107  SURGSIM_ASSERT(m_directory == namedData.m_directory) << "Incompatible NamedData contents in assignment!";
108  }
109 
110  m_data = std::move(namedData.m_data);
111  m_isDataValid = std::move(namedData.m_isDataValid);
112 
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!";
116 
117  return *this;
118 }
119 
120 template <typename T>
121 inline bool NamedData<T>::isValid() const
122 {
123  return static_cast<bool>(m_directory);
124 }
125 
126 template <typename T>
127 inline std::shared_ptr<const IndexDirectory> NamedData<T>::getDirectory() const
128 {
129  return m_directory;
130 }
131 
132 template <typename T>
133 inline int NamedData<T>::getIndex(const std::string& name) const
134 {
135  if (! isValid())
136  {
137  return -1;
138  }
139  return m_directory->getIndex(name);
140 }
141 
142 template <typename T>
143 inline std::string NamedData<T>::getName(int index) const
144 {
145  if (! isValid())
146  {
147  return "";
148  }
149  return m_directory->getName(index);
150 }
151 
152 template <typename T>
153 inline bool NamedData<T>::hasEntry(int index) const
154 {
155  return ((index >= 0) && (index < static_cast<int>(m_data.size())));
156 }
157 
158 template <typename T>
159 inline bool NamedData<T>::hasEntry(const std::string& name) const
160 {
161  if (! isValid())
162  {
163  return false;
164  }
165  return m_directory->hasEntry(name);
166 }
167 
168 template <typename T>
169 inline bool NamedData<T>::hasData(int index) const
170 {
171  return hasEntry(index) && m_isDataValid[index];
172 }
173 
174 template <typename T>
175 inline bool NamedData<T>::hasData(const std::string& name) const
176 {
177  int index = getIndex(name);
178  if (index < 0)
179  {
180  return false;
181  }
182  else
183  {
184  SURGSIM_ASSERT(hasEntry(index));
185  return m_isDataValid[index];
186  }
187 }
188 
189 template <typename T>
190 inline bool NamedData<T>::get(int index, T* value) const
191 {
192  if (! hasData(index))
193  {
194  return false;
195  }
196  else
197  {
198  *value = m_data[index];
199  return true;
200  }
201 }
202 
203 template <typename T>
204 inline bool NamedData<T>::get(const std::string& name, T* value) const
205 {
206  int index = getIndex(name);
207  if ((index < 0) || ! m_isDataValid[index])
208  {
209  return false;
210  }
211  else
212  {
213  SURGSIM_ASSERT(hasEntry(index));
214  *value = m_data[index];
215  return true;
216  }
217 }
218 
219 template <typename T>
220 inline bool NamedData<T>::set(int index, const T& value)
221 {
222  if (! hasEntry(index))
223  {
224  return false;
225  }
226  else
227  {
228  m_data[index] = value;
229  m_isDataValid[index] = true;
230  return true;
231  }
232 }
233 
234 template <typename T>
235 inline bool NamedData<T>::set(int index, T&& value)
236 {
237  if (! hasEntry(index))
238  {
239  return false;
240  }
241  else
242  {
243  m_data[index] = std::move(value);
244  m_isDataValid[index] = true;
245  return true;
246  }
247 }
248 
249 template <typename T>
250 inline bool NamedData<T>::set(const std::string& name, const T& value)
251 {
252  int index = getIndex(name);
253  if (index < 0)
254  {
255  return false;
256  }
257  else
258  {
259  SURGSIM_ASSERT(set(index, value) == true)
260  << "The directory returned an index larger than the number of entries in the stored data.";
261  return true;
262  }
263 }
264 
265 template <typename T>
266 inline bool NamedData<T>::set(const std::string& name, T&& value)
267 {
268  int index = getIndex(name);
269  if (index < 0)
270  {
271  return false;
272  }
273  else
274  {
275  SURGSIM_ASSERT(set(index, std::move(value)) == true)
276  << "The directory returned an index larger than the number of entries in the stored data.";
277  return true;
278  }
279 }
280 
281 template <typename T>
282 inline bool NamedData<T>::reset(int index)
283 {
284  if (! hasEntry(index))
285  {
286  return false;
287  }
288  else
289  {
290  m_isDataValid[index] = false;
291  return true;
292  }
293 }
294 
295 template <typename T>
296 inline bool NamedData<T>::reset(const std::string& name)
297 {
298  int index = getIndex(name);
299  if (index < 0)
300  {
301  return false;
302  }
303  else
304  {
305  SURGSIM_ASSERT(reset(index) == true)
306  << "The directory returned an index larger than the number of entries in the stored data.";
307  return true;
308  }
309 }
310 
311 template <typename T>
313 {
314  m_isDataValid.assign(m_data.size(), false);
315 }
316 
317 template <typename T>
318 inline size_t NamedData<T>::size() const
319 {
320  return m_data.size();
321 }
322 
323 template <typename T>
324 inline int NamedData<T>::getNumEntries() const
325 {
326  return static_cast<int>(m_data.size());
327 }
328 
329 template <typename T>
330 template <typename N>
331 inline void NamedData<T>::copy(const NamedData<N>& source, const NamedDataCopyMap& map)
332 {
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)
335  {
336  T value;
337  if (source.get(it->first, &value))
338  {
339  set(it->second, value);
340  }
341  else
342  {
343  reset(it->second);
344  }
345  }
346 }
347 
348 template <typename T>
350 {
351  if (*index < 0)
352  {
353  *index = getIndex(name);
354  }
355 }
356 
357 }; // namespace Input
358 }; // namespace SurgSim
359 
360 #endif // SURGSIM_DATASTRUCTURES_NAMEDDATA_INL_H
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
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
SurgSim::DataStructures::IndexDirectory
A simple bidirectional mapping between names (strings) and distinct consecutive non-negative indices.
Definition: IndexDirectory.h:32
NamedData.h
SurgSim::DataStructures::NamedData::size
size_t size() const
Check the number of existing entries.
Definition: NamedData-inl.h:318
Assert.h
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Math::isValid
bool isValid(float value)
Check if a float value is valid.
Definition: Valid-inl.h:98
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::NamedData
NamedData()
Create an empty object, with no associated names and indices yet.
Definition: NamedData-inl.h:30
set
set(SURGSIM_COLLISION_SOURCES BoxCapsuleContact.cpp BoxDoubleSidedPlaneContact.cpp BoxPlaneContact.cpp BoxSphereContact.cpp CapsuleSphereContact.cpp CollisionPair.cpp CompoundShapeContact.cpp ContactCalculation.cpp ContactFilter.cpp DefaultContactCalculation.cpp ElementContactFilter.cpp OctreeCapsuleContact.cpp OctreeContact.cpp OctreeDoubleSidedPlaneContact.cpp OctreePlaneContact.cpp OctreeSphereContact.cpp Representation.cpp SegmentMeshTriangleMeshContact.cpp SegmentSegmentCcdIntervalCheck.cpp SegmentSegmentCcdMovingContact.cpp SegmentSegmentCcdStaticContact.cpp SegmentSelfContact.cpp ShapeCollisionRepresentation.cpp SphereDoubleSidedPlaneContact.cpp SpherePlaneContact.cpp SphereSphereContact.cpp TriangleMeshParticlesContact.cpp TriangleMeshPlaneContact.cpp TriangleMeshSurfaceMeshContact.cpp TriangleMeshTriangleMeshContact.cpp) set(SURGSIM_COLLISION_HEADERS BoxCapsuleContact.h BoxDoubleSidedPlaneContact.h BoxPlaneContact.h BoxSphereContact.h CapsuleSphereContact.h CcdDcdCollision.h CollisionPair.h CompoundShapeContact.h ContactCalculation.h ContactFilter.h DefaultContactCalculation.h ElementContactFilter.h OctreeCapsuleContact.h OctreeContact.h OctreeDoubleSidedPlaneContact.h OctreePlaneContact.h OctreeSphereContact.h Representation.h SegmentMeshTriangleMeshContact.h SegmentSegmentCcdIntervalCheck.h SegmentSegmentCcdMovingContact.h SegmentSegmentCcdStaticContact.h SegmentSelfContact.h ShapeCollisionRepresentation.h ShapeShapeContactCalculation.h SphereDoubleSidedPlaneContact.h SpherePlaneContact.h SphereSphereContact.h TriangleMeshParticlesContact.h TriangleMeshPlaneContact.h TriangleMeshSurfaceMeshContact.h TriangleMeshTriangleMeshContact.h) surgsim_create_library_header(Collision.h "$
Definition: CMakeLists.txt:16
SurgSim::DataStructures::NamedData::m_isDataValid
std::vector< bool > m_isDataValid
The array storing whether the data is currently valid.
Definition: NamedData.h:320
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_directory
std::shared_ptr< const IndexDirectory > m_directory
The mapping between names and indices.
Definition: NamedData.h:314
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::m_data
std::vector< T > m_data
The array of values.
Definition: NamedData.h:317
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::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....
Definition: NamedData-inl.h:121
SurgSim::DataStructures::NamedData::cacheIndex
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
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38