AutoPtr.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_AUTOPTR_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 
00042 namespace BLOCXX_NAMESPACE
00043 {
00044 
00060 // TODO: Rename this
00061 template <class X> class AutoPtr
00062 {
00063 private:
00064    X* _ptr;
00065    
00066    // no copying
00067    AutoPtr(const AutoPtr& a);
00068    AutoPtr& operator= (const AutoPtr& a);
00069 
00070 public:
00071    typedef X element_type;
00072 
00078    explicit AutoPtr(X* p = 0);
00079 
00083    ~AutoPtr();
00084 
00093    AutoPtr& operator= (X* p);
00094 
00099    X& operator*() const;
00100 
00104    X* operator->() const;
00105 
00109    X* get() const;
00110 
00116    X* release();
00117 
00124    void reset(X* p=0);
00125 };
00126 
00127 template <class X>
00128 inline AutoPtr<X>::AutoPtr(X* p) : _ptr(p) {}
00129 
00130 template <class X>
00131 inline AutoPtr<X>& AutoPtr<X>::operator= (X* p)
00132 {
00133    if (p != _ptr)
00134    {
00135       reset();
00136       _ptr = p;
00137    }
00138    return *this;
00139 }
00140 
00141 template <class X>
00142 inline AutoPtr<X>::~AutoPtr()
00143 {
00144    typedef char type_must_be_complete[sizeof(X)];
00145    delete _ptr;
00146 }
00147 
00148 template <class X>
00149 inline X& AutoPtr<X>::operator*() const {  return *_ptr;}
00150 
00151 template <class X>
00152 inline X* AutoPtr<X>::operator->() const {  return _ptr;}
00153 
00154 template <class X>
00155 inline X* AutoPtr<X>::get() const {  return _ptr;}
00156 
00157 template <class X>
00158 inline X* AutoPtr<X>::release()
00159 {
00160    X* rval = _ptr;
00161    _ptr = 0;
00162    return rval;
00163 }
00164 
00165 template <class X>
00166 inline void AutoPtr<X>::reset(X* p)
00167 {
00168    delete _ptr;
00169    _ptr = p;
00170 }
00171 
00183 template <class X> class AutoPtrVec
00184 {
00185 private:
00186    X* _ptr;
00187    
00188    // no copying
00189    AutoPtrVec(const AutoPtrVec& a);
00190    AutoPtrVec& operator= (const AutoPtrVec& a);
00191 
00192 public:
00193    typedef X element_type;
00194 
00200    explicit AutoPtrVec(X* p = 0);
00201 
00205    ~AutoPtrVec();
00206 
00215    AutoPtrVec& operator= (X* p);
00216 
00221    X& operator*() const;
00222 
00226    X* operator->() const;
00227 
00232    X& operator[](unsigned n);
00233 
00238    const X& operator[](unsigned i) const;
00239 
00243    X* get() const;
00244 
00250    X* release();
00251 
00258    void reset(X* p=0);
00259 };
00260 
00261 
00262 template <class X>
00263 inline AutoPtrVec<X>::AutoPtrVec(X* p) : _ptr(p) {}
00264 
00265 template <class X>
00266 AutoPtrVec<X>& AutoPtrVec<X>::operator= (X* p)
00267 {
00268    if (p != _ptr)
00269    {
00270       reset();
00271       _ptr = p;
00272    }
00273    return *this;
00274 }
00275 
00276 template <class X>
00277 AutoPtrVec<X>::~AutoPtrVec()
00278 {
00279    typedef char type_must_be_complete[sizeof(X)];
00280    delete [] _ptr;
00281 }
00282 
00283 template <class X>
00284 X& AutoPtrVec<X>::operator*() const {  return *_ptr;}
00285 
00286 template <class X>
00287 X* AutoPtrVec<X>::operator->() const {  return _ptr;}
00288 
00289 template <class X>
00290 X& AutoPtrVec<X>::operator[](unsigned i) { return _ptr[i]; }
00291 
00292 template <class X>
00293 const X& AutoPtrVec<X>::operator[](unsigned i) const { return _ptr[i]; }
00294 
00295 template <class X>
00296 X* AutoPtrVec<X>::get() const {  return _ptr;}
00297 
00298 template <class X>
00299 X* AutoPtrVec<X>::release()
00300 {
00301    X* rval = _ptr;
00302    _ptr = 0;
00303    return rval;
00304 }
00305 
00306 template <class X>
00307 void AutoPtrVec<X>::reset(X* p)
00308 {
00309    delete [] _ptr;
00310    _ptr = p;
00311 }
00312 
00313 } // end namespace BLOCXX_NAMESPACE
00314 
00315 #endif

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6