Fix build error
[platform/framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / PrivateObject.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_
18
19 #include <JavaScriptCore/JavaScript.h>
20 #include <dpl/noncopyable.h>
21 #include <dpl/assert.h>
22 #include <Commons/Exception.h>
23 #include <Commons/TypeTraits.h>
24
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
27 template<class T>
28 struct NoAcquire
29 {
30     void acquire(T*)
31     {}
32
33   protected:
34     ~NoAcquire()
35     {}
36 };
37
38 template<class T>
39 struct AcquireByProtect
40 {
41     void acquire(T* object)
42     {
43         Assert(object && "Object passed to protect can't be NULL.");
44         JSValueProtect(object->getContext(), object->getObject());
45     }
46
47   protected:
48     ~AcquireByProtect()
49     {}
50 };
51
52 template<class T>
53 struct NoRelease
54 {
55     void release(T* object)
56     {
57         (void)object;
58     }
59
60   protected:
61     ~NoRelease()
62     {}
63 };
64
65 template<class T>
66 struct ReleaseByDelete
67 {
68     void release(T* object)
69     {
70         delete object->getObject();
71     }
72
73   protected:
74     ~ReleaseByDelete()
75     {}
76 };
77
78 template<class T>
79 struct ReleaseByUnprotect
80 {
81     void release(T* object)
82     {
83         Assert(object && "Object passed to unprotect can't be NULL.");
84         JSValueUnprotect(object->getContext(), object->getObject());
85     }
86
87   protected:
88     ~ReleaseByUnprotect()
89     {}
90 };
91
92 template<class T>
93 struct NoOwnership : protected NoAcquire<T>,
94     protected NoRelease<T>
95 {
96   protected:
97     ~NoOwnership()
98     {}
99 };
100
101 template<class T>
102 struct OwnershipByAcquisition : protected NoAcquire<T>,
103     protected ReleaseByDelete<T>
104 {
105   protected:
106     ~OwnershipByAcquisition()
107     {}
108 };
109
110 template<class T>
111 struct OwnershipByProtection : protected AcquireByProtect<T>,
112     protected ReleaseByUnprotect<T>
113 {
114   protected:
115     ~OwnershipByProtection()
116     {}
117 };
118
119 template<class PrivateClass,
120          template <class> class OwnershipPolicy = OwnershipByAcquisition>
121 class PrivateObject : public DPL::Noncopyable,
122     protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
123 {
124   public:
125     typedef PrivateClass ObjectType;
126
127   public:
128     /**
129      * Creates storage object for JS private data.
130      * @param context JS (root/global) context.
131      * @param object Object to store.
132      * @throw NullPointerException When object is pointer and is set to NULL.
133      */
134     PrivateObject(JSContextRef context,
135                   const PrivateClass& object) :
136         m_context(context),
137         m_object(object)
138     {
139         Assert(NULL != m_context && "Context is NULL.");
140         Assert(!Commons::IsNull<PrivateClass>::value(
141                    object) && "Object is NULL.");
142         this->acquire(this);
143     }
144
145     /**
146      * Destroys instance of the object.
147      */
148     virtual ~PrivateObject()
149     {
150         this->release(this);
151     }
152
153     /**
154      * Gets stored JS context.
155      * @return JavaScript context.
156      */
157     virtual JSContextRef getContext() const
158     {
159         return m_context;
160     }
161
162     /**
163      * Gets stored object.
164      * @return Stored object.
165      */
166     virtual PrivateClass getObject() const
167     {
168         return m_object;
169     }
170
171   protected:
172     JSContextRef m_context; ///< JS context.
173     PrivateClass m_object; ///< Stored object.
174 };
175
176 /**
177  * Specialization for type void.
178  */
179 template<>
180 class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
181 {
182   public:
183     /**
184      * Creates storage object for JS private data.
185      * @param context JS (root/global) context.
186      * @remarks Takes ownership over stored object.
187      */
188     explicit PrivateObject(JSContextRef context) : m_context(context)
189     {
190         Assert(NULL != m_context && "Context is NULL.");
191     }
192
193     /**
194      * Destroys instance of the object.
195      */
196     virtual ~PrivateObject()
197     {}
198
199     /**
200      * Gets stored JS context.
201      * @return JavaScript context.
202      */
203     virtual JSContextRef getContext() const
204     {
205         return m_context;
206     }
207
208   protected:
209     JSContextRef m_context;
210 };
211
212 template<class C>
213 struct PrivateObjectT
214 {
215     typedef PrivateObject<C, NoOwnership> Type;
216 };
217
218 template<class C>
219 struct PrivateObjectT<C*>
220 {
221     typedef PrivateObject<C*, OwnershipByAcquisition> Type;
222 };
223
224 template<>
225 struct PrivateObjectT<JSObjectRef>
226 {
227     typedef PrivateObject<JSObjectRef, NoOwnership> Type;
228 };
229
230 template<>
231 struct PrivateObjectT<void>
232 {
233     typedef PrivateObject<void, NoOwnership> Type;
234 };
235 } // CommonsJavaScript
236 } // WrtDeviceApis
237
238 #endif /* PRIVATEOBJECT_H_ */