C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Container for polymorphic types

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ulrich Eckhardt
Guest





PostPosted: Wed Dec 07, 2005 1:57 pm    Post subject: Container for polymorphic types Reply with quote



Fellows,

I think the topic itself is not something new and there are already
containers made for storage of polymorphic types (e.g. the one in Boost)
but there I had the idea of a different approach: instead of wrapping
pointers and cloning objects on demand, I'd rather drop the ability to
insert object.
Sounds strange? Yes, maybe the idea is not useful but please read on, I must
also confess that I've had lots of coffee today and that this is a
braindump and not the result of proper research.

The idea is that you create objects inside the container instead of creating
them externally and copying them into the container. For the particular
cases I have in mind (i.e. my work) this is all I need (yes, apart from
destroying and iteration). Now, the difference is that you can't detach
them from the container. You can also make them non-copyable, even
nonclonable (ever wanted a list<ostream> for logging?). Of course, this is
only feasible for node-based containers (linked lists, trees), creating a
polymorphic vector that still guarantees contiguous storage is a)
unnecessary (you only need the contiguous for array replacement, and there
are no polymorphic arrays) and b) would only be possible if you knew the
size of the objects in advance, which you don't.


Now, how would this be used?

typedef polymorphic_list<ostream> streams;

streams s;
// create an object of type 'ofstream' and pass "fou.txt" to its ctor
// considering the varying amount of possible arguments create()
// needs to be overloaded
s.create<ofstream>("fou.txt");

// create new object at the beginning
// this will probably be a bit tricky because one needs to
// distinguish between an iterator and an argument to the ctor
s.create<ostringstream>( s.begin());

streams::iterator it = s.begin();

// '*it' yields an ostream&
*it << "Hallo monde!";

// destroy the last element
s.erase(*it);


Credits: this was inspired by someone that strongly opposed the use of
containers because of their overhead while copying object into them and out
of them. I forgot who it was, the only things I remember was that I
otherwise didn't agree with that person at all but that it inspired me to
think of how it would be like when this transferring into and out of was
removed from containers.


OK, so much for now, back to work...


Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thant Tessman
Guest





PostPosted: Wed Dec 07, 2005 4:01 pm    Post subject: Re: Container for polymorphic types Reply with quote



Ulrich Eckhardt wrote:

[...]

Quote:
I think the topic itself is not something new and there are already
containers made for storage of polymorphic types (e.g. the one in Boost)
but there I had the idea of a different approach: instead of wrapping
pointers and cloning objects on demand, I'd rather drop the ability to
insert object.

[...]


Quote:
Credits: this was inspired by someone that strongly opposed the use of
containers because of their overhead while copying object into them and out
of them. [...]

Another approach is to build variant-type functionality into your
garbage-collecting smart pointer. This avoids unecessary copying, but in
a way that reduces the artificial constraints on their use imposed by
the need to manage memory. One project that uses this approach is
described here:

http://www.standarddeviance.com/sigma.html

Full disclosure: The implementation of smart pointers in the above
project is not exactly light-weight, but it has so far demonstrated
itself to be fairly bulletproof.

-thant

--
The nationalist not only does not disapprove of atrocities
committed by his own side, but he has a remarkable capacity
for not even hearing about them. -- George Orwell

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mirek Fidler
Guest





PostPosted: Wed Dec 07, 2005 6:49 pm    Post subject: Re: Container for polymorphic types Reply with quote




Quote:
The idea is that you create objects inside the container instead of creating
them externally and copying them into the container. For the particular

Yes, that is what I do for more than 7 years now....


http://upp.sourceforge.net/src-us.html

see the first member functions listed :)


Quote:
cases I have in mind (i.e. my work) this is all I need (yes, apart from
destroying and iteration). Now, the difference is that you can't detach
them from the container.

Why not? You can if you need it. My petty Array allows it all - just
make your item types satisfy requirements defined on PER METHOD basis
rather than per library :)

Quote:
typedef polymorphic_list<ostream> streams;

streams s;
// create an object of type 'ofstream' and pass "fou.txt" to its ctor
// considering the varying amount of possible arguments create()
// needs to be overloaded
s.create<ofstream>("fou.txt");

However, this one maybe be a little bit difficult to achieve - you will
have to find a way how to pass parameters to constructor (ok, can be
done using more template parameters). I prefer

s.Create<Stream>().Open("foo.txt");

or

Stream& str = s.Create<Stream>();
....do whatever with 'str'

Quote:
Credits: this was inspired by someone that strongly opposed the use of
containers because of their overhead while copying object into them and out
of them.

Use containers. Do not use STL :)

Mirek

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.