c6fb66e7adf660581644de6a40b9a733ae7fc081
[platform/upstream/iotivity.git] / service / simulator / src / common / simulator_utils.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics 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 /**
22  * @file simulator_utils.h
23  *
24  * @brief This file provides utility methods used by both client and servers.
25  */
26
27 #ifndef SIMULATOR_UTILS_H_
28 #define SIMULATOR_UTILS_H_
29
30 #include "simulator_exceptions.h"
31 #include "octypes.h"
32 #include "OCException.h"
33
34 #include <map>
35
36 /**
37  * Utilities for Invocation of OC platfrom level APIs.
38  */
39 template <typename FnT, typename... ArgsT>
40 typename std::enable_if<std::is_same<OCStackResult, decltype(std::declval<FnT>()(std::declval<ArgsT>()...))>::value>::type
41 invokeocplatform(FnT fn, ArgsT &&...args)
42 {
43     try
44     {
45         OCStackResult ocResult = fn(std::forward<ArgsT>(args)...);
46         if (OC_STACK_OK != ocResult)
47         {
48             // Throw SimulatorException converting error codes
49             throw SimulatorException(static_cast<SimulatorResult>(ocResult), OC::OCException::reason(ocResult));
50         }
51     }
52     catch (OC::OCException &e)
53     {
54         // Throw SimulatorException converting error codes
55         throw SimulatorException(static_cast<SimulatorResult>(e.code()), e.reason());
56     }
57 }
58
59 namespace OC
60 {
61     class OCRepresentation;
62 }
63
64 std::string getPayloadString(const OC::OCRepresentation &);
65 std::string getPayloadTypeString(OCPayloadType type);
66 std::string getRequestString(const std::map<std::string, std::string> &queryParams,
67                              const OC::OCRepresentation &rep);
68 std::string getRequestString(const std::map<std::string, std::string> &queryParams);
69
70
71 #define VALIDATE_INPUT(CONDITION, MSG) if (CONDITION) {throw InvalidArgsException(SIMULATOR_INVALID_PARAM, MSG);}
72 #define VALIDATE_CALLBACK(CALLBACK) if (!CALLBACK){throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");}
73
74 #endif