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 {
39 struct AcquireByProtect
41 void acquire(T* object)
43 Assert(object && "Object passed to protect can't be NULL.");
44 JSValueProtect(object->getContext(), object->getObject());
55 void release(T* object)
66 struct ReleaseByDelete
68 void release(T* object)
70 delete object->getObject();
79 struct ReleaseByUnprotect
81 void release(T* object)
83 Assert(object && "Object passed to unprotect can't be NULL.");
84 JSValueUnprotect(object->getContext(), object->getObject());
93 struct NoOwnership : protected NoAcquire<T>,
94 protected NoRelease<T>
102 struct OwnershipByAcquisition : protected NoAcquire<T>,
103 protected ReleaseByDelete<T>
106 ~OwnershipByAcquisition()
111 struct OwnershipByProtection : protected AcquireByProtect<T>,
112 protected ReleaseByUnprotect<T>
115 ~OwnershipByProtection()
119 template<class PrivateClass,
120 template <class> class OwnershipPolicy = OwnershipByAcquisition>
121 class PrivateObject : public DPL::Noncopyable,
122 protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
125 typedef PrivateClass ObjectType;
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.
134 PrivateObject(JSContextRef context,
135 const PrivateClass& object) :
139 Assert(NULL != m_context && "Context is NULL.");
140 Assert(!Commons::IsNull<PrivateClass>::value(
141 object) && "Object is NULL.");
146 * Destroys instance of the object.
148 virtual ~PrivateObject()
154 * Gets stored JS context.
155 * @return JavaScript context.
157 virtual JSContextRef getContext() const
163 * Gets stored object.
164 * @return Stored object.
166 virtual PrivateClass getObject() const
172 JSContextRef m_context; ///< JS context.
173 PrivateClass m_object; ///< Stored object.
177 * Specialization for type void.
180 class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
184 * Creates storage object for JS private data.
185 * @param context JS (root/global) context.
186 * @remarks Takes ownership over stored object.
188 explicit PrivateObject(JSContextRef context) : m_context(context)
190 Assert(NULL != m_context && "Context is NULL.");
194 * Destroys instance of the object.
196 virtual ~PrivateObject()
200 * Gets stored JS context.
201 * @return JavaScript context.
203 virtual JSContextRef getContext() const
209 JSContextRef m_context;
213 struct PrivateObjectT
215 typedef PrivateObject<C, NoOwnership> Type;
219 struct PrivateObjectT<C*>
221 typedef PrivateObject<C*, OwnershipByAcquisition> Type;
225 struct PrivateObjectT<JSObjectRef>
227 typedef PrivateObject<JSObjectRef, NoOwnership> Type;
231 struct PrivateObjectT<void>
233 typedef PrivateObject<void, NoOwnership> Type;
235 } // CommonsJavaScript
238 #endif /* PRIVATEOBJECT_H_ */