replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / include / PrimitiveResourceImpl.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 #ifndef COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H
22 #define COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H
23
24 #include "PrimitiveResource.h"
25 #include "AssertUtils.h"
26
27 #include "ResourceAttributesConverter.h"
28
29 namespace OIC
30 {
31     namespace Service
32     {
33
34         template< typename BaseResource >
35         class PrimitiveResourceImpl: public PrimitiveResource
36         {
37         private:
38             typedef std::shared_ptr< BaseResource > BaseResourcePtr;
39
40         private:
41             static RCSRepresentation convertRepresentation(
42                     const OC::OCRepresentation& rep)
43             {
44                 return RCSRepresentation::fromOCRepresentation(rep);
45             }
46
47             template< typename CALLBACK, typename ...ARGS >
48             static inline void checkedCall(const std::weak_ptr< const PrimitiveResource >& resource,
49                     const CALLBACK& cb, ARGS&&... args)
50             {
51                 auto checkedRes = resource.lock();
52
53                 if (!checkedRes) return;
54
55                 cb(std::forward< ARGS >(args)...);
56             }
57
58             template< typename CALLBACK >
59             static void safeCallback(const std::weak_ptr< const PrimitiveResource >& resource,
60                     const CALLBACK& cb, const HeaderOptions& headerOptions,
61                     const OC::OCRepresentation& rep, int errorCode)
62             {
63                 checkedCall(resource, cb, headerOptions, convertRepresentation(rep), errorCode);
64             }
65
66             static void safeObserveCallback(const std::weak_ptr< const PrimitiveResource >& res,
67                     const PrimitiveResource::ObserveCallback& cb,
68                     const HeaderOptions& headerOptions, const OC::OCRepresentation& rep,
69                     int errorCode, int sequenceNumber)
70             {
71                 checkedCall(res, cb, headerOptions, convertRepresentation(rep), errorCode,
72                         sequenceNumber);
73             }
74
75             std::weak_ptr< PrimitiveResource > WeakFromThis()
76             {
77                 return shared_from_this();
78             }
79
80             std::weak_ptr< const PrimitiveResource > WeakFromThis() const
81             {
82                 return shared_from_this();
83             }
84
85         public:
86             PrimitiveResourceImpl(const BaseResourcePtr& baseResource) :
87                     m_baseResource{ baseResource }
88             {
89             }
90
91             void requestGet(GetCallback callback)
92             {
93                 requestGetWith("", "", {}, std::move(callback));
94             }
95
96             void requestGetWith(const std::string& resourceType,
97                     const std::string& resourceInterface,
98                     const OC::QueryParamsMap& queryParametersMap, GetCallback callback)
99             {
100                 using namespace std::placeholders;
101
102                 typedef OCStackResult(BaseResource::*GetFunc)(
103                         const std::string&, const std::string&,
104                         const OC::QueryParamsMap&, OC::GetCallback);
105
106                 invokeOC(m_baseResource, static_cast< GetFunc >(&BaseResource::get),
107                         resourceType, resourceInterface, queryParametersMap,
108                         std::bind(safeCallback< GetCallback >, WeakFromThis(),
109                                 std::move(callback), _1, _2, _3));
110             }
111
112             void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
113             {
114                 requestSetWith("", "", {}, attrs, std::move(callback));
115             }
116
117             void requestSetWith(const std::string& resourceType,
118                           const std::string& resourceInterface,
119                           const OC::QueryParamsMap& queryParametersMap,
120                           const RCSResourceAttributes& attrs, GetCallback callback)
121             {
122                 using namespace std::placeholders;
123
124                 typedef OCStackResult (BaseResource::*PostFunc)(const std::string&,
125                         const std::string&, const OC::OCRepresentation&, const OC::QueryParamsMap&,
126                         OC::GetCallback);
127
128                 invokeOC(m_baseResource, static_cast< PostFunc >(&BaseResource::post),
129                         resourceType, resourceInterface,
130                         ResourceAttributesConverter::toOCRepresentation(attrs), queryParametersMap,
131                         std::bind(safeCallback< SetCallback >, WeakFromThis(), std::move(callback),
132                                 _1, _2, _3));
133             }
134
135             void requestSetWith(const std::string& resourceType,
136                           const std::string& resourceInterface,
137                           const OC::QueryParamsMap& queryParametersMap,
138                           const RCSRepresentation & rep, SetCallback callback)
139             {
140                 using namespace std::placeholders;
141
142                 typedef OCStackResult (BaseResource::*PostFunc)(const std::string&,
143                         const std::string&, const OC::OCRepresentation&, const OC::QueryParamsMap&,
144                         OC::GetCallback);
145
146                 invokeOC(m_baseResource, static_cast< PostFunc >(&BaseResource::post),
147                         resourceType, resourceInterface,
148                         RCSRepresentation::toOCRepresentation(rep), queryParametersMap,
149                         std::bind(safeCallback< SetCallback >, WeakFromThis(), std::move(callback),
150                                 _1, _2, _3));
151             }
152
153             void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
154             {
155                 using namespace std::placeholders;
156
157                 typedef OCStackResult(BaseResource::*PutFunc)(
158                         const OC::OCRepresentation&,
159                         const OC::QueryParamsMap&, OC::PutCallback);
160
161                 invokeOC(m_baseResource, static_cast< PutFunc >(&BaseResource::put),
162                         ResourceAttributesConverter::toOCRepresentation(attrs),
163                         OC::QueryParamsMap{ },
164                         std::bind(safeCallback< PutCallback >, WeakFromThis(),
165                                 std::move(callback), _1, _2, _3));
166             }
167
168             void requestObserve(ObserveCallback callback)
169             {
170                 using namespace std::placeholders;
171
172                 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
173                         const OC::QueryParamsMap&, OC::ObserveCallback);
174
175                 invokeOC(m_baseResource, static_cast< ObserveFunc >(&BaseResource::observe),
176                         OC::ObserveType::ObserveAll, OC::QueryParamsMap{ },
177                         std::bind(safeObserveCallback, WeakFromThis(),
178                                 std::move(callback), _1, _2, _3, _4));
179             }
180
181             void cancelObserve()
182             {
183                 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
184
185                 invokeOC(m_baseResource,
186                         static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
187             }
188
189             std::string getSid() const
190             {
191                 return invokeOC(m_baseResource, &BaseResource::sid);
192             }
193
194             std::string getUri() const
195             {
196                 return invokeOC(m_baseResource, &BaseResource::uri);
197             }
198
199             std::string getHost() const
200             {
201                 return invokeOC(m_baseResource, &BaseResource::host);
202             }
203
204             std::vector< std::string > getTypes() const
205             {
206                 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
207             }
208
209             std::vector< std::string > getInterfaces() const
210             {
211                 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
212             }
213
214             OCConnectivityType getConnectivityType() const
215             {
216                 return invokeOC(m_baseResource, &BaseResource::connectivityType);
217             }
218
219             bool isObservable() const
220             {
221                 return invokeOC(m_baseResource, &BaseResource::isObservable);
222             }
223
224         private:
225             BaseResourcePtr m_baseResource;
226         };
227
228     }
229 }
230
231 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H