blocxx

AutoPtr.hpp

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