A wrapped std::shared_ptr. #534
-
Dear professor and other developers, I am writing python bindings for my still unborn software. I do use I have, however, wrapped all my https://github.com/andre-caldas/ParaCADis/blob/main/src/base/expected_behaviour/SharedPtr.h I do not want to use Of course, I could just copy and paste So... what would be the ideal way to handle my case? For example, I have a I would like to work with it as if instead of a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I think I am supposed to implement a type caster. It seems that the correct way to implement a type caster is by specializing the Instead of copying the I am not really sure if it is correct to use template <typename T>
struct type_caster<SharedPtr<T>>
: type_caster<std::shared_ptr<T>>
{
NB_TYPE_CASTER(SharedPtr<T>, type_caster<std::shared_ptr<T>>::Caster::Name)
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept
{
if(!type_caster<std::shared_ptr<T>>::from_python(src, flags, cleanup))
{
return false;
}
// Can we use std::move()? Or ::value is accessed by other people?
value = std::move(type_caster<std::shared_ptr<T>>::value);
return true;
}
static handle from_cpp(
const Value &value, rv_policy p, cleanup_list *cleanup) noexcept
{
return type_caster<std::shared_ptr<T>>::from_cpp(value.sliced(), p, cleanup);
}
}; Executed commands::
If I do not execute Please, let me know if I am doing anything wrong. |
Beta Was this translation helpful? Give feedback.
I think I am supposed to implement a type caster. It seems that the correct way to implement a type caster is by specializing the
type_caster
template.Instead of copying the
nanobind/stl/shared_ptr.h
code, I have derived fromtype_caster<std::shared_ptr<T>>
and then I convert back and forth, from mySharedPtr<T>
tostd::shared_ptr<T>
.I am not really sure if it is correct to use
std::move()
where I did.