Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

wefts_referenced.h

Go to the documentation of this file.
00001 /*
00002    wefts_referenced.h
00003    Reference counter base class.
00004 
00005    $Id: wefts_referenced.h,v 1.2 2003/08/05 15:40:20 jonnymind Exp $
00006 ---------------------------------------------
00007    Begin      : 2003-08-04 20:15
00008    Author     : Giancarlo Niccolai
00009 
00010    Last modified because:
00011 
00012 */
00013 
00014 /**************************************************************************
00015 *   This program is free software; you can redistribute it and/or modify  *
00016 *   it under the terms of the GNU Library General Public License as       *
00017 *   published by the Free Software Foundation; either version 2.1 of the  *
00018 *   License, or (at your option) any later version.                       *
00019 ***************************************************************************/
00020 
00021 #ifndef WT_REFERENCED_H
00022 #define WT_REFERENCED_H
00023 
00024 #include <wefts_rwmutex.h>
00025 #ifndef NO_ASSERTIONS
00026 #include <cassert>
00027 #endif
00028 
00029 
00030 namespace Wefts {
00031 
00048 class Referenced: public RWMutex
00049 {
00050 
00051 private:
00053    int m_count;
00055    bool m_disposeable;
00056 
00057 protected:
00072    Referenced( int count = 1, bool dispose = true ):
00073       RWMutex()
00074    {
00075       #ifndef NO_ASSERTIONS
00076       assert ( count > 0 );
00077       #endif
00078       m_count = count > 1 ? count : 1;
00079       m_disposeable = dispose;
00080    }
00081 
00083    virtual ~Referenced() {}
00084 
00085 public:
00086 
00091    void incRef() {
00092       lockWrite();
00093          if (m_count > 0 ) // if m_count == 0 we are going to be destroyed now.
00094             m_count++;
00095       unlock();
00096    }
00097 
00103    void decRef() {
00104       bool destroy = false;
00105       lockWrite();
00106       if ( m_disposeable && m_count > 0 ) m_count--;
00107       if ( m_count <= 0 ) destroy = true;
00108       unlock();
00109       // now, an incref could arrive here, but it would be an error,
00110       // a race caused by misusing this object.
00111       // An assert here should do, but I don't know if asserting in this
00112       // lib would be appreciated by users; anyhow, the user will get a
00113       // segfault after incref.
00114       if ( destroy ) delete this;
00115    }
00116 
00145    int references() { return m_count; }
00146 
00151    void incRefUnlocked() { m_count++; }
00152 
00160    void setRefUnlocked( int count )
00161    {
00162       #ifndef NO_ASSERTIONS
00163       assert( count > 0 );
00164       #endif
00165       m_count = count;
00166    }
00167 
00168 };
00169 
00170 }
00171 
00172 #endif
00173 /* end of wefts_referenced.h */

Generated on Tue Aug 5 18:09:00 2003 for Wefts by doxygen1.2.18