Add cloning of identity to CACloneInfo
[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 "AssertUtils.h"
26
27 #include "ResourceAttributesConverter.h"
28
29 namespace OIC
30 {
31     namespace Service
32     {
33
34         template< typename BaseResource >
35         class PrimitiveResourceImpl: public PrimitiveResource
36         {
37         private:
38             typedef std::shared_ptr< BaseResource > BaseResourcePtr;
39
40         private:
41             static RCSRepresentation convertRepresentation(
42                     const OC::OCRepresentation& rep)
43             {
44                 return RCSRepresentation::fromOCRepresentation(rep);
45             }
46
47             template< typename CALLBACK, typename ...ARGS >
48             static inline void checkedCall(const std::weak_ptr< const PrimitiveResource >& resource,
49                     const CALLBACK& cb, ARGS&&... args)
50             {
51                 auto checkedRes = resource.lock();
52
53                 if (!checkedRes) return;
54
55                 cb(std::forward< ARGS >(args)...);
56             }
57
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)
62             {
63                 checkedCall(resource, cb, headerOptions, convertRepresentation(rep), errorCode);
64             }
65
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)
70             {
71                 checkedCall(res, cb, headerOptions, convertRepresentation(rep), errorCode,
72                         sequenceNumber);
73             }
74
75             std::weak_ptr< PrimitiveResource > WeakFromThis()
76             {
77                 return shared_from_this();
78             }
79
80             std::weak_ptr< const PrimitiveResource > WeakFromThis() const
81             {
82                 return shared_from_this();
83             }
84
85         public:
86             PrimitiveResourceImpl(const BaseResourcePtr& baseResource) :
87                     m_baseResource{ baseResource }
88             {
89             }
90
91             void requestGet(GetCallback callback)
92             {
93                 requestGetWith("", "", {}, std::move(callback));
94             }
95
96             void requestGetWith(const std::string& resourceType,
97                     const std::string& resourceInterface,
98                     const OC::QueryParamsMap& queryParametersMap, GetCallback callback)
99             {
100                 using namespace std::placeholders;
101
102                 typedef OCStackResult(BaseResource::*GetFunc)(
103                         const std::string&, const std::string&,
104                         const OC::QueryParamsMap&, OC::GetCallback);
105
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));
110             }
111
112             void requestSet(const RCSResourceAttributes& attrs, SetCallback callback)
113             {
114                 using namespace std::placeholders;
115
116                 typedef OCStackResult(BaseResource::*PostFunc)(
117                         const OC::OCRepresentation&,
118                         const OC::QueryParamsMap&, OC::PostCallback);
119
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));
125             }
126
127             void requestPut(const RCSResourceAttributes& attrs, PutCallback callback)
128             {
129                 using namespace std::placeholders;
130
131                 typedef OCStackResult(BaseResource::*PutFunc)(
132                         const OC::OCRepresentation&,
133                         const OC::QueryParamsMap&, OC::PutCallback);
134
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));
140             }
141
142             void requestObserve(ObserveCallback callback)
143             {
144                 using namespace std::placeholders;
145
146                 typedef OCStackResult (BaseResource::*ObserveFunc)(OC::ObserveType,
147                         const OC::QueryParamsMap&, OC::ObserveCallback);
148
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));
153             }
154
155             void cancelObserve()
156             {
157                 typedef OCStackResult (BaseResource::*CancelObserveFunc)();
158
159                 invokeOC(m_baseResource,
160                         static_cast< CancelObserveFunc >(&BaseResource::cancelObserve));
161             }
162
163             std::string getSid() const
164             {
165                 return invokeOC(m_baseResource, &BaseResource::sid);
166             }
167
168             std::string getUri() const
169             {
170                 return invokeOC(m_baseResource, &BaseResource::uri);
171             }
172
173             std::string getHost() const
174             {
175                 return invokeOC(m_baseResource, &BaseResource::host);
176             }
177
178             std::vector< std::string > getTypes() const
179             {
180                 return invokeOC(m_baseResource, &BaseResource::getResourceTypes);
181             }
182
183             std::vector< std::string > getInterfaces() const
184             {
185                 return invokeOC(m_baseResource, &BaseResource::getResourceInterfaces);
186             }
187
188             bool isObservable() const
189             {
190                 return invokeOC(m_baseResource, &BaseResource::isObservable);
191             }
192
193         private:
194             BaseResourcePtr m_baseResource;
195         };
196
197     }
198 }
199
200 #endif // COMMON_INTERNAL_PRIMITIVERESOURCEIMPL_H