diff options
author | Ted Ross <tross@apache.org> | 2010-07-29 18:14:20 +0000 |
---|---|---|
committer | Ted Ross <tross@apache.org> | 2010-07-29 18:14:20 +0000 |
commit | 3f490a660690ec61dd6bb670c52fa8d8ada2fab4 (patch) | |
tree | a85ae15cce1b05158270e23455a66cd999207e51 | |
parent | fdf959cf333c52389821000ddb369975b99458b1 (diff) | |
download | qpid-python-3f490a660690ec61dd6bb670c52fa8d8ada2fab4.tar.gz |
QPID-2777 - Additional bindings for the C++ messaging API (Python, Ruby)
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@980531 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | cpp/Makefile.am | 5 | ||||
-rw-r--r-- | cpp/bindings/qpid/Makefile.am | 33 | ||||
-rw-r--r-- | cpp/bindings/qpid/python/Makefile.am | 48 | ||||
-rw-r--r-- | cpp/bindings/qpid/python/python.i | 38 | ||||
-rw-r--r-- | cpp/bindings/qpid/qpid.i | 73 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/Makefile.am | 43 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/ruby.i | 38 | ||||
-rw-r--r-- | cpp/bindings/swig_python_typemaps.i | 354 | ||||
-rw-r--r-- | cpp/bindings/swig_ruby_typemaps.i | 313 | ||||
-rw-r--r-- | cpp/configure.ac | 3 |
10 files changed, 946 insertions, 2 deletions
diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 7cd022fb08..60ef976b07 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -25,9 +25,10 @@ ACLOCAL_AMFLAGS = -I m4 EXTRA_DIST = \ LICENSE NOTICE README.txt SSL RELEASE_NOTES DESIGN \ xml/cluster.xml INSTALL-WINDOWS CMakeLists.txt BuildInstallSettings.cmake \ - packaging/NSIS QPID_VERSION.txt + packaging/NSIS QPID_VERSION.txt bindings/swig_python_typemaps.i \ + bindings/swig_ruby_typemaps.i -SUBDIRS = managementgen etc src docs/api docs/man examples bindings/qmf +SUBDIRS = managementgen etc src docs/api docs/man examples bindings/qmf bindings/qpid # Update libtool, if needed. libtool: $(LIBTOOL_DEPS) diff --git a/cpp/bindings/qpid/Makefile.am b/cpp/bindings/qpid/Makefile.am new file mode 100644 index 0000000000..13ec310a9e --- /dev/null +++ b/cpp/bindings/qpid/Makefile.am @@ -0,0 +1,33 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +if HAVE_SWIG + +EXTRA_DIST = qpid.i +SUBDIRS = + +if HAVE_RUBY_DEVEL +SUBDIRS += ruby +endif + +if HAVE_PYTHON_DEVEL +SUBDIRS += python +endif + +endif diff --git a/cpp/bindings/qpid/python/Makefile.am b/cpp/bindings/qpid/python/Makefile.am new file mode 100644 index 0000000000..6701ce0ac3 --- /dev/null +++ b/cpp/bindings/qpid/python/Makefile.am @@ -0,0 +1,48 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +if HAVE_PYTHON_DEVEL + +INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/src/qmf -I$(top_srcdir)/src -I$(top_builddir)/src + +generated_file_list = \ + qpidw.cpp \ + qpidw.py + +EXTRA_DIST = python.i +BUILT_SOURCES = $(generated_file_list) + +$(generated_file_list): $(srcdir)/python.i $(srcdir)/../qpid.i $(srcdir)/../../swig_python_typemaps.i + swig -c++ -python -Wall $(INCLUDES) $(QPID_CXXFLAGS) -I$(top_srcdir)/src/qmf -I/usr/include -o qpidw.cpp $(srcdir)/python.i + +pylibdir = $(PYTHON_LIB) + +lib_LTLIBRARIES = _qpidw.la + +#_qpidw_la_LDFLAGS = -avoid-version -module -shrext "$(PYTHON_SO)" +#_qpidw_la_LDFLAGS = -avoid-version -module -shrext ".so" +_qpidw_la_LDFLAGS = -avoid-version -module -shared +_qpidw_la_LIBADD = $(PYTHON_LIBS) -L$(top_builddir)/src/.libs -lqpidmessaging -lqpidtypes $(top_builddir)/src/libqpidmessaging.la $(top_builddir)/src/libqpidtypes.la +_qpidw_la_CXXFLAGS = $(INCLUDES) -I$(PYTHON_INC) +nodist__qpidw_la_SOURCES = qpidw.cpp + +CLEANFILES = $(generated_file_list) + +endif # HAVE_PYTHON_DEVEL + diff --git a/cpp/bindings/qpid/python/python.i b/cpp/bindings/qpid/python/python.i new file mode 100644 index 0000000000..e69c6b01cc --- /dev/null +++ b/cpp/bindings/qpid/python/python.i @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +%module qpidw +%include "std_string.i" +%include "std_map.i" +%include "std_list.i" +%include "../../swig_python_typemaps.i" + +/* Define the general-purpose exception handling */ +%exception { + try { + $action + } + catch (qpid::messaging::MessagingException& mex) { + PyErr_SetString(PyExc_RuntimeError, mex.what()); + return NULL; + } +} + +%include "../qpid.i" + diff --git a/cpp/bindings/qpid/qpid.i b/cpp/bindings/qpid/qpid.i new file mode 100644 index 0000000000..3dd07f90ab --- /dev/null +++ b/cpp/bindings/qpid/qpid.i @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +%{ + +#include <qpid/messaging/exceptions.h> +#include <qpid/messaging/Address.h> +#include <qpid/messaging/Connection.h> +#include <qpid/messaging/Session.h> +#include <qpid/messaging/Receiver.h> +#include <qpid/messaging/Sender.h> +#include <qpid/messaging/Message.h> +#include <qpid/messaging/Duration.h> + +// +// Wrapper functions for map-decode and list-decode. This allows us to avoid +// the complexity of output parameter mapping. +// +qpid::types::Variant::Map& decodeMap(const qpid::messaging::Message& msg) { + static qpid::types::Variant::Map map; + map.clear(); + qpid::messaging::decode(msg, map); + return map; +} + +qpid::types::Variant::List& decodeList(const qpid::messaging::Message& msg) { + static qpid::types::Variant::List list; + list.clear(); + qpid::messaging::decode(msg, list); + return list; +} + +%} + + +%include <qpid/types/ImportExport.h> +%include <qpid/messaging/ImportExport.h> +%include <qpid/types/Exception.h> +%include <qpid/messaging/exceptions.h> +%include <qpid/types/Uuid.h> +%include <qpid/types/Variant.h> +%include <qpid/messaging/Address.h> +%include <qpid/messaging/Duration.h> +%include <qpid/messaging/Message.h> +%include <qpid/messaging/Receiver.h> +%include <qpid/messaging/Sender.h> +%include <qpid/messaging/Session.h> +%include <qpid/messaging/Connection.h> + +qpid::types::Variant::Map& decodeMap(const qpid::messaging::Message&); +qpid::types::Variant::List& decodeList(const qpid::messaging::Message&); + + +%{ + +%}; + diff --git a/cpp/bindings/qpid/ruby/Makefile.am b/cpp/bindings/qpid/ruby/Makefile.am new file mode 100644 index 0000000000..cb187f8dc3 --- /dev/null +++ b/cpp/bindings/qpid/ruby/Makefile.am @@ -0,0 +1,43 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +if HAVE_RUBY_DEVEL + +INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/src -I$(top_builddir)/src + +EXTRA_DIST = ruby.i +BUILT_SOURCES = qpidw.cpp + +rubylibdir = $(RUBY_LIB) + +qpidw.cpp: $(srcdir)/ruby.i $(srcdir)/../qpid.i $(srcdir)/../../swig_ruby_typemaps.i + $(SWIG) -ruby -c++ -Wall $(INCLUDES) $(QPID_CXXFLAGS) -I/usr/include -o qpidw.cpp $(srcdir)/ruby.i + +rubylibarchdir = $(RUBY_LIB_ARCH) +rubylibarch_LTLIBRARIES = qpidw.la + +qpidw_la_LDFLAGS = -avoid-version -module -shrext ".$(RUBY_DLEXT)" +qpidw_la_LIBADD = $(RUBY_LIBS) -L$(top_builddir)/src/.libs -lqpidmessaging -lqpidtypes \ + $(top_builddir)/src/libqpidmessaging.la $(top_builddir)/src/libqpidtypes.la +qpidw_la_CXXFLAGS = $(INCLUDES) -I$(RUBY_INC) -I$(RUBY_INC_ARCH) +nodist_qpidw_la_SOURCES = qpidw.cpp + +CLEANFILES = qpidw.cpp + +endif # HAVE_RUBY_DEVEL diff --git a/cpp/bindings/qpid/ruby/ruby.i b/cpp/bindings/qpid/ruby/ruby.i new file mode 100644 index 0000000000..926548b3ee --- /dev/null +++ b/cpp/bindings/qpid/ruby/ruby.i @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +%module qpidw +%include "std_string.i" +%include "std_map.i" +%include "std_list.i" +%include "../../swig_ruby_typemaps.i" + +/* Define the general-purpose exception handling */ +%exception { + try { + $action + } + catch (qpid::messaging::MessagingException& mex) { + static VALUE merror = rb_define_class("MessagingError", rb_eStandardError); + rb_raise(merror, mex.what()); + } +} + +%include "../qpid.i" + diff --git a/cpp/bindings/swig_python_typemaps.i b/cpp/bindings/swig_python_typemaps.i new file mode 100644 index 0000000000..3f6be5f4e2 --- /dev/null +++ b/cpp/bindings/swig_python_typemaps.i @@ -0,0 +1,354 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +%wrapper %{ + + PyObject* MapToPy(const qpid::types::Variant::Map*); + PyObject* ListToPy(const qpid::types::Variant::List*); + void PyToMap(PyObject*, qpid::types::Variant::Map*); + void PyToList(PyObject*, qpid::types::Variant::List*); + + qpid::types::Variant PyToVariant(PyObject* value) { + if (PyFloat_Check(value)) return qpid::types::Variant(PyFloat_AS_DOUBLE(value)); + if (PyString_Check(value)) return qpid::types::Variant(std::string(PyString_AS_STRING(value))); + if (PyInt_Check(value)) return qpid::types::Variant(PyInt_AS_LONG(value)); + if (PyLong_Check(value)) return qpid::types::Variant(int64_t(PyLong_AsLongLong(value))); + if (PyBool_Check(value)) return qpid::types::Variant(PyInt_AS_LONG(value) ? true : false); + if (PyDict_Check(value)) { + qpid::types::Variant::Map map; + PyToMap(value, &map); + return qpid::types::Variant(map); + } + if (PyList_Check(value)) { + qpid::types::Variant::List list; + PyToList(value, &list); + return qpid::types::Variant(list); + } + return qpid::types::Variant(); + } + + PyObject* VariantToPy(const qpid::types::Variant* v) { + PyObject* result; + try { + switch (v->getType()) { + case qpid::types::VAR_VOID: { + result = Py_None; + break; + } + case qpid::types::VAR_BOOL : { + result = v->asBool() ? Py_True : Py_False; + break; + } + case qpid::types::VAR_UINT8 : + case qpid::types::VAR_UINT16 : + case qpid::types::VAR_UINT32 : { + result = PyInt_FromLong((long) v->asUint32()); + break; + } + case qpid::types::VAR_UINT64 : { + result = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) v->asUint64()); + break; + } + case qpid::types::VAR_INT8 : + case qpid::types::VAR_INT16 : + case qpid::types::VAR_INT32 : { + result = PyInt_FromLong((long) v->asInt32()); + break; + } + case qpid::types::VAR_INT64 : { + result = PyLong_FromLongLong((PY_LONG_LONG) v->asInt64()); + break; + } + case qpid::types::VAR_FLOAT : { + result = PyFloat_FromDouble((double) v->asFloat()); + break; + } + case qpid::types::VAR_DOUBLE : { + result = PyFloat_FromDouble((double) v->asDouble()); + break; + } + case qpid::types::VAR_STRING : { + const std::string val(v->asString()); + result = PyString_FromStringAndSize(val.c_str(), val.size()); + break; + } + case qpid::types::VAR_MAP : { + result = MapToPy(&(v->asMap())); + break; + } + case qpid::types::VAR_LIST : { + result = ListToPy(&(v->asList())); + break; + } + case qpid::types::VAR_UUID : { + } + } + } catch (qpid::types::Exception& ex) { + PyErr_SetString(PyExc_RuntimeError, ex.what()); + result = 0; + } + + if (result) + Py_INCREF(result); + return result; + } + + PyObject* MapToPy(const qpid::types::Variant::Map* map) { + PyObject* result = PyDict_New(); + qpid::types::Variant::Map::const_iterator iter; + for (iter = map->begin(); iter != map->end(); iter++) { + const std::string key(iter->first); + PyObject* pyval = VariantToPy(&(iter->second)); + if (pyval == 0) + return 0; + PyDict_SetItem(result, PyString_FromStringAndSize(key.c_str(), key.size()), pyval); + } + return result; + } + + PyObject* ListToPy(const qpid::types::Variant::List* list) { + PyObject* result = PyList_New(list->size()); + qpid::types::Variant::List::const_iterator iter; + Py_ssize_t idx(0); + for (iter = list->begin(); iter != list->end(); iter++) { + PyObject* pyval = VariantToPy(&(*iter)); + if (pyval == 0) + return 0; + PyList_SetItem(result, idx, pyval); + idx++; + } + return result; + } + + void PyToMap(PyObject* obj, qpid::types::Variant::Map* map) { + map->clear(); + Py_ssize_t iter(0); + PyObject *key; + PyObject *val; + while (PyDict_Next(obj, &iter, &key, &val)) + (*map)[std::string(PyString_AS_STRING(key))] = PyToVariant(val); + } + + void PyToList(PyObject* obj, qpid::types::Variant::List* list) { + list->clear(); + Py_ssize_t count(PyList_Size(obj)); + for (Py_ssize_t idx = 0; idx < count; idx++) + list->push_back(PyToVariant(PyList_GetItem(obj, idx))); + } + +%} + + +/* unsigned32 Convert from Python --> C */ +%typemap(in) uint32_t { + if (PyInt_Check($input)) { + $1 = (uint32_t) PyInt_AsUnsignedLongMask($input); + } else if (PyLong_Check($input)) { + $1 = (uint32_t) PyLong_AsUnsignedLong($input); + } else { + SWIG_exception_fail(SWIG_ValueError, "unknown integer type"); + } +} + +/* unsinged32 Convert from C --> Python */ +%typemap(out) uint32_t { + $result = PyInt_FromLong((long)$1); +} + + +/* unsigned16 Convert from Python --> C */ +%typemap(in) uint16_t { + if (PyInt_Check($input)) { + $1 = (uint16_t) PyInt_AsUnsignedLongMask($input); + } else if (PyLong_Check($input)) { + $1 = (uint16_t) PyLong_AsUnsignedLong($input); + } else { + SWIG_exception_fail(SWIG_ValueError, "unknown integer type"); + } +} + +/* unsigned16 Convert from C --> Python */ +%typemap(out) uint16_t { + $result = PyInt_FromLong((long)$1); +} + + +/* signed32 Convert from Python --> C */ +%typemap(in) int32_t { + if (PyInt_Check($input)) { + $1 = (int32_t) PyInt_AsLong($input); + } else if (PyLong_Check($input)) { + $1 = (int32_t) PyLong_AsLong($input); + } else { + SWIG_exception_fail(SWIG_ValueError, "unknown integer type"); + } +} + +/* signed32 Convert from C --> Python */ +%typemap(out) int32_t { + $result = PyInt_FromLong((long)$1); +} + + +/* unsigned64 Convert from Python --> C */ +%typemap(in) uint64_t { +%#ifdef HAVE_LONG_LONG + if (PyLong_Check($input)) { + $1 = (uint64_t)PyLong_AsUnsignedLongLong($input); + } else if (PyInt_Check($input)) { + $1 = (uint64_t)PyInt_AsUnsignedLongLongMask($input); + } else +%#endif + { + SWIG_exception_fail(SWIG_ValueError, "unsupported integer size - uint64_t input too large"); + } +} + +/* unsigned64 Convert from C --> Python */ +%typemap(out) uint64_t { +%#ifdef HAVE_LONG_LONG + $result = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)$1); +%#else + SWIG_exception_fail(SWIG_ValueError, "unsupported integer size - uint64_t output too large"); +%#endif +} + +/* signed64 Convert from Python --> C */ +%typemap(in) int64_t { +%#ifdef HAVE_LONG_LONG + if (PyLong_Check($input)) { + $1 = (int64_t)PyLong_AsLongLong($input); + } else if (PyInt_Check($input)) { + $1 = (int64_t)PyInt_AsLong($input); + } else +%#endif + { + SWIG_exception_fail(SWIG_ValueError, "unsupported integer size - int64_t input too large"); + } +} + +/* signed64 Convert from C --> Python */ +%typemap(out) int64_t { +%#ifdef HAVE_LONG_LONG + $result = PyLong_FromLongLong((PY_LONG_LONG)$1); +%#else + SWIG_exception_fail(SWIG_ValueError, "unsupported integer size - int64_t output too large"); +%#endif +} + + +/* Convert from Python --> C */ +%typemap(in) void * { + $1 = (void *)$input; +} + +/* Convert from C --> Python */ +%typemap(out) void * { + $result = (PyObject *) $1; + Py_INCREF($result); +} + +%typemap(typecheck) qpid::types::Variant::Map& { + $1 = PyDict_Check($input) ? 1 : 0; +} + +/* + * Variant types: C++ --> Python + */ +%typemap(out) qpid::types::Variant::Map& { + $result = MapToPy($1); + if ($result) + Py_INCREF($result); +} + +%typemap(out) qpid::types::Variant::List& { + $result = ListToPy($1); + if ($result) + Py_INCREF($result); +} + +%typemap(out) qpid::types::Variant& { + $result = VariantToPy($1); + if ($result) + Py_INCREF($result); +} + + +/* + * Variant types: Ruby --> C++ + */ +%typemap(in) qpid::types::Variant& { + $1 = new qpid::types::Variant(PyToVariant($input)); +} + +%typemap(in) qpid::types::Variant::Map& { + $1 = new qpid::types::Variant::Map(); + PyToMap($input, $1); +} + +%typemap(in) qpid::types::Variant::List& { + $1 = new qpid::types::Variant::List(); + PyToList($input, $1); +} + +%typemap(freearg) qpid::types::Variant& { + delete $1; +} + +%typemap(freearg) qpid::types::Variant::Map& { + delete $1; +} + +%typemap(freearg) qpid::types::Variant::List& { + delete $1; +} + + +/* + * Variant types: typecheck maps + */ +%typemap(typecheck) qpid::types::Variant::Map& { + $1 = PyDict_Check($input) ? 1 : 0; +} + +%typemap(typecheck) qpid::types::Variant::List& { + $1 = PyList_Check($input) ? 1 : 0; +} + +%typemap(typecheck) qpid::types::Variant& { + $1 = (PyFloat_Check($input) || + PyString_Check($input) || + PyInt_Check($input) || + PyBool_Check($input)) ? 1 : 0; +} + +%typemap(typecheck) bool { + $1 = PyBool_Check($input) ? 1 : 0; +} + + + +%typemap (typecheck, precedence=SWIG_TYPECHECK_UINT64) uint64_t { + $1 = PyLong_Check($input) ? 1 : 0; +} + +%typemap (typecheck, precedence=SWIG_TYPECHECK_UINT32) uint32_t { + $1 = PyInt_Check($input) ? 1 : 0; +} + diff --git a/cpp/bindings/swig_ruby_typemaps.i b/cpp/bindings/swig_ruby_typemaps.i new file mode 100644 index 0000000000..beae77d3d1 --- /dev/null +++ b/cpp/bindings/swig_ruby_typemaps.i @@ -0,0 +1,313 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +%wrapper %{ + +#include <stdarg.h> + + VALUE MapToRb(const qpid::types::Variant::Map*); + VALUE ListToRb(const qpid::types::Variant::List*); + void RbToMap(VALUE, qpid::types::Variant::Map*); + void RbToList(VALUE, qpid::types::Variant::List*); + + qpid::types::Variant RbToVariant(VALUE value) { + switch (TYPE(value)) { + case T_FLOAT: return qpid::types::Variant(NUM2DBL(value)); + case T_STRING: return qpid::types::Variant(StringValuePtr(value)); + case T_FIXNUM: return qpid::types::Variant(FIX2LONG(value)); + case T_BIGNUM: return qpid::types::Variant((int64_t) NUM2LL(value)); + case T_TRUE: return qpid::types::Variant(true); + case T_FALSE: return qpid::types::Variant(false); + case T_HASH: { + qpid::types::Variant::Map map; + RbToMap(value, &map); + return qpid::types::Variant(map); + } + case T_ARRAY: { + qpid::types::Variant::List list; + RbToList(value, &list); + return qpid::types::Variant(list); + } + default: return qpid::types::Variant(); + } + } + + VALUE VariantToRb(const qpid::types::Variant* v) { + VALUE result; + try { + switch (v->getType()) { + case qpid::types::VAR_VOID: { + result = Qnil; + break; + } + case qpid::types::VAR_BOOL : { + result = v->asBool() ? Qtrue : Qfalse; + break; + } + case qpid::types::VAR_UINT8 : + case qpid::types::VAR_UINT16 : + case qpid::types::VAR_UINT32 : { + result = UINT2NUM(v->asUint32()); + break; + } + case qpid::types::VAR_UINT64 : { + result = ULL2NUM(v->asUint64()); + break; + } + case qpid::types::VAR_INT8 : + case qpid::types::VAR_INT16 : + case qpid::types::VAR_INT32 : { + result = INT2NUM(v->asInt32()); + break; + } + case qpid::types::VAR_INT64 : { + result = LL2NUM(v->asInt64()); + break; + } + case qpid::types::VAR_FLOAT : { + result = rb_float_new((double) v->asFloat()); + break; + } + case qpid::types::VAR_DOUBLE : { + result = rb_float_new(v->asDouble()); + break; + } + case qpid::types::VAR_STRING : { + const std::string val(v->asString()); + result = rb_str_new(val.c_str(), val.size()); + break; + } + case qpid::types::VAR_MAP : { + result = MapToRb(&(v->asMap())); + break; + } + case qpid::types::VAR_LIST : { + result = ListToRb(&(v->asList())); + break; + } + case qpid::types::VAR_UUID : { + } + } + } catch (qpid::types::Exception& ex) { + static VALUE error = rb_define_class("Error", rb_eStandardError); + rb_raise(error, ex.what()); + } + + return result; + } + + VALUE MapToRb(const qpid::types::Variant::Map* map) { + VALUE result = rb_hash_new(); + qpid::types::Variant::Map::const_iterator iter; + for (iter = map->begin(); iter != map->end(); iter++) { + const std::string key(iter->first); + VALUE rbval = VariantToRb(&(iter->second)); + rb_hash_aset(result, rb_str_new(key.c_str(), key.size()), rbval); + } + return result; + } + + VALUE ListToRb(const qpid::types::Variant::List* list) { + VALUE result = rb_ary_new2(list->size()); + qpid::types::Variant::List::const_iterator iter; + for (iter = list->begin(); iter != list->end(); iter++) { + VALUE rbval = VariantToRb(&(*iter)); + rb_ary_push(result, rbval); + } + return result; + } + + VALUE HashIter(VALUE data_ary, VALUE context) { + VALUE key = rb_ary_entry(data_ary, 0); + VALUE val = rb_ary_entry(data_ary, 1); + qpid::types::Variant::Map* map((qpid::types::Variant::Map*) context); + (*map)[std::string(StringValuePtr(key))] = RbToVariant(val); + return data_ary; + } + + VALUE AryIter(VALUE data, VALUE context) { + qpid::types::Variant::List* list((qpid::types::Variant::List*) context); + list->push_back(RbToVariant(data)); + return data; + } + + void RbToMap(VALUE hash, qpid::types::Variant::Map* map) { + map->clear(); + rb_iterate(rb_each, hash, (VALUE(*)(ANYARGS))HashIter, (VALUE) map); + } + + void RbToList(VALUE ary, qpid::types::Variant::List* list) { + list->clear(); + rb_iterate(rb_each, ary, (VALUE(*)(ANYARGS))AryIter, (VALUE) list); + } +%} + +%typemap (in) void * +{ + $1 = (void *) $input; +} + +%typemap (out) void * +{ + $result = (VALUE) $1; +} + +%typemap (in) uint16_t +{ + $1 = NUM2UINT ($input); +} + +%typemap (out) uint16_t +{ + $result = UINT2NUM((uint16_t) $1); +} + +%typemap (in) uint32_t +{ + if (TYPE($input) == T_BIGNUM) + $1 = NUM2UINT($input); + else + $1 = FIX2UINT($input); +} + +%typemap (out) uint32_t +{ + $result = UINT2NUM((uint32_t) $1); +} + +%typemap (in) int32_t +{ + if (TYPE($input) == T_BIGNUM) + $1 = NUM2INT($input); + else + $1 = FIX2INT($input); +} + +%typemap (out) int32_t +{ + $result = INT2NUM((int32_t) $1); +} + +%typemap (typecheck, precedence=SWIG_TYPECHECK_INTEGER) uint32_t { + $1 = FIXNUM_P($input); +} + +%typemap (in) uint64_t +{ + if (TYPE($input) == T_BIGNUM) + $1 = NUM2ULL($input); + else + $1 = (uint64_t) FIX2ULONG($input); +} + +%typemap (out) uint64_t +{ + $result = ULL2NUM((uint64_t) $1); +} + +%typemap (in) int64_t +{ + if (TYPE($input) == T_BIGNUM) + $1 = NUM2LL($input); + else + $1 = (int64_t) FIX2LONG($input); +} + +%typemap (out) int64_t +{ + $result = LL2NUM((int64_t) $1); +} + +/* + * Variant types: C++ --> Ruby + */ +%typemap(out) qpid::types::Variant::Map& { + $result = MapToRb($1); +} + +%typemap(out) qpid::types::Variant::List& { + $result = ListToRb($1); +} + +%typemap(out) qpid::types::Variant& { + $result = VariantToRb($1); +} + + +/* + * Variant types: Ruby --> C++ + */ +%typemap(in) qpid::types::Variant& { + $1 = new qpid::types::Variant(RbToVariant($input)); +} + +%typemap(in) qpid::types::Variant::Map& { + $1 = new qpid::types::Variant::Map(); + RbToMap($input, $1); +} + +%typemap(in) qpid::types::Variant::List& { + $1 = new qpid::types::Variant::List(); + RbToList($input, $1); +} + +%typemap(freearg) qpid::types::Variant& { + delete $1; +} + +%typemap(freearg) qpid::types::Variant::Map& { + delete $1; +} + +%typemap(freearg) qpid::types::Variant::List& { + delete $1; +} + + +/* + * Variant types: typecheck maps + */ +%typemap(typecheck) qpid::types::Variant::Map& { + $1 = (TYPE($input) == T_HASH) ? 1 : 0; +} + +%typemap(typecheck) qpid::types::Variant::List& { + $1 = (TYPE($input) == T_ARRAY) ? 1 : 0; +} + +%typemap(typecheck) qpid::types::Variant& { + $1 = (TYPE($input) == T_FLOAT || + TYPE($input) == T_STRING || + TYPE($input) == T_FIXNUM || + TYPE($input) == T_BIGNUM || + TYPE($input) == T_TRUE || + TYPE($input) == T_FALSE) ? 1 : 0; +} + +%typemap(typecheck) bool { + $1 = (TYPE($input) == T_TRUE || + TYPE($input) == T_FALSE) ? 1 : 0; +} + + + +%typemap (typecheck, precedence=SWIG_TYPECHECK_INTEGER) uint64_t { + $1 = FIXNUM_P($input); +} + diff --git a/cpp/configure.ac b/cpp/configure.ac index 08e512a835..282095c20c 100644 --- a/cpp/configure.ac +++ b/cpp/configure.ac @@ -527,6 +527,9 @@ AC_CONFIG_FILES([ bindings/qmf/ruby/Makefile bindings/qmf/python/Makefile bindings/qmf/tests/Makefile + bindings/qpid/Makefile + bindings/qpid/python/Makefile + bindings/qpid/ruby/Makefile managementgen/Makefile etc/Makefile src/Makefile |