| 1 |  | 
|---|
| 2 | // This code is based on bits/std_memory.h from GCC. | 
|---|
| 3 | // The copyright and license information from the | 
|---|
| 4 | // original code is below. | 
|---|
| 5 |  | 
|---|
| 6 | // Copyright (C) 2001 Free Software Foundation, Inc. | 
|---|
| 7 | // | 
|---|
| 8 | // This file is part of the GNU ISO C++ Library.  This library is free | 
|---|
| 9 | // software; you can redistribute it and/or modify it under the | 
|---|
| 10 | // terms of the GNU General Public License as published by the | 
|---|
| 11 | // Free Software Foundation; either version 2, or (at your option) | 
|---|
| 12 | // any later version. | 
|---|
| 13 |  | 
|---|
| 14 | // This library is distributed in the hope that it will be useful, | 
|---|
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | // GNU General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | // You should have received a copy of the GNU General Public License along | 
|---|
| 20 | // with this library; see the file COPYING.  If not, write to the Free | 
|---|
| 21 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | 
|---|
| 22 | // USA. | 
|---|
| 23 |  | 
|---|
| 24 | // As a special exception, you may use this file as part of a free software | 
|---|
| 25 | // library without restriction.  Specifically, if other files instantiate | 
|---|
| 26 | // templates or use macros or inline functions from this file, or you compile | 
|---|
| 27 | // this file and link it with other files to produce an executable, this | 
|---|
| 28 | // file does not by itself cause the resulting executable to be covered by | 
|---|
| 29 | // the GNU General Public License.  This exception does not however | 
|---|
| 30 | // invalidate any other reasons why the executable file might be covered by | 
|---|
| 31 | // the GNU General Public License. | 
|---|
| 32 |  | 
|---|
| 33 | /* | 
|---|
| 34 | * Copyright (c) 1997-1999 | 
|---|
| 35 | * Silicon Graphics Computer Systems, Inc. | 
|---|
| 36 | * | 
|---|
| 37 | * Permission to use, copy, modify, distribute and sell this software | 
|---|
| 38 | * and its documentation for any purpose is hereby granted without fee, | 
|---|
| 39 | * provided that the above copyright notice appear in all copies and | 
|---|
| 40 | * that both that copyright notice and this permission notice appear | 
|---|
| 41 | * in supporting documentation.  Silicon Graphics makes no | 
|---|
| 42 | * representations about the suitability of this software for any | 
|---|
| 43 | * purpose.  It is provided "as is" without express or implied warranty. | 
|---|
| 44 | * | 
|---|
| 45 | */ | 
|---|
| 46 |  | 
|---|
| 47 | #ifndef _util_misc_autovec_h | 
|---|
| 48 | #define _util_misc_autovec_h | 
|---|
| 49 |  | 
|---|
| 50 | #include <stddef.h> | 
|---|
| 51 |  | 
|---|
| 52 | namespace sc { | 
|---|
| 53 |  | 
|---|
| 54 | /** The auto_vec class functions much like auto_ptr, except | 
|---|
| 55 | it contains references to arrays.  The delete[] operator will be used | 
|---|
| 56 | to deallocate data. */ | 
|---|
| 57 | template <class T> | 
|---|
| 58 | class auto_vec { | 
|---|
| 59 | T* d_; | 
|---|
| 60 | public: | 
|---|
| 61 | typedef T element_type; | 
|---|
| 62 |  | 
|---|
| 63 | /** Creates a new auto_vec for a vector, d, of type T. | 
|---|
| 64 | The d argument must be created with the vector new | 
|---|
| 65 | operator: new T[...]. */ | 
|---|
| 66 | explicit auto_vec(T*d = 0) throw(): d_(d) {} | 
|---|
| 67 |  | 
|---|
| 68 | /** Create a auto_vec, transferring the storage from another. */ | 
|---|
| 69 | auto_vec(auto_vec &av) throw(): d_(av.release()) {} | 
|---|
| 70 |  | 
|---|
| 71 | /** This will delete the vector. */ | 
|---|
| 72 | ~auto_vec() throw() { delete[] d_; } | 
|---|
| 73 |  | 
|---|
| 74 | /** This member transfers the data from av to this. */ | 
|---|
| 75 | auto_vec &operator = (auto_vec &av) throw() { | 
|---|
| 76 | reset(av.release()); | 
|---|
| 77 | return *this; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** Returns the pointer. */ | 
|---|
| 81 | T* get() const throw() { return d_; } | 
|---|
| 82 |  | 
|---|
| 83 | /** Returns the i'th element. */ | 
|---|
| 84 | T &operator[](size_t i) throw() { return d_[i]; } | 
|---|
| 85 |  | 
|---|
| 86 | /** Release ownership. */ | 
|---|
| 87 | T* release() throw() { | 
|---|
| 88 | T *r = d_; | 
|---|
| 89 | d_ = 0; | 
|---|
| 90 | return r; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | /** Assign to a new value. */ | 
|---|
| 94 | void reset(T*d=0) throw() { | 
|---|
| 95 | if (d != d_) { | 
|---|
| 96 | delete[] d_; | 
|---|
| 97 | d_ = d; | 
|---|
| 98 | } | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | #endif // _util_misc_autovec_h | 
|---|
| 106 |  | 
|---|