M
Mosfet
Hi,
I would like to wrapp a C library I wrote used to access address book on
windows mobile and basically here is how it is designed :
typedef void GDAddrBook;
typedef void GDAbItem;
GYNOID_API ErrorCode
GDAddrBook_Open(OsHandle osParam, GDAddrBook** gppAddrbook);
GYNOID_API ErrorCode
GDAddrBook_Close(GDAddrBook* gpAddrbook);
GYNOID_API ErrorCode
GDAddrBook_GetCount(GDAddrBook* gpAddrbook, int* ulCount);
GYNOID_API ErrorCode
GDAbItem_Release(GDAbItem* gpContact);
GYNOID_API ErrorCode
GDAddrBook_GetItem(GDAddrBook* gpAddrbook, int iIndex, GDAbItem**
gppAbItem);
GYNOID_API void*
GDAbItem_GetProperty(GDAbItem* gpContact, EAbItemProp eContactProp);
So A typical use to get the first addrbook item would be (simple case
with no error checking):
GDCTSTR lpFirstName;
GDAddrBook* gdAb;
GDAbItem* gdAbItem;
GDAddrBook_Open(0, &gdAb);
GDAddrBook_GetItem(gdAb, 0, &gdAbItem);
lpFirstName = (GDCTSTR) GDAbItem_GetProperty(gdAbItem, eAbFirstName);
// Now we release our two "objects"
GDAbItem_Release(gdAbItem);
GDAddrBook_Release(gdAb)
Now I would like to provide a C++ wrapper where C++ objects would hold
GDxxxx pointers and would call Gdxxxx_Release automatically
AddrBook ab;
AddrBookItem abItem;
abItem = ab.getItem(0);
GDString = abItem.getProperty(eAbFirstName);
The problem is about
AddrBookItem AddrBook::getItem(int iIndex)
{
GDAbItem* gpAbItem = NULL;
GDAddrBook_GetItem(m_gpAb, iIndex, &gpAbItem);
AddrBookItem abItem(gpAbItem);
return abItem;
}
Because If I write something like that, my local abItem will be
destroyed and will call its destructor, so my internal pointer will be
released.
The only way I can see is to use reference counting but is it the only way ?
Another approach would be to use reference like that :
ab.getItem(0, abItem );
but what is the best way and how to solve my issues ?
I would like to wrapp a C library I wrote used to access address book on
windows mobile and basically here is how it is designed :
typedef void GDAddrBook;
typedef void GDAbItem;
GYNOID_API ErrorCode
GDAddrBook_Open(OsHandle osParam, GDAddrBook** gppAddrbook);
GYNOID_API ErrorCode
GDAddrBook_Close(GDAddrBook* gpAddrbook);
GYNOID_API ErrorCode
GDAddrBook_GetCount(GDAddrBook* gpAddrbook, int* ulCount);
GYNOID_API ErrorCode
GDAbItem_Release(GDAbItem* gpContact);
GYNOID_API ErrorCode
GDAddrBook_GetItem(GDAddrBook* gpAddrbook, int iIndex, GDAbItem**
gppAbItem);
GYNOID_API void*
GDAbItem_GetProperty(GDAbItem* gpContact, EAbItemProp eContactProp);
So A typical use to get the first addrbook item would be (simple case
with no error checking):
GDCTSTR lpFirstName;
GDAddrBook* gdAb;
GDAbItem* gdAbItem;
GDAddrBook_Open(0, &gdAb);
GDAddrBook_GetItem(gdAb, 0, &gdAbItem);
lpFirstName = (GDCTSTR) GDAbItem_GetProperty(gdAbItem, eAbFirstName);
// Now we release our two "objects"
GDAbItem_Release(gdAbItem);
GDAddrBook_Release(gdAb)
Now I would like to provide a C++ wrapper where C++ objects would hold
GDxxxx pointers and would call Gdxxxx_Release automatically
AddrBook ab;
AddrBookItem abItem;
abItem = ab.getItem(0);
GDString = abItem.getProperty(eAbFirstName);
The problem is about
AddrBookItem AddrBook::getItem(int iIndex)
{
GDAbItem* gpAbItem = NULL;
GDAddrBook_GetItem(m_gpAb, iIndex, &gpAbItem);
AddrBookItem abItem(gpAbItem);
return abItem;
}
Because If I write something like that, my local abItem will be
destroyed and will call its destructor, so my internal pointer will be
released.
The only way I can see is to use reference counting but is it the only way ?
Another approach would be to use reference like that :
ab.getItem(0, abItem );
but what is the best way and how to solve my issues ?