ncutMemory.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Paul Guerrero   *
00003  *   paul@Glendin   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef NCUTMEMORY_H
00022 #define NCUTMEMORY_H 1
00023 
00024 #include <memory>
00025 
00033 template<typename _Tp1>
00034 struct auto_array_ref
00035 {
00036   _Tp1* _M_ptr;
00037 
00038   explicit
00039   auto_array_ref(_Tp1* __p): _M_ptr(__p) { }
00040 };
00041 
00061 template<typename _Tp>
00062 class auto_array
00063 {
00064   private:
00065 
00066     _Tp* _M_ptr;
00067 
00068   public:
00069 
00071     typedef _Tp element_type;
00072 
00079     explicit
00080     auto_array(element_type* __p = 0) throw() : _M_ptr(__p) { }
00081 
00089     auto_array(auto_array& __a) throw() : _M_ptr(__a.release()) { }
00090 
00100     template<typename _Tp1>
00101     auto_array(auto_array<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
00102 
00111     auto_array& operator=(auto_array& __a) throw()
00112     {
00113       reset(__a.release());
00114       return *this;
00115     }
00116 
00127     template<typename _Tp1>
00128     auto_array& operator=(auto_array<_Tp1>& __a) throw()
00129     {
00130       reset(__a.release());
00131       return *this;
00132     }
00133 
00144     ~auto_array() { delete[] _M_ptr; }
00145 
00154     element_type& operator[](unsigned int i) const throw() { return _M_ptr[i]; }
00155 
00164     element_type& operator*() const throw() { return *_M_ptr; }
00165 
00172     element_type* operator->() const throw() { return _M_ptr; }
00173 
00184     element_type* get() const throw() { return _M_ptr; }
00185 
00197     element_type* release() throw() {
00198       element_type* __tmp = _M_ptr;
00199       _M_ptr = 0;
00200       return __tmp;
00201     }
00202 
00210     void reset(element_type* __p = 0) throw() {
00211       if (__p != _M_ptr) {
00212         delete[] _M_ptr;
00213         _M_ptr = __p;
00214       }
00215     }
00216 
00228     auto_array(auto_array_ref<element_type> __ref) throw()
00229       : _M_ptr(__ref._M_ptr) { }
00230 
00231     auto_array& operator=(auto_array_ref<element_type> __ref) throw() {
00232       if (__ref._M_ptr != this->get()) {
00233         delete[] _M_ptr;
00234         _M_ptr = __ref._M_ptr;
00235       }
00236       return *this;
00237     }
00238 
00239     template<typename _Tp1>
00240     operator auto_array_ref<_Tp1>() throw()
00241     { return auto_array_ref<_Tp1>(this->release()); }
00242 
00243     template<typename _Tp1>
00244     operator auto_array<_Tp1>() throw()
00245     { return auto_array<_Tp1>(this->release()); }
00247 };
00248 
00256 template<typename _Tp1>
00257 struct greedy_ptr_ref
00258 {
00259   _Tp1* _M_ptr;
00260 
00261   explicit
00262   greedy_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
00263 };
00264 
00265 
00277 template<typename _Tp>
00278 class greedy_ptr
00279 {
00280   private:
00281     _Tp* _M_ptr;
00282     bool _owns;
00283 
00284   public:
00286     typedef _Tp element_type;
00287 
00294     explicit
00295     greedy_ptr(element_type* __p = 0) throw()
00296       : _M_ptr(__p), _owns((__p != 0) ? true:false)
00297     { }
00298 
00305     explicit
00306     greedy_ptr(element_type& __p) throw()
00307       : _M_ptr(&__p), _owns(false)
00308     { }
00309 
00317     greedy_ptr(const greedy_ptr& __a) throw()
00318       : _M_ptr(__a.get()), _owns(false)
00319     { }
00320 
00330     template<typename _Tp1>
00331     greedy_ptr(std::auto_ptr<_Tp1>& __a) throw()
00332       : _M_ptr(__a.release()), _owns(true)
00333     { }
00334 
00342     greedy_ptr(std::auto_ptr<element_type>& __a) throw()
00343       : _M_ptr(__a.release()), _owns(true)
00344     { }
00345 
00355     template<typename _Tp1>
00356     greedy_ptr(const greedy_ptr<_Tp1>& __a) throw()
00357       :_M_ptr(__a.get()), _owns(false)
00358     { }
00359 
00369     greedy_ptr&
00370     operator=(const greedy_ptr& __a) throw()
00371     {
00372       reset(__a.get());
00373       _owns = false;
00374       return *this;
00375     }
00376 
00388     template<typename _Tp1>
00389     greedy_ptr&
00390     operator=(const greedy_ptr<_Tp1>& __a) throw()
00391     {
00392       reset(__a.get());
00393       _owns = false;
00394       return *this;
00395     }
00396 
00403     greedy_ptr&
00404     operator=(element_type* __p) throw()
00405     {
00406       reset(__p);
00407       return *this;
00408     }
00409 
00416     greedy_ptr&
00417     operator=(element_type& __p) throw()
00418     {
00419       reset(__p);
00420       return *this;
00421     }
00422 
00432     ~greedy_ptr() { if (_owns) delete _M_ptr; }
00433 
00440     element_type&
00441     operator*() const throw() { return *_M_ptr; }
00442 
00449     element_type*
00450     operator->() const throw() { return _M_ptr; }
00451 
00462     element_type*
00463     get() const throw() { return _M_ptr; }
00464 
00473     bool
00474     owns() const throw() {return _owns;}
00475 
00487     element_type*
00488     release() throw()
00489     {
00490       _owns = false;
00491       return _M_ptr;
00492     }
00493 
00501     void
00502     reset(element_type* __p = 0) throw()
00503     {
00504       if (__p != _M_ptr) {
00505         if (_owns) delete _M_ptr;
00506         _M_ptr = __p;
00507         if (__p != 0) _owns = true;
00508         else _owns = false;
00509       }
00510     }
00511 
00519     void
00520     reset(element_type& __p = 0) throw()
00521     {
00522       if (&__p != _M_ptr) {
00523         if (_owns) delete _M_ptr;
00524         _M_ptr = &__p;
00525         _owns = false;
00526       }
00527     }
00528 
00540     greedy_ptr(greedy_ptr_ref<element_type> __ref) throw()
00541       : _M_ptr(__ref._M_ptr), _owns(false) { }
00542 
00543     greedy_ptr&
00544     operator=(greedy_ptr_ref<element_type> __ref) throw()
00545     {
00546       if (__ref._M_ptr != this->get()) {
00547         if (_owns) delete _M_ptr;
00548         _M_ptr = __ref._M_ptr;
00549         _owns = false;
00550       }
00551       return *this;
00552     }
00553 
00554     template<typename _Tp1>
00555     operator greedy_ptr_ref<_Tp1>() throw()
00556     { return greedy_ptr_ref<_Tp1>(this->get()); }
00557 
00558     template<typename _Tp1>
00559     operator greedy_ptr<_Tp1>() throw()
00560     { return greedy_ptr<_Tp1>(*this); }
00562 };
00563 
00564 #endif // NCUTMEMORY_H

Generated on Thu Jun 22 14:47:20 2006 for ncut.kdevelop by  doxygen 1.4.6