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::*PutFunc)(
111 const OC::OCRepresentation&,
112 const OC::QueryParamsMap&, OC::PutCallback);
114 invokeOC(m_baseResource, static_cast< PutFunc >(&BaseResource::put),
115 ResourceAttributesConverter::toOCRepresentation(attrs),
116 OC::QueryParamsMap{ },
117 std::bind(safeCallback< SetCallback >, WeakFromThis(),
118 std::move(callback), _1, _2, _3));
121 void requestObserve(ObserveCallback callback)
123 using namespace std::placeholders;
125 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
126 const OC::QueryParamsMap&, OC::ObserveCallback);
128 invokeOC(m_baseResource, static_cast< ObserveFunc >(&BaseResource::observe),
129 OC::ObserveType::ObserveAll, OC::QueryParamsMap{ },
130 std::bind(safeObserveCallback, WeakFromThis(),
131 std::move(callback), _1, _2, _3, _4));
136 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
138 invokeOC(m_baseResource,
139 static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
142 std::string getSid() const
144 return invokeOC(m_baseResource, &BaseResource::sid);
147 std::string getUri() const
149 return invokeOC(m_baseResource, &BaseResource::uri);
152 std::string getHost() const
154 return invokeOC(m_baseResource, &BaseResource::host);
157 std::vector< std::string > getTypes() const
159 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
162 std::vector< std::string > getInterfaces() const
164 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
167 bool isObservable() const
169 return invokeOC(m_baseResource, &BaseResource::isObservable);
173 BaseResourcePtr m_baseResource;
179 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H