RulbusDeviceProxy.h

00001 /*
00002  * RulbusDeviceProxy.h - class RulbusDeviceProxy.
00003  *
00004  * This file is part of the Rulbus Device Class Library (RDCL).
00005  *
00006  * Copyright (C) 2003-2004, Leiden University.
00007  *
00008  * This library is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * The library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with mngdriver; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * $Id: RulbusDeviceProxy.h 2 2004-10-12 11:54:15Z moene $
00023  */
00024 
00025 #ifndef __RULBUSDEVICEPROXY_H
00026 #define __RULBUSDEVICEPROXY_H
00027 
00028 #ifndef __COMMON_H
00029    #include "Common.h"
00030 #endif
00031 
00032 #include "winutils/semaphore.h" // for WindowsUtils::Semaphore
00033 #include <windows.h>            // for InterlockedIncrement()
00034 
00035 namespace WindowsUtils
00036 {
00037    DECLARE_CLASS( Semaphore );
00038 }
00039 
00040 namespace Rulbus
00041 {
00042    DECLARE_CLASS( RulbusDevice      );
00043    DECLARE_CLASS( RulbusDeviceProxy );
00044 
00068    class RulbusDeviceProxy
00069    {
00070    public:
00071       RulbusDeviceProxy  ( RulbusDevicePtr aPtr     );
00072       RulbusDeviceProxy  ( RulbusDeviceProxyRef rhs );
00073       ~RulbusDeviceProxy ( );
00074 
00075       RulbusDeviceProxyRef operator= ( RulbusDeviceProxyRef rhs );
00076 
00077                            operator RulbusDevicePtr ( );
00078                            operator RulbusDeviceRef ( );
00079 
00080       RulbusDevicePtr      operator->( );
00081       RulbusDeviceRef      operator* ( );
00082 
00083       RulbusDeviceRef      get( );
00084 
00085    protected:
00086       RulbusDevicePtr      transfer();
00087 
00088    protected:
00089       RulbusDevicePtr      thePtr;              
00090       static WindowsUtils::Semaphore s;         
00091    };
00092 
00097    inline RulbusDeviceProxy::RulbusDeviceProxy( RulbusDevicePtr aPtr ) :
00098       thePtr( aPtr )
00099    {
00100       s.wait();
00101    }
00102 
00107    inline RulbusDeviceProxy::RulbusDeviceProxy( RulbusDeviceProxyRef rhs ) :
00108       thePtr( rhs.transfer() )
00109    {
00110       ; // do nothing
00111    }
00112 
00117    inline RulbusDeviceProxy::~RulbusDeviceProxy( )
00118    {
00119       if ( thePtr )
00120       {
00121          s.post();                              // critical section end
00122          thePtr = 0;
00123       }
00124       else
00125       {
00126          ; // do nothing
00127       }
00128    }
00129 
00134    RulbusDeviceProxyRef RulbusDeviceProxy::operator=( RulbusDeviceProxyRef rhs )
00135    {
00136       thePtr = rhs.transfer();
00137 
00138       return *this;
00139    }
00140 
00145    inline RulbusDeviceProxy::operator RulbusDevicePtr( )
00146    {
00147       return &get();
00148    }
00149 
00154    inline RulbusDeviceProxy::operator RulbusDeviceRef( )
00155    {
00156       return get();
00157    }
00158 
00163    inline RulbusDevicePtr RulbusDeviceProxy::operator->( )
00164    {
00165       return &get();
00166    }
00167 
00172    inline RulbusDeviceRef RulbusDeviceProxy::operator*( )
00173    {
00174       return get();
00175    }
00176 
00181    RulbusDeviceRef RulbusDeviceProxy::get()
00182    {
00183       return *thePtr;
00184    }
00185 
00190    RulbusDevicePtr RulbusDeviceProxy::transfer()
00191    {
00192       RulbusDevicePtr tmpPtr = thePtr;
00193       thePtr                 = 0;
00194 
00195       return tmpPtr;
00196    }
00197 
00202    template <typename T>
00203    inline T to( RulbusDeviceProxyRef proxy )
00204    {
00205       return PROXY_CAST( T, static_cast<RulbusDevicePtr>( proxy ) );
00206    }
00207 
00208 }       // namespace Rulbus
00209 
00210 #endif  // __RULBUSDEVICEPROXY_H
00211 
00212 /*
00213  * end of file
00214  */

Generated on Tue Oct 12 14:11:58 2004 for Rulbus Device Class Library for Microsoft Windows by doxygen 1.3.4