diff options
Diffstat (limited to 'mobile_src/Common/JSArray.h')
-rwxr-xr-x | mobile_src/Common/JSArray.h | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/mobile_src/Common/JSArray.h b/mobile_src/Common/JSArray.h new file mode 100755 index 0000000..c27ca21 --- /dev/null +++ b/mobile_src/Common/JSArray.h @@ -0,0 +1,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__ + |