fix upcasting problem (#77 thanks to cantora)

This commit is contained in:
Siu Kwan Lam 2013-08-26 12:18:04 -05:00
commit 0801df41dc
3 changed files with 29 additions and 6 deletions

View file

@ -333,7 +333,7 @@ class Class(SubModule, _Type):
writer.die_if_false(raw, verbose=name)
ptrty = ptr(self).fullname
ty = self.fullname
fmt = 'typecast< %(ty)s >::from(%(raw)s)'
fmt = 'unwrap_as<%(ty)s, %(name)s >::from(%(raw)s)'
casted = writer.declare(ptrty, fmt % locals())
writer.die_if_false(casted)
return casted

View file

@ -202,7 +202,8 @@ class CppCodeWriter(CodeWriterBase):
def pycapsule_new(self, ptr, name, clsname):
name_soften = mangle(name)
ret = self.call('pycapsule_new', 'PyObject*', ptr, quote(name),
cast_to_base = 'cast_to_base<%s >::from(%s)' % (name, ptr)
ret = self.call('pycapsule_new', 'PyObject*', cast_to_base, quote(name),
quote(clsname))
with self.block('if (!%(ret)s)' % locals()):
self.return_null()

View file

@ -254,10 +254,11 @@ PyObject* py_float_from(const double& val) {
// casting
template<class Td>
struct typecast {
template<class Ts> static
Td* from(Ts* src) {
return llvm::dyn_cast<Td>(src);
}
// Unused
// template<class Ts> static
// Td* from(Ts* src) {
// return llvm::dyn_cast<Td>(src);
// }
static
Td* from(void* src) {
@ -265,3 +266,24 @@ struct typecast {
}
};
template<class Td, class Tbase>
struct unwrap_as {
static
Td* from(void* src) {
Tbase* base = static_cast<Tbase*>(src);
return static_cast<Td*>(base);
}
};
template<class Td>
struct cast_to_base {
template<class Ts> static
Td* from(Ts* src){
return static_cast<Td*>(src);
}
template<class Ts> static
const Td* from(const Ts* src){
return static_cast<const Td*>(src);
}
};