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 

proposal: new auto_CONTAINER.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Dhruv
Guest





PostPosted: Thu Oct 23, 2003 8:25 pm    Post subject: proposal: new auto_CONTAINER. Reply with quote



I was wondering whether if containers like auto_ptr<> can be designed and
used, because I have frequently required such functionality, and upto now,
I've been using a combination of the standard containers, and
boost::shared_ptr, but shared_ptr does have it's own overhead, and
efficiency glitches, so why not have a container that does exactly what
auto_ptr does? Manage memory........... So, it can destroy it's elements
when it it's scope is over, and also the basic iterators work like a
normal container, so that the client does not have to dereference then
while using. Makes the code neater.

I have here a rough sketch of what I have in mind. Any suggestions are
most welcome....... The begin(), end(), front(), back(), and all forms of
erase need to be overridden. Right now, I have only overridden the dtor to
show what use this container can be of. Also, the iterators will have to
be rewritten as shown, to change the behaviour of operator*, and
operator->.



//BEGIN CODE.


#include <vector>
#include <iostream>

namespace nstd {

template <class Type, class Allocator = std::allocator
class auto_vector : public std::vector<Type*, Allocator> {

typedef typename std::vector<Type*, Allocator> Base;

class ptr_iterator : public Base::iterator {
typedef typename Base::iterator Iterator_Base;

public:
ptr_iterator () { }
ptr_iterator (Iterator_Base IB_) : Iterator_Base (IB_) { }
Type& operator* () { return *Iterator_Base::operator*(); }
};

public:
typedef typename Base::value_type value_type;
typedef typename Base::const_reference const_reference;
typedef ptr_iterator iterator;
// void push_back (const_reference data)
// {
// std::cout<<"In the derived function"< // Base::push_back (data);
// }

iterator begin() { return static_cast ~auto_vector ()
{
typename Base::iterator rover = Base::begin ();
typename Base::iterator last = Base::end ();
while (rover != last) {
delete *rover;
++rover;
}
}
};
}


int main ()
{
nstd::auto_vector<int> avi;
avi.push_back (new int(23));
nstd::auto_vector<int>::iterator iter = avi.begin();
std::cout<<*iter;
}

//Container automatically destroyes the pointer's contents.





//END CODE.


Regards,
-Dhruv.






---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Mirek Fidler
Guest





PostPosted: Fri Oct 24, 2003 9:16 pm    Post subject: Re: proposal: new auto_CONTAINER. Reply with quote



Quote:
auto_ptr does? Manage memory........... So, it can destroy it's
elements
when it it's scope is over, and also the basic iterators work like a
normal container, so that the client does not have to dereference then
while using. Makes the code neater.

int main ()
{
nstd::auto_vector<int> avi;
avi.push_back (new int(23));
nstd::auto_vector<int>::iterator iter = avi.begin();
std::cout<<*iter;
}


Looks like NTL Array had some influence on you in the end :)

(for others, this idea is very similiar to Array template class of
[url]www.ntllib.org)[/url].

Mirek


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Dhruv
Guest





PostPosted: Mon Oct 27, 2003 1:44 am    Post subject: Re: proposal: new auto_CONTAINER. Reply with quote



On Fri, 24 Oct 2003 21:16:54 +0000, Mirek Fidler wrote:

Quote:
Looks like NTL Array had some influence on you in the end :)

(for others, this idea is very similiar to Array template class of
[url]www.ntllib.org)[/url].

Yes, in fact one of your colleague has something very similar in the boost
sandbox, named ptr_container...


Regards,
-Dhruv.



---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards 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.