home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / listpm7.zip / winthr.hpp < prev   
C/C++ Source or Header  |  1999-06-12  |  5KB  |  169 lines

  1. /*
  2.     listPM list files under Presentation Manager. Uses Open Class Libarary.
  3.     Copyright (C) 1998, 1999  Paul Elliott
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Paul Elliott
  20.     PMB # 181
  21.     11900 Metric Blvd Ste. J
  22.     Austin Tx 78758-3117
  23.     pelliott@io.com
  24. */
  25. #ifndef WINTHR
  26. #define WINTHR
  27.  
  28. #include <iwindow.hpp>
  29. #include <icmdhdr.hpp>
  30. #include <ithread.hpp>
  31.  
  32. /*
  33.  *  This class is used to run code on another thread
  34.  * from a windows procedure.
  35.  * To use:
  36.  * 1) derive from this class defining
  37.  *    a) thread() code to run on other threads. Required as this is
  38.  *        an abstract base class
  39.  *    b) begin() optional code to run on this thread before thread is started.
  40.  *    c) finish() optional code to run on this thread after thread runs1
  41.  *    if desired private data can be added to this derived class.
  42.  * 2) construct specify a IWindow and a long denoting a command id.
  43.  * the window must be serviced by the current thread.
  44.  * the command id is to notified the window/current thread of completion.
  45.  * 3)Then from a handler call back call win_th_start.
  46.  * to start anonther thread running.
  47.  */
  48. class WindowThread
  49. // handler to detect completion.
  50. {
  51.    private:
  52.  
  53.  
  54.  
  55.       // event id for completion notification.
  56.       unsigned long eventid;
  57.  
  58.       // window to use/notify
  59.       IWindow& window;
  60.  
  61.       // notes that we are notifying orig thread thru a post
  62.       // event. Event will be ignored otherwise.
  63.  
  64.       // access to this variable must be protected by crit section.
  65.       Boolean notifing;
  66.  
  67.       // we could derive privately from ICcommandHandler
  68.       // but then the user of this class can not derive from
  69.       // this class and ICommandHandler without conflict.
  70.  
  71.       // this is the command handler used to be called back
  72.       // on completion.
  73.       class ICom : public ICommandHandler
  74.       {
  75.         private:
  76.  
  77.           // remember our parent' class for call back.
  78.           WindowThread& win_thread;
  79.  
  80.  
  81.           // disabled, not defined.
  82.           ICom( const ICom&);
  83.           ICom& operator=( const ICom& );
  84.         public:
  85.  
  86.           // conruct from window thread
  87.           ICom( WindowThread& win_thread ) : win_thread(win_thread)
  88.           {
  89.             // handle events for our window.
  90.             handleEventsFor( &win_thread.window );
  91.           };
  92.  
  93.         protected:
  94.  
  95.  
  96.           // command handler detects notification.
  97.           virtual Boolean command( ICommandEvent& event );
  98.       } thread_hand;
  99.  
  100.       // cooperate with our nested command handler class.
  101.       friend class ICom;
  102.  
  103.          // disable default constructor, assignment operator, not defined
  104.          WindowTheread( const WindowThread& );
  105.          WindowThread& operator=(const WindowThread&);
  106.  
  107.  
  108.       // nested thread class to run thread code.
  109.       class WinThrFn : public IThreadFn
  110.       {
  111.          private:
  112.            // disable default constructor, assignment operator
  113.            WinThrFn();
  114.            WinThrFn& operator=(const WinThrFn&);
  115.  
  116.            // save the window thread in use.
  117.            WindowThread & wt;
  118.          public:
  119.            WinThrFn(WindowThread& wt) : wt(wt) {};
  120.  
  121.            // virtual destructor.
  122.            virtual ~WinThrFn() {};
  123.  
  124.            // call the run call.
  125.            virtual void run ();
  126.  
  127.       };
  128.  
  129.       // make the run code a friend.
  130.       friend void WinThrFn::run();
  131.  
  132.    public:
  133.  
  134.       // constructor remembers IWindow and eveint id #
  135.       WindowThread(IWindow& window,const long eventid);
  136.  
  137.       // virtual destructor.
  138.       virtual ~WindowThread() {};
  139.  
  140.    public:
  141.  
  142.        // called by user to start the thread.
  143.        virtual void win_th_start( Boolean autoInitGUI =
  144.                                      IThread::defaultAutoInitGUI() );
  145.  
  146.    protected:
  147.  
  148.        // optional call back to user before running thread.
  149.        virtual void begin() {} ;
  150.  
  151.        // code to run on other thread.
  152.  
  153.        // must be overridden.
  154.        // this is thread code tor run.
  155.        virtual void thread() = 0;
  156.  
  157.        // optional call back to user after running thread.
  158.        virtual void finish() {} ;
  159.  
  160.        // runs thread internal, but overridable.
  161.        virtual void run();
  162.  
  163.        // remember window we are on.
  164.        IWindow& OurWindow() { return window; };
  165.  
  166. };
  167.  
  168. #endif  //WINTHR
  169.