Fixed InProcClientWrapper's option parameter to OCDoResource
[platform/upstream/iotivity.git] / resource / include / OCUtilities.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef _INTEL_OCUTILITIES_H_
22 #define _INTEL_OCUTILITIES_H_
23
24 #include <map>
25 #include <vector>
26 #include <memory>
27 #include <utility>
28 #include <exception>
29
30 #include <OCException.h>
31 #include <StringConstants.h>
32
33 namespace OC {
34     namespace Utilities {
35
36         typedef std::map<std::string, std::string> QueryParamsKeyVal;
37         /*
38          * @brief Helper function to get query parameter from a URI
39          * @remarks      Its okay to return a copy of the container.\
40          *               The size is not expected to be huge.
41          * @remarks      Temporary: The URI must strictly have\
42          *               coap as the protocol in the fully qualified URI\
43          *               e.g., coap://1.2.3.4:5657/foo?bar=0)
44          * @remarks      If a separate class for URI parser is needed,\
45          *               please talk to Erich Keane.
46          * @todo         If more URI elements need to be parsed,\
47          *               please move the common parsing logic to a
48          *               different function
49          */
50         QueryParamsKeyVal getQueryParams(const std::string& uri);
51
52     }
53 }
54
55 /* The C++11 standard unfortunately forgot to provide make_unique<>! However, if we're
56 using C++14 or later, we want to take the standard library's implementation: */
57 namespace OC {
58 #if defined(__cplusplus) && __cplusplus < 201300
59
60     template<typename T, typename ...XS>
61     std::unique_ptr<T> make_unique(XS&& ...xs)
62     {
63         return std::unique_ptr<T>(new T(std::forward<XS>(xs)...));
64     }
65
66 #else
67     using std::make_unique;
68 #endif
69 } // namespace OC
70
71 namespace OC {
72
73     /* Examine an OCStackResult, and either forward its value or raise an exception: */
74     OCStackResult result_guard(const OCStackResult r);
75
76     /* Check for a nullptr, and throw an exception if we see one; otherwise, return the
77     result of the function call: */
78     template <typename PtrT, typename FnT, typename ...ParamTs>
79     auto nil_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
80     {
81         if(nullptr == p)
82         {
83             throw OCException(OC::Exception::NIL_GUARD_NULL, OC_STACK_INVALID_PARAM);
84         }
85
86         // Note that although parameters are being forwarded, std::bind() will make a single copy:
87         return std::bind(fn, p, std::forward<ParamTs>(params)...)();
88     }
89
90     /* Check for nullptr and forward the result of an OC function call on success; raise
91     an exception on failure or exceptional result: */
92     template <typename PtrT, typename FnT, typename ...ParamTs>
93     auto checked_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
94     {
95         return result_guard(nil_guard(p, fn, std::forward<ParamTs>(params)...));
96     }
97
98 } // namespace OC
99
100 namespace OC
101 {
102     template<typename T, typename = void>
103     struct is_vector
104     {
105         constexpr static bool value = false;
106     };
107
108     template<typename T>
109     struct is_vector<T,
110         typename std::enable_if<
111             std::is_same<T, std::vector<typename T::value_type, typename T::allocator_type>>::value
112         >::type
113     >
114     {
115         constexpr static bool value = true;
116     };
117 } // namespace OC
118
119 #endif