home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / runnable / ibmc / ibmclass / icallbk.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-26  |  12.7 KB  |  308 lines

  1. #ifndef _ICALLBK_
  2. #define _ICALLBK_
  3. /*******************************************************************************
  4. * FILE NAME: icallbk.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     I0ArgCallback         - 0 argument "callback object"                     *
  9. *     I0ArgCallbackMemberFn - "callback object" wrapper for a C++ member       *
  10. *                             callback accepting no arguments                  *
  11. *     I0ArgCallbackFn       - "callback object" wrapper for a plain C or C++   *
  12. *                             callback accepting no arguments                  *
  13. *     I1ArgCallback         - 1 argument "callback object"                     *
  14. *     I1ArgCallbackMemberFn - "callback object" wrapper for a C++ member       *
  15. *                             callback accepting one argument                  *
  16. *     I1ArgCallbackFn       - "callback object" wrapper for a plain C or C++   *
  17. *                             callback accepting one argument                  *
  18. *     I2ArgCallback         - 2 argument "callback object"                     *
  19. *     I2ArgCallbackMemberFn - "callback object" wrapper for a C++ member       *
  20. *                             callback accepting two arguments                 *
  21. *     I2ArgCallbackFn       - "callback object" wrapper for a plain C or C++   *
  22. *                             callback accepting two arguments                 *
  23. *                                                                              *
  24. * COPYRIGHT:                                                                   *
  25. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  26. *   All Rights Reserved                                                        *
  27. *   Licensed Materials - Property of IBM                                       *
  28. *                                                                              *
  29. *   US Government Users Restricted Rights -                                    *
  30. *   Use, duplication, or disclosure restricted                                 *
  31. *   by GSA ADP Schedule Contract with IBM Corp.                                *
  32. *                                                                              *
  33. *$Log:   R:/IBMCLASS/IBASE/VCS/ICALLBK.HPV  $                                                                         *
  34. //
  35. //   Rev 1.2   25 Oct 1992 16:46:22   nunn
  36. //changed library name to ICLUI
  37. //
  38. //   Rev 1.1   19 Oct 1992 16:08:42   law
  39. //Made callbacks reference counted.
  40. *******************************************************************************/
  41. #ifndef _IRECNT_
  42.   #include <irefcnt.hpp>
  43. #endif
  44.  
  45. class ICallbackBase : public IRefCounted {
  46. /*******************************************************************************
  47. * This class serves as the base for all the real callback classes.  It         *
  48. * inherits from IRefCounted in order to permit objects that use them to        *
  49. * participate in the control of the destruction of such objects.  That is,     *
  50. * the destruction of the callbacks must be deferred until the objects that     *
  51. * might call them no longer will do so.                                        *
  52. *                                                                              *
  53. * In addition, this class is used as a convenient place to declare some        *
  54. * enumeration values used throughout the callback hierarchy.                   *
  55. *******************************************************************************/
  56. protected:
  57. typedef enum
  58.   {
  59.   optlink,
  60.   system
  61.   } LinkageType;
  62. };
  63.  
  64. template < class Result >
  65. class I0ArgCallback : public ICallbackBase {
  66. /*******************************************************************************
  67. * This template class is used to define abstract callbacks that accept zero    *
  68. * arguments and have return type of Result.  Objects of such classes are used  *
  69. * as arguments to various member functions in the ICLUI library that        *
  70. * require "callbacks", or, that permit the application of user-provided code.  *
  71. *                                                                              *
  72. * This template class requires a single template argument:                     *
  73. *   Result - return type of the "callback object"                              *
  74. *                                                                              *
  75. * This template generates abstract superclasses.  Two concrete subclass        *
  76. * implementations are defined below:                                           *
  77. *   I0ArgCallbackMemberFn - wrapper for a C++ member callback of this "type"   *
  78. *   I0ArgCallbackFn       - wrapper for a plain C or C++ function of           *
  79. *                           this "type"                                        *
  80. *                                                                              *
  81. * Each of these classes implements the pure virtual callback f() as            *
  82. * appropriate (by invocation of the specified callback or member function).    *
  83. *                                                                              *
  84. * This class has no other functions.                                           *
  85. *******************************************************************************/
  86. public:
  87. virtual Result
  88.   f ( ) const = 0;
  89. }; // I0ArgCallback
  90.  
  91. template < class Result >
  92. class I0ArgCallbackFn : public I0ArgCallback<Result> {
  93. public:
  94. virtual Result
  95.   f ( ) const
  96.     {
  97.     Result result;
  98.     switch ( this->linkage )
  99.       {
  100.       case optlink;
  101.         result = pOpt();
  102.         break;
  103.       case system:
  104.         result = pSys();
  105.         break;
  106.       }
  107.     return result;
  108.     }
  109.   I0ArgCallbackFn ( Result (* _Optlink p)() )
  110.     : linkage( optlink ), pOpt( p )
  111.     {
  112.     }
  113.   I0ArgCallbackFn ( Result (* _System p)() )
  114.     : linkage( system ), pSys( p )
  115.     {
  116.     }
  117. union
  118.   {
  119.   Result (* _Optlink pOpt)();
  120.   Result (* _System  pSys)();
  121.   };
  122. LinkageType
  123.   linkage;
  124. }; // I0ArgCallbackFn
  125.  
  126. template < class Class, class Result >
  127. class I0ArgCallbackMemberFn : public I0ArgCallback<Result> {
  128. public:
  129. virtual Result
  130.   f ( ) const
  131.     {
  132.     return (object.*member)();
  133.     }
  134.   I0ArgCallbackMemberFn ( Class &anObject, Result (Class::*p)() )
  135.     : object( anObject ), member( p )
  136.     {
  137.     }
  138. Class
  139.  &object;
  140. Result (Class::*member)();
  141. }; // I0ArgCallbackMemberFn
  142.  
  143. template < class Result, class Arg >
  144. class I1ArgCallback : public ICallbackBase {
  145. /*******************************************************************************
  146. * This template class is used to define abstract callbacks that accept one     *
  147. * argument and have return type of Result.  Objects of such classes are used   *
  148. * as arguments to various member functions in the ICLUI library that        *
  149. * require "callbacks", or, that permit the application of user-provided code.  *
  150. *                                                                              *
  151. * This template class requires two template arguments:                         *
  152. *   Result - return type of the "callback object"                              *
  153. *   Arg    - argument type                                                     *
  154. *                                                                              *
  155. * This template generates abstract superclasses.  Two concrete subclass        *
  156. * implementations are defined below:                                           *
  157. *   I1ArgCallbackMemberFn - wrapper for a C++ member callback of this "type"   *
  158. *   I1ArgCallbackFn       - wrapper for a plain C or C++ function of           *
  159. *                           this "type"                                        *
  160. *                                                                              *
  161. * Each of these classes implements the pure virtual callback f() as            *
  162. * appropriate (by invocation of the specified callback or member function).    *
  163. *                                                                              *
  164. * This class has no other functions.                                           *
  165. *******************************************************************************/
  166. public:
  167. virtual Result
  168.   f ( Arg anArg ) const = 0;
  169. }; // I1ArgCallback
  170.  
  171. template < class Result, class Arg >
  172. class I1ArgCallbackFn : public I1ArgCallback<Result,Arg> {
  173. public:
  174. virtual Result
  175.   f ( Arg anArg ) const
  176.     {
  177.     Result result;
  178.     switch ( this->linkage )
  179.       {
  180.       case optlink;
  181.         result = pOpt( anArg );
  182.         break;
  183.       case system:
  184.         result = pSys( anArg );
  185.         break;
  186.       }
  187.     return result;
  188.     }
  189.   I1ArgCallbackFn ( Result (* _Optlink p)(Arg) )
  190.     : linkage( optlink ), pOpt( p )
  191.     {
  192.     }
  193.   I1ArgCallbackFn ( Result (* _System p)(Arg) )
  194.     : linkage( system ), pSys( p )
  195.     {
  196.     }
  197. union
  198.   {
  199.   Result (* _Optlink pOpt)(Arg);
  200.   Result (* _System  pSys)(Arg);
  201.   };
  202. LinkageType
  203.   linkage;
  204. }; // I1ArgCallbackFn
  205.  
  206. template < class Class, class Result, class Arg >
  207. class I1ArgCallbackMemberFn : public I1ArgCallback<Result,Arg> {
  208. public:
  209. virtual Result
  210.   f ( Arg anArg ) const
  211.     {
  212.     return (object.*member)( anArg );
  213.     }
  214.   I1ArgCallbackMemberFn ( Class &anObject, Result (Class::*p)(Arg) )
  215.     : object( anObject ), member( p )
  216.     {
  217.     }
  218. Class
  219.  &object;
  220. Result (Class::*member)(Arg);
  221. }; // I1ArgCallbackMemberFn
  222.  
  223. template < class Result, class Arg1, class Arg2 >
  224. class I2ArgCallback : public ICallbackBase {
  225. /*******************************************************************************
  226. * This template class is used to define abstract callbacks that accept two     *
  227. * arguments and have return type of Result.  Objects of such classes are used  *
  228. * as arguments to various member functions in the ICLUI library that        *
  229. * require "callbacks", or, that permit the application of user-provided code.  *
  230. *                                                                              *
  231. * This template class requires two template arguments:                         *
  232. *   Result - return type of the "callback object"                              *
  233. *   Arg1   - argument 1 type                                                   *
  234. *   Arg2   - argument 2 type                                                   *
  235. *                                                                              *
  236. * This template generates abstract superclasses.  Two concrete subclass        *
  237. * implementations are defined below:                                           *
  238. *   I2ArgCallbackMemberFn - wrapper for a C++ member callback of this "type"   *
  239. *   I2ArgCallbackFn       - wrapper for a plain C or C++ function of           *
  240. *                           this "type"                                        *
  241. *                                                                              *
  242. * Each of these classes implements the pure virtual callback f() as            *
  243. * appropriate (by invocation of the specified callback or member function).    *
  244. *                                                                              *
  245. * This class has no other functions.                                           *
  246. *******************************************************************************/
  247. public:
  248. virtual Result
  249.   f ( Arg1 arg1, 
  250.       Arg2 arg2 ) const = 0;
  251. }; // I2ArgCallback
  252.  
  253. template < class Result, class Arg1, class Arg2 >
  254. class I2ArgCallbackFn : public I2ArgCallback<Result,Arg1,Arg2> {
  255. public:
  256. virtual Result
  257.   f ( Arg1 arg1,
  258.       Arg2 arg2 ) const
  259.     {
  260.     Result result;
  261.     switch ( this->linkage )
  262.       {
  263.       case optlink;
  264.         result = pOpt( arg1, arg2 );
  265.         break;
  266.       case system:
  267.         result = pSys( arg1, arg2 );
  268.         break;
  269.       }
  270.     return result;
  271.     }
  272.   I2ArgCallbackFn ( Result (* _Optlink p)(Arg1,Arg2) )
  273.     : linkage( optlink ), pOpt( p )
  274.     {
  275.     }
  276.   I2ArgCallbackFn ( Result (* _System p)(Arg1,Arg2) )
  277.     : linkage( system ), pSys( p )
  278.     {
  279.     }
  280. union
  281.   {
  282.   Result (* _Optlink pOpt)(Arg1,Arg2);
  283.   Result (* _System  pSys)(Arg1,Arg2);
  284.   };
  285. LinkageType
  286.   linkage;
  287. }; // I2ArgCallbackFn
  288.  
  289. template < class Class, class Result, class Arg1, class Arg2 >
  290. class I2ArgCallbackMemberFn : public I2ArgCallback<Result,Arg1,Arg2> {
  291. public:
  292. virtual Result
  293.   f ( Arg1 arg1,
  294.       Arg2 arg2 ) const
  295.     {
  296.     return (object.*member)( arg1, arg2 );
  297.     }
  298.   I2ArgCallbackMemberFn ( Class &anObject, Result (Class::*p)(Arg1,Arg2) )
  299.     : object( anObject ), member( p )
  300.     {
  301.     }
  302. Class
  303.  &object;
  304. Result (Class::*member)(Arg1,Arg2);
  305. }; // I2ArgCallbackMemberFn
  306.  
  307. #endif /* _ICALLBK_ */
  308.