Main Page   Modules   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.3 2003/08/15 00:43:58 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 
00051 class Referenced: public RWMutex
00052 {
00053 
00054 private:
00056    int m_count;
00058    bool m_disposeable;
00059 
00060 protected:
00075    Referenced( int count = 1, bool dispose = true ):
00076       RWMutex()
00077    {
00078       #ifndef NO_ASSERTIONS
00079       assert ( count > 0 );
00080       #endif
00081       m_count = count > 1 ? count : 1;
00082       m_disposeable = dispose;
00083    }
00084 
00086    virtual ~Referenced() {}
00087 
00088 public:
00089 
00094    void incRef() {
00095       lockWrite();
00096          if (m_count > 0 ) // if m_count == 0 we are going to be destroyed now.
00097             m_count++;
00098       unlock();
00099    }
00100 
00106    void decRef() {
00107       bool destroy = false;
00108       lockWrite();
00109       if ( m_disposeable && m_count > 0 ) m_count--;
00110       if ( m_count <= 0 ) destroy = true;
00111       unlock();
00112       // now, an incref could arrive here, but it would be an error,
00113       // a race caused by misusing this object.
00114       // An assert here should do, but I don't know if asserting in this
00115       // lib would be appreciated by users; anyhow, the user will get a
00116       // segfault after incref.
00117       if ( destroy ) delete this;
00118    }
00119 
00148    int references() { return m_count; }
00149 
00154    void incRefUnlocked() { m_count++; }
00155 
00163    void setRefUnlocked( int count )
00164    {
00165       #ifndef NO_ASSERTIONS
00166       assert( count > 0 );
00167       #endif
00168       m_count = count;
00169    }
00170 
00171 };
00172 
00173 }
00174 
00175 #endif
00176 /* end of wefts_referenced.h */

Generated on Mon Aug 18 05:53:37 2003 for Wefts by doxygen1.2.18