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 <ResponseStatement.h>
26 #include <AssertUtils.h>
28 #include <ResourceAttributesConverter.h>
35 template< typename BaseResource >
36 class PrimitiveResourceImpl: public PrimitiveResource
39 typedef std::shared_ptr< BaseResource > BaseResourcePtr;
42 static ResponseStatement createResponseStatement(
43 const OC::OCRepresentation& rep)
45 return ResponseStatement::create(
46 ResourceAttributesConverter::fromOCRepresentation(rep));
49 template< typename CALLBACK, typename ...ARGS >
50 static inline void checkedCall(const std::weak_ptr< const PrimitiveResource >& resource,
51 const CALLBACK& cb, ARGS&&... args)
53 auto checkedRes = resource.lock();
55 if (!checkedRes) return;
57 cb(std::forward< ARGS >(args)...);
60 template< typename CALLBACK >
61 static void safeCallback(const std::weak_ptr< const PrimitiveResource >& resource,
62 const CALLBACK& cb, const HeaderOptions& headerOptions,
63 const OC::OCRepresentation& rep, int errorCode)
65 checkedCall(resource, cb, headerOptions, createResponseStatement(rep), errorCode);
68 static void safeObserveCallback(const std::weak_ptr< const PrimitiveResource >& res,
69 const PrimitiveResource::ObserveCallback& cb,
70 const HeaderOptions& headerOptions, const OC::OCRepresentation& rep,
71 int errorCode, int sequenceNumber)
73 checkedCall(res, cb, headerOptions, createResponseStatement(rep), errorCode,
77 std::weak_ptr< PrimitiveResource > WeakFromThis()
79 return shared_from_this();
82 std::weak_ptr< const PrimitiveResource > WeakFromThis() const
84 return shared_from_this();
88 PrimitiveResourceImpl(const BaseResourcePtr& baseResource) :
89 m_baseResource{ baseResource }
93 void requestGet(GetCallback callback)
95 using namespace std::placeholders;
97 typedef OCStackResult(BaseResource::*GetFunc)(
98 const OC::QueryParamsMap&, OC::GetCallback);
100 invokeOC(m_baseResource, static_cast< GetFunc >(&BaseResource::get),
101 OC::QueryParamsMap{ },
102 std::bind(safeCallback< GetCallback >, WeakFromThis(),
103 std::move(callback), _1, _2, _3));
106 void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
108 using namespace std::placeholders;
110 typedef OCStackResult(BaseResource::*PostFunc)(
111 const OC::OCRepresentation&,
112 const OC::QueryParamsMap&, OC::PostCallback);
114 invokeOC(m_baseResource, static_cast< PostFunc >(&BaseResource::post),
115 ResourceAttributesConverter::toOCRepresentation(attrs),
116 OC::QueryParamsMap{ },
117 std::bind(safeCallback< SetCallback >, WeakFromThis(),
118 std::move(callback), _1, _2, _3));
121 void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
123 using namespace std::placeholders;
125 typedef OCStackResult(BaseResource::*PutFunc)(
126 const OC::OCRepresentation&,
127 const OC::QueryParamsMap&, OC::PutCallback);
129 invokeOC(m_baseResource, static_cast< PutFunc >(&BaseResource::put),
130 ResourceAttributesConverter::toOCRepresentation(attrs),
131 OC::QueryParamsMap{ },
132 std::bind(safeCallback< PutCallback >, WeakFromThis(),
133 std::move(callback), _1, _2, _3));
136 void requestObserve(ObserveCallback callback)
138 using namespace std::placeholders;
140 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
141 const OC::QueryParamsMap&, OC::ObserveCallback);
143 invokeOC(m_baseResource, static_cast< ObserveFunc >(&BaseResource::observe),
144 OC::ObserveType::ObserveAll, OC::QueryParamsMap{ },
145 std::bind(safeObserveCallback, WeakFromThis(),
146 std::move(callback), _1, _2, _3, _4));
151 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
153 invokeOC(m_baseResource,
154 static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
157 std::string getSid() const
159 return invokeOC(m_baseResource, &BaseResource::sid);
162 std::string getUri() const
164 return invokeOC(m_baseResource, &BaseResource::uri);
167 std::string getHost() const
169 return invokeOC(m_baseResource, &BaseResource::host);
172 std::vector< std::string > getTypes() const
174 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
177 std::vector< std::string > getInterfaces() const
179 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
182 bool isObservable() const
184 return invokeOC(m_baseResource, &BaseResource::isObservable);
188 BaseResourcePtr m_baseResource;
194 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H