MethodProxy.hpp

00001 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00002 //
00003 //        This file is part of E-Cell Simulation Environment package
00004 //
00005 //                Copyright (C) 2004 Keio University
00006 //
00007 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //
00009 //
00010 // E-Cell is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 //
00015 // E-Cell is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 // See the GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public
00021 // License along with E-Cell -- see the file COPYING.
00022 // If not, write to the Free Software Foundation, Inc.,
00023 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 //
00025 //END_HEADER
00026 // 
00027 // written by Koichi Takahashi <shafi@e-cell.org>,
00028 // E-Cell Project.
00029 //
00030 
00031 #ifndef __METHODPROXY_HPP
00032 #define __METHODPROXY_HPP
00033 
00034 #include "libecs.hpp"
00035 
00036 
00037 template < class CLASS, typename RET, typename ARG1 = void > 
00038 class MethodProxy;
00039 
00040 template < class CLASS, typename RET >
00041 class MethodProxy<CLASS,RET>
00042 {
00043 private:
00044 
00045   typedef const RET (* Invoker )( CLASS* const );
00046 
00047 public:
00048 
00049   inline const RET operator()( CLASS* const anObject ) const 
00050   { 
00051     return theInvoker( anObject ); 
00052   }
00053 
00054   inline const RET operator()( const CLASS* const anObject ) const 
00055   { 
00056     return theInvoker( const_cast<CLASS* const>( anObject ) ); 
00057   }
00058 
00059   template < const RET (CLASS::*METHOD)() const >
00060   static MethodProxy create()
00061   {
00062 #if defined( LIBECS_USE_PMF_CONVERSIONS )
00063     return MethodProxy( Invoker( METHOD ));
00064 #else  /* defined( LIBECS_USE_PMF_CONVERSIONS ) */
00065     return MethodProxy( invoke<METHOD> );
00066 #endif /* defined( LIBECS_USE_PMF_CONVERSIONS ) */
00067   }
00068 
00069 private:
00070 
00071   MethodProxy()
00072     : 
00073     theInvoker( 0 )
00074   {
00075     ; // do nothing
00076   }
00077     
00078   MethodProxy( Invoker anInvoker ) 
00079     : 
00080     theInvoker( anInvoker )
00081   {
00082     ; // do nothing
00083   }
00084 
00085   template < const RET (CLASS::*METHOD)() const >
00086   inline static const RET invoke( CLASS* const anObject )
00087   {
00088     return ( anObject->*METHOD )();
00089   }
00090 
00091 private:
00092     
00093   Invoker   theInvoker;
00094 
00095 };
00096 
00097 
00098 
00099 template < typename RET, typename ARG1 = void > 
00100 class ObjectMethodProxy;
00101 
00102 template < typename RET >
00103 class ObjectMethodProxy<RET>
00104 {
00105 private:
00106 
00107   typedef const RET (* Invoker )( void* const );
00108 
00109 public:
00110 
00111   inline const RET operator()() const 
00112   { 
00113     return theInvoker( theObject ); 
00114   }
00115 
00116   template < class T, const RET (T::*TMethod)() const >
00117   static ObjectMethodProxy create( T* const anObject )
00118   {
00119 #if defined( LIBECS_USE_PMF_CONVERSIONS )
00120     return ObjectMethodProxy( Invoker( TMethod ), anObject );
00121 #else  /* defined( LIBECS_USE_PMF_CONVERSIONS ) */
00122     return ObjectMethodProxy( invoke<T,TMethod>, anObject );
00123 #endif /* defined( LIBECS_USE_PMF_CONVERSIONS ) */
00124   }
00125 
00126 private:
00127 
00128   ObjectMethodProxy()
00129     : 
00130     theInvoker( 0 ),
00131     theObject( 0 )
00132   {
00133     ; // do nothing
00134   }
00135     
00136   ObjectMethodProxy( Invoker anInvoker, void* anObject ) 
00137     : 
00138     theInvoker( anInvoker ),
00139     theObject( anObject )
00140   {
00141     ; // do nothing
00142   }
00143 
00144   template < class T, const RET (T::*TMethod)() const >
00145   inline static const RET invoke( void* const anObject )
00146   {
00147     return ( static_cast<T*>(anObject)->*TMethod )();
00148   }
00149 
00150 private:
00151     
00152   Invoker   theInvoker;
00153   void*     theObject;
00154 
00155 };
00156 
00157 
00158 
00159 
00160 #endif /* __METHODPROXY_HPP */

Generated on Fri Aug 31 18:42:39 2007 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.5.3