2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_PRIVATEOBJECT_H_
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>
25 namespace WrtDeviceApis {
26 namespace CommonsJavaScript {
41 struct AcquireByProtect
43 void acquire(T* object)
45 Assert(object && "Object passed to protect can't be NULL.");
46 JSValueProtect(object->getContext(), object->getObject());
57 void release(T* object)
68 struct ReleaseByDelete
70 void release(T* object)
72 delete object->getObject();
81 struct ReleaseByUnprotect
83 void release(T* object)
85 Assert(object && "Object passed to unprotect can't be NULL.");
86 JSValueUnprotect(object->getContext(), object->getObject());
95 struct NoOwnership : protected NoAcquire<T>,
96 protected NoRelease<T>
105 struct OwnershipByAcquisition : protected NoAcquire<T>,
106 protected ReleaseByDelete<T>
109 ~OwnershipByAcquisition()
115 struct OwnershipByProtection : protected AcquireByProtect<T>,
116 protected ReleaseByUnprotect<T>
119 ~OwnershipByProtection()
124 template<class PrivateClass,
125 template <class> class OwnershipPolicy = OwnershipByAcquisition>
126 class PrivateObject : public DPL::Noncopyable,
127 protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
130 typedef PrivateClass ObjectType;
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.
139 PrivateObject(JSContextRef context,
140 const PrivateClass& object) :
144 Assert(NULL != m_context && "Context is NULL.");
145 Assert(!Commons::IsNull<PrivateClass>::value(object) && "Object is NULL.");
150 * Destroys instance of the object.
152 virtual ~PrivateObject()
158 * Gets stored JS context.
159 * @return JavaScript context.
161 virtual JSContextRef getContext() const
167 * Gets stored object.
168 * @return Stored object.
170 virtual PrivateClass getObject() const
176 JSContextRef m_context; ///< JS context.
177 PrivateClass m_object; ///< Stored object.
181 * Specialization for type void.
184 class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
188 * Creates storage object for JS private data.
189 * @param context JS (root/global) context.
190 * @remarks Takes ownership over stored object.
192 explicit PrivateObject(JSContextRef context) : m_context(context)
194 Assert(NULL != m_context && "Context is NULL.");
198 * Destroys instance of the object.
200 virtual ~PrivateObject()
205 * Gets stored JS context.
206 * @return JavaScript context.
208 virtual JSContextRef getContext() const
214 JSContextRef m_context;
218 struct PrivateObjectT
220 typedef PrivateObject<C, NoOwnership> Type;
224 struct PrivateObjectT<C*>
226 typedef PrivateObject<C*, OwnershipByAcquisition> Type;
230 struct PrivateObjectT<JSObjectRef>
232 typedef PrivateObject<JSObjectRef, NoOwnership> Type;
236 struct PrivateObjectT<void>
238 typedef PrivateObject<void, NoOwnership> Type;
241 } // CommonsJavaScript
244 #endif /* PRIVATEOBJECT_H_ */