home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / schedule.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-15  |  4.1 KB  |  129 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //  SCHEDULE.H
  12.  
  13. #ifndef __CAMSchedule__
  14. #define __CAMSchedule__
  15.  
  16. class CAMSchedule
  17. {
  18. public: 
  19.     virtual ~CAMSchedule();
  20.     CAMSchedule();
  21.  
  22.     DWORD GetAdviseCount();
  23.     REFERENCE_TIME GetNextAdviseTime();
  24.  
  25.     // We need a method for derived classes to add advise packets, we return the cookie
  26.     DWORD AddAdvisePacket( const REFERENCE_TIME & time1, const REFERENCE_TIME & time2, HANDLE h, BOOL periodic );
  27.     // And a way to cancel
  28.     STDMETHODIMP Unadvise(DWORD dwAdviseCookie);
  29.  
  30.     // Tell us the time please, and we'll dispatch the expired events.  We return the time of the next event.
  31.     // NB: The time returned will be "useless" if you start adding extra Advises.  But that's the problem of
  32.     // whoever is using this helper class (typically a clock).
  33.     REFERENCE_TIME Advise( const REFERENCE_TIME & rtTime );
  34.  
  35. protected:
  36.     virtual void Dispatch( DWORD Cookie, REFERENCE_TIME & time1, REFERENCE_TIME & time2, HANDLE & h, BOOL & periodic );
  37.  
  38. private:
  39.     // We define the nodes that will be used in our singly linked list
  40.     // of advise packets.  The list is ordered by time, with the
  41.     // elements that will expire first at the front.
  42.     class CAdvisePacket
  43.     {
  44.     public:
  45.         CAdvisePacket()
  46.         {}
  47.  
  48.         CAdvisePacket * m_next;
  49.         DWORD           m_dwAdviseCookie;
  50.         REFERENCE_TIME  m_rtEventTime;      // Time at which event should be set
  51.         REFERENCE_TIME  m_rtPeriod;         // Periodic time
  52.         HANDLE          m_hNotify;          // Handle to event or semephore
  53.         BOOL            m_bPeriodic;        // TRUE => Periodic event
  54.  
  55.         CAdvisePacket( CAdvisePacket * next, LONGLONG time ) : m_next(next), m_rtEventTime(time)
  56.         {}
  57.  
  58.         void InsertAfter( CAdvisePacket * p )
  59.         {
  60.             p->m_next = m_next;
  61.             m_next    = p;
  62.         }
  63.  
  64.         int IsZ() const // That is, is it the node that represents the end of the list
  65.     { return m_next == 0; }
  66.  
  67.     CAdvisePacket * RemoveNext()
  68.     {
  69.         CAdvisePacket *const next = m_next;
  70.         CAdvisePacket *const new_next = next->m_next;
  71.         m_next = new_next;
  72.         return next;
  73.     }
  74.  
  75.     void DeleteNext()
  76.     {
  77.         delete RemoveNext();
  78.     }
  79.  
  80.     CAdvisePacket * Next() const
  81.     {
  82.         CAdvisePacket * result = m_next;
  83.         if (result->IsZ()) result = 0;
  84.         return result;
  85.     }
  86.  
  87.         DWORD Cookie() const
  88.         { return m_dwAdviseCookie; }
  89.     };
  90.     
  91.     // Structure is:             
  92.     // head -> elmt1 -> elmt2 -> z -> null
  93.     // So an empty list is:       head -> z -> null
  94.     // Having head & z as links makes insertaion,
  95.     // deletion and shunting much easier.
  96.     CAdvisePacket   head, z;            // z is both a tail and a sentry
  97.  
  98.     volatile DWORD  m_dwNextCookie;     // Strictly increasing
  99.     volatile DWORD  m_dwAdviseCount;    // Number of elements on list
  100.  
  101.     CCritSec        m_Serialize;
  102.  
  103.     // AddAdvisePacket: adds the packet, returns the cookie (0 if failed)
  104.     DWORD AddAdvisePacket( CAdvisePacket * pPacket );
  105.  
  106.     // A Shunt is where we have changed the first element in the
  107.     // list and want it re-evaluating (i.e. repositioned) in
  108.     // the list.
  109.     void ShuntHead();
  110.  
  111.     // Rather than delete advise packets, we cache them for future use
  112.     CAdvisePacket * m_pAdviseCache;
  113.     DWORD           m_dwCacheCount;
  114.     enum { dwCacheMax = 5 };             // Don't bother caching more than five
  115.  
  116.     void Delete( CAdvisePacket * pLink );// This "Delete" will cache the Link
  117.  
  118. // Attributes and methods for debugging
  119. public:
  120. #ifdef DEBUG
  121.     void DumpLinkedList();
  122. #else
  123.     #define DumpLinkedList() {}
  124. #endif
  125.  
  126. };
  127.  
  128. #endif // __CAMSchedule__
  129.