/*------------------------------------------------------------------------ * (The MIT License) * * Copyright (c) 2008-2011 Rhomobile, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * http://rhomobile.com *------------------------------------------------------------------------*/ #ifndef _RHODES_H_D9BFC7B4FD394BECAF7EC535453253CC #define _RHODES_H_D9BFC7B4FD394BECAF7EC535453253CC #include #include #include JNIEnv *jnienv(); jclass rho_find_class(JNIEnv *env, const char *c); extern "C" void *rho_nativethread_start(); extern "C" void rho_nativethread_end(void *); std::string const &rho_root_path(); std::string const &rho_apk_path(); std::string rho_cur_path(); template class jholder { jholder& operator=(const jholder& ); public: jholder(T obj) : m_object(obj) {} ~jholder() { if (m_object) jnienv()->DeleteLocalRef(m_object); } jholder& operator=(T obj) { m_object = obj; return *this; } T get() { return m_object; } const T get() const { return m_object; } T release() { T ret = m_object; m_object = NULL; return ret; } void deleteRef() { if (m_object) jnienv()->DeleteLocalRef(m_object); m_object = 0; } operator bool () const { return m_object != 0; } bool operator!() const { return !m_object; } private: T m_object; }; template <> class jholder { typedef jboolean T; T m_value; public: jholder() : m_value(JNI_FALSE) {} jholder(T val) : m_value(val) {} jholder& operator=(T val) { m_value = val; return *this; } T get() const { return m_value; } T release() { return m_value; } operator bool () const { return true; } bool operator! () const { return false; } }; template <> class jholder { typedef jdouble T; T m_value; public: jholder() : m_value(0.0) {} jholder(T val) : m_value(val) {} jholder& operator=(T val) { m_value = val; return *this; } T get() const { return m_value; } T release() { return m_value; } operator bool () const { return true; } bool operator! () const { return false; } }; template <> class jholder { typedef jint T; T m_value; public: jholder() : m_value(0) {} jholder(T val) : m_value(val) {} jholder& operator=(T val) { m_value = val; return *this; } T get() const { return m_value; } T release() { return m_value; } operator bool () const { return true; } bool operator! () const { return false; } }; typedef jholder jhobject; typedef jholder jhstring; typedef jholder jharray; namespace rho { template class Hashtable; template class Vector; } typedef std::auto_ptr > HStringVector; typedef std::auto_ptr > HStringMap; template T rho_cast(JNIEnv *env, U u); template T rho_cast(U u); template T rho_cast(JNIEnv *env, U keys, U vals); template T rho_cast(U keys, U vals); template struct ArgumentsAdapter; template ArgumentsAdapter argumentsAdapter(const T& arguments) { return ArgumentsAdapter(arguments); } template struct PropertyMapConvertor; template jobject convertToPropertyMap(JNIEnv *env, T value) { PropertyMapConvertor convertor; return convertor.convertToPropertyMap(env, value); } namespace details { template struct rho_cast_helper; template <> struct rho_cast_helper { std::string operator()(JNIEnv *env, jstring ); }; template <> struct rho_cast_helper { jstring operator()(JNIEnv *env, char const *); }; template <> struct rho_cast_helper { jstring operator()(JNIEnv *env, char *s) { return rho_cast_helper()(env, s);} }; template struct rho_cast_helper { jstring operator()(JNIEnv *env, char (&s)[N]) { return rho_cast_helper()(env, &s[0]);} }; template <> struct rho_cast_helper { jstring operator()(JNIEnv *env, std::string const &s) { return rho_cast_helper()(env, s.c_str());} }; template <> struct rho_cast_helper { jboolean operator()(JNIEnv *env, bool arg) { return static_cast(arg); } }; template <> struct rho_cast_helper { jint operator()(JNIEnv *env, int arg) { return static_cast(arg); } }; template <> struct rho_cast_helper { jdouble operator()(JNIEnv *env, double arg) { return static_cast(arg); } }; struct RhoJniConvertor { static jclass clsBoolean; static jclass clsInteger; static jclass clsDouble; static jclass clsString; static jclass clsCollection; static jclass clsMap; static jclass clsSet; static jclass clsHashMap; static jclass clsArrayList; static jclass clsIterator; static jmethodID midCollectionIterator; static jmethodID midMapGet; static jmethodID midMapKeySet; static jmethodID midArrayList; static jmethodID midArrayListAdd; static jmethodID midHashMap; static jmethodID midHashMapPut; static jmethodID midSetIterator; static jmethodID midIteratorHasNext; static jmethodID midIteratorNext; static jmethodID midBooleanValue; static jmethodID midBooleanValueOf; static jmethodID midIntValue; static jmethodID midInteger; static jmethodID midDoubleValue; static jmethodID midDoubleValueOf; RhoJniConvertor() : m_env(0) {} bool initConvertor(JNIEnv *env); JNIEnv* m_env; jobject getBooleanObject(bool val); jobject getIntegerObject(int val); jobject getDoubleObject(double val); }; template <> struct rho_cast_helper: public RhoJniConvertor { typedef HStringMap value_type; value_type operator()(JNIEnv *env, jobject jObj); }; template <> struct rho_cast_helper { typedef HStringMap value_type; value_type operator()(JNIEnv *env, jobjectArray jKeys, jobjectArray jVals); }; template <> struct rho_cast_helper { typedef HStringVector value_type; value_type operator()(JNIEnv *env, jobjectArray jArr); }; template <> struct rho_cast_helper: public RhoJniConvertor { typedef HStringVector value_type; value_type operator()(JNIEnv *env, jobject jList); }; template <> struct rho_cast_helper >: public RhoJniConvertor { jobject operator()(JNIEnv* env, const rho::Vector& stringVector); }; } // namespace details template T rho_cast(JNIEnv *env, U u) { return details::rho_cast_helper()(env, u); } template T rho_cast(U u) { return details::rho_cast_helper()(jnienv(), u); } template T rho_cast(JNIEnv *env, U keys, U vals) { return details::rho_cast_helper()(env, keys, vals); } template T rho_cast(U keys, U vals) { return details::rho_cast_helper()(jnienv(), keys, vals); } template T rho_cast(JNIEnv *env, const jholder& u) { return details::rho_cast_helper()(env, u.get()); } template T rho_cast(const jholder& u) { return details::rho_cast_helper()(jnienv(), u.get()); } template T rho_cast(JNIEnv *env, const jholder& keys, const jholder& vals) { return details::rho_cast_helper()(env, keys.get(), vals.get()); } template T rho_cast(const jholder& keys, const jholder& vals) { return details::rho_cast_helper()(jnienv(), keys.get(), vals.get()); } #endif // _RHODES_H_D9BFC7B4FD394BECAF7EC535453253CC