auto_buffer.h.

(B)Leading Edge
Simple Memory Management Classes

Jack Reeves
Listing 1. auto_buffer.h.


#ifndef BEST_AUTO_BUFFER_H
#define BEST_AUTO_BUFFER_H

namespace BEST {

  template<class T>
  class auto_buffer {
    T*	_ptr;
    size_t	_siz;
  public:
    typedef T element_type;
    // construct/destroy/copy
    explicit auto_buffer(T* p = 0) throw()
      : _ptr(p), _siz(-1) {}

    auto_buffer(auto_buffer<T>& rhs) throw()
      : _ptr(rhs._ptr), _siz(rhs.siz) { rhs.release(); }

    ~auto_buffer()
      { delete[] _ptr; }

    auto_buffer<T>& operator=(auto_buffer<T>& rhs);

    // operations
    T* data() const throw()
      { return _ptr; }
    T* release() throw();
    size_t size() const throw()
      { return _siz; }
    void size(size_t val) throw()
      { _siz = val; }
  };

  // All operations are inlined
  template<class T>
  inline auto_buffer<T>&
  auto_buffer<T>::operator=(auto_buffer<T>& rhs)
  {
    delete[] _ptr;
    _siz = rhs._siz; // release clears size
    _ptr = rhs.release();
    return *this;
  }

  template<class T>
  inline  T*
  auto_buffer<T>::release() throw()
  {
    T* p = _ptr;
    _ptr = 0;
    _siz = 0;
    return p;
  }

} // end namespace

#endif

Featured

Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.