tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Common / JSArray.h
1 //
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
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
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.
13 //
14
15 #ifndef __TIZEN_COMMON_JSARRAY_H__
16 #define __TIZEN_COMMON_JSARRAY_H__
17
18 #include <JavaScriptCore/JavaScript.h>
19 #include <vector>
20 #include "JSUtil.h"
21
22 namespace DeviceAPI {
23 namespace Common {
24
25 class JSArrayBase{
26 public:
27     JSArrayBase( JSContextRef context, JSObjectRef array );
28     JSArrayBase( JSContextRef context );
29
30     virtual ~JSArrayBase();
31     size_t size() const;
32     void resize(size_t size);
33     JSValueRef get(int index);
34     bool set(int index, JSValueRef value);
35     bool append(JSValueRef value);
36
37 protected:
38     JSContextRef mContext;
39     JSObjectRef mArray;
40 };
41
42
43 template <typename T>
44 class JSArray : protected JSArrayBase{
45     friend class ItemProxy;
46 public:
47     typedef JSValueRef (*ToJSFunction)(JSContextRef, T);
48     typedef T (*ToNativeFunction)(JSContextRef, JSValueRef );
49
50
51     JSArray( JSContextRef context, JSObjectRef array, ToNativeFunction nativefun, ToJSFunction jsfun)
52        :JSArrayBase(context,array),mToNative(nativefun), mToJs(jsfun) {
53     }
54     JSArray( JSContextRef context, ToNativeFunction nativefun, ToJSFunction jsfun)
55        :JSArrayBase(context),mToNative(nativefun), mToJs(jsfun) {
56     }
57
58     ~JSArray(){
59     }
60
61     class ItemProxy {
62         JSArray<T> *mArray;
63         int mIndex;
64         public:
65             ItemProxy(JSArray<T>* array, int index):mArray(array), mIndex(index){
66             }
67             operator T(){
68                 return mArray->mToNative(mArray->mContext, mArray->get(mIndex));
69             }
70             ItemProxy& operator=( const T native){
71                 JSValueRef v = mArray->mToJs(mArray->mContext, native);
72                 mArray->set(mIndex, v);
73                 return *this;
74             }
75             ItemProxy& operator=( const ItemProxy& other){
76                 JSValueRef v = other.mArray->get(other.mIndex);
77                 mArray->set(mIndex, v);
78                 return *this;
79             }
80
81     };
82     size_t size() const{
83         return JSArrayBase::size();
84     }
85
86     void resize(size_t array_size){
87         JSArrayBase::resize(array_size);
88     }
89
90     bool empty() const{
91         return size() == 0;
92     }
93
94     bool append( T v){
95         return JSArrayBase::set( size(), mToJs(mContext, v));
96     }
97
98     ItemProxy operator[]( int index ){
99         return ItemProxy(this, index);
100     }
101
102     operator JSObjectRef(){
103         return mArray;
104     }
105
106     operator std::vector<T>(){
107         std::vector<T> v;
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);
112             v.push_back(tmp);
113         }
114         return v;
115     }
116
117     void operator=( const std::vector<T>& list ){
118         overwrite(list);
119     }
120
121     void operator=( const JSArray<T>& rhs){
122         resize(rhs.size());
123         for(unsigned int i = 0 ; i < rhs.size(); i++){
124             set(i, rhs.get(i));
125         }
126     }
127
128     JSContextRef getContext(){
129         return mContext;
130     }
131
132 protected:
133     void overwrite( const std::vector<T>& list ){
134         unsigned int i;
135         unsigned int listSize = list.size();
136         resize(listSize);
137         for( i = 0 ; i < listSize ; i++){
138             JSValueRef v = mToJs(mContext, list[i]);
139             set(i, v);
140         }
141     }
142
143 private:
144     ToNativeFunction mToNative;
145     ToJSFunction mToJs;
146 };
147
148
149 class JSStringArray : public JSArray<std::string>{
150     static JSValueRef makeJSValue(JSContextRef ctx, std::string v){
151         return JSUtil::toJSValueRef(ctx, v);
152     }
153     public:
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);
164         }
165 };
166
167
168 class JSLongArray : public JSArray<long>{
169     static JSValueRef makeJSValue(JSContextRef ctx, long v){
170         return JSUtil::toJSValueRef(ctx, v);
171     }
172     public:
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);
177         }
178
179 };
180
181
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);
186     }
187     public:
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
191         // JSValueRef.
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);
200         }
201 };
202
203 }
204 }
205 #endif //__TIZEN_COMMON_JSARRAY_H__
206