1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef __TIZEN_COMMON_JSARRAY_H__
#define __TIZEN_COMMON_JSARRAY_H__
#include <JavaScriptCore/JavaScript.h>
#include <vector>
#include "JSUtil.h"
namespace DeviceAPI {
namespace Common {
class JSArrayBase{
public:
JSArrayBase( JSContextRef context, JSObjectRef array );
JSArrayBase( JSContextRef context );
virtual ~JSArrayBase();
size_t size() const;
void resize(size_t size);
JSValueRef get(int index) const;
bool set(int index, JSValueRef value);
bool append(JSValueRef value);
protected:
JSContextRef mContext;
JSObjectRef mArray;
};
template <typename T>
class JSArray : protected JSArrayBase{
friend class ItemProxy;
public:
typedef JSValueRef (*ToJSFunction)(JSContextRef, T);
typedef T (*ToNativeFunction)(JSContextRef, JSValueRef );
JSArray( JSContextRef context, JSObjectRef array, ToNativeFunction nativefun, ToJSFunction jsfun)
:JSArrayBase(context,array),mToNative(nativefun), mToJs(jsfun) {
}
JSArray( JSContextRef context, ToNativeFunction nativefun, ToJSFunction jsfun)
:JSArrayBase(context),mToNative(nativefun), mToJs(jsfun) {
}
~JSArray(){
}
class ItemProxy {
JSArray<T> *mArray;
int mIndex;
public:
ItemProxy(JSArray<T>* array, int index):mArray(array), mIndex(index){
}
operator T(){
return mArray->mToNative(mArray->mContext, mArray->get(mIndex));
}
ItemProxy& operator=( const T native){
JSValueRef v = mArray->mToJs(mArray->mContext, native);
mArray->set(mIndex, v);
return *this;
}
ItemProxy& operator=( const ItemProxy& other){
JSValueRef v = other.mArray->get(other.mIndex);
mArray->set(mIndex, v);
return *this;
}
};
size_t size() const{
return JSArrayBase::size();
}
void resize(size_t size){
JSArrayBase::resize(size);
}
bool append( T v){
return JSArrayBase::set( size(), mToJs(mContext, v));
}
ItemProxy operator[]( int index ){
return ItemProxy(this, index);
}
operator JSObjectRef(){
return mArray;
}
operator std::vector<T>(){
std::vector<T> v;
size_t length = size();
for( unsigned int i = 0 ; i < length ; i++){
JSValueRef t = get(i);
T tmp = mToNative(mContext, t);
v.push_back(tmp);
}
return v;
}
void operator=( const std::vector<T>& list ){
overwrite(list);
}
void operator=( const JSArray<T>& rhs){
resize(rhs.size());
for(unsigned int i = 0 ; i < rhs.size(); i++){
set(i, rhs.get(i));
}
}
protected:
void overwrite( const std::vector<T>& list ){
unsigned int i;
unsigned int listSize = list.size();
resize(listSize);
for( i = 0 ; i < listSize ; i++){
JSValueRef v = mToJs(mContext, list[i]);
set(i, v);
}
}
private:
ToNativeFunction mToNative;
ToJSFunction mToJs;
};
class JSStringArray : public JSArray<std::string>{
static JSValueRef makeJSValue(JSContextRef ctx, std::string v){
return JSUtil::toJSValueRef(ctx, v);
}
public:
JSStringArray(JSContextRef ctx, JSObjectRef array): JSArray<std::string>(ctx, array, JSUtil::JSValueToString, makeJSValue){}
JSStringArray(JSContextRef ctx): JSArray<std::string>(ctx, JSUtil::JSValueToString, makeJSValue){}
void operator=( const std::vector<std::string>& list ){overwrite(list);}
};
class JSLongArray : public JSArray<long>{
static JSValueRef makeJSValue(JSContextRef ctx, long v){
return JSUtil::toJSValueRef(ctx, v);
}
public:
JSLongArray(JSContextRef ctx, JSObjectRef array): JSArray<long>(ctx, array, JSUtil::JSValueToLong, makeJSValue){}
JSLongArray(JSContextRef ctx): JSArray<long>(ctx, JSUtil::JSValueToLong, makeJSValue){}
void operator=( const std::vector<long>& list ){overwrite(list);}
};
}
}
#endif //__TIZEN_COMMON_JSARRAY_H__
|