Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Wrapping C code into a C++ object

Reply
Thread Tools

Wrapping C code into a C++ object

 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-05-2005

Ben Pope wrote:
> Jonathan Mcdougall wrote:
> > Ben Pope wrote:
> >> Tom the Canuck wrote:
> >>> What would be the best way to proceed?
> >>> Should I make a pure virtual class and then
> >>> derive from that? I want the base class to have
> >>> functions defined so that I don't have to do the
> >>> work all over again in the derived class. Is this
> >>> not what objects are for?
> >>> Please illuminate on how to do this properly.
> >>> I don't want code, just a pointer on how to
> >>> do the job right.
> >>> Any comments or links will help.
> >>>
> >>> The C code is OpenGL. I just want a class
> >>> to make life easier for future projects.
> >>> If requested, I can send the source code done
> >>> up to now. I do not believe in wasting space, so
> >>> it is not in this post.
> >>
> >>
> >> Just a quick example of wrapping an existing library and RAII:
> >>
> >> // Keep the old library hidden
> >> namespace libraryImpl {
> >> # include "library.h"
> >> }

> >
> > This won't work if the functions were defined in another namespace than
> > libraryImpl (as they probably will). libraryImpl::f() is *not* ::f()!

>
> Yeah, I did think about this after posting. I've seen the same problem
> when including windows.h.
>
> Presumably it doesn't matter if you #include from the source file and
> bring everything into the global namespace as it's local to the
> translation unit anyway.


When wrapping a library in a class, it is common to need member objects
defined by that library.

# include "the library"

class Wrapper
{
public:

private:
LibraryStruct s;
};

The problem here is that you have no choice but to include the library
header in the wrapper's header, making it visible to users. You could
make "s" a pointer and only declare LibraryStruct,

// don't need that anymore
// # include "the library"

class LibraryStruct;

class Wrapper
{
public:

private:
LibraryStruct *s;
};

but that could also mean some problems, for example if another version
of the library changes LibraryStruct to be a typedef of a template
(such as std::string).

The usual solution would be to use the "pimpl" idiom (or the Bridge
pattern).

// w.h
# include <memory>

class WrapperImpl;

namespace N
{

class Wrapper
{
public:
Wrapper();

private:
std::auto_ptr<WrapperImpl> impl_;
};

}

// w.cpp
# include "w.h"
# include "the library" // clients don't know about it

namespace N
{

class WrapperImpl
{
public:
LibraryStruct s;
};

Wrapper::Wrapper()
: impl_(new WrapperImpl)
{
library_init(&impl_->s);
}

}

Jonathan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrapping ruby into one directory Cary Fitzhugh Ruby 5 11-04-2008 06:41 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
wrapping C++, how to wrap an object? Torsten Mohr Python 1 03-07-2004 10:53 AM



Advertisments