4693: Simplify the construction of dyn.data objects in SWIG wrappers.

* swig/dyn.i (make_data_by_cpy): Remove inlined wrapper. (generate_data_ctor) (generate_data_ctor_val) (generate_data_ctor_ref): New macros. Use them to generate dyn::data ctors using a proxy-by-value for `int' and `std::string'. * swig/python/milena.py: Adjust: use dyn.data directly instead of helpers dyn.integer and dyn.string. --- dynamic-use-of-static-c++/ChangeLog | 14 ++++++ dynamic-use-of-static-c++/swig/dyn.i | 53 +++++++++++++++------- dynamic-use-of-static-c++/swig/python/milena.py | 10 ++-- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/dynamic-use-of-static-c++/ChangeLog b/dynamic-use-of-static-c++/ChangeLog index ce53b40..9eff03c 100644 --- a/dynamic-use-of-static-c++/ChangeLog +++ b/dynamic-use-of-static-c++/ChangeLog @@ -1,3 +1,17 @@ +2009-10-30 Roland Levillain <roland@lrde.epita.fr> + + Simplify the construction of dyn.data objects in SWIG wrappers. + + * swig/dyn.i (make_data_by_cpy): Remove inlined wrapper. + (generate_data_ctor) + (generate_data_ctor_val) + (generate_data_ctor_ref): + New macros. + Use them to generate dyn::data ctors using a proxy-by-value for + `int' and `std::string'. + * swig/python/milena.py: Adjust: use dyn.data directly instead of + helpers dyn.integer and dyn.string. + 2009-10-29 Roland Levillain <roland@lrde.epita.fr> Fix issues w.r.t. MacPorts' libiberty to have Python bindings work. diff --git a/dynamic-use-of-static-c++/swig/dyn.i b/dynamic-use-of-static-c++/swig/dyn.i index eaeacdf..b3b6751 100644 --- a/dynamic-use-of-static-c++/swig/dyn.i +++ b/dynamic-use-of-static-c++/swig/dyn.i @@ -35,7 +35,7 @@ #include "dyn-all.hh" %} -// Ignore global objects causing trouble. +// Ignore global objects causing trouble to swig. %ignore dyn::logger; %ignore dyn::internal::operator_push; %ignore dyn::internal::operator_pop; @@ -49,29 +49,48 @@ %include "dyn-all.hh"; -%inline %{ - /* The natural, single-argument ctors of dyn::data manipulate the - encapulsated by reference, which is wrong when these data are of - builtin types of the target language (e.g., Pythons' `int's), - because we have no control on them and they can vanish at any - moment. +/*---------------. +| Construction. | +`---------------*/ + +/* The natural, single-argument ctors of dyn::data manipulate the + encapsulated member by reference, which is wrong when these data + are of builtin types of the target language (e.g., Pythons' + `int's), because we have no control on them and they can vanish at + any moment. - To prevent this, provide construction helpers creating dyn::data - objects manipulating data by value (copy). */ - template <typename T> - dyn::data make_data_by_cpy(T i) + To prevent this, provide constructors helpers creating dyn::data + objects manipulating data by value (copy). */ +%define generate_data_ctor(Arg, Type) +%extend dyn::data +{ + data(Arg v) { dyn::proxy_tag* dummy = 0; // This dummy pointer passed as second argument is required to // call the right ctor. - dyn::data d(new dyn::data_proxy_by_cpy<T>(i), dummy); - return d; + return new dyn::data(new dyn::data_proxy_by_cpy< Type >(v), dummy); } -%} +} +%enddef // !generate_data_ctor + +// Shortcut to generate a ctor taking its argument by value (copy). +%define generate_data_ctor_val(Type) +generate_data_ctor(Type, Type) +%enddef + +// Shortcut to generate a ctor taking its argument by (const) reference. +%define generate_data_ctor_ref(Type) +generate_data_ctor(const Type &, Type) +%enddef + +// Generate dyn::data ctors using a proxy-by-value for classic types. +generate_data_ctor_val(int) +generate_data_ctor_ref(std::string) -// Instantiate make_data_by_cpy ctors for some types. -%template(integer) make_data_by_cpy<int>; -%template(string) make_data_by_cpy<std::string>; +/*--------------. +| Conversions. | +`--------------*/ // Instantiate dyn::data explicit conversion routines for some types. %extend dyn::data diff --git a/dynamic-use-of-static-c++/swig/python/milena.py b/dynamic-use-of-static-c++/swig/python/milena.py index 85a1cfc..93c384b 100644 --- a/dynamic-use-of-static-c++/swig/python/milena.py +++ b/dynamic-use-of-static-c++/swig/python/milena.py @@ -41,11 +41,11 @@ println = dyn.fun("mln::debug::println") # # but we just can't. `mk_image2d_int' only accept `dyn.data' as # arguments, so we have to encapsulate integers in `dyn.data' objects -# using `dyn.integer' (likewise for strings with `dyn.string'). +# (likewise for strings). -ima = mk_image2d_int(dyn.integer(3), dyn.integer(3)) +ima = mk_image2d_int(dyn.data(3), dyn.data(3)) -fill(ima, dyn.integer(0)) -println(dyn.string("ima (before) ="), ima) +fill(ima, dyn.data(0)) +println(dyn.data("ima (before) ="), ima) iota(ima) -println(dyn.string("ima (after) ="), ima) +println(dyn.data("ima (after) ="), ima) -- 1.6.5
participants (1)
-
Roland Levillain