diff options
| author | Rajith Muditha Attapattu <rajith@apache.org> | 2012-06-07 17:35:14 +0000 |
|---|---|---|
| committer | Rajith Muditha Attapattu <rajith@apache.org> | 2012-06-07 17:35:14 +0000 |
| commit | ee0fa412153e309878ee8971b0872b97b4f13979 (patch) | |
| tree | cb43b627a478ffc91cdaeec3e32971eadddee97b | |
| parent | 3fa6a5e39c2ff12e9b37426b5e7f0e7f88a345b9 (diff) | |
| download | qpid-python-ee0fa412153e309878ee8971b0872b97b4f13979.tar.gz | |
QPID-4027 Fixed the handling of VariantMaps and Variant types.
Added freeargs to delete the args created on the heap.
WriteOnlyVariantMapWrapper takes care of mapping a Java Map into a
VariantMap. This is inefficient, but it's only used in non critical
code.
The ReadOnlyVariantMapWrapper wraps a VariantMap and provides a
java.util.Map interface to the Java code. As the name suggests it's
read-only :).
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/address-refactor2@1347731 13f79535-47bb-0310-9956-ffa450edef68
| -rw-r--r-- | qpid/cpp/bindings/swig_java_cpp_helper.i | 57 | ||||
| -rw-r--r-- | qpid/cpp/bindings/swig_java_helper.i | 10 | ||||
| -rw-r--r-- | qpid/cpp/bindings/swig_java_typemaps.i | 16 |
3 files changed, 69 insertions, 14 deletions
diff --git a/qpid/cpp/bindings/swig_java_cpp_helper.i b/qpid/cpp/bindings/swig_java_cpp_helper.i index e782d20535..7a5fadd843 100644 --- a/qpid/cpp/bindings/swig_java_cpp_helper.i +++ b/qpid/cpp/bindings/swig_java_cpp_helper.i @@ -342,8 +342,8 @@ class ReadOnlyVariantMapWrapper { ReadOnlyVariantMapWrapper(); ReadOnlyVariantMapWrapper(JNIEnv* jniEnv, qpid::types::Variant::Map& map); ~ReadOnlyVariantMapWrapper(); - jobject get(const std::string key) const; - jboolean containsKey (const std::string key) const; + jobject get(const std::string& key) const; + jboolean containsKey (const std::string& key) const; jboolean isEmpty() const; // keys_ is populated on demand (i.e if the keys() method is called). // since this method modifies keys_ it cannot be marked const. @@ -361,11 +361,19 @@ class ReadOnlyVariantMapWrapper { * Provides a write-only Variant::Map * This is only intended to be used with code that is not in the critical path. * Ex: not be used with setting message properties. - * This is intneded to be used when setting connection properties + * This is intended to be used when setting connection properties * or address properties. */ class WriteOnlyVariantMapWrapper { + public: + WriteOnlyVariantMapWrapper(); + ~WriteOnlyVariantMapWrapper(){} + void put(const std::string& key, jobject obj); + qpid::types::Variant::Map getVariantMap() const { return varMap_; } + + private: + qpid::types::Variant::Map varMap_; }; @@ -388,7 +396,7 @@ ReadOnlyVariantMapWrapper::~ReadOnlyVariantMapWrapper() } } -jobject ReadOnlyVariantMapWrapper::get(const std::string key) const +jobject ReadOnlyVariantMapWrapper::get(const std::string& key) const { using namespace qpid::types; jobject result; @@ -464,10 +472,11 @@ jobject ReadOnlyVariantMapWrapper::get(const std::string key) const } } + env_->DeleteLocalRef(result); return result; } -jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string key) const +jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string& key) const { return (jboolean)(varMap_.find(key) != varMap_.end()); } @@ -499,6 +508,35 @@ jobjectArray ReadOnlyVariantMapWrapper::keys() return keys_; } +WriteOnlyVariantMapWrapper::WriteOnlyVariantMapWrapper() +{ + varMap_ = qpid::types::Variant::Map(); +} + +void WriteOnlyVariantMapWrapper::put(const std::string& key, jobject obj) +{ + JNIEnv* env; + cachedJVM->GetEnv((void **)&env, JNI_VERSION_1_4); + + if (key.empty()) + { + env->ThrowNew(JAVA_ILLEGAL_ARGUMENT_EXP,"Key cannot be empty."); + } + + qpid::types::Variant v = convertJavaObjectToVariant(env,obj); + + if (v) + { + varMap_[key] = v; + } + else + { + // There will be an exception on the java side, + // thrown by convertJavaObjectToVariant method. + return; + } +} + %} /** =================== SWIG Interface definitions ============================ @@ -508,8 +546,13 @@ jobjectArray ReadOnlyVariantMapWrapper::keys() ReadOnlyVariantMapWrapper::ReadOnlyVariantMapWrapper(); ReadOnlyVariantMapWrapper::ReadOnlyVariantMapWrapper(JNIEnv* jniEnv, qpid::types::Variant::Map& map); ReadOnlyVariantMapWrapper::~ReadOnlyVariantMapWrapper(); -jobject ReadOnlyVariantMapWrapper::get(const std::string key) const; -jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string key) const; +jobject ReadOnlyVariantMapWrapper::get(const std::string& key) const; +jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string& key) const; jboolean ReadOnlyVariantMapWrapper::isEmpty() const; jobjectArray ReadOnlyVariantMapWrapper::keys(); jint ReadOnlyVariantMapWrapper::size() const; + +WriteOnlyVariantMapWrapper::WriteOnlyVariantMapWrapper(); +WriteOnlyVariantMapWrapper::~WriteOnlyVariantMapWrapper(); +void WriteOnlyVariantMapWrapper::put(const std::string& key, jobject obj); +qpid::types::Variant::Map WriteOnlyVariantMapWrapper::getVariantMap() const; diff --git a/qpid/cpp/bindings/swig_java_helper.i b/qpid/cpp/bindings/swig_java_helper.i index a6648d3c5e..a5bd8d1b9d 100644 --- a/qpid/cpp/bindings/swig_java_helper.i +++ b/qpid/cpp/bindings/swig_java_helper.i @@ -61,10 +61,14 @@ import java.util.Set; } } - /** We don't support setting maps into C++ atm, but adding here to get around swig **/ - static ReadOnlyVariantMapWrapper getVariantMap(final Map<String,Object> map) + static long getVariantMap(final Map<String,Object> m) { - return new ReadOnlyVariantMapWrapper(); + WriteOnlyVariantMapWrapper map = new WriteOnlyVariantMapWrapper(); + for (String key: m.keySet()) + { + map.put(key,m.get(key)); + } + return WriteOnlyVariantMapWrapper.getCPtr(map); } static Map<String, Object> getJavaMap(final ReadOnlyVariantMapWrapper map) diff --git a/qpid/cpp/bindings/swig_java_typemaps.i b/qpid/cpp/bindings/swig_java_typemaps.i index 6dbefc1a7e..07b89cffa3 100644 --- a/qpid/cpp/bindings/swig_java_typemaps.i +++ b/qpid/cpp/bindings/swig_java_typemaps.i @@ -51,8 +51,8 @@ %typemap(jtype) qpid::messaging::Message::BYTE_BUFFER "java.nio.ByteBuffer" %typemap(jstype) qpid::messaging::Message::BYTE_BUFFER "java.nio.ByteBuffer" -%typemap(jni) qpid::types::Variant::Map& "jobject" -%typemap(jtype) qpid::types::Variant::Map& "ReadOnlyVariantMapWrapper" +%typemap(jni) qpid::types::Variant::Map& "jlong" +%typemap(jtype) qpid::types::Variant::Map& "long" %typemap(jstype) qpid::types::Variant::Map& "java.util.Map" %typemap(jni) const qpid::types::Variant& "jobject" @@ -90,17 +90,22 @@ /* -- qpid::types::Variant::Map& -- */ %typemap(in) (qpid::types::Variant::Map&){ - $1 = new qpid::types::Variant::Map(); + WriteOnlyVariantMapWrapper* mapper = *(WriteOnlyVariantMapWrapper **)&$input; + $1 = new qpid::types::Variant::Map((mapper->getVariantMap())); } %typemap(javain) (qpid::types::Variant::Map&) "$module.getVariantMap($javainput)" +%typemap(freearg) qpid::types::Variant::Map& { + delete $1; +} + %typemap(out) qpid::types::Variant::Map& { *(ReadOnlyVariantMapWrapper **)&jresult = new ReadOnlyVariantMapWrapper(jenv,*$1); } %typemap(javaout) qpid::types::Variant::Map& { - return $module.getJavaMap($jnicall); + return $module.getJavaMap(new ReadOnlyVariantMapWrapper($jnicall, $owner)); } /* -- qpid::types::Variant& -- */ @@ -120,6 +125,9 @@ %typemap(javain) (const qpid::types::Variant&) "$javainput" +%typemap(freearg) qpid::types::Variant& { + delete $1; +} /* -- qpid::types::uint8_t -- */ %typemap(in) uint8_t { |
