home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Core Utilities / Module.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-20  |  2.5 KB  |  79 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Module.h
  3.  
  4.     The module class allows us to have independent pieces of code which are initialized 
  5.     automatically at system startup. Unlike the C++ static constructor method, this 
  6.     is guaranteed to happen at a safe time
  7.  
  8. */
  9.  
  10. #ifndef __MODULE__
  11. #define __MODULE__
  12. #pragma once
  13.  
  14. #include "Object.h"
  15. #include "OTList.h"
  16.  
  17. #define qCWoffsetofBug    1
  18.  
  19. //--------------------------------------------------------------------------------
  20. class TModule : public virtual TObject
  21. {
  22.     DeclareClassSingleAbstract(TModule, TObject);
  23.  
  24. private:
  25.     OTLink            fNextModule;
  26.     
  27. protected:    
  28.     TModule(bool early = false);
  29.     virtual ~TModule();
  30.  
  31.     virtual bool    Validate(void);                    // Check against system, possibly re-order
  32.     
  33.     virtual bool    Available(void);                // Return true if you have been initialized
  34.     virtual bool    Initialized(void);                // Return true if you have been initialized
  35.     virtual bool    InitializeCalled(void);            // Return true if you have been initialized
  36.     virtual bool    InitializeAfter(TModule& mod);    // May be called during Initialize() to make
  37.                                                     // sure another module has been inti
  38.     virtual bool    IsRequired();
  39.     virtual bool    InitializeWhenUsed(); 
  40.  
  41.     static bool        TestBit(long flags, int bit)    { return flags & (1 << bit); }
  42.  
  43. public:
  44.     static bool        InitializeModules(void);    // Initialize all modules in system
  45.     static void        FinalizeModules(void);        // Reverse initialization
  46.  
  47.     static inline    void UseModule(TModule& /*module*/)    {}
  48.  
  49.     static OTList    gUninitList;    // list of uninitialized modules
  50.     static OTList    gInitedList;    // list of initialized modules
  51.     static OTList    gFailedList;    // list of modules which failed to initialize
  52.  
  53. #if !qCWoffsetofBug
  54.     typedef EmbeddedLinkPtr(TModule, fNextModule) ModuleLinkPtr;
  55.     typedef EmbeddedLIFO(TModule, fNextModule) ModuleLIFO;
  56.     typedef EmbeddedList(TModule, fNextModule) ModuleList;
  57.  
  58.     static inline ModuleList& GetUninitList()    { return *(ModuleList*) &TModule::gUninitList; }
  59.     static inline ModuleList& GetInitedList()    { return *(ModuleList*) &TModule::gInitedList; }
  60.     static inline ModuleList& GetFailedList()    { return *(ModuleList*) &TModule::gFailedList; }
  61. #endif 
  62.  
  63. };
  64.  
  65.  
  66. #if qCWoffsetofBug
  67.  
  68. typedef OTLinkPtr<TModule, 8> ModuleLinkPtr;
  69. typedef OTLIFOOf <TModule, 8> ModuleLIFO;
  70. typedef OTListOf <TModule, 8> ModuleList;
  71.  
  72. inline ModuleList& GetUninitList()    { return *(ModuleList*) &TModule::gUninitList; }
  73. inline ModuleList& GetInitedList()    { return *(ModuleList*) &TModule::gInitedList; }
  74. inline ModuleList& GetFailedList()    { return *(ModuleList*) &TModule::gFailedList; }
  75.  
  76. #endif
  77.  
  78. #endif // __MODULE__
  79.