ADTF
ucom_cast.h
Go to the documentation of this file.
1 
8 #ifndef _ADTF_UCOM_ANT_UCOM_CAST_INCLUDES_HEADER_
9 #define _ADTF_UCOM_ANT_UCOM_CAST_INCLUDES_HEADER_
10 
11 namespace adtf
12 {
13 namespace ucom
14 {
15 namespace ant
16 {
17 
18 class IObject; //forward declaration for ucom_cast_convert template
19 
21 namespace detail
22 {
28 template<typename QueriedInterfaceType, bool>
29 struct ucom_cast_to_implicit_cast
30 {
38  template<typename ObjectType>
39  static QueriedInterfaceType* perform_cast(ObjectType* i_pObject)
40  {
41  //provided for const correctness
42  typedef typename std::conditional<std::is_const<QueriedInterfaceType>::value,
43  const IObject,
44  IObject>::type interface_type;
45  typedef typename std::conditional<std::is_const<QueriedInterfaceType>::value,
46  const IObject*,
47  IObject*>::type base_pointer;
48  typedef typename std::conditional<std::is_const<QueriedInterfaceType>::value,
49  const void*,
50  void*>::type void_pointer;
51 
52  //perform the actual cast
53  if (nullptr != i_pObject)
54  { //the cast from the interface to void must be performed inside the GetInterface()
55  //method, because only there we have all necessary cast information!
56  QueriedInterfaceType* nRet = nullptr;
57  const char* const strIID = get_iid<QueriedInterfaceType*>();
58  base_pointer const pBase = ucom_cast_to_implicit_cast<interface_type, true>::perform_cast(i_pObject);
59  if ( pBase->GetInterface(strIID, reinterpret_cast<void_pointer&>(nRet)).IsOk() )
60  {
61  return nRet;
62  }
63  }
64 
65  return nullptr;
66  }
67 };//template<> struct<false> ucom_cast_to_implicit_cast
68 
73 template<typename QueriedInterfaceType>
74 struct ucom_cast_to_implicit_cast<QueriedInterfaceType, true>
75 {
76 public: //private methods
85  template<typename ObjectType>
86  static typename std::enable_if<detail::has_hierarchy_type<ObjectType>::value,
87  QueriedInterfaceType>::type*
88  perform_cast(ObjectType* i_pObject)
89  {
90  //resolve possible ambiguous calls (multiple inheritance)
91  return ucom_resolve<QueriedInterfaceType>(i_pObject);
92  }
93 
102  template<typename ObjectType>
103  static typename std::enable_if<!detail::has_hierarchy_type<ObjectType>::value,
104  QueriedInterfaceType>::type*
105  perform_cast(ObjectType* i_pObject)
106  {
107  return i_pObject;
108  }
109 };//template<> struct<true> ucom_cast_to_implicit_cast
110 
111 }//ns detail
113 
125 template<typename InterfacePointerType, typename ObjectPointerType>
126 InterfacePointerType ucom_cast(ObjectPointerType i_pObject)
127 {
128  typedef typename std::remove_pointer<ObjectPointerType>::type object_type;
129  typedef typename std::remove_pointer<InterfacePointerType>::type interface_type;
130  //make sure the cast is performed const correct
131 
132  static_assert(std::is_pointer<ObjectPointerType>::value,
133  "ucom_cast<> failed. \"i_pObject\" is not a pointer");
134  static_assert(
135  std::is_base_of<IObject, typename std::remove_const<object_type>::type>::value,
136  "ucom_cast<> failed. \"i_pObject\" does not point to a type derived from IObject");
137 
138  //if the interface_type is base of the object type, we can use an implicit cast
139  return detail::ucom_cast_to_implicit_cast
140  <
141  typename std::conditional
142  <
143  std::is_const<object_type>::value,
144  const interface_type,
145  interface_type
146  >::type,
147  std::is_base_of<interface_type, object_type>::value
148  >::perform_cast(i_pObject);
149 }//fn ucom_cast<>
150 
151 }//ns ant
152 
154 using ant::ucom_cast;
155 
156 }//ns ucom
157 }//ns adtf
158 
159 #endif //_ADTF_UCOM_ANT_UCOM_CAST_INCLUDES_HEADER_
Base class for every interface type within the uCOM.
Definition: object_intf.h:31
InterfacePointerType ucom_cast(ObjectPointerType i_pObject)
Used to cast arbitrary interface types within the UCOM.
Definition: ucom_cast.h:126
ant::IObject IObject
Alias always bringing the latest version of ant::IObject into scope.
Definition: object_intf.h:102
Namespace for entire ADTF SDK.