Upload
nora-wilkins
View
214
Download
2
Embed Size (px)
Citation preview
Swig wrapping
• Compiles interfaces connecting C/C++ code to scripting languages (Perl, Python, …)
• Generates glue code (wrappers) that scripting language needs to access underlying C/C++ code
Swig C++ supported features
• Classes
• Virtual functions
• Pubilc inheritance
• Operator overloading
• References
• templates
Swig unsupported C++ features
• Namespaces
• Functions and method overloading
• Nested classes
• Overloaded versions of certain operators (new, delete, etc.)
Boost
• Is a set of free portable C++ source libraries
• Some of the libraries have already been proposed for inclusion in the C++ Standard Library
• Among available libraries : any, date_time, math, pool, python, smart_ptr, thread, timer
Boost.Python wrapping
• Boost.python is a C++ library which enables seamless interoperability between C++ and Python
• Allows python to access C++ functions and data directly as if they were written in Python
• Does not involve any changes to the code
Boost.Python supported features
• References and Pointers
• Globally registered type coercions
• Automatic cross-module type conversions
• Efficient function overloading
• C++ to Python Exception translation
• Manipulating python objects in C++
• Documentation strings
Tango Client Binding
Using boost.python
#include <tango.h>#include <boost/any.hpp>#include <boost/python/dict.hpp> class DeviceProxy{ public: DeviceProxy(std::string name) : deviceName(name) { deviceProxy = new Tango::DeviceProxy(deviceName); } boost::python::dict info(); Tango::DevState state(); std::string status(); int ping(); int get_idl_version(); void set_source(Tango::DevSource source); Tango::DevSource get_source(); boost::python::tuple black_box(int n); boost::python::dict import_info(); boost::python::dict command_query(std::string command); boost::python::list command_list_query(); std::string description(); boost::python::object command_inout(std::string name, boost::python::object pyData);};
#include <device_proxy.h>#include <boost/python/class.hpp>#include <boost/python/module.hpp> BOOST_PYTHON_MODULE(PyTango){ using namespace boost::python; enum_<Tango::DevState>("DevState") .value("ON",Tango::ON) .value("OFF",Tango::OFF) .value("CLOSE",Tango::CLOSE) .value("UNKNOWN",Tango::UNKNOWN) ; enum_<Tango::DevSource>("DevSource") .value("DEV",Tango::DEV) .value("CACHE",Tango::CACHE) .value("CACHE_DEV",Tango::CACHE_DEV) ; enum_<Tango::DispLevel>("DispLevel") .value("OPERATOR",Tango::OPERATOR) .value("EXPERT",Tango::EXPERT) ; class_<DeviceProxy>("DeviceProxy", init<std::string>()) .def("status", &DeviceProxy::status) .def("description", &DeviceProxy::description) .def("ping", &DeviceProxy::ping) .def("command_query", &DeviceProxy::command_query) .def("command_list_query", &DeviceProxy::command_list_query) .def("command_inout", &DeviceProxy::command_inout)
;}
[ounsy@/home/ounsy/python]: pythonPython 2.2.1 (#1, Apr 9 2002, 13:10:27)[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import PyTango>>> dev = PyTango.DeviceProxy("tango/tangotest/1")>>> dev.info(){'server_host': 'ganymede.synchrotron-soleil.fr', 'server_id':'tangotest/1', 'dev_type': 0, 'dev_class': 'TangoTest', 'doc_url': 'http://www.esrf.fr/tango','server_version': 2}>>> dev.description()'A TANGO device'>>> dev.command_inout("DevString","Hello, World!")'Hello, World!'>>>