What is a Wrapper

U

utab

Dear all,

In programming terminology, what is a wrapper and where is it used?

Regards
 
M

Moonlit

Hi

-


utab said:
Dear all,

In programming terminology, what is a wrapper and where is it used?
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
 
U

utab

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

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,
 
M

Moonlit

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?

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
 
F

Frederick Gotham

utab posted:
In programming terminology, what is a wrapper and where is it used?


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;
}
};
 
F

Frederick Gotham

Frederick Gotham posted:
BitHandle &operator++() const


Of course, that shouldn't be const (but then again it wasn't a full example
to start off with).
 
U

utab

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

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,
 
U

utab

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;
}
};

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).

Regards
 
A

Arve Sollie

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

Does C++ implement a Vector type ?
STL maybe, but not C++ as an intrinsic type, as far as I know.
 
F

Frederick Gotham

utab posted:
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).


The idea is "encapsulation", i.e. the separation of one complicated idea
from another.

Let's take "std::string" for example. People always cheer about how much
more simple it is to work with than null-terminated arrays of char.

But the reality is this: Nothing has been simplified -- the code for
"std::string" contains the same nitty-gritty pointers and manipulation of
null-terminated char arrays.

The only thing that's changed is that a partition has been put up between
"this stuff" and "that stuff", giving the idea that things have been
simplified.

It's like having two petry dishes side by side, each containing a little
blob of liquid. If we zoom out, we don't seen anything that complicated,
just two blobs of liquid side by side... but if we zoom in on one of them,
we'll see all the complexity of the molecular bonds and so forth...

The idea behind encapsulation is to zoom out and look at the big picture --
it makes everything look nice and simple in our minds.

Wrappers make encapsulation possible. You could say that "std::string" is a
wrapper for simplifing the complexity of working with strings.
 
G

Greg Comeau

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).

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. For instance,
a thread here yesterday showed somebody wrapped std::vector to
detect a possible out of range situation.
 
M

Moonlit

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?

Hm, that's a bit hard for me to answer since I never have come across code
that uses C++ functions from C.

As mentioned in other parts of this thread I think it is mostly used for
encapsulation. Making something easier to use. You put the effort in once to
make the class correct and handle all the dificulties of the encapsulated
thing (be it a library, a bunch of functions or maybe even other C++
classes). And for then on you only have to use the simple interface of your
C++ class.

But I don't think there is really a 'tight' definition of a wrapper class
(at least not that I know of). So as long as you wrap things inside it, it
probably is a moot point whether it is a wrapper or not. But I think the way
most people feel about it is in the previous paragraph, i.e. encapsulation,
hiding the complexity of something inside the wrapper class..
Regards,
Regards,

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
T

Thomas J. Gritzan

utab said:
In programming terminology, what is a wrapper and where is it used?

I think it is the same as the Adapter design pattern:
http://en.wikipedia.org/wiki/Wrapper
http://en.wikipedia.org/wiki/Adapter_pattern

A wrapper/adapter presents an interface using some code with has another
interface.

The reason could be everything from interlanguage programming (calling C++
from C or vice versa) to wrapping an ostream into a C++ interator
(ostream_iterator<> template). The links give other examples too.
 
R

Roland Pibinger

A wrapper/adapter presents an interface using some code with has another
interface.

IMO, there is a subtle difference between adapter and wrapper in C++.
Adapted code is somehow 'visible' to the user (think of container
adapters) whereas wrappers entirely hide the wrapped code. Adapters
are workarounds (like a USB to PS/2 keyboard adaper).

Best wishes,
Roland Pibinger
 
S

Steve Pope

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.

Yes, I agree with this -- a wrapper doesn't necessarily form
a simpler interface than the one below it, nor does it necessarily
interface one underlying programming language to another. It
does change the interface (while doing little or no actual
processing) to meet some set of different expectation.

Steve
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top