1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H
22 #define COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H
24 #include "PrimitiveResource.h"
25 #include "AssertUtils.h"
27 #include "ResourceAttributesConverter.h"
34 template< typename BaseResource >
35 class PrimitiveResourceImpl: public PrimitiveResource
38 typedef std::shared_ptr< BaseResource > BaseResourcePtr;
41 static RCSRepresentation convertRepresentation(
42 const OC::OCRepresentation& rep)
44 return RCSRepresentation::fromOCRepresentation(rep);
47 template< typename CALLBACK, typename ...ARGS >
48 static inline void checkedCall(const std::weak_ptr< const PrimitiveResource >& resource,
49 const CALLBACK& cb, ARGS&&... args)
51 auto checkedRes = resource.lock();
53 if (!checkedRes) return;
55 cb(std::forward< ARGS >(args)...);
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)
63 checkedCall(resource, cb, headerOptions, convertRepresentation(rep), errorCode);
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)
71 checkedCall(res, cb, headerOptions, convertRepresentation(rep), errorCode,
75 std::weak_ptr< PrimitiveResource > WeakFromThis()
77 return shared_from_this();
80 std::weak_ptr< const PrimitiveResource > WeakFromThis() const
82 return shared_from_this();
86 PrimitiveResourceImpl(const BaseResourcePtr& baseResource) :
87 m_baseResource{ baseResource }
91 void requestGet(GetCallback callback)
93 requestGetWith("", "", {}, std::move(callback));
96 void requestGetWith(const std::string& resourceType,
97 const std::string& resourceInterface,
98 const OC::QueryParamsMap& queryParametersMap, GetCallback callback)
100 using namespace std::placeholders;
102 typedef OCStackResult(BaseResource::*GetFunc)(
103 const std::string&, const std::string&,
104 const OC::QueryParamsMap&, OC::GetCallback);
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));
112 void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
114 requestSetWith("", "", {}, attrs, std::move(callback));
117 void requestSetWith(const std::string& resourceType,
118 const std::string& resourceInterface,
119 const OC::QueryParamsMap& queryParametersMap,
120 const RCSResourceAttributes& attrs, GetCallback callback)
122 using namespace std::placeholders;
124 typedef OCStackResult (BaseResource::*PostFunc)(const std::string&,
125 const std::string&, const OC::OCRepresentation&, const OC::QueryParamsMap&,
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),
135 void requestSetWith(const std::string& resourceType,
136 const std::string& resourceInterface,
137 const OC::QueryParamsMap& queryParametersMap,
138 const RCSRepresentation & rep, SetCallback callback)
140 using namespace std::placeholders;
142 typedef OCStackResult (BaseResource::*PostFunc)(const std::string&,
143 const std::string&, const OC::OCRepresentation&, const OC::QueryParamsMap&,
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),
153 void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
155 using namespace std::placeholders;
157 typedef OCStackResult(BaseResource::*PutFunc)(
158 const OC::OCRepresentation&,
159 const OC::QueryParamsMap&, OC::PutCallback);
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));
168 void requestObserve(ObserveCallback callback)
170 using namespace std::placeholders;
172 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
173 const OC::QueryParamsMap&, OC::ObserveCallback);
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));
183 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
185 invokeOC(m_baseResource,
186 static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
189 std::string getSid() const
191 return invokeOC(m_baseResource, &BaseResource::sid);
194 std::string getUri() const
196 return invokeOC(m_baseResource, &BaseResource::uri);
199 std::string getHost() const
201 return invokeOC(m_baseResource, &BaseResource::host);
204 std::vector< std::string > getTypes() const
206 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
209 std::vector< std::string > getInterfaces() const
211 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
214 bool isObservable() const
216 return invokeOC(m_baseResource, &BaseResource::isObservable);
220 BaseResourcePtr m_baseResource;
226 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H