3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
5 // Distributed under the MIT License (See accompanying file LICENSE)
8 * Instructions for generating and building the extension:
10 * 1. swig -c++ -Wall -python -I../../../include -o obj_usage_wrap.cpp obj_usage.i
12 * 2. python setup.py build
16 * \example obj_usage.i
18 * A simple SWIG interface for a Python extension module that shows the
19 * mds_utils::python::Obj usage.
21 * \remarks Here I've used SWIG for convenience only. The
22 * mds_utils::python::Obj class does not impose you
28 // Include the following interface files for having the proper typemaps
29 %include "std_string.i"
30 %include "mds_utils/python/common.i"
31 %include "mds_utils/python/obj.i"
34 #include <mds_utils/python/obj.hpp>
35 #include <mds_utils/python/conversion.hpp>
38 namespace mdspy = mds_utils::python;
41 %feature("autodoc","3");
45 // Tests attribute retrieval, when the attribute is a double
46 void print_attr(mds_utils::python::Obj o,const std::string& name) {
51 if (!o.has_attr(name)) {
52 cout << "The object has not the \"" << name << "\" attribute." << endl;
56 cout << "Attribute value: " << mdspy::get<double>(o.attr(name)) << endl;
62 // Tests the set attribute
63 void set_attr(mds_utils::python::Obj o,const std::string& name,double val) {
68 // Tests the transfer method
69 PyObject* dup_obj(mds_utils::python::Obj o) {
73 PyObject *pret(o.transfer());
79 // Tests the operator () for callable objects
80 mds_utils::python::Obj test_call(mds_utils::python::Obj o) {
88 // Tests the operator () on a callable attribute
89 mds_utils::python::Obj test_call_attr(mds_utils::python::Obj o,const std::string& name) {
98 // Tests calling an object with positional arguments
99 mds_utils::python::Obj test_call_args(mds_utils::python::Obj o,mds_utils::python::Obj args) {
108 // Tests calling an object with positional arguments and keyword arguments
109 mds_utils::python::Obj test_call_args_kw(mds_utils::python::Obj o,
110 mds_utils::python::Obj args,
111 mds_utils::python::Obj kw) {