Jonathan Mcdougall
2005-10-12 21:27:10 UTC
Devices store callbacks in a vector or CallbackPtr. This means they
take ownership of the memory (this is nowhere stated, it should be).
Since unregisterCallback() does not return the object, it is
automatically destroyed when unregistered. Also, because
registerCallback() takes a plain pointer, it is impossible to share
the ref-counted pointer in the vector.
Because of this, it is impossible to own the object after it has been
added to the device. This causes me a problem because my callback may
be expensive to create and I frequently register and unregister it. I
need to be able to reuse the pointer.
I can see three solutions:
1) make unregisterCallback() return the removed pointer (by a plain
pointer or, better, by a CallbackPtr)
2) make registerCallback() take a CallbackPtr which will only
increases the reference count
3) store the pointers as plain and delete them in the dtor
Number 1) makes it assymetric with clearCallbacks(), which is why I
think 2) would be better. Functionnaly, 2) and 3) are identical, but
2) is safer.
Jonathan
take ownership of the memory (this is nowhere stated, it should be).
Since unregisterCallback() does not return the object, it is
automatically destroyed when unregistered. Also, because
registerCallback() takes a plain pointer, it is impossible to share
the ref-counted pointer in the vector.
Because of this, it is impossible to own the object after it has been
added to the device. This causes me a problem because my callback may
be expensive to create and I frequently register and unregister it. I
need to be able to reuse the pointer.
I can see three solutions:
1) make unregisterCallback() return the removed pointer (by a plain
pointer or, better, by a CallbackPtr)
2) make registerCallback() take a CallbackPtr which will only
increases the reference count
3) store the pointers as plain and delete them in the dtor
Number 1) makes it assymetric with clearCallbacks(), which is why I
think 2) would be better. Functionnaly, 2) and 3) are identical, but
2) is safer.
Jonathan