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 using namespace std::placeholders;
116 typedef OCStackResult(BaseResource::*PostFunc)(
117 const OC::OCRepresentation&,
118 const OC::QueryParamsMap&, OC::PostCallback);
120 invokeOC(m_baseResource, static_cast< PostFunc >(&BaseResource::post),
121 ResourceAttributesConverter::toOCRepresentation(attrs),
122 OC::QueryParamsMap{ },
123 std::bind(safeCallback< SetCallback >, WeakFromThis(),
124 std::move(callback), _1, _2, _3));
127 void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
129 using namespace std::placeholders;
131 typedef OCStackResult(BaseResource::*PutFunc)(
132 const OC::OCRepresentation&,
133 const OC::QueryParamsMap&, OC::PutCallback);
135 invokeOC(m_baseResource, static_cast< PutFunc >(&BaseResource::put),
136 ResourceAttributesConverter::toOCRepresentation(attrs),
137 OC::QueryParamsMap{ },
138 std::bind(safeCallback< PutCallback >, WeakFromThis(),
139 std::move(callback), _1, _2, _3));
142 void requestObserve(ObserveCallback callback)
144 using namespace std::placeholders;
146 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
147 const OC::QueryParamsMap&, OC::ObserveCallback);
149 invokeOC(m_baseResource, static_cast< ObserveFunc >(&BaseResource::observe),
150 OC::ObserveType::ObserveAll, OC::QueryParamsMap{ },
151 std::bind(safeObserveCallback, WeakFromThis(),
152 std::move(callback), _1, _2, _3, _4));
157 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
159 invokeOC(m_baseResource,
160 static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
163 std::string getSid() const
165 return invokeOC(m_baseResource, &BaseResource::sid);
168 std::string getUri() const
170 return invokeOC(m_baseResource, &BaseResource::uri);
173 std::string getHost() const
175 return invokeOC(m_baseResource, &BaseResource::host);
178 std::vector< std::string > getTypes() const
180 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
183 std::vector< std::string > getInterfaces() const
185 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
188 bool isObservable() const
190 return invokeOC(m_baseResource, &BaseResource::isObservable);
194 BaseResourcePtr m_baseResource;
200 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H