opensurgsim
Accessible-inl.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 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_FRAMEWORK_ACCESSIBLE_INL_H
17 #define SURGSIM_FRAMEWORK_ACCESSIBLE_INL_H
18 
20 
21 template <class T>
22 bool SurgSim::Framework::Accessible::getValue(const std::string& name, T* value) const
23 {
24  bool result = false;
25  auto functors = m_functors.find(name);
26  if (value != nullptr && functors != m_functors.end() && functors->second.getter != nullptr)
27  {
28  try
29  {
30  *value = boost::any_cast<T>(functors->second.getter());
31  result = true;
32  }
33  catch (boost::bad_any_cast&)
34  {
35 
36  }
37  }
38  return result;
39 }
40 
41 template <class T>
42 T SurgSim::Framework::Accessible::getValue(const std::string& name) const
43 {
44  T result;
45  try
46  {
47  result = boost::any_cast<T>(getValue(name));
48  }
49  catch (const boost::bad_any_cast& exception)
50  {
51  SURGSIM_FAILURE() << "Failure to cast to the given type. <" << exception.what() << ">";
52  return T();
53  }
54  return result;
55 }
56 
57 template <class T>
58 T SurgSim::Framework::convert(boost::any val)
59 {
60  return boost::any_cast<T>(val);
61 }
62 
63 #endif
#define SURGSIM_FAILURE()
Report that something very bad has happened and abort program execution.
Definition: Assert.h:95
The header that provides the assertion API.
T getValue(const std::string &name) const
Retrieves the value with the name by executing the getter if it is found and tries to convert it to t...
Definition: Accessible-inl.h:42