U
utab
Dear all,
In programming terminology, what is a wrapper and where is it used?
Regards
In programming terminology, what is a wrapper and where is it used?
Regards
Well for instance you could use a C++ wrapper class around regular C code.utab said:Dear all,
In programming terminology, what is a wrapper and where is it used?
Regards
Moonlit said:Hi
-
Well for instance you could use a C++ wrapper class around regular C code.
For instance in my personal generic library I have a wrapper class around
the pnglib libary I can the open an ifstream of a .png file and read it with
operator>> or write png files with operator<<. This also works for memory
streams etc. The C++ wrapper takes care of memory allocation and freeing the
memory and of the complexity of setting the pointers to static members to
replace the standard C FILE oriented operations.
i.e. the complexity is inside the C++ wrapper class. The wrapper class
itself is extremely simple to use. Instead of looking in the manual of png
everytime I can now load and save png in a very simple way.
Regards, Ron AF Greve
http://moonlit.xs4all.nl
utab said:Thanks Ron,
So in a simple manner, it is a code block to make things easier for
mixed-language programming(for the original C library in your case.).
Then briefly speaking, this is related to using more than one language
in your code. Am I correct?
Regards,
In programming terminology, what is a wrapper and where is it used?
BitHandle &operator++() const
Moonlit said:I think it is indeed mostly ( if not always ) used to make things easier.
The second statement is a bit more difficult to answer since C is for the
most part also C++. But all of my wrapper classes are indeed around regular
C functions/ libraries and not around C++ libraries. But I could imagine
that when you have a complex C++ library and you only want to use part of it
you might still want to write a wrapper class around it (then again maybe
the library itself should have been redesigned) . But from a practical point
of view I think that is right.
--
Regards, Ron AF Greve
http://moonlit.xs4all.nl
Frederick said:utab posted:
I'm not sure if this is a generally accepted meaning of the term, but I
take "wrapper" to mean anything which erects a 20-foot-tall wall around
something and provides a nice simple interface to the outside world. For
instance, we might create a wrapper for working with individual bits in
memory:
#include <climits>
class BitHandle {
private:
char unsigned *p;
unsigned index;
public:
operator bool() const
{
return *p & 1U<<index;
}
BitHandle &operator=(bool const val)
{
if(val) *p |= 1U<<index;
else *p &= ~(1U<<index);
return *this;
}
BitHandle &operator++() const
{
if(CHAR_BIT == index) ++p, index = 0;
else ++index;
return *this;
}
};
mean type compatibility, for instance C does not have a built-in vector
type but C++ has. And while writing some code which needs to use
Arve said:Does C++ implement a Vector type ?
STL maybe, but not C++ as an intrinsic type, as far as I know.
Dear Frederick,
So that is a way to make things more understantable from the
programmers point of view. Using the existing but in a fancier
interface(in a different form).
So that is a way to make things more understantable from the
programmers point of view. Using the existing but in a fancier
interface(in a different form).
Does C++ implement a Vector type ?
STL maybe, but not C++ as an intrinsic type, as far as I know.
utab said:Thanks again,
What do you mean by this
"But I could imagine
that when you have a complex C++ library and you only want to use part
of it
you might still want to write a wrapper class around it "
You mean to copy the parts you like to use for a complete C
application(in a class).
Is that also related with compatibility while using two languages. I
mean type compatibility, for instance C does not have a built-in vector
type but C++ has. And while writing some code which needs to use
vectors in the code for instance, you use a proper C++ wrapper in the
proper interface to use these features of C++ in C. Am I completely
confusing or on the right way?
Regards,
Regards,
utab said:In programming terminology, what is a wrapper and where is it used?
A wrapper/adapter presents an interface using some code with has another
interface.
Kind of. What wrappers often intend to provide is some way to
modify a set of existing interfaces to be more in line with the
criterea and view and design of something else. Obviously then
it is not always successful, or easy. Sometimes it is a way to
establish a bridge to a library or between 2 libraries. An
adapter if you will.
Also, wrappers can often be used to constrain or change or even
check the expectations of the thing be wrapped.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.