Modifying version number for building on tizen 3.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
32 namespace OC {
33     namespace Utilities {
34
35         typedef std::map<std::string, std::string> QueryParamsKeyVal;
36         /*
37          * @brief Helper function to get query parameter from a URI
38          * @remarks      Its okay to return a copy of the container.\
39          *               The size is not expected to be huge.
40          * @remarks      Temporary: The URI must strictly have\
41          *               coap as the protocol in the fully qualified URI\
42          *               e.g., coap://1.2.3.4:5657/foo?bar=0)
43          * @remarks      If a separate class for URI parser is needed,\
44          *               please talk to Erich Keane.
45          * @todo         If more URI elements need to be parsed,\
46          *               please move the common parsing logic to a
47          *               different function
48          */
49         QueryParamsKeyVal getQueryParams(const std::string& uri);
50
51     }
52 }
53
54 /* The C++11 standard unfortunately forgot to provide make_unique<>! However, if we're
55 using C++14 or later, we want to take the standard library's implementation: */
56 #if defined(__cplusplus) && __cplusplus < 201300
57 namespace OC {
58
59     template<typename T, typename ...XS>
60     std::unique_ptr<T> make_unique(XS&& ...xs)
61     {
62         return std::unique_ptr<T>(new T(std::forward<XS>(xs)...));
63     }
64
65 } // namespace OC
66 #else
67     using std::make_unique;
68 #endif
69
70 namespace OC {
71
72     using OC::make_unique;
73
74     /* Examine an OCStackResult, and either forward its value or raise an exception: */
75     OCStackResult result_guard(const OCStackResult r);
76
77     /* Check for a nullptr, and throw an exception if we see one; otherwise, return the
78     result of the function call: */
79     template <typename PtrT, typename FnT, typename ...ParamTs>
80     auto nil_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
81     {
82         if(nullptr == p)
83         {
84             throw OCException(OC::Exception::NIL_GUARD_NULL, OC_STACK_INVALID_PARAM);
85         }
86
87         // Note that although parameters are being forwarded, std::bind() will make a single copy:
88         return std::bind(fn, p, std::forward<ParamTs>(params)...)();
89     }
90
91     /* Check for nullptr and forward the result of an OC function call on success; raise
92     an exception on failure or exceptional result: */
93     template <typename PtrT, typename FnT, typename ...ParamTs>
94     auto checked_guard(PtrT&& p, FnT&& fn, ParamTs&& ...params) -> OCStackResult
95     {
96         return result_guard(nil_guard(p, fn, std::forward<ParamTs>(params)...));
97     }
98
99 } // namespace OC
100
101 #endif