Wrapper objects

N

niidjan

I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?
 
J

John Harrison

I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary?

A wrapper object is an object that acts as a wrapper round another
object. That could mean almost anything.

I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?

No it would not.


First you need to clarify your requirements. Do you mean a list which
can hold objects of one single type (a homogenous list)? Well C++
already has that. Or do you mean a list which can hold different types
simultanously (a heterogenous list)? If that is what you mean then where
does the template fit in?

Can't help until you answer this fundamental question.

john
 
M

mlimber

I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?

A wrapper object is probably a reference to using the constructor and
destructor for automatic and exception safe resource allocation and
deallocation. I'm not sure what your source means by suggesting it, but
perhaps s/he was thinking of something like boost::shared_ptr to hold
elements if they are pointers (this doesn't really apply if the objects
are not pointers).

See Stroustrup's paper on the subject of wrapping:

http://www.research.att.com/~bs/wrapper.pdf

BTW, there's already a templatized linked list in the STL, and you
shouldn't roll your own unless you have to (e.g. for a data structures
class).

Cheers! --M
 
B

ben

I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary? I am trying to create a templated
linked list data structure to store any object or primitive however I'm
told it would work best with a wrapper object. I did a google search
and came up with a java
definition(http://javaalmanac.com/egs/java.lang/Wrap.html) would it
work for C++ the same way?


A wrapper object most likely would act on behalf of the object in wraps,
in a way that would make accessing the wrapped object:
possible (e.g. if the wrapped object is somewhere on the Internet);
easier (e.g. if the wrapped object has a crumblesome interface);
safer (e.g. keeps locks, reference counting, etc);
faster (by utilizing optimized operation, by caching last result, etc);
recordable (bookkeeping debug information, records exception, etc);
abstract (by doing away some unnecessary operations and so make it
compliant to other similar objects);
more flexible (e.g. the damage done by changing the public interface of
the wrapped object is contained);
more managible (e.g. if the wrapper does give a simpler interface, etc.)

There are also cases when a wrapper object wraps around an interface
that does not explicitly use C++ class objects. These wrappers
simplifies, encapsulates, restructures, refactors and in some cases
adapts the wrapped interface to suit your needs.

Ben
 
E

Eric Pruneau

I would like to know more about C++ wrappers. What is a wrapper object
in C++ and why is it neccessary?

Example:
I develop algorithms to do image processing. I can use a few image
processing library to help me
do several basic operations on images. lets say I have 2 library A and B and
each of them have a function
to transform a color image to grey scale.

Now the 2 available functions (one in library A and 1 in B) dont share the
same parameters list and dont even share
the same name. But I want to flexibility to easily use eigther the function
from A or B. So I make a wrapper
around the 2 functions. Basically its a function that can call library A or
B and it deal with the details like the possible
diffenrence in the parameters list.

Once you have wrapped everything you want, its now easy to add a third
library or to kill one you dont want anymore,
since you always deal with generic functions, and never a library specific
interface.

Eric
 
J

Jordan

Here is another way to look at it. A wrapper is simply a method or
class that delegates traffic to another method or class.

Consider a DLL. A DLL can only expose methods - not objects
themselves. If I want to have a class object in a DLL, you have to
expose methods that are public from outside the DLL. Those methods
would then delegate the calls to the inner class object methods. The
exposed DLL methods would then be called wrapper methods.

Here is another example. Let's take Java for instance. Java runs on
top of a JVM meaning it is garbage collected, etc. To communicate with
the outside world, Java uses what is called the JNI (or Java Native
Interfaces). All communication with the outside world (e.g. external
processes, system libraries, DLLs, etc.) must go through the JNI. A
common thing to do is to create "proxy" or "wrapper" objects that
mimick the functionality of the external libraries and handle all of
the JNI communication. That way, your application would just use the
wrapper objects without worrying how it works with the JNI below. Make
sense?
 

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

Similar Threads

Wrapper functions 1
wrapper objects 2
wrapper objects 0
Cache System for millions of objects 2
JNI C++ Wrapper 1
Best way to create true wrapper? 5
objects and trolls 2
Wrapper objects 12

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top