home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / listpm3.zip / source / winthr.hpp < prev   
Text File  |  1996-09-10  |  5KB  |  168 lines

  1. /*
  2.     listPM list files under Presentation Manager. Uses Open Class Libarary.
  3.     Copyright (C) 1996  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.     3987 South Gessner #224
  21.     Houston Tx 77063
  22.     Paul.Elliott@Hrnowl.LoneStar.Org
  23. */
  24. #ifndef WINTHR
  25. #define WINTHR
  26.  
  27. #include <iwindow.hpp>
  28. #include <icmdhdr.hpp>
  29. #include <ithread.hpp>
  30.  
  31. /*
  32.  *  This class is used to run code on another thread
  33.  * from a windows procedure.
  34.  * To use:
  35.  * 1) derive from this class defining
  36.  *    a) thread() code to run on other threads. Required as this is
  37.  *        an abstract base class
  38.  *    b) begin() optional code to run on this thread before thread is started.
  39.  *    c) finish() optional code to run on this thread after thread runs1
  40.  *    if desired private data can be added to this derived class.
  41.  * 2) construct specify a IWindow and a long denoting a command id.
  42.  * the window must be serviced by the current thread.
  43.  * the command id is to notified the window/current thread of completion.
  44.  * 3)Then from a handler call back call win_th_start.
  45.  * to start anonther thread running.
  46.  */
  47. class WindowThread
  48. // handler to detect completion.
  49. {
  50.    private:
  51.  
  52.  
  53.  
  54.       // event id for completion notification.
  55.       unsigned long eventid;
  56.  
  57.       // window to use/notify
  58.       IWindow& window;
  59.  
  60.       // notes that we are notifying orig thread thru a post
  61.       // event. Event will be ignored otherwise.
  62.  
  63.       // access to this variable must be protected by crit section.
  64.       Boolean notifing;
  65.  
  66.       // we could derive privately from ICcommandHandler
  67.       // but then the user of this class can not derive from
  68.       // this class and ICommandHandler without conflict.
  69.  
  70.       // this is the command handler used to be called back
  71.       // on completion.
  72.       class ICom : public ICommandHandler
  73.       {
  74.         private:
  75.  
  76.           // remember our parent' class for call back.
  77.           WindowThread& win_thread;
  78.  
  79.  
  80.           // disabled, not defined.
  81.           ICom( const ICom&);
  82.           ICom& operator=( const ICom& );
  83.         public:
  84.  
  85.           // conruct from window thread
  86.           ICom( WindowThread& win_thread ) : win_thread(win_thread)
  87.           {
  88.             // handle events for our window.
  89.             handleEventsFor( &win_thread.window );
  90.           };
  91.  
  92.         protected:
  93.  
  94.  
  95.           // command handler detects notification.
  96.           virtual Boolean command( ICommandEvent& event );
  97.       } thread_hand;
  98.  
  99.       // cooperate with our nested command handler class.
  100.       friend class ICom;
  101.  
  102.          // disable default constructor, assignment operator, not defined
  103.          WindowTheread( const WindowThread& );
  104.          WindowThread& operator=(const WindowThread&);
  105.  
  106.  
  107.       // nested thread class to run thread code.
  108.       class WinThrFn : public IThreadFn
  109.       {
  110.          private:
  111.            // disable default constructor, assignment operator
  112.            WinThrFn();
  113.            WinThrFn& operator=(const WinThrFn&);
  114.  
  115.            // save the window thread in use.
  116.            WindowThread & wt;
  117.          public:
  118.            WinThrFn(WindowThread& wt) : wt(wt) {};
  119.  
  120.            // virtual destructor.
  121.            virtual ~WinThrFn() {};
  122.  
  123.            // call the run call.
  124.            virtual void run ();
  125.  
  126.       };
  127.  
  128.       // make the run code a friend.
  129.       friend void WinThrFn::run();
  130.  
  131.    public:
  132.  
  133.       // constructor remembers IWindow and eveint id #
  134.       WindowThread(IWindow& window,const long eventid);
  135.  
  136.       // virtual destructor.
  137.       virtual ~WindowThread() {};
  138.  
  139.    public:
  140.  
  141.        // called by user to start the thread.
  142.        virtual void win_th_start( Boolean autoInitGUI =
  143.                                      IThread::defaultAutoInitGUI() );
  144.  
  145.    protected:
  146.  
  147.        // optional call back to user before running thread.
  148.        virtual void begin() {} ;
  149.  
  150.        // code to run on other thread.
  151.  
  152.        // must be overridden.
  153.        // this is thread code tor run.
  154.        virtual void thread() = 0;
  155.  
  156.        // optional call back to user after running thread.
  157.        virtual void finish() {} ;
  158.  
  159.        // runs thread internal, but overridable.
  160.        virtual void run();
  161.  
  162.        // remember window we are on.
  163.        IWindow& OurWindow() { return window; };
  164.  
  165. };
  166.  
  167. #endif  //WINTHR
  168.