473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Wrapping C code into a C++ object

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.

Thanks in advance,
Tom the Cunuck.
Dec 3 '05 #1
10 2343
On Sat, 3 Dec 2005 11:04:11 -0500, "Tom the Canuck"
<tv***@sympatico.ca> 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?
'Implementation inheritance' isn't necessarily the best way to
proceed. C frameworks are often 'object based' but not polymorphic. So
'delegation' may be better than 'inheritance'.
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.


I know nothing about OpenGL but a Google search with 'OpenGL C++'
yields many interesting links. Maybe someone has already done the work
for you.

Best wishes,
Roland Pibinger
Dec 3 '05 #2

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?
Woah! Why do you reckon you need inheritance here?

Inheriting from a pure virtual class is incredibly
useful if you want multiple objects to share a
common interface and be polymorphic. If you
don't, don't do that.

If you want to share some code between
different classes derived from an interface, then
one option is to put the code in another class
and use multiple inheritance. Derive from
both the interface (pure virtual class) and the
implementation class.

However, if you just want the C code wrapped up
for convenient use -- don't use inheritance, there's
no point. Just make the class.

If you want advice on OO in C++ then a good
place to start is Bjarne Stroustrup's homepage.
He is not shy about expressing his views on the
topic. <wink>
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.

Thanks in advance,
Tom the Cunuck.


HTH

James M

Dec 3 '05 #3
Tom the Canuck wrote:
What would be the best way to proceed?
There is no "best" way in programming.
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?
Well that is a singular way to define what "objects" are for.

http://en.wikipedia.org/wiki/Object_...ter_science%29
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.


One easy way would be to wrap the C code into a simple class.

class A
{
public:
void f()
{
c_f(&acs); // call the C function from f()
}

private:
a_C_struct acs;
};

If you think the implementation could change (by changing the library
for example), you could hide the implementation (openGL in this case)
using the so called "pimpl" idiom (google for it).
Jonathan

Dec 3 '05 #4

"Tom the Canuck" <tv***@sympatico.ca> wrote in message
news:hm********************@news20.bellglobal.com. ..
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.

Thanks in advance,
Tom the Cunuck.


Thanks for all the replies. My path is now clear and straight ahead.
With a bit of work, I will do what I wanted to do.

Tom the Canuck.
Dec 3 '05 #5
On Sat, 3 Dec 2005 11:04:11 -0500, "Tom the Canuck"
<tv***@sympatico.ca> 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.

Thanks in advance,
Tom the Cunuck.


As others have pointed out, there is no "best answer" here. But it
would be interesting to point out in very general terms what
advantages C++ can give you when wrapping a set of C functions which
together constitute some kind of system or library.

Let's start with some of the more important ones:

- RAII ("Resource Acquisition Is Initialization"). Design the C++
class so that it automatically (i.e. upon construction of an object)
allocates all the necessary memory for its members, possibly using
smart pointers for exception safety, and cleans up after itself on
destruction. This might or might not necessarily entail calling memory
allocation and de-allocation functions such as "new" and "delete", but
also other things such as initializing and freeing "handles" or
calling global C initialization and termination functions in the
third-party library. This, IMHO, is one of the areas where C++ really
shines.

- Type safety. Clients need not fool around with passing void pointers
to a function in some circumstances, but casting the same argument to
unsigned int in others and passing an additional "flag" argument so
that the function knows which type to expect (you can tell I'm talking
about ODBC here <g>). Take the all-purpose C library function and
provide several context-specific C++ member functions which pass the
appropriate types.

- Encapsulation. Hide your handles, buffers, etc. from the clients and
use STL containers such as std::string, std::vector, std::list, etc.

- Provide meaningful error parsing, logging and recovery by using C++
exception classes.

Well, I'm just getting started here ... but maybe some other people
would like to continue the list?

--
Bob Hairgrove
No**********@Home.com
Dec 3 '05 #6
Ian
Bob Hairgrove wrote:

- Type safety. Clients need not fool around with passing void pointers
to a function in some circumstances, but casting the same argument to
unsigned int in others and passing an additional "flag" argument so
that the function knows which type to expect (you can tell I'm talking
about ODBC here <g>). Take the all-purpose C library function and
provide several context-specific C++ member functions which pass the
appropriate types.
UNIX iotcl are another example of this.
Well, I'm just getting started here ... but maybe some other people
would like to continue the list?

- adding functions or operators to C types, one example that comes to
mind are time type structures where math operators come in very handy.

- portability!

Ian
Dec 3 '05 #7
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"
}

// create a namespace for the library
namespace library {
class MyWrapper {
// Do Initialisation in Constructor
MyWrapper() { libraryImpl::init(); }

// Do Deinitialisation in destructor
~MyWrapper() { libraryImpl::deInit(); }

// Wrap functions that rely on above initialisation
DoSomething() { libraryImpl::DoSomething(); }
};

// Perhaps this does not rely on the initialisation
FreeFunction() { libraryImpl::FreeFunction(); }
}

int main() {
library::MyWrapper lib;

lib.DoSomething();

library::FreeFunction();
}
I'm sure you get the idea.

Ben Pope
Dec 5 '05 #8

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()!
// create a namespace for the library
namespace library {
class MyWrapper {
// Do Initialisation in Constructor
MyWrapper() { libraryImpl::init(); }

// Do Deinitialisation in destructor
~MyWrapper() { libraryImpl::deInit(); }

// Wrap functions that rely on above initialisation
DoSomething() { libraryImpl::DoSomething(); }
};

// Perhaps this does not rely on the initialisation
FreeFunction() { libraryImpl::FreeFunction(); }
}

int main() {
library::MyWrapper lib;

lib.DoSomething();

library::FreeFunction();
}
I'm sure you get the idea.

Jonathan

Dec 5 '05 #9
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.

Header
--
namespace library {
class MyWrapper{
...
};
}
Source
--
#include "wrappedLibrary.h"

namespace library {
MyWrapper::MyWrapper ...
}
Ben Pope
Dec 5 '05 #10

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

Dec 5 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
0
by: sklett | last post by:
I'm having a really hard time with wrapping an unmanaged class with a managed class, then calling that managed class from my C# code. I will show you the three pieces, then explain: --------...
2
by: Paul Kenny | last post by:
Hi, I am trying to expose the functionality of an unmanaged C++ class to the other languages available in the .NET Framework. I have decided to do this by wrapping the unmanaged C++ class in a...
4
by: Richard MSL | last post by:
I have an application using WinForms under C#. I do not use the IDE, I use the command line compiler, I build my controls in my application code. The application generally works fine, except for...
2
by: Andrzej Kaczmarczyk | last post by:
Hi I am experiencing something weird. maybe you could help me. I have two ineditable classes from outsource libraries: DataColumn and GridColumn I have built a wrapper class around...
1
by: jsroberts | last post by:
I have been desperately trying to solve a problem where I am wrapping an unmanaged dll with managed extensions for c++ and then calling the managed dll functions from C#. However, whenever I call...
3
by: gabriel.becedillas | last post by:
Hi, I'm having problems wrapping a hierarchy of classes, actually having problems wrapping the base class. I don't need to use the WrapClass mechanism since I don't want to override classes in...
1
by: lisa232 | last post by:
Hi All, I have a problem with some javascript code. I am trying to toggle display between two tables upon change of a select box ( to alternate between search forms) eg. one called...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.