Merge "Add scons script and things manager samples."
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / Common / ObjectManager.h
1 /******************************************************************
2 *
3 * Copyright 2014 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 _ObjectManager_H_
22 #define _ObjectManager_H_
23
24 #include "PlatformLayer.h"
25 #include "SSMInterface/SSMCore.h"
26
27 /* Define NULL pointer value */
28 #ifndef NULL
29 #ifdef __cplusplus
30 #define NULL    0
31 #else
32 #define NULL    ((void *)0)
33 #endif
34 #endif
35
36 typedef struct _OID
37 {
38     unsigned long  data1;
39     unsigned short data2;
40     unsigned short data3;
41     unsigned char  data4[8];
42 } OID;
43
44 const OID OID_IBase = {0x3b465976, 0x6486, 0x4c1f, {0x84, 0xb9, 0xeb, 0x80, 0x79, 0x78, 0x2b, 0x8}};
45
46 /**
47 * @fn    IsEqualOID
48 * @brief Compare both OID
49 *
50 * @param [in] SSMRESULT res - return code
51 *
52 * @return Return positive value if both equal
53 * @warning
54 * @exception
55 * @see
56 */
57 inline int IsEqualOID(const OID &oid1, const OID &oid2)
58 {
59     return (( oid1.data1 ==  oid2.data1) &&
60             ( oid1.data2 ==  oid2.data2) &&
61             ( oid1.data3 ==  oid2.data3) &&
62             ( oid1.data4[0] ==  oid2.data4[0]) &&
63             ( oid1.data4[1] ==  oid2.data4[1]) &&
64             ( oid1.data4[2] ==  oid2.data4[2]) &&
65             ( oid1.data4[3] ==  oid2.data4[3]) &&
66             ( oid1.data4[4] ==  oid2.data4[4]) &&
67             ( oid1.data4[5] ==  oid2.data4[5]) &&
68             ( oid1.data4[6] ==  oid2.data4[6]) &&
69             ( oid1.data4[7] ==  oid2.data4[7]));
70 }
71
72 /**
73 * @class    IBase
74 * @brief    IBase Interface
75 *            This class represents top interface of managed Object's interface\n
76 *            If any class wants to work as managed Object, must inherit this interface.
77 *
78 * @see
79 */
80 class IBase
81 {
82     public:
83         /**
84         * @fn     queryInterface
85         * @brief Query if requested interface support calling instance
86         *
87         * @param [in] const OID& interfaceID - Interface Id looking for
88         * @param [out] IBase** ppObject - Interface pointer holds the object
89         *
90         * @return SSMRESULT
91         * @warning
92         * @exception
93         * @see
94         */
95         virtual SSMRESULT queryInterface(IN const OID &interfaceID, OUT IBase **ppObject) = 0;
96
97         /**
98         * @fn     addRef
99         * @brief Add reference counter, returns current counter value
100         *
101         * @param NONE
102         *
103         * @return unsigned long
104         * @warning
105         * @exception
106         * @see
107         */
108         virtual unsigned long addRef() = 0;
109
110         /**
111         * @fn     release
112         * @brief Release reference counter, returns current counter value and self destroyed if zero
113         *
114         * @param NONE
115         *
116         * @return unsigned long
117         * @warning
118         * @exception
119         * @see
120         */
121         virtual unsigned long release() = 0;
122         virtual ~IBase() {};
123 };
124
125 class CObjectMultiThreadModel;
126 /**
127 * @class    CObjectRoot
128 * @brief    CObjectRoot Interface
129 *            This class represents top class of managed Object\n
130 *            If any class wants to work as managed Object, must inherit this class.
131 *
132 * @see
133 */
134 template <class ThreadModel = CObjectMultiThreadModel>
135 class CObjectRoot
136 {
137     private:
138         volatile unsigned long m_dwRef;
139
140     public:
141         CObjectRoot()
142         {
143             m_dwRef = 0;
144         }
145
146         ~CObjectRoot()
147         {
148         }
149
150         /**
151         * @fn     internalAddRef
152         * @brief Add reference counter, returns current counter value
153         *
154         * @param NONE
155         *
156         * @return unsigned long
157         * @warning
158         * @exception
159         * @see
160         */
161         unsigned long internalAddRef()
162         {
163             return ThreadModel::increment(&m_dwRef);
164         }
165
166         /**
167         * @fn     internalRelease
168         * @brief Release reference counter, returns current counter value
169         *
170         * @param NONE
171         *
172         * @return unsigned long
173         * @warning
174         * @exception
175         * @see
176         */
177         unsigned long internalRelease()
178         {
179             return ThreadModel::decrement(&m_dwRef);
180         }
181 };
182
183 /**
184 * @class    CObject
185 * @brief    CObject Interface
186 *            This class provides a way to declare instance of managed Object\n
187 *            If user wants to declare instance of managed Object, must use this class.
188 *
189 * @see
190 */
191 template <class Base>
192 class CObject :
193     public Base
194 {
195     public:
196         CObject()
197         {
198         }
199
200         virtual ~CObject()
201         {
202         }
203
204         /**
205         * @fn     addRef
206         * @brief Add reference counter, returns current counter value
207         *
208         * @param NONE
209         *
210         * @return unsigned long
211         * @warning
212         * @exception
213         * @see
214         */
215         unsigned long addRef()
216         {
217             return this->internalAddRef();
218         }
219
220         /**
221         * @fn     release
222         * @brief Release reference counter, returns current counter value and self destroyed if zero
223         *
224         * @param NONE
225         *
226         * @return unsigned long
227         * @warning
228         * @exception
229         * @see
230         */
231         unsigned long release()
232         {
233             unsigned long ref = this->internalRelease();
234             if (ref == 0)
235             {
236                 this->finalRelease();
237                 delete this;
238             }
239             return ref;
240         }
241
242         /**
243         * @fn createInstance
244         * @brief Create instance of current template class
245         *
246         * @param [out] CObject<Base>** pp - reference pointer to get instance pointer
247         *
248         * @return SSMRESULT
249         * @warning
250         * @exception
251         * @see
252         */
253         static SSMRESULT createInstance(CObject<Base> **pp)
254         {
255             SSMRESULT res = SSM_E_FAIL;
256
257             if (pp == NULL)
258             {
259                 return SSM_E_POINTER;
260             }
261
262             *pp = NULL;
263
264             CObject<Base> *p = new CObject<Base>();
265
266             if (p == NULL)
267             {
268                 return SSM_E_OUTOFMEMORY;
269             }
270
271             res = p->finalConstruct();
272
273             if (res != SSM_S_OK)
274             {
275                 delete p;
276                 p = NULL;
277                 return res;
278             }
279
280             *pp = p;
281
282             return SSM_S_OK;
283         }
284 };
285
286
287 /**
288 * @class    _noAddRefReleaseOnCObjectPtr
289 * @brief    _noAddRefReleaseOnCObjectPtr Interface
290 *            This class used for CObject's Smart pointer mechanism. Internal use only
291 *
292 * @see
293 */
294 template <class T>
295 class _noAddRefReleaseOnCObjectPtr :
296     public T
297 {
298     private:
299         virtual unsigned long addRef() = 0;
300         virtual unsigned long release() = 0;
301 };
302
303 /**
304 * @class    CObjectPtr
305 * @brief    CObjectPtr Interface
306 *            This class used for declaring CObject's interface to work as Smart Pointer
307 *
308 * @see
309 */
310 template <class T>
311 class CObjectPtr
312 {
313     private:
314         T *p;
315     public:
316         CObjectPtr()
317         {
318             p = NULL;
319         }
320
321         ~CObjectPtr()
322         {
323             SAFE_RELEASE(p);
324         }
325
326         _noAddRefReleaseOnCObjectPtr<T> *operator->() const
327         {
328             return (_noAddRefReleaseOnCObjectPtr<T> *)p;
329         }
330
331         operator T *() const
332         {
333             return p;
334         }
335
336         T **operator&()
337         {
338             return &p;
339         }
340         /*
341         CObjectPtr(T* lp)
342         {
343             p = lp;
344             if(p != NULL)
345             p->AddRef();
346         }
347
348         CObjectPtr(const CObjectPtr<T>& lp)
349         :CObjectPtr(lp.p)
350         {
351         }
352
353         T& operator*() const
354         {
355             return *p;
356         }
357
358         T* operator=(T* lp)
359         {
360             return *this;
361         }
362
363         T* operator=(CObjectPtr<T>& lp)
364         {
365             return *this;
366         }
367
368         bool operator!() const
369         {
370             return (p == NULL);
371         }
372
373         bool operator<(T* pT) const
374         {
375             return !operator==(pT);
376         }
377
378         bool operator==(T* pT) const
379         {
380             return p == pT;
381         }
382         */
383 };
384
385 /**
386 * @fn CreateNewObject
387 * @brief Create instance of declared template
388 *
389 * @param [in] const OID& interfaceID - Interface Id looking for
390 * @param [out] IBase** ppObject - reference pointer to get instance pointer
391 * @return SSMRESULT
392 * @warning
393 * @exception
394 * @see
395 */
396 template <class T>
397 SSMRESULT CreateNewObject(IN const OID &objectID, OUT IBase **ppObject)
398 {
399     SSMRESULT res = SSM_E_OUTOFMEMORY;
400
401     CObject<T> *p = new CObject<T>();
402
403     if (p == NULL)
404     {
405         goto END;
406     }
407
408     res = p->finalConstruct();
409
410     if (res != SSM_S_OK)
411     {
412         delete p;
413         p = NULL;
414         goto END;
415     }
416
417     res = p->queryInterface(objectID, ppObject);
418
419 END:
420     return res;
421 }
422
423 #endif