2 // Licensed under the Apache License, Version 2.0 (the License);
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
15 #ifndef __TIZEN_COMMON_JSARRAY_H__
16 #define __TIZEN_COMMON_JSARRAY_H__
18 #include <JavaScriptCore/JavaScript.h>
27 JSArrayBase( JSContextRef context, JSObjectRef array );
28 JSArrayBase( JSContextRef context );
30 virtual ~JSArrayBase();
32 void resize(size_t size);
33 JSValueRef get(int index);
34 bool set(int index, JSValueRef value);
35 bool append(JSValueRef value);
38 JSContextRef mContext;
44 class JSArray : protected JSArrayBase{
45 friend class ItemProxy;
47 typedef JSValueRef (*ToJSFunction)(JSContextRef, T);
48 typedef T (*ToNativeFunction)(JSContextRef, JSValueRef );
51 JSArray( JSContextRef context, JSObjectRef array, ToNativeFunction nativefun, ToJSFunction jsfun)
52 :JSArrayBase(context,array),mToNative(nativefun), mToJs(jsfun) {
54 JSArray( JSContextRef context, ToNativeFunction nativefun, ToJSFunction jsfun)
55 :JSArrayBase(context),mToNative(nativefun), mToJs(jsfun) {
65 ItemProxy(JSArray<T>* array, int index):mArray(array), mIndex(index){
68 return mArray->mToNative(mArray->mContext, mArray->get(mIndex));
70 ItemProxy& operator=( const T native){
71 JSValueRef v = mArray->mToJs(mArray->mContext, native);
72 mArray->set(mIndex, v);
75 ItemProxy& operator=( const ItemProxy& other){
76 JSValueRef v = other.mArray->get(other.mIndex);
77 mArray->set(mIndex, v);
83 return JSArrayBase::size();
86 void resize(size_t array_size){
87 JSArrayBase::resize(array_size);
95 return JSArrayBase::set( size(), mToJs(mContext, v));
98 ItemProxy operator[]( int index ){
99 return ItemProxy(this, index);
102 operator JSObjectRef(){
106 operator std::vector<T>(){
108 size_t length = size();
109 for( unsigned int i = 0 ; i < length ; i++){
110 JSValueRef t = get(i);
111 T tmp = mToNative(mContext, t);
117 void operator=( const std::vector<T>& list ){
121 void operator=( const JSArray<T>& rhs){
123 for(unsigned int i = 0 ; i < rhs.size(); i++){
128 JSContextRef getContext(){
133 void overwrite( const std::vector<T>& list ){
135 unsigned int listSize = list.size();
137 for( i = 0 ; i < listSize ; i++){
138 JSValueRef v = mToJs(mContext, list[i]);
144 ToNativeFunction mToNative;
149 class JSStringArray : public JSArray<std::string>{
150 static JSValueRef makeJSValue(JSContextRef ctx, std::string v){
151 return JSUtil::toJSValueRef(ctx, v);
154 // We need to pass pointer to function converting
155 // std::string -> JSValueRef. JSUtil::toJSValueRef is overloaded so we
156 // specify which version to use here by defining makeJSValue function.
157 JSStringArray(JSContextRef ctx, JSObjectRef array): JSArray<std::string>(ctx, array, JSUtil::JSValueToString, makeJSValue){}
158 JSStringArray(JSContextRef ctx): JSArray<std::string>(ctx, JSUtil::JSValueToString, makeJSValue){}
159 // We need to duplicate this function from JSArray because otherwise
160 // compiler see only assignment operator for JSArray and it doesn't
161 // propagate to derived classes.
162 void operator=( const std::vector<std::string>& list ){
163 JSArray<std::string>::operator=(list);
168 class JSLongArray : public JSArray<long>{
169 static JSValueRef makeJSValue(JSContextRef ctx, long v){
170 return JSUtil::toJSValueRef(ctx, v);
173 JSLongArray(JSContextRef ctx, JSObjectRef array): JSArray<long>(ctx, array, JSUtil::JSValueToLong, makeJSValue){}
174 JSLongArray(JSContextRef ctx): JSArray<long>(ctx, JSUtil::JSValueToLong, makeJSValue){}
175 void operator=( const std::vector<long>& list ){
176 JSArray<long>::operator=(list);
182 template <class ObjectSharedPtr, class ObjectJSClass>
183 class JSObjectArray : public JSArray<ObjectSharedPtr>{
184 static JSValueRef makeJSValue(JSContextRef ctx, ObjectSharedPtr v){
185 return ObjectJSClass::makeJSObject(ctx, v);
188 // We need to pass pointer to function converting
189 // ObjectSharedPtr -> JSValueRef. ObjectJSClass::makeJSObject returns
190 // JSObjectRef so we use makeJSValue function to convert JSObjectRef to
192 JSObjectArray(JSContextRef ctx, JSObjectRef array):
193 JSArray<ObjectSharedPtr>(ctx, array,
194 ObjectJSClass::getPrivateObject, makeJSValue){}
195 JSObjectArray(JSContextRef ctx):
196 JSArray<ObjectSharedPtr>(ctx,
197 ObjectJSClass::getPrivateObject, makeJSValue){}
198 void operator=( const std::vector<ObjectSharedPtr>& list ){
199 JSArray<ObjectSharedPtr>::operator=(list);
205 #endif //__TIZEN_COMMON_JSARRAY_H__