By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

What is a Wrapper

P: n/a
Dear all,

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

Regards

Oct 1 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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
Regards, Ron AF Greve

http://moonlit.xs4all.nl
Oct 1 '06 #2

P: n/a

Moonlit wrote:
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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

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,

Oct 1 '06 #3

P: n/a

"utab" <um********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>
Moonlit wrote:
>Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
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

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?
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,
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl
Oct 1 '06 #4

P: n/a
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;
}
};

--

Frederick Gotham
Oct 1 '06 #5

P: n/a
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).

--

Frederick Gotham
Oct 1 '06 #6

P: n/a

Moonlit wrote:
"utab" <um********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

Moonlit wrote:
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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


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?

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,

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

Oct 1 '06 #7

P: n/a

Frederick Gotham wrote:
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;
}
};

--

Frederick Gotham
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

Oct 1 '06 #8

P: n/a
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.

"utab" <um********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>
Moonlit wrote:
>"utab" <um********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegr oups.com...
>
Moonlit wrote:
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
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
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?

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,

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

Oct 1 '06 #9

P: n/a

Arve Sollie wrote:
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.

"utab" <um********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

Moonlit wrote:
"utab" <um********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

Moonlit wrote:
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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


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?

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,


--
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,
Of course STL, thanks for the warning

Oct 1 '06 #10

P: n/a
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.

--

Frederick Gotham
Oct 1 '06 #11

P: n/a
In article <11**********************@h48g2000cwc.googlegroups .com>,
utab <um********@gmail.comwrote:
>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.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 1 '06 #12

P: n/a
In article <12*************@corp.supernews.com>,
Arve Sollie <ne********@codeworks.nowrote:
>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 ?
Builtin arrays.
>STL maybe, but not C++ as an intrinsic type, as far as I know.
Otherwise yeah, stuff like std::vector, std::string etc
which are library.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 1 '06 #13

P: n/a


"utab" <um********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>
Moonlit wrote:
>"utab" <um********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegr oups.com...
>
Moonlit wrote:
Hi

-
"utab" <um********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
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
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?

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,

--
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?
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
Oct 1 '06 #14

P: n/a
utab schrieb:
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 1 '06 #15

P: n/a
On Sun, 01 Oct 2006 17:46:55 +0200, "Thomas J. Gritzan" wrote:
>utab schrieb:
>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.
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
Oct 1 '06 #16

P: n/a
Greg Comeau <co****@comeaucomputing.comwrote:
>utab <um********@gmail.comwrote:
>>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.
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
Oct 3 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.