home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / weakreference.h < prev   
Encoding:
C/C++ Source or Header  |  2005-09-10  |  3.1 KB  |  132 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000-2002 Stefan Westerfeld
  4.                             stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. /*
  24.  * BC - Status (2002-03-08): WeakReference(Base)
  25.  *
  26.  * Has to be kept binary compatible by not touching it. Add a new class if
  27.  * you need something else.
  28.  */
  29.  
  30. #ifndef MCOP_WEAKREFERENCE_H
  31. #define MCOP_WEAKREFERENCE_H
  32.  
  33. #include "arts_export.h"
  34.  
  35. namespace Arts {
  36.  
  37. class ARTS_EXPORT WeakReferenceBase {
  38. public:
  39.     virtual void release() = 0;
  40. };
  41.  
  42. /**
  43.  * The WeakReference class can be used to safely store an object, without
  44.  * disallowing that it gets destroyed. A typical situation where you may want
  45.  * to use this is when you implement a datastructure, where A refers B, and
  46.  * B refers A. Using "normal" references, this structure would never get
  47.  * freed.
  48.  *
  49.  * Usage example:
  50.  *
  51.  *     Synth_WAVE_SIN sin;
  52.  *     WeakReference<Synth_PLAY_WAV> weak_sin;
  53.  *
  54.  *     {
  55.  *       Synth_WAVE_SIN test = weak_sin;
  56.  *         if(test.isNull())
  57.  *           printf("#1 missing object\n");
  58.  *         else
  59.  *           test.start();
  60.  *     }
  61.  *
  62.  *     // now, the strong reference leaves
  63.  *     sin = Synth_WAVE_SIN::null();
  64.  *
  65.  *     {
  66.  *       Synth_WAVE_SIN test = weak_sin;
  67.  *         if(test.isNull())
  68.  *         printf("#2 missing object\n");
  69.  *       else
  70.  *         test.stop();
  71.  *
  72.  * This would output "#2 missing object".
  73.  *
  74.  */
  75.  
  76. template<class SmartWrapper>
  77. class WeakReference : public WeakReferenceBase
  78. {
  79. private:
  80.     typename SmartWrapper::_base_class *content;
  81.  
  82. public:
  83.     WeakReference()
  84.     {
  85.         content = 0;
  86.     }
  87.     WeakReference(const WeakReference<SmartWrapper>& source)
  88.         :WeakReferenceBase(source), content(source.content)
  89.     {
  90.         if(content)
  91.             content->_addWeakReference(this);
  92.     }
  93.     WeakReference(SmartWrapper& source)
  94.     {
  95.         content = source._base();
  96.         if(content)
  97.             content->_addWeakReference(this);
  98.     }
  99.     inline WeakReference<SmartWrapper>& operator=(const
  100.                         WeakReference<SmartWrapper>& source)
  101.     {
  102.         release();
  103.         content = source.content;
  104.         if(content)
  105.             content->_addWeakReference(this);
  106.         return *this;
  107.     }
  108.     virtual ~WeakReference()
  109.     {
  110.         release();
  111.     }
  112.     void release()
  113.     {
  114.         if(content)
  115.         {
  116.             content->_removeWeakReference(this);
  117.             content = 0;
  118.         }
  119.     }
  120.     inline operator SmartWrapper() const
  121.     {
  122.         if(content)
  123.             return SmartWrapper::_from_base(content->_copy());
  124.         else
  125.             return SmartWrapper::null();
  126.     }
  127. };
  128.  
  129. }
  130.  
  131. #endif /* MCOP_WEAKREFERENCE_H */
  132.