FrameworkConvert-inl.h
Go to the documentation of this file.
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_FRAMEWORKCONVERT_INL_H
17 #define SURGSIM_FRAMEWORK_FRAMEWORKCONVERT_INL_H
18 
19 #include <type_traits>
21 
22 // Use of enable_if. this function is only created if 'T' is a subclass of SurgSim::Framework::Component
23 // Which means for each subclass that is being deserialized, a new converter function is created
24 template <class T>
25 YAML::Node YAML::convert<std::shared_ptr<T>>::encode(
26  const typename std::enable_if <std::is_base_of <SurgSim::Framework::Component, T>::value,
27  std::shared_ptr<T> >::type rhs)
28 {
29  return YAML::convert<std::shared_ptr<SurgSim::Framework::Component>>::encode(rhs);
30 }
31 
32 template <class T>
33 bool YAML::convert<std::shared_ptr<T>>::decode(const Node& node,
34  typename std::enable_if <std::is_base_of<SurgSim::Framework::Component, T>::value,
35  std::shared_ptr<T> >::type& rhs) //NOLINT
36 {
37  std::shared_ptr<SurgSim::Framework::Component> temporary;
38  bool success = YAML::convert<std::shared_ptr<SurgSim::Framework::Component>>::decode(node, temporary);
39  if (success)
40  {
41  rhs = std::dynamic_pointer_cast<T>(temporary);
42  SURGSIM_ASSERT(rhs != nullptr) << "Failure to convert to target type in " << __FUNCTION__;
43  }
44  return success;
45 }
46 
47 
48 #endif
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Assert.h
The header that provides the assertion API.