>>>>> Steinar Bang <sb_at_dod.no>:
> What exactly is it you want to do? Have a thin C++ wrapper around C
> objects, where the C objects "do the work"? Why do you want to?
> Ie. what benefit do you expect to get, compared to just using the C
> objects from inside your C++ code?
Another pattern that I've used with some success (eg. when wrapping the
W3C libwww as HTTP/FTP/etc. support in a Qt application):
- Create a C++ class that corresponds to the C API you would like to
wrap
- Where the C API would return a pointer to some struct, return a C++
object that holds a pointer to the struct and has methods
corresponding to the operations that can be done on the struct using
the API
- Life cycle management would be handled by the API and the C++ object
wrapping the API (though in some cases the wrapper object destructor
might hand the C pointer back to the API)
Variations over the theme:
- The C++ wrappers have virtual methods
- The API wrapping object return pointers to the C++ objects rather
than instances (typically for objects with virtual methods)
- The API wrapping object return smart pointers to the C++ objects (but
as these instances will be pointer sized objects, this doesn't seem
very useful...)
The first trouble spot one will run into is non-primitive argument
values and method return values. Eg.
- Life cycle of returned wrapper objects
- Strings
- Minimize copies in and out of std::string
- Minimize recodning from UTF-8-coded char* to UTF-16, or UCS-4 in
std::wstring
But they are overcomable.
Received on 2010-09-25 15:11:49 CEST