home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / starting / wrapper.hpp < prev   
Encoding:
C/C++ Source or Header  |  1996-10-29  |  3.1 KB  |  58 lines

  1. #ifndef _WRAPPER_
  2. #define _WRAPPER_
  3. /***************************************************************
  4. * FILE NAME: wrapper.hpp                                       *
  5. *                                                              *
  6. * DESCRIPTION:                                                 *
  7. *   This file contains the declaration(s) of the class(es):    *
  8. *     WrapperFor - Template class to "wrapper" functions       *
  9. *                  compatible with Windows' CreateThread API.  *                               
  10. *                                                              *
  11. * COPYRIGHT:                                                   *
  12. *   Licensed Materials - Property of Solution Frameworks       *
  13. *   Copyright (C) 1996, Solution Frameworks                    *
  14. *   All Rights Reserved                                        *
  15. ***************************************************************/
  16.  
  17. /*------------------------ WrapperFor --------------------------
  18. | This is a class template that is used to generate            |
  19. | "wrappers" for functions compatible with Windows'            |
  20. | CreateThread API.                                            |
  21. |                                                              |
  22. | The template argument is the name of the function you        |
  23. | wish to wrapper.  Note that the template argument is         |
  24. | *not* a type name!                                           |
  25. |                                                              |
  26. | The class does two things:                                   |
  27. |   1 - Implements a static member function that calls         |
  28. |       the template argument.  This function has _Optlink     |
  29. |       linkage and is thus compatible with IThread::start.    |
  30. |   2 - Provides an operator that permits an object of this    |
  31. |       class to be converted to a function pointer            |
  32. |       compatible with IThread.  The returned function        |
  33. |       address is that of the static member function          |
  34. |       described above.  This operator permits an object of   |
  35. |       this class to be passed to IThread::start (or an       |
  36. |       IThread constructor).                                  |
  37. |                                                              |
  38. | You use this class template like so:                         |
  39. |                                                              |
  40. |    // Function we want to run on a thread:                   |
  41. |    unsigned long _stdcall winFunction ( void * );            |
  42. |                                                              |
  43. |    // How to use this class template to run it:              |
  44. |    IThread t( WrapperFor<winFunction>(), arg );              |
  45. --------------------------------------------------------------*/
  46. template < unsigned long (_stdcall *WinFunction)( void * ) >
  47. struct WrapperFor {
  48. static void
  49.   wrapper( void *p ) {
  50.     WinFunction( p );
  51.   }
  52.   operator IThread::OptlinkFnPtr () const {
  53.     return (IThread::OptlinkFnPtr)wrapper;
  54.   }
  55. }; // WrapperFor<>
  56.  
  57. #endif // _WRAPPER_
  58.