home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-21 | 5.1 KB | 277 lines | [TEXT/CWIE] |
- /*
- File: Module.cp
-
- The module class allows us to have independent pieces of code which are initialized
- automatically at system startup. Unlike the C++ static constructor method, this
- is guaranteed to happen at a safe time
-
- */
-
- #include "Module.h"
-
- #include "Exceptions.h"
-
- #include <Gestalt.h>
- #include <CodeFragments.h>
-
- DefineClassSingleAbstract(TModule);
-
- OTList TModule::gUninitList = {0}; // list of uninitialized modules
- OTList TModule::gInitedList = {0}; // list of initialized modules
- OTList TModule::gFailedList = {0}; // list of initialized modules
-
- #if FeatureOptional(qThreadManagerSupport)
- bool gHasThreadManager = false;
- #endif
-
- //static char gModuleListStorage[sizeof(LinkedListOf<TModule>)];
-
- #pragma segment Main
- //--------------------------------------------------------------------------------
- TModule::TModule(bool early)
- {
- if (offsetof(TModule, fNextModule) != 8)
- {
- Debugger();
- }
-
- OTLink* link = gUninitList.fHead;
-
- if (early || link == nil)
- {
- fNextModule.fNext = link;
- gUninitList.fHead = &fNextModule;
- }
- else
- {
- OTLink* next = link->fNext;
-
- while (next != nil)
- {
- link = next;
- next = next->fNext;
- }
-
- fNextModule.fNext = nil;
- link->fNext = &fNextModule;
- }
- }
-
-
- #pragma segment AppCleanup
- //--------------------------------------------------------------------------------
- TModule::~TModule()
- {
- GetUninitList().Remove(*this);
- GetInitedList().Remove(*this);
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool TModule::Validate(void)
- {
- return true;
- }
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool TModule::Available(void)
- {
- return true;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- bool TModule::Initialized(void)
- {
- return true;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- // Determine if we have tried to initialize this module (successful or not)
- // by default, we scan back through the lined list from gInitModule, but
- // subclasses which cache wether they have been initialized may want to override
- // this method for speed
- bool TModule::InitializeCalled(void)
- {
- return !GetUninitList().IsInList(this);
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- // This routine may be called from within a manager's Initialize method to
- // force another package to be initialized first returning
- bool TModule::InitializeAfter(TModule& mod)
- {
- Assert(&mod != nil);
-
- if (GetUninitList().Remove(mod))
- {
- try
- {
- // module wasn't initialized
-
- InitializeObject(&mod); // Initialize it
-
- // ••• handle failure
-
- GetInitedList().AddFirst(&mod);
- }
- catch(...)
- {
- GetFailedList().AddFirst(&mod);
-
- throw;
- }
- }
-
- return true;
- }
-
- bool TModule::IsRequired()
- {
- return true;
- }
-
- bool TModule::InitializeWhenUsed()
- {
- return false;
- }
-
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- // This function is used to initialize all of the managers after the system
- // is up and running
-
- /*
- bool TModule::ValidateSystem(void)
- {
- EmbeddedLinkPtr(TModule, fNextModule) link = GetUninitList().GetFirst();
-
- bool valid = true;
-
- while (link)
- {
- valid = false;
-
- try
- {
- valid = link->Validate();
- }
- catch(...)
- {
- }
-
- if (!valid)
- {
- if (GetUninitList().Remove(link))
- {
- GetFailedList().AddFirst(link);
- }
- }
-
- link = link.Next();
- }
-
- return valid;
- }
- */
-
- #pragma segment AppInit
- //--------------------------------------------------------------------------------
- // This function is used to initialize all of the managers after the system
- // is up and running
- bool TModule::InitializeModules(void)
- {
- EmbeddedLinkPtr(TModule, fNextModule) mod = GetUninitList().GetFirst();
-
- bool valid = true;
-
- while (mod)
- {
- valid = false;
-
- try
- {
- valid = mod->Validate();
- }
- catch(...)
- {
- }
-
- if (!valid)
- {
- if (GetUninitList().Remove(mod))
- {
- GetFailedList().AddFirst(mod);
- }
-
- if (!mod->IsRequired())
- {
- valid = true;
- }
- }
-
- mod = mod.Next();
- }
-
- if (valid)
- {
- try
- {
- while (mod = GetUninitList().RemoveFirst())
- {
- InitializeObject(mod);
-
- GetInitedList().AddFirst(mod);
- }
-
- return true;
- }
- catch(...)
- {
- FinalizeModules();
-
- return false;
- }
- }
-
- return valid;
- }
-
-
- #pragma segment AppCleanup
- //--------------------------------------------------------------------------------
- // Tear down any modules which have beem initialized
- void TModule::FinalizeModules(void)
- {
- TModule* module;
-
- while ((module = GetInitedList().RemoveFirst()) != nil)
- {
- try
- {
- FinalizeObject(module);
- }
- catch(...)
- {
- #if qDebug
- SysBreakStr("\pFailure in CleanupModules()");
- #endif
- }
-
- GetUninitList().AddFirst(*module);
- }
- }
-
- /*
- this isi a test for the next sixty seconds the broadcasters in your area
- in cooperation with the FCC and other authorities, will conduct a test of
- the emergency broadcast network. This is only a test.
- */