# autovec.h

    00001
    00002 // This code is based on bits/std_memory.h from GCC.
    00003 // The copyright and license information from the
    00004 // original code is below.
    00005
    00006 // Copyright (C) 2001 Free Software Foundation, Inc.
    00007 //
    00008 // This file is part of the GNU ISO C++ Library.  This library is free
    00009 // software; you can redistribute it and/or modify it under the
    00010 // terms of the GNU General Public License as published by the
    00011 // Free Software Foundation; either version 2, or (at your option)
    00012 // any later version.
    00013
    00014 // This library is distributed in the hope that it will be useful,
    00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017 // GNU General Public License for more details.
    00018
    00019 // You should have received a copy of the GNU General Public License along
    00020 // with this library; see the file COPYING.  If not, write to the Free
    00021 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00022 // USA.
    00023
    00024 // As a special exception, you may use this file as part of a free software
    00025 // library without restriction.  Specifically, if other files instantiate
    00026 // templates or use macros or inline functions from this file, or you compile
    00027 // this file and link it with other files to produce an executable, this
    00028 // file does not by itself cause the resulting executable to be covered by
    00029 // the GNU General Public License.  This exception does not however
    00030 // invalidate any other reasons why the executable file might be covered by
    00031 // the GNU General Public License.
    00032
    00033 /*
    00034  * Copyright (c) 1997-1999
    00035  * Silicon Graphics Computer Systems, Inc.
    00036  *
    00037  * Permission to use, copy, modify, distribute and sell this software
    00038  * and its documentation for any purpose is hereby granted without fee,
    00039  * provided that the above copyright notice appear in all copies and
    00040  * that both that copyright notice and this permission notice appear
    00041  * in supporting documentation.  Silicon Graphics makes no
    00042  * representations about the suitability of this software for any
    00043  * purpose.  It is provided "as is" without express or implied warranty.
    00044  *
    00045  */
    00046
    00047 #ifndef _util_misc_autovec_h
    00048 #define _util_misc_autovec_h
    00049
    00050 #include <stddef.h>
    00051
    00052 namespace sc {
    00053
    00057 template <class T>
    00058 class auto_vec {
    00059     T* d_;
    00060   public:
    00061     typedef T element_type;
    00062
    00066     explicit auto_vec(T*d = 0) throw(): d_(d) {}
    00067
    00069     auto_vec(auto_vec &av) throw(): d_(av.release()) {}
    00070
    00072     ~auto_vec() throw() { delete[] d_; }
    00073
    00075     auto_vec &operator = (auto_vec &av) throw() {
    00076       reset(av.release());
    00077       return *this;
    00078     }
    00079
    00081     T* get() const throw() { return d_; }
    00082
    00084     T &operator[](size_t i) throw() { return d_[i]; }
    00085
    00087     T* release() throw() {
    00088       T *r = d_;
    00089       d_ = 0;
    00090       return r;
    00091     }
    00092
    00094     void reset(T*d=0) throw() {
    00095       if (d != d_) {
    00096           delete[] d_;
    00097           d_ = d;
    00098         }
    00099     }
    00100
    00101 };
    00102
    00103 }
    00104
    00105 #endif // _util_misc_autovec_h
    00106