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>
24 #include <CommonsJavaScript/JSLifeManager.h>
26 namespace WrtDeviceApis {
27 namespace CommonsJavaScript {
40 struct AcquireByProtect
42 void acquire(T* object)
44 Assert(object && "Object passed to protect can't be NULL.");
45 JSValueSafeProtect(object->getContext(), object->getObject());
56 void release(T* object)
67 struct ReleaseByDelete
69 void release(T* object)
71 delete object->getObject();
80 struct ReleaseByUnprotect
82 void release(T* object)
84 Assert(object && "Object passed to unprotect can't be NULL.");
85 JSValueSafeUnprotect(object->getContext(), object->getObject());
94 struct NoOwnership : protected NoAcquire<T>,
95 protected NoRelease<T>
103 struct OwnershipByAcquisition : protected NoAcquire<T>,
104 protected ReleaseByDelete<T>
107 ~OwnershipByAcquisition()
112 struct OwnershipByProtection : protected AcquireByProtect<T>,
113 protected ReleaseByUnprotect<T>
116 ~OwnershipByProtection()
120 template<class PrivateClass,
121 template <class> class OwnershipPolicy = OwnershipByAcquisition>
122 class PrivateObject : public DPL::Noncopyable,
123 protected OwnershipPolicy<PrivateObject<PrivateClass, OwnershipPolicy> >
126 typedef PrivateClass ObjectType;
130 * Creates storage object for JS private data.
131 * @param context JS (root/global) context.
132 * @param object Object to store.
133 * @throw NullPointerException When object is pointer and is set to NULL.
135 PrivateObject(JSContextRef context,
136 const PrivateClass& object) :
140 Assert(NULL != m_context && "Context is NULL.");
141 Assert(!Commons::IsNull<PrivateClass>::value(
142 object) && "Object is NULL.");
147 * Destroys instance of the object.
149 virtual ~PrivateObject()
155 * Gets stored JS context.
156 * @return JavaScript context.
158 virtual JSContextRef getContext() const
164 * Gets stored object.
165 * @return Stored object.
167 virtual PrivateClass getObject() const
173 JSContextRef m_context; ///< JS context.
174 PrivateClass m_object; ///< Stored object.
178 * Specialization for type void.
181 class PrivateObject<void, NoOwnership> : private DPL::Noncopyable
185 * Creates storage object for JS private data.
186 * @param context JS (root/global) context.
187 * @remarks Takes ownership over stored object.
189 explicit PrivateObject(JSContextRef context) : m_context(context)
191 Assert(NULL != m_context && "Context is NULL.");
195 * Destroys instance of the object.
197 virtual ~PrivateObject()
201 * Gets stored JS context.
202 * @return JavaScript context.
204 virtual JSContextRef getContext() const
210 JSContextRef m_context;
214 struct PrivateObjectT
216 typedef PrivateObject<C, NoOwnership> Type;
220 struct PrivateObjectT<C*>
222 typedef PrivateObject<C*, OwnershipByAcquisition> Type;
226 struct PrivateObjectT<JSObjectRef>
228 typedef PrivateObject<JSObjectRef, NoOwnership> Type;
232 struct PrivateObjectT<void>
234 typedef PrivateObject<void, NoOwnership> Type;
236 } // CommonsJavaScript
239 #endif /* PRIVATEOBJECT_H_ */