1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef COMMON_INTERNAL_ASSERTUTILS_H
22 #define COMMON_INTERNAL_ASSERTUTILS_H
29 #include <OCException.h>
31 #include <RCSException.h>
39 struct NotOCStackResult;
41 template <typename FUNC, typename ...PARAMS>
44 typedef decltype(std::declval<FUNC>()(std::declval<PARAMS>()...)) type;
47 template< typename A, typename B, typename ENABLER = void >
48 struct EnableIfTypeIs;
50 template< typename A >
51 struct EnableIfTypeIs< A, OCStackResult,
52 typename std::enable_if< std::is_same< A, OCStackResult >::value >::type >
57 template< typename A >
58 struct EnableIfTypeIs< A, NotOCStackResult,
59 typename std::enable_if< !std::is_same< A, OCStackResult >::value >::type >
64 template< typename T, typename = typename std::enable_if<
65 std::is_class<T>::value && std::is_pointer<T>::value>::type >
66 struct EnableIfClassPointer
71 template< typename T, typename = typename std::enable_if< std::is_class< T >::value > >
78 inline void expectOCStackResult(OCStackResult actual,
79 std::initializer_list<OCStackResult> allowed)
81 for (auto r : allowed)
83 if (actual == r) return;
86 throw RCSPlatformException(actual);
89 inline void expectOCStackResult(OCStackResult actual, OCStackResult expected)
91 if (actual != expected)
93 throw RCSPlatformException(actual);
97 inline void expectOCStackResultOK(OCStackResult actual)
99 expectOCStackResult(actual, OC_STACK_OK);
102 template< typename FUNC, typename ...PARAMS >
103 typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
104 OCStackResult >::type
105 invokeOCFuncWithResultExpect(std::initializer_list<OCStackResult> allowed,
106 FUNC&& fn, PARAMS&& ...params)
110 expectOCStackResult(fn(std::forward< PARAMS >(params)...), std::move(allowed));
112 catch (const OC::OCException& e)
114 throw RCSPlatformException(e.code());
119 template< typename FUNC, typename ...PARAMS >
120 typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
121 OCStackResult >::type
122 invokeOCFunc(FUNC&& fn, PARAMS&& ...params)
126 expectOCStackResultOK(fn(std::forward< PARAMS >(params)...));
128 catch (const OC::OCException& e)
130 throw RCSPlatformException(e.code());
134 template< typename FUNC, typename ...PARAMS >
135 typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
136 Detail::NotOCStackResult >::type
137 invokeOCFunc(FUNC* fn, PARAMS&& ...params)
141 return fn(std::forward< PARAMS >(params)...);
143 catch (const OC::OCException& e)
145 throw RCSPlatformException(e.code());
149 template< typename OBJ, typename = typename Detail::EnableIfClassPointer<OBJ>::type,
150 typename FUNC, typename ...PARAMS >
151 inline auto invokeOC(OBJ&& obj, FUNC&& fn, PARAMS&& ...params) ->
152 typename Detail::EnableIfTypeIs<
153 decltype((obj->*fn)(std::forward< PARAMS >(params)...)), OCStackResult>::
158 expectOCStackResultOK(obj->*fn(std::forward< PARAMS >(params)...));
160 catch (const OC::OCException& e)
162 throw RCSPlatformException(e.code());
166 template< typename OBJ, typename = typename Detail::EnableIfClassPointer<OBJ>::type,
167 typename FUNC, typename ...PARAMS >
168 inline auto invokeOC(OBJ&& obj, FUNC&& fn, PARAMS&& ...params) ->
169 typename Detail::EnableIfTypeIs<
170 decltype((obj->*fn)(std::forward< PARAMS >(params)...)),
171 Detail::NotOCStackResult>::
176 obj->*fn(std::forward< PARAMS >(params)...);
178 catch (const OC::OCException& e)
180 throw RCSPlatformException(e.code());
184 template< typename OBJ, typename = typename Detail::EnableIfClass<OBJ>::type,
185 typename FUNC, typename ...PARAMS >
186 inline auto invokeOC(const std::shared_ptr< OBJ >& obj, FUNC&& fn, PARAMS&& ...params) ->
187 typename Detail::EnableIfTypeIs<
188 decltype((obj.get()->*fn)(std::forward< PARAMS >(params)...)), OCStackResult>::
193 expectOCStackResultOK((obj.get()->*fn)(std::forward< PARAMS >(params)...));
195 catch (const OC::OCException& e)
197 throw RCSPlatformException(e.code());
201 template< typename OBJ, typename = typename Detail::EnableIfClass< OBJ >::type,
202 typename FUNC, typename ...PARAMS >
203 inline auto invokeOC(const std::shared_ptr<OBJ>& obj, FUNC&& fn, PARAMS&& ...params) ->
204 typename Detail::EnableIfTypeIs<
205 decltype((obj.get()->*fn)(std::forward< PARAMS >(params)...)),
206 Detail::NotOCStackResult>::
211 return (obj.get()->*fn)(std::forward< PARAMS >(params)...);
213 catch (const OC::OCException& e)
215 throw RCSPlatformException(e.code());
222 #endif // COMMON_INTERNAL_ASSERTUTILS_H