NamedVariantData-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_NAMEDVARIANTDATA_INL_H
17 #define SURGSIM_DATASTRUCTURES_NAMEDVARIANTDATA_INL_H
18 
20 
21 namespace SurgSim
22 {
23 namespace DataStructures
24 {
25 
27 {
28 }
29 
31  NamedData<boost::any>(namedData)
32 {
33 }
34 
35 template <typename T>
36 inline bool NamedVariantData::hasTypedData(int index) const
37 {
38  if (! hasData(index))
39  {
40  return false;
41  }
42 
43  boost::any a;
44  if (! NamedData::get(index, &a))
45  {
46  return false;
47  }
48 
49  if (a.empty())
50  {
51  return false;
52  }
53 
54  return (a.type() == typeid(T));
55 }
56 
57 template <typename T>
58 inline bool NamedVariantData::hasTypedData(const std::string& name) const
59 {
60  if (! hasData(name))
61  {
62  return false;
63  }
64  int index = getIndex(name);
65  return hasTypedData<T>(index);
66 }
67 
68 template <typename T>
69 inline bool NamedVariantData::get(int index, T* value) const
70 {
71  boost::any a;
72  if (!NamedData::get(index, &a))
73  return false;
74  try
75  {
76  *value = boost::any_cast<T>(a);
77  }
78  catch(const boost::bad_any_cast &)
79  {
80  SURGSIM_FAILURE() << "Cannot cast the named value to the specified type.";
81  }
82  return true;
83 }
84 
85 template <typename T>
86 inline bool NamedVariantData::get(const std::string& name, T* value) const
87 {
88  int index = getIndex(name);
89  return get(index, value);
90 }
91 
92 
93 }; // namespace DataStructures
94 }; // namespace SurgSim
95 
96 #endif // SURGSIM_DATASTRUCTURES_NAMEDVARIANTDATA_INL_H
Definition: DriveElementFromInputBehavior.cpp:27
NamedVariantData()
Definition: NamedVariantData-inl.h:26
bool hasData(int index) const
Check whether the entry with the specified index contains valid data.
bool hasTypedData(int index) const
Check whether the entry with the specified index contains valid data.
Definition: NamedVariantData-inl.h:36
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
The header that provides the assertion API.
bool get(int index, T *value) const
Given an index, get the corresponding value.
Definition: NamedData-inl.h:194
bool get(int index, T *value) const
Given an index, get the corresponding value.
Definition: NamedVariantData-inl.h:69