Imported Upstream version 1.0.0
[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         /*
39          * @brief helper function that parses the query parameters component
40          * of a URI into a key-value map.  This function expects the uri
41          * parameter to contain the query parameters component of a URI
42          * (everything after the '?', excluding anything anchors).
43          *
44          * Note that output will not perform URL decoding
45          */
46         QueryParamsKeyVal getQueryParams(const std::string& uri);
47     }
48 }
49
50 /* The C++11 standard unfortunately forgot to provide make_unique<>! However, if we're
51 using C++14 or later, we want to take the standard library's implementation: */
52 namespace OC {
53 #if defined(__cplusplus) && __cplusplus < 201300
54
55     template<typename T, typename ...XS>
56     std::unique_ptr<T> make_unique(XS&& ...xs)
57     {
58         return std::unique_ptr<T>(new T(std::forward<XS>(xs)...));
59     }
60
61 #else
62     using std::make_unique;
63 #endif
64 } // namespace OC
65
66 namespace OC {
67
68     /* Examine an OCStackResult, and either forward its value or raise an exception: */
69     OCStackResult result_guard(const OCStackResult r);
70
71     /* Check for a nullptr, and throw an exception if we see one; otherwise, return the
72     result of the function call: */
73     template <typename PtrT, typename FnT, typename ...ParamTs>
74     auto nil_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
75     {
76         if(nullptr == p)
77         {
78             throw OCException(OC::Exception::NIL_GUARD_NULL, OC_STACK_INVALID_PARAM);
79         }
80
81         // Note that the parameters are being passed by reference to std::bind. This is not an
82         // issue, as it is this function's parameters that are being passed by reference.  So,
83         // unless the parameters are being passed by reference to here (or to checked_guard),
84         // they won't be modified.
85         return std::bind(fn, p, std::ref(params)...)();
86     }
87
88     /* Check for nullptr and forward the result of an OC function call on success; raise
89     an exception on failure or exceptional result: */
90     template <typename PtrT, typename FnT, typename ...ParamTs>
91     auto checked_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
92     {
93         return result_guard(nil_guard(p, fn, std::forward<ParamTs>(params)...));
94     }
95
96 } // namespace OC
97
98 namespace OC
99 {
100     template<typename T, typename = void>
101     struct is_vector
102     {
103         constexpr static bool value = false;
104     };
105
106     template<typename T>
107     struct is_vector<T,
108         typename std::enable_if<
109             std::is_same<T, std::vector<typename T::value_type, typename T::allocator_type>>::value
110         >::type
111     >
112     {
113         constexpr static bool value = true;
114     };
115 } // namespace OC
116
117 #endif
118