tizen beta release
[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
28 template<class T>
29 struct NoAcquire
30 {
31     void acquire(T*)
32     {
33     }
34   protected:
35     ~NoAcquire()
36     {
37     }
38 };
39
40 template<class T>
41 struct AcquireByProtect
42 {
43     void acquire(T* object)
44     {
45         Assert(object && "Object passed to protect can't be NULL.");
46         JSValueProtect(object->getContext(), object->getObject());
47     }
48   protected:
49     ~AcquireByProtect()
50     {
51     }
52 };
53
54 template<class T>
55 struct NoRelease
56 {
57     void release(T* object)
58     {
59         (void)object;
60     }
61   protected:
62     ~NoRelease()
63     {
64     }
65 };
66
67 template<class T>
68 struct ReleaseByDelete
69 {
70     void release(T* object)
71     {
72         delete object->getObject();
73     }
74   protected:
75     ~ReleaseByDelete()
76     {
77     }
78 };
79
80 template<class T>
81 struct ReleaseByUnprotect
82 {
83     void release(T* object)
84     {
85         Assert(object && "Object passed to unprotect can't be NULL.");
86         JSValueUnprotect(object->getContext(), object->getObject());
87     }
88   protected:
89     ~ReleaseByUnprotect()
90     {
91     }
92 };
93
94 template<class T>
95 struct NoOwnership : protected NoAcquire<T>,
96     protected NoRelease<T>
97 {
98   protected:
99     ~NoOwnership()
100     {
101     }
102 };
103
104 template<class T>
105 struct OwnershipByAcquisition : protected NoAcquire<T>,
106     protected ReleaseByDelete<T>
107 {
108   protected:
109     ~OwnershipByAcquisition()
110     {
111     }
112 };
113
114 template<class T>
115 struct OwnershipByProtection : protected AcquireByProtect<T>,
116     protected ReleaseByUnprotect<T>
117 {
118   protected:
119     ~OwnershipByProtection()
120     {
121     }
122 };
123
124 template<class PrivateClass,
125          template <class> class OwnershipPolicy = OwnershipByAcquisition>
126 class PrivateObject : public DPL::Noncopyable,
127     protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
128 {
129   public:
130     typedef PrivateClass ObjectType;
131
132   public:
133     /**
134      * Creates storage object for JS private data.
135      * @param context JS (root/global) context.
136      * @param object Object to store.
137      * @throw NullPointerException When object is pointer and is set to NULL.
138      */
139     PrivateObject(JSContextRef context,
140             const PrivateClass& object) :
141         m_context(context),
142         m_object(object)
143     {
144         Assert(NULL != m_context && "Context is NULL.");
145         Assert(!Commons::IsNull<PrivateClass>::value(object) && "Object is NULL.");
146         acquire(this);
147     }
148
149     /**
150      * Destroys instance of the object.
151      */
152     virtual ~PrivateObject()
153     {
154         release(this);
155     }
156
157     /**
158      * Gets stored JS context.
159      * @return JavaScript context.
160      */
161     virtual JSContextRef getContext() const
162     {
163         return m_context;
164     }
165
166     /**
167      * Gets stored object.
168      * @return Stored object.
169      */
170     virtual PrivateClass getObject() const
171     {
172         return m_object;
173     }
174
175   protected:
176     JSContextRef m_context; ///< JS context.
177     PrivateClass m_object; ///< Stored object.
178 };
179
180 /**
181  * Specialization for type void.
182  */
183 template<>
184 class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
185 {
186   public:
187     /**
188      * Creates storage object for JS private data.
189      * @param context JS (root/global) context.
190      * @remarks Takes ownership over stored object.
191      */
192     explicit PrivateObject(JSContextRef context) : m_context(context)
193     {
194         Assert(NULL != m_context && "Context is NULL.");
195     }
196
197     /**
198      * Destroys instance of the object.
199      */
200     virtual ~PrivateObject()
201     {
202     }
203
204     /**
205      * Gets stored JS context.
206      * @return JavaScript context.
207      */
208     virtual JSContextRef getContext() const
209     {
210         return m_context;
211     }
212
213   protected:
214     JSContextRef m_context;
215 };
216
217 template<class C>
218 struct PrivateObjectT
219 {
220     typedef PrivateObject<C, NoOwnership> Type;
221 };
222
223 template<class C>
224 struct PrivateObjectT<C*>
225 {
226     typedef PrivateObject<C*, OwnershipByAcquisition> Type;
227 };
228
229 template<>
230 struct PrivateObjectT<JSObjectRef>
231 {
232     typedef PrivateObject<JSObjectRef, NoOwnership> Type;
233 };
234
235 template<>
236 struct PrivateObjectT<void>
237 {
238     typedef PrivateObject<void, NoOwnership> Type;
239 };
240
241 } // CommonsJavaScript
242 } // WrtDeviceApis
243
244 #endif /* PRIVATEOBJECT_H_ */