Exposing a generic template system in Boost.Python
Greetings, I have been trying to think of a solution about this for a few days now, but I did not get very far. I am exposing specific instances of C++ template classes using Boost.Python. Let's say for the sake of argument that I am exposing std::vector<of-a-few-types>. What I would like to achieve on the Python side would be some kind of unified interface of this kind: # Define a vector-of-ints type. vec_int = vector["int"] # Define a vector-of-doubles type. vec_float = vector["double"] ... etc., possibly with exceptions being raised if a template type which has not been exposed is requested. Additionally, the system should automagically work with nested generic types: # Define a vector-of-vectors-of-ints type. vec_vec_int = vector[vector["int"]] I have played around with some ideas, but it seems like I can never get to a point where the design does not look bloated and/or brittle. Thus I am wondering, is there anyone that tackled a problem like this before in Boost.Python and has pointers/suggestions on how to do this properly? Support for things like default template arguments would definitely be a plus. FWIW, I have access to C++11 on the C++ side, so I can deal with variadic templates and other amenities in a not-so-kludgy way. Thanks for any input, Francesco.
Francesco, I have done something like what you are suggesting. It essentially boils down to defining a function template like template <typename T> void define_vector(char const *name) { class_<...> vector(name); ... } and then calling that multiple times: define_vector<int>("IVector"); define_vector<long>("LVector"); ... Providing a factory function that instantiates one of those based on a value-type selector as you want can be easily done on the Python side. I don't think this can be automated any further. In particular, it is clear that any type you may want to instantiate in Python has to be compiled explicitly into the extension module. I.e., you need to explicitly instantiate the templates above, by explicitly calling the functions for the types you want to see supported at runtime. There is no JIT compilation for this. Stefan -- ...ich hab' noch einen Koffer in Berlin...
To add to Stefan's answer: I do the the same and also use visitors to add the same sets of members to classes, with possible customizations via trait classes. HTH On Wed, Mar 19, 2014 at 1:36 PM, Stefan Seefeld <stefan@seefeld.name> wrote:
Francesco,
I have done something like what you are suggesting. It essentially boils down to defining a function template like
template <typename T> void define_vector(char const *name) { class_<...> vector(name); ... }
and then calling that multiple times:
define_vector<int>("IVector"); define_vector<long>("LVector"); ...
Providing a factory function that instantiates one of those based on a value-type selector as you want can be easily done on the Python side.
I don't think this can be automated any further. In particular, it is clear that any type you may want to instantiate in Python has to be compiled explicitly into the extension module. I.e., you need to explicitly instantiate the templates above, by explicitly calling the functions for the types you want to see supported at runtime. There is no JIT compilation for this.
Stefan
--
...ich hab' noch einen Koffer in Berlin...
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
Hello Stefan and Nikolay, thanks for the replies. I have been using as well approaches similar to those you outlined. My main concern is the automation of the whole procedure. I have a fairly complicated hierarchy of template classes that I am exposing, and I need to be able to add new exposed instances in a kind of programmatic way (e.g., I am using a global counter n that gets increased each time a new instance is exposed, and I am calling the exposed classes "__vecor_type_0", "__vecor_type_1", ..., "__vecor_type_n"). Then I need to re-establish the connection between these names and the "template system" implemented on the Python side. I think it would be nice to have a Boost.Python mechanism to handle this type of usage, but for now I will try to get something working for my specific case (and then maybe reply here if I manage and if there is interest). Thanks for the help and kind regards, Francesco. On 19 March 2014 20:27, Nikolay Mladenov <nikolay.mladenov@gmail.com> wrote:
To add to Stefan's answer:
I do the the same and also use visitors to add the same sets of members to classes, with possible customizations via trait classes.
HTH
On Wed, Mar 19, 2014 at 1:36 PM, Stefan Seefeld <stefan@seefeld.name>wrote:
Francesco,
I have done something like what you are suggesting. It essentially boils down to defining a function template like
template <typename T> void define_vector(char const *name) { class_<...> vector(name); ... }
and then calling that multiple times:
define_vector<int>("IVector"); define_vector<long>("LVector"); ...
Providing a factory function that instantiates one of those based on a value-type selector as you want can be easily done on the Python side.
I don't think this can be automated any further. In particular, it is clear that any type you may want to instantiate in Python has to be compiled explicitly into the extension module. I.e., you need to explicitly instantiate the templates above, by explicitly calling the functions for the types you want to see supported at runtime. There is no JIT compilation for this.
Stefan
--
...ich hab' noch einen Koffer in Berlin...
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
participants (3)
-
Francesco Biscani -
Nikolay Mladenov -
Stefan Seefeld