home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / improved / improved.hpp < prev    next >
C/C++ Source or Header  |  1996-10-29  |  4KB  |  105 lines

  1. #ifndef _IMPROVED_
  2. #define _IMPROVED_
  3. /***************************************************************
  4. * FILE NAME: improved.hpp                                      *
  5. *                                                              *
  6. * DESCRIPTION:                                                 *
  7. *   This file contains the declaration(s) of the class(es):    *
  8. *     ImprovedThreadFn - Improved version of IThreadFn         *
  9. *     ImprovedThread   - Improved thread class that knows how  *
  10. *                        to start ImprovedThreadFn objects     *
  11. *                                                              *
  12. * COPYRIGHT:                                                   *
  13. *   Licensed Materials - Property of Solution Frameworks       *
  14. *   Copyright (C) 1996, Solution Frameworks                    *
  15. *   All Rights Reserved                                        *
  16. ***************************************************************/
  17. #include <ithread.hpp>
  18.  
  19. /*--------------------- ImprovedThreadFn -----------------------
  20. | The trick is that objects of this class can make copies      |                
  21. | of themselves.  The improved thread class uses copies        |                
  22. | rather than muck with reference counting the original.       |                
  23. | We use private inheritance to ensure that these objects      |                
  24. | don't get used accidentally with plain IThreads.             |                
  25. --------------------------------------------------------------*/
  26. class ImprovedThreadFn : private IThreadFn {
  27. public:
  28. // Default constructor.
  29.   ImprovedThreadFn ( ) {
  30.   }
  31. // Pure virtual function to self-replicate.
  32. virtual ImprovedThreadFn
  33.  *copy ( ) const = 0;
  34. // Promote inherited use count value.
  35. public: IThreadFn::useCount;
  36. // Let ImprovedThread use objects as IThreadFns.
  37. friend class ImprovedThread;
  38. private:
  39. // Prohibit copies via other means.
  40. ImprovedThreadFn( const ImprovedThreadFn& );
  41. operator = ( const ImprovedThreadFn& );
  42. };
  43.  
  44. /*-------------------- ThreadableMemberOf ----------------------
  45. | This class template is used to generate "improved" function  |
  46. | objects that can be used to start member functions on        |
  47. | threads.  You use it just like IThreadMemberFn.              |
  48. --------------------------------------------------------------*/
  49. template < class T >
  50. class ThreadableMemberOf : public ImprovedThreadFn {
  51. public:
  52.   ThreadableMemberOf ( T &object, void (T::*member)() )
  53.     : obj( object ), mem( member )
  54.     {
  55.     }
  56. virtual ThreadableMemberOf<T>
  57.  *copy ( ) const
  58.     {
  59.     return new ThreadableMemberOf<T>( obj, mem );
  60.     }
  61. virtual void
  62.   run ( )
  63.     {
  64.     (obj.*mem)();
  65.     }
  66. private:
  67. T
  68.  &obj;
  69. typedef void (T::*ptrToMember)();
  70. ptrToMember
  71.   mem;
  72. ThreadableMemberOf ( const ThreadableMemberOf<T> & );
  73. operator = ( const ThreadableMemberOf<T> & );
  74. };
  75.  
  76. /*------------------------ ImprovedThread ----------------------
  77. | Improved thread class that handles reference-counted         |
  78. | IThreadFn objects more elegantly.                            |
  79. --------------------------------------------------------------*/
  80. class ImprovedThread : public IThread {
  81. public:
  82.   ImprovedThread ( const ImprovedThreadFn *fnObject )
  83.     : IThread( IReference<IThreadFn>( fnObject->copy() ) )
  84.     {
  85.     }
  86.   ImprovedThread ( const ImprovedThreadFn &fnObject )
  87.     : IThread( IReference<IThreadFn>( fnObject.copy() ) )
  88.     {
  89.     }
  90. };
  91.  
  92. /*--------------------------- threadFn -------------------------
  93. | Template function that serves as an IThreadMemberFn          |
  94. | generator.                                                   |
  95. |                                                              |
  96. | Use this function in place of the more verbose               |
  97. | "new IThreadMemberFn<YourClass>( object, YourClass::foo )."  |
  98. --------------------------------------------------------------*/
  99. template < class T >
  100. IThreadMemberFn<T> *threadFn( T &obj, void (T::*mem)(void) ) {
  101.   return new IThreadMemberFn<T>( obj, mem );
  102. }
  103.  
  104. #endif // _IMPROVED_
  105.