Exposing c++ class template to Python (known template instances)
Assume I have this Interface: class FooInterface { public: ... }; class FooFilePolicy : public FooInterface { ... }; class FooIoPolicy : public FooInterface { ... }; And I have this template using the upper interface policy: template< typename FooPolicy > class BarWithPolicy { ... FooPolicy* policy; ... }; How can I expose BarWithPolicy to python that can be generically instantiate with both FooIoPolicy and FooFilePolicy ? I don't want to do this : BarWithPolicy<FooFilePolicy >& GetBarWithPolicyFile() { return new BarWithPolicy<FooFilePolicy >; } BarWithPolicy<FooIoPolicy>& GetBarWithPolicyIo() { return new BarWithPolicy<FooIoPolicy >; } BOOST_PYTHON_MODULE(PyBar) { def("GetBarWithPolicyFile", &GetBarWithPolicyFile, return_value_policy<reference_existing_object>(), "" ); def("GetBarWithPolicyIo", &GetBarWithPolicyIo, return_value_policy<reference_existing_object>(), "" ); } I would like to have some thing like this in python:
barPolicy = GetBarWithPolic(Io) Or barPolicy = GetBarWithPolic(File)
Thanks -Dotan -- View this message in context: http://boost.2283326.n4.nabble.com/Exposing-c-class-template-to-Python-known... Sent from the Python - c++-sig mailing list archive at Nabble.com.
Dotan, in
barPolicy = GetBarWithPolic(Io) barPolicy = GetBarWithPolic(File) what type of object would you imaginge `Io` and `File` to be?
You already exported the functions as `GetBarWithPolicIo` and `GetBarWithPolicFile` and then you could implement in python `GetBarWithPolic(str)` and there use something like `return globals()["GetBarWithPolic%s" % (str,)]()` where you would now do barPolicy = GetBarWithPolic("Io") barPolicy = GetBarWithPolic("File") -Holger On Sun, Mar 17, 2013 at 6:49 AM, dbarak <dbarak@gmail.com> wrote:
Assume I have this Interface: class FooInterface { public: ...
};
class FooFilePolicy : public FooInterface { ... };
class FooIoPolicy : public FooInterface { ... };
And I have this template using the upper interface policy:
template< typename FooPolicy > class BarWithPolicy { ... FooPolicy* policy; ... };
How can I expose BarWithPolicy to python that can be generically instantiate with both FooIoPolicy and FooFilePolicy ?
I don't want to do this :
BarWithPolicy<FooFilePolicy >& GetBarWithPolicyFile() { return new BarWithPolicy<FooFilePolicy >; }
BarWithPolicy<FooIoPolicy>& GetBarWithPolicyIo() { return new BarWithPolicy<FooIoPolicy >; }
BOOST_PYTHON_MODULE(PyBar) { def("GetBarWithPolicyFile", &GetBarWithPolicyFile, return_value_policy<reference_existing_object>(), "" ); def("GetBarWithPolicyIo", &GetBarWithPolicyIo, return_value_policy<reference_existing_object>(), "" ); }
I would like to have some thing like this in python:
barPolicy = GetBarWithPolic(Io) Or barPolicy = GetBarWithPolic(File)
Thanks -Dotan
-- View this message in context: http://boost.2283326.n4.nabble.com/Exposing-c-class-template-to-Python-known... Sent from the Python - c++-sig mailing list archive at Nabble.com. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Thanks for the response That trick can help me out in the python level since python is has a duck typing attribute. do you familiar with any a procedure for the C++ ?. what I mean is this : I would like to have some generic pointer that can either point to BarWithPolicy<FooFilePolicy
nor to BarWithPolicy<FooIoPolicy > ?
thanks -Dotan On Sun, Mar 17, 2013 at 9:36 AM, Holger Brandsmeier <brandsmeier@gmx.de>wrote:
Dotan,
in
barPolicy = GetBarWithPolic(Io) barPolicy = GetBarWithPolic(File) what type of object would you imaginge `Io` and `File` to be?
You already exported the functions as `GetBarWithPolicIo` and `GetBarWithPolicFile` and then you could implement in python `GetBarWithPolic(str)` and there use something like `return globals()["GetBarWithPolic%s" % (str,)]()` where you would now do barPolicy = GetBarWithPolic("Io") barPolicy = GetBarWithPolic("File")
-Holger
On Sun, Mar 17, 2013 at 6:49 AM, dbarak <dbarak@gmail.com> wrote:
Assume I have this Interface: class FooInterface { public: ...
};
class FooFilePolicy : public FooInterface { ... };
class FooIoPolicy : public FooInterface { ... };
And I have this template using the upper interface policy:
template< typename FooPolicy > class BarWithPolicy { ... FooPolicy* policy; ... };
How can I expose BarWithPolicy to python that can be generically instantiate with both FooIoPolicy and FooFilePolicy ?
I don't want to do this :
BarWithPolicy<FooFilePolicy >& GetBarWithPolicyFile() { return new BarWithPolicy<FooFilePolicy >; }
BarWithPolicy<FooIoPolicy>& GetBarWithPolicyIo() { return new BarWithPolicy<FooIoPolicy >; }
BOOST_PYTHON_MODULE(PyBar) { def("GetBarWithPolicyFile", &GetBarWithPolicyFile, return_value_policy<reference_existing_object>(), "" ); def("GetBarWithPolicyIo", &GetBarWithPolicyIo, return_value_policy<reference_existing_object>(), "" ); }
I would like to have some thing like this in python:
barPolicy = GetBarWithPolic(Io) Or barPolicy = GetBarWithPolic(File)
Thanks -Dotan
-- View this message in context: http://boost.2283326.n4.nabble.com/Exposing-c-class-template-to-Python-known... Sent from the Python - c++-sig mailing list archive at Nabble.com. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
-- Dotan Barak
Dotan, Well, you can derive your template class BarWithPolicy from a non-template class IBarWithPolicy and maybe that interface is enough to work with. Otherwise I usually drag the template arguments along on the C++ side. What exactly do you want to do with that object, that can then either be a `BarWithPolicy<FooFilePolicy >` or `BarWithPolicy<FooIoPolicy >`? -Holger On Sun, Mar 17, 2013 at 1:50 PM, Dotan Barak <dbarak@gmail.com> wrote:
Thanks for the response
That trick can help me out in the python level since python is has a duck typing attribute.
do you familiar with any a procedure for the C++ ?. what I mean is this : I would like to have some generic pointer that can either point to BarWithPolicy<FooFilePolicy > nor to BarWithPolicy<FooIoPolicy > ?
thanks -Dotan
On Sun, Mar 17, 2013 at 9:36 AM, Holger Brandsmeier <brandsmeier@gmx.de> wrote:
Dotan,
in
barPolicy = GetBarWithPolic(Io) barPolicy = GetBarWithPolic(File) what type of object would you imaginge `Io` and `File` to be?
You already exported the functions as `GetBarWithPolicIo` and `GetBarWithPolicFile` and then you could implement in python `GetBarWithPolic(str)` and there use something like `return globals()["GetBarWithPolic%s" % (str,)]()` where you would now do barPolicy = GetBarWithPolic("Io") barPolicy = GetBarWithPolic("File")
-Holger
On Sun, Mar 17, 2013 at 6:49 AM, dbarak <dbarak@gmail.com> wrote:
Assume I have this Interface: class FooInterface { public: ...
};
class FooFilePolicy : public FooInterface { ... };
class FooIoPolicy : public FooInterface { ... };
And I have this template using the upper interface policy:
template< typename FooPolicy > class BarWithPolicy { ... FooPolicy* policy; ... };
How can I expose BarWithPolicy to python that can be generically instantiate with both FooIoPolicy and FooFilePolicy ?
I don't want to do this :
BarWithPolicy<FooFilePolicy >& GetBarWithPolicyFile() { return new BarWithPolicy<FooFilePolicy >; }
BarWithPolicy<FooIoPolicy>& GetBarWithPolicyIo() { return new BarWithPolicy<FooIoPolicy >; }
BOOST_PYTHON_MODULE(PyBar) { def("GetBarWithPolicyFile", &GetBarWithPolicyFile, return_value_policy<reference_existing_object>(), "" ); def("GetBarWithPolicyIo", &GetBarWithPolicyIo, return_value_policy<reference_existing_object>(), "" ); }
I would like to have some thing like this in python:
barPolicy = GetBarWithPolic(Io) Or barPolicy = GetBarWithPolic(File)
Thanks -Dotan
-- View this message in context: http://boost.2283326.n4.nabble.com/Exposing-c-class-template-to-Python-known... Sent from the Python - c++-sig mailing list archive at Nabble.com. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
-- Dotan Barak
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
It is basically a logger policy and the idea is to create cross platform logger that can be accessed from C++ and from python. Except from the creation point I don't rely care what is the policy and that is why I can't drag the template argument. I think I understand what you mean by using an base class interface to a template class I will try this. Thanks -Dotan On Sun, Mar 17, 2013 at 3:13 PM, Holger Brandsmeier <brandsmeier@gmx.de>wrote:
Dotan,
Well, you can derive your template class BarWithPolicy from a non-template class IBarWithPolicy and maybe that interface is enough to work with. Otherwise I usually drag the template arguments along on the C++ side.
What exactly do you want to do with that object, that can then either be a `BarWithPolicy<FooFilePolicy >` or `BarWithPolicy<FooIoPolicy >`?
-Holger
On Sun, Mar 17, 2013 at 1:50 PM, Dotan Barak <dbarak@gmail.com> wrote:
Thanks for the response
That trick can help me out in the python level since python is has a duck typing attribute.
do you familiar with any a procedure for the C++ ?. what I mean is this : I would like to have some generic pointer that can either point to BarWithPolicy<FooFilePolicy > nor to BarWithPolicy<FooIoPolicy > ?
thanks -Dotan
On Sun, Mar 17, 2013 at 9:36 AM, Holger Brandsmeier <brandsmeier@gmx.de> wrote:
Dotan,
in
barPolicy = GetBarWithPolic(Io) barPolicy = GetBarWithPolic(File) what type of object would you imaginge `Io` and `File` to be?
You already exported the functions as `GetBarWithPolicIo` and `GetBarWithPolicFile` and then you could implement in python `GetBarWithPolic(str)` and there use something like `return globals()["GetBarWithPolic%s" % (str,)]()` where you would now do barPolicy = GetBarWithPolic("Io") barPolicy = GetBarWithPolic("File")
-Holger
On Sun, Mar 17, 2013 at 6:49 AM, dbarak <dbarak@gmail.com> wrote:
Assume I have this Interface: class FooInterface { public: ...
};
class FooFilePolicy : public FooInterface { ... };
class FooIoPolicy : public FooInterface { ... };
And I have this template using the upper interface policy:
template< typename FooPolicy > class BarWithPolicy { ... FooPolicy* policy; ... };
How can I expose BarWithPolicy to python that can be generically instantiate with both FooIoPolicy and FooFilePolicy ?
I don't want to do this :
BarWithPolicy<FooFilePolicy >& GetBarWithPolicyFile() { return new BarWithPolicy<FooFilePolicy >; }
BarWithPolicy<FooIoPolicy>& GetBarWithPolicyIo() { return new BarWithPolicy<FooIoPolicy >; }
BOOST_PYTHON_MODULE(PyBar) { def("GetBarWithPolicyFile", &GetBarWithPolicyFile, return_value_policy<reference_existing_object>(), "" ); def("GetBarWithPolicyIo", &GetBarWithPolicyIo, return_value_policy<reference_existing_object>(), "" ); }
I would like to have some thing like this in python:
> barPolicy = GetBarWithPolic(Io) Or > barPolicy = GetBarWithPolic(File)
Thanks -Dotan
-- View this message in context:
http://boost.2283326.n4.nabble.com/Exposing-c-class-template-to-Python-known...
Sent from the Python - c++-sig mailing list archive at Nabble.com. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
-- Dotan Barak
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
-- Dotan Barak
participants (3)
-
dbarak -
Dotan Barak -
Holger Brandsmeier