Boost logo

Boost :

From: Greg Colvin (greg_at_[hidden])
Date: 2001-01-06 15:04:16


You might first look at the "Smart pointers and array types" thread.

----- Original Message -----
From: Daryle Walker <darylew_at_[hidden]>
To: Boost <boost_at_[hidden]>
Sent: Saturday, January 06, 2001 12:55 PM
Subject: [boost] Suggestion: auto_ary

> Should we have a class template that is the array analogue for
> std::auto_ptr? Here's a sample implementation (not compiled), inspired from
> _The C++ Standard Library: A Tutorial and Reference_ by Nicolai M. Josuttis:
>
> namespace boost
> {
> // auxiliary type for copies and assignments
> template <typename Y>
> struct auto_ary_ref
> {
> Y * ya_;
>
> auto_ary_ref( Y *rhs )
> : ya_( rhs )
> {}
> };
>
> // allocated array memory management
> template <typename T>
> class auto_ary
> {
> private:
> T * aa_;
>
> public:
> typedef T element_type
>
> // constructors and destructor
> explicit auto_ary( T *ary = 0 ) throw()
> : aa_( ary )
> {}
> explicit auto_ary( std::size_t ary_size, T const *init_val = 0 )
> throw()
> : aa_( new T[ary_size] )
> {
> if ( init_val )
> {
> std::copy( init_val, init_val + ary_size, aa_ );
> }
> }
> auto_ary( auto_ary &old ) throw()
> : aa_( old.release() )
> {}
> auto_ary( auto_ary_ref<T> old ) throw()
> : aa_( old.ya_ )
> {}
> ~auto_ary() throw()
> { delete [] aa_; }
>
> // operators
> auto_ary & operator =( auto_ary &old ) throw()
> { reset( old.release() ); return *this; }
> auto_ary & operator =( auto_ary_ref<T> old ) throw()
> { reset( old.ya_ ); return *this; }
> T & operator []( std::size_t index ) const throw()
> { return aa_[index]; }
> operator T*() const throw()
> { return aa_; }
> operator auto_ary_ref<T>() throw()
> { return auto_ary_ref<T>( release() ); }
>
> // operations
> T * get() const throw()
> { return aa_; }
> T * release() throw()
> { T *tmp( aa_ ); aa_ = 0; return tmp; }
> void reset( T *ary = 0 ) throw()
> { if ( aa_ != ary ) { delete [] aa_; aa_ = ary; } }
> };
> }
>
> I was thinking of this because:
> 1. It complements the std::auto_ptr class template for single objects
> 2. The "use a STL container, like std::vector, instead of arrays" refrain
> also brings a bigger interface, which could be overkill if all you wanted
> was a quick-and-dirty resource manager. And, as mentioned by Howard Hinnant
> in a recent post by William Kempf*, a specialized class could bring
> advantages in code size or speed.
> 3. The STL suggestion assumes that the user can switch his or her container
> paradigm. If there's some constraint against this, then the STL suggestion
> is insultingly useless.
>
> * William's post has Howard's suggestion of making versions of std::auto_ptr
> partially specialized for array types. I used a separate class template
> since auto_ary and auto_ptr have different semantics, so one should not be
> forced to match the other's interface.
>
> --
> Daryle Walker
> Mac, Internet, and Video Game Junkie
> darylew AT mac DOT com
>
>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk