[C++-sig] help on abstract class export
Hi, I'm trying to export the following code to python, but failed. Please
help me!
Here is the code:
--
c++ code:
--
class IWorker {
public:
virtual int getData() = 0;
};
class Worker : public IWorker {
public:
int getData() {
return 100;
}
};
extern "C" {
IWorker* createWorker();
}
--
py++ code:
--
mb.free_fun('createWorker').call_policies =
call_policies.return_value_policy(call_policies.return_pointee_value)
--
In python:
--
>>> w = createWorker()
I got 'TypeError: No to_python (by-value) converter found for C++ type:
class IWorker
I found that if the IWorker class is not abstract, for example:
class IWorker {
public:
virtual int getData() { return 0; }
};
Python doesn't report that error, but the result of the following code is
not expected(I think it should be 100).
>>> w = createWorker()
>>> w.getData()
0
How to solve this problem? Please give some advise.
THANK YOU.
--
View this message in context:
http://boost.2283326.n4.nabble.com/help-on-abstract-class-export-tp3483090p3483090.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] help on abstract class export
Thank you very much. It works! -- View this message in context: http://boost.2283326.n4.nabble.com/help-on-abstract-class-export-tp3483090p3483251.html Sent from the Python - c++-sig mailing list archive at Nabble.com. ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] transfer ownership problem
hi, I have some problem about transfer-ownership. C++ code is like this:
class RefCounter {
int _count;
public:
RefCounter() : _count(1) {}
virtual RefCounter() {}
void addRef() { ++_count; }
void release() { --_count; if (_count == 0) delete this; }
};
class A : public RefCounter {...};
class B {
public:
void setA(A* a) {// B takes the ownership of A.
_a = a;
a.addRef();
}
private:
A* _a;
};
So, in python, I use these classes like:
a = A() # the reference count of a is 1 now.
b = B()
b.setA(a) # B take the ownership of A, and now the reference count is 2.
a.release() # a should call release to decrease the reference count, but now
a is invalid. Python says: did not match C++ signature.
I can change the C++ code to solve this problem, but I really don't want to
change C++ code.
It's better to solve it by other way.
Is there any one could give me some advice?
THANK YOU VERY MUCH.
--
View this message in context:
http://boost.2283326.n4.nabble.com/transfer-ownership-problem-tp3495846p3495846.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] transfer ownership problem
Thank you, Jim. ^-^ By using boost::intrusive_ptr, the problem is solved. Everything works well. -- View this message in context: http://boost.2283326.n4.nabble.com/transfer-ownership-problem-tp3495846p3497818.html Sent from the Python - c++-sig mailing list archive at Nabble.com. ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] how to call base class method?
I have 2 class exposed to python:
class A {
public:
virtual int get() { return 1; }
};
class B : public A {
public:
virtual int get() { return 2; }
};
In python:
obj = B()
obj.get() # this will call B's get()
For some reason, I want to call A's get() in python, like obj->A::get() in
C++. So I have tried:
@ Directly call base class's method in python: A.get(obj) # failed.
@ make a wrap function to cast B to A:
A* cast(B* obj) {
return (A*)obj;
}
And try to use different call policy, like manage_new_object,
reference_existing_object, return_internal_reference, but all failed.
After casting, the two object actually the same object.
>>> objB = B()
>>> objA = cast(objB)
>>> objA == objB
True
Please help me
THANK YOU
--
View this message in context:
http://boost.2283326.n4.nabble.com/how-to-call-base-class-method-tp3504749p3504749.html
Sent from the Python - c++-sig mailing list archive at Nabble.com.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] how to call base class method?
Making a A_get wrapper seems to be the only way to solve this problem. THANK YOU JIM -- View this message in context: http://boost.2283326.n4.nabble.com/how-to-call-base-class-method-tp3504749p3505083.html Sent from the Python - c++-sig mailing list archive at Nabble.com. ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
