Imported Upstream version 0.9.2
[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 <ResponseStatement.h>
26 #include <AssertUtils.h>
27
28 #include <ResourceAttributesConverter.h>
29
30 namespace OIC
31 {
32     namespace Service
33     {
34
35         template< typename BaseResource >
36         class PrimitiveResourceImpl: public PrimitiveResource
37         {
38         private:
39             typedef std::shared_ptr< BaseResource > BaseResourcePtr;
40
41         private:
42             static ResponseStatement createResponseStatement(
43                     const OC::OCRepresentation& rep)
44             {
45                 return ResponseStatement::create(
46                         ResourceAttributesConverter::fromOCRepresentation(rep));
47             }
48
49             template< typename CALLBACK, typename ...ARGS >
50             static inline void checkedCall(const std::weak_ptr< const PrimitiveResource >& resource,
51                     const CALLBACK& cb, ARGS&&... args)
52             {
53                 auto checkedRes = resource.lock();
54
55                 if (!checkedRes) return;
56
57                 cb(std::forward< ARGS >(args)...);
58             }
59
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)
64             {
65                 checkedCall(resource, cb, headerOptions, createResponseStatement(rep), errorCode);
66             }
67
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)
72             {
73                 checkedCall(res, cb, headerOptions, createResponseStatement(rep), errorCode,
74                         sequenceNumber);
75             }
76
77             std::weak_ptr< PrimitiveResource > WeakFromThis()
78             {
79                 return shared_from_this();
80             }
81
82             std::weak_ptr< const PrimitiveResource > WeakFromThis() const
83             {
84                 return shared_from_this();
85             }
86
87         public:
88             PrimitiveResourceImpl(const BaseResourcePtr& baseResource) :
89                     m_baseResource{ baseResource }
90             {
91             }
92
93             void requestGet(GetCallback callback)
94             {
95                 using namespace std::placeholders;
96
97                 typedef OCStackResult(BaseResource::*GetFunc)(
98                         const OC::QueryParamsMap&, OC::GetCallback);
99
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));
104             }
105
106             void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
107             {
108                 using namespace std::placeholders;
109
110                 typedef OCStackResult(BaseResource::*PutFunc)(
111                         const OC::OCRepresentation&,
112                         const OC::QueryParamsMap&, OC::PutCallback);
113
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));
119             }
120
121             void requestObserve(ObserveCallback callback)
122             {
123                 using namespace std::placeholders;
124
125                 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
126                         const OC::QueryParamsMap&, OC::ObserveCallback);
127
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));
132             }
133
134             void cancelObserve()
135             {
136                 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
137
138                 invokeOC(m_baseResource,
139                         static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
140             }
141
142             std::string getSid() const
143             {
144                 return invokeOC(m_baseResource, &BaseResource::sid);
145             }
146
147             std::string getUri() const
148             {
149                 return invokeOC(m_baseResource, &BaseResource::uri);
150             }
151
152             std::string getHost() const
153             {
154                 return invokeOC(m_baseResource, &BaseResource::host);
155             }
156
157             std::vector< std::string > getTypes() const
158             {
159                 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
160             }
161
162             std::vector< std::string > getInterfaces() const
163             {
164                 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
165             }
166
167             bool isObservable() const
168             {
169                 return invokeOC(m_baseResource, &BaseResource::isObservable);
170             }
171
172         private:
173             BaseResourcePtr m_baseResource;
174         };
175
176     }
177 }
178
179 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H