00001 /******************************************************************************* 00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * - Redistributions of source code must retain the above copyright notice, 00008 * this list of conditions and the following disclaimer. 00009 * 00010 * - Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * - Neither the name of Vintela, Inc. nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS 00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 *******************************************************************************/ 00030 00036 #ifndef OWBI1_AUTOPTR_HPP_INCLUDE_GUARD_ 00037 #define OWBI1_AUTOPTR_HPP_INCLUDE_GUARD_ 00038 #include "OWBI1_config.h" 00039 00040 namespace OWBI1 00041 { 00042 00043 // TODO: Rename this 00044 template <class X> class AutoPtr 00045 { 00046 private: 00047 X* _ptr; 00048 00049 // no copying 00050 AutoPtr(const AutoPtr& a); 00051 AutoPtr& operator= (const AutoPtr& a); 00052 00053 public: 00054 typedef X element_type; 00059 explicit AutoPtr(X* p = 0); 00060 AutoPtr& operator= (X* p); 00061 ~AutoPtr(); 00062 X& operator*() const; 00063 X* operator->() const; 00064 X* get() const; 00065 X* release(); 00066 void reset(X* p=0); 00067 }; 00068 00069 template <class X> 00070 inline AutoPtr<X>::AutoPtr(X* p) : _ptr(p) {} 00071 00072 template <class X> 00073 inline AutoPtr<X>& AutoPtr<X>::operator= (X* p) 00074 { 00075 if (p != _ptr) 00076 { 00077 reset(); 00078 _ptr = p; 00079 } 00080 return *this; 00081 } 00082 00083 template <class X> 00084 inline AutoPtr<X>::~AutoPtr() 00085 { 00086 typedef char type_must_be_complete[sizeof(X)]; 00087 delete _ptr; 00088 } 00089 00090 template <class X> 00091 inline X& AutoPtr<X>::operator*() const { return *_ptr;} 00092 00093 template <class X> 00094 inline X* AutoPtr<X>::operator->() const { return _ptr;} 00095 00096 template <class X> 00097 inline X* AutoPtr<X>::get() const { return _ptr;} 00098 00099 template <class X> 00100 inline X* AutoPtr<X>::release() 00101 { 00102 X* rval = _ptr; 00103 _ptr = 0; 00104 return rval; 00105 } 00106 00107 template <class X> 00108 inline void AutoPtr<X>::reset(X* p) 00109 { 00110 delete _ptr; 00111 _ptr = p; 00112 } 00113 00114 template <class X> class AutoPtrVec 00115 { 00116 private: 00117 X* _ptr; 00118 00119 // no copying 00120 AutoPtrVec(const AutoPtrVec& a); 00121 AutoPtrVec& operator= (const AutoPtrVec& a); 00122 00123 public: 00124 typedef X element_type; 00129 explicit AutoPtrVec(X* p = 0); 00130 AutoPtrVec& operator= (X* p); 00131 ~AutoPtrVec(); 00132 X& operator*() const; 00133 X* operator->() const; 00134 X& operator[](unsigned i); 00135 const X& operator[](unsigned i) const; 00136 X* get() const; 00137 X* release(); 00138 void reset(X* p=0); 00139 }; 00140 00141 00142 template <class X> 00143 inline AutoPtrVec<X>::AutoPtrVec(X* p) : _ptr(p) {} 00144 00145 template <class X> 00146 AutoPtrVec<X>& AutoPtrVec<X>::operator= (X* p) 00147 { 00148 if (p != _ptr) 00149 { 00150 reset(); 00151 _ptr = p; 00152 } 00153 return *this; 00154 } 00155 00156 template <class X> 00157 AutoPtrVec<X>::~AutoPtrVec() 00158 { 00159 typedef char type_must_be_complete[sizeof(X)]; 00160 delete [] _ptr; 00161 } 00162 00163 template <class X> 00164 X& AutoPtrVec<X>::operator*() const { return *_ptr;} 00165 00166 template <class X> 00167 X* AutoPtrVec<X>::operator->() const { return _ptr;} 00168 00169 template <class X> 00170 X& AutoPtrVec<X>::operator[](unsigned i) { return _ptr[i]; } 00171 00172 template <class X> 00173 const X& AutoPtrVec<X>::operator[](unsigned i) const { return _ptr[i]; } 00174 00175 template <class X> 00176 X* AutoPtrVec<X>::get() const { return _ptr;} 00177 00178 template <class X> 00179 X* AutoPtrVec<X>::release() 00180 { 00181 X* rval = _ptr; 00182 _ptr = 0; 00183 return rval; 00184 } 00185 00186 template <class X> 00187 void AutoPtrVec<X>::reset(X* p) 00188 { 00189 delete [] _ptr; 00190 _ptr = p; 00191 } 00192 00193 } // end namespace OWBI1 00194 00195 #endif