Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DynamicPluggableFactory.cxx

Go to the documentation of this file.
00001 
00002 // $Id: DynamicPluggableFactory.cxx,v 1.8 2002/07/30 19:35:20 gmieg Exp $
00003 //
00004 // DynamicPluggableFactory.cxx
00005 //
00006 // Begin_Html<img src="../../pedestrians.gif" align=center>
00007 // <a href="../source_warning.html">Warning for beginners</a>.<br> 
00008 //
00009 // DynamicPluggableFactory implements Dynamic Pluggable Factory Pattern.
00010 // Adapted from "Pluggable Factory in Practice" by Anthony Lauder,
00011 // C++ Report, October 1999, p. 27
00012 //
00013 // Author:  G. Irwin 10/2000
00014 //
00015 // Also see <a href="../../root_crib/index.html">The ROOT Crib</a> and 
00016 // <a href="../DynamicFactory.html"> DynamicFactory Classes</a> (part of
00017 // <a href="../index.html">The MINOS Class User Guide</a>)End_Html
00019 
00020 #include <cassert>
00021 
00022 #include "TClass.h"
00023 #include "TROOT.h"
00024 #include "TSystem.h"
00025 
00026 #include "Algorithm/AlgConfig.h"
00027 #include "DynamicFactory/DynAlgReg.h"
00028 #include "DynamicFactory/DynamicPluggableFactory.h"
00029 #include "MessageService/MsgService.h"
00030 
00031 ClassImp(DynamicPluggableFactory)
00032 
00033 // Define static variables.  They get initialized to zero by default.
00034   DynamicPluggableFactory *DynamicPluggableFactory::fsTheInstance;
00035 
00036 //......................................................................
00037 CVSID("$Id: DynamicPluggableFactory.cxx,v 1.8 2002/07/30 19:35:20 gmieg Exp $");
00038 
00039 //______________________________________________________________________
00040 DynamicPluggableFactory::DynamicPluggableFactory()
00041 {
00042   MSG("DynFac", Msg::kDebug) << "DynamicPluggableFactory::Constructor\n";
00043   fsTheInstance = this;
00044 }
00045 
00046 //______________________________________________________________________
00047 DynamicPluggableFactory::~DynamicPluggableFactory()
00048 {
00049   MSG("DynFac", Msg::kDebug) << "DynamicPluggableFactory::Destructor\n";
00050   fDynamicAlgRegistry.Delete();
00051   fsTheInstance = 0;
00052 }
00053 
00054 //______________________________________________________________________
00055 DynamicPluggableFactory &DynamicPluggableFactory::GetInstance()
00056 {
00057 
00058 // Cleaner destructor calls DynamicPluggableFactory dtor
00059    static Cleaner cleaner;
00060 
00061    if (!fsTheInstance) {
00062      MSG("DynFac", Msg::kDebug)
00063                     << "Instantiating DynamicPluggableFactory " << endl;
00064      cleaner.UseMe();           // dummy call to quiet compiler warnings
00065      fsTheInstance = new DynamicPluggableFactory();
00066    }
00067    return *fsTheInstance;
00068 }
00069 
00070 //______________________________________________________________________
00071 AlgBase *DynamicPluggableFactory::GetPrototypicalAlg(const char *algnm)
00072 {
00073   const TString algstr(algnm);
00074   DynAlgReg *dynalgfound = 0;
00075 
00076   TIter algregiter(&fDynamicAlgRegistry);
00077   DynAlgReg *dar;
00078   while ((dar = (DynAlgReg *) algregiter())) {
00079     if (dar->GetAlgName() == algstr) {
00080       dynalgfound = dar;
00081       break;
00082     }
00083   }
00084 
00085   if (!dynalgfound) return ((AlgBase *) 0);
00086 
00087 // See if TUnixSystem knows about this Algorithm class yet.
00088   AlgBase *prototypicalalg = LinkPrototypicalAlg(algnm);
00089 
00090   if (prototypicalalg) {
00091     RegisterPrototypicalAlg(prototypicalalg);
00092     return prototypicalalg;
00093   }
00094 
00095 // Need to link the dynamic library containing the Algorithm class.
00096   Int_t irc =
00097              gSystem->Load(dynalgfound->GetDynamicLibraryName().Data());
00098   if (irc < 0) {                                    // Library not found
00099 
00100     MSG("DynFac", Msg::kError)
00101       << "DynamicPluggableFactory::GetPrototypicalAlg(" << algnm << ")"
00102                                                  << ". Dynamic library "
00103      << dynalgfound->GetDynamicLibraryName().Data() <<  " not found.\n";
00104 
00105     return ((AlgBase *) 0);
00106   }
00107   
00108   prototypicalalg = LinkPrototypicalAlg(algnm);
00109 
00110   if (prototypicalalg) RegisterPrototypicalAlg(prototypicalalg);
00111 
00112   return prototypicalalg;
00113 }
00114 
00115 //______________________________________________________________________
00116 AlgConfig *DynamicPluggableFactory::GetPrototypicalAlgConfig(
00117                                                       const char *regnm)
00118 {
00119   const TString regstr(regnm);
00120   DynAlgReg *dynalgfound = 0;
00121 
00122   TIter algregiter(&fDynamicAlgRegistry);
00123   DynAlgReg *dar;
00124   while ((dar = (DynAlgReg *) algregiter())) {
00125     if (TString(dar->GetName()) == regstr) {
00126       dynalgfound = dar;
00127       break;
00128     }
00129   }
00130 
00131   if (!dynalgfound) return ((AlgConfig *) 0);
00132 
00133 // See if TUnixSystem knows about this AlgConfig class yet.
00134   const char *cclnm = dar->GetAlgConfigName();             // ClassName
00135   AlgConfig *prototypicalalgconfig = LinkPrototypicalAlgConfig(cclnm);
00136 
00137   if (prototypicalalgconfig) {
00138     prototypicalalgconfig->SetName(regnm);
00139     RegisterPrototypicalAlgConfig(prototypicalalgconfig);
00140     return prototypicalalgconfig;
00141   }
00142 
00143 // Need to link the dynamic library containing the Algorithm class.
00144   Int_t irc =
00145              gSystem->Load(dynalgfound->GetDynamicLibraryName().Data());
00146   if (irc < 0) {                                    // Library not found
00147 
00148     MSG("DynFac", Msg::kError)
00149       << "DynamicPluggableFactory::GetPrototypicalAlgConfig("
00150                                  << regnm << ")" << ". Dynamic library "
00151      << dynalgfound->GetDynamicLibraryName().Data() <<  " not found.\n";
00152 
00153     return ((AlgConfig *) 0);
00154   }
00155   
00156   prototypicalalgconfig = LinkPrototypicalAlgConfig(cclnm);
00157 
00158   if (prototypicalalgconfig) {
00159     prototypicalalgconfig->SetName(regnm);
00160     RegisterPrototypicalAlgConfig(prototypicalalgconfig);
00161   }
00162 
00163   return prototypicalalgconfig;
00164 }
00165 
00166 //______________________________________________________________________
00167 Bool_t DynamicPluggableFactory::IsRegistered(const char *regnm) const
00168 {
00169 
00170   return (fDynamicAlgRegistry.FindObject(regnm) != 0);
00171 
00172 }
00173 
00174 //______________________________________________________________________
00175 AlgBase &DynamicPluggableFactory::LendAlg(const char *algnm)
00176 {
00177   AlgBase *algbase = GetAlgPtr(algnm);
00178 
00179   if (!algbase) {
00180     AlgBase *prototypicalalg = GetPrototypicalAlg(algnm);
00181     algbase = (prototypicalalg ? prototypicalalg : 0);
00182     if (!algbase) {
00183     MSG("DynFac", Msg::kError)
00184                   << "DynamicPluggableFactory::LendAlg(" << algnm << ")"
00185           << ":  Prototypical Algorithm not found.  Abort job." << endl;
00186     }
00187     assert(algbase);
00188   }
00189 
00190   return *algbase;
00191 }
00192 
00193 //______________________________________________________________________
00194 AlgConfig &DynamicPluggableFactory::LendAlgConfig(const char *regnm)
00195 {
00196   AlgConfig *algconfig = GetAlgConfigPtr(regnm);
00197 
00198   if (!algconfig) {
00199     AlgConfig *prototypicalalgconfig = GetPrototypicalAlgConfig(regnm);
00200     algconfig = (prototypicalalgconfig ? prototypicalalgconfig : 0);
00201     if (!algconfig) {
00202     MSG("DynFac", Msg::kError)
00203            << "DynamicPluggableFactory::LendAlgConfig(" << regnm << ")"
00204          << ":  Prototypical AlgConfig not found.  Abort job." << endl;
00205     }
00206     assert(algconfig);
00207   }
00208 
00209   return *algconfig;
00210 }
00211 
00212 //______________________________________________________________________
00213 AlgBase *DynamicPluggableFactory::LinkPrototypicalAlg(const char *algnm)
00214 {
00215   MSG("DynFac", Msg::kDebug)
00216    << "DynamicPluggableFactory::LinkPrototypicalAlg(" << algnm << ")\n";
00217 
00218   AlgBase *prototypicalalg = 0;
00219   if (gROOT->GetClass(algnm,kTRUE))
00220     prototypicalalg = (AlgBase *) gROOT->GetClass(algnm,kTRUE)->New();
00221 
00222   return prototypicalalg ? prototypicalalg : 0;
00223 }
00224 
00225 //______________________________________________________________________
00226 AlgConfig *DynamicPluggableFactory::LinkPrototypicalAlgConfig(
00227                                                       const char *cclnm)
00228 {
00229   MSG("DynFac", Msg::kDebug)
00230    << "DynamicPluggableFactory::LinkPrototypicalAlg(" << cclnm << ")\n";
00231 
00232   AlgConfig *prototypicalalgconfig = 0;
00233   if (gROOT->GetClass(cclnm,kTRUE))
00234     prototypicalalgconfig =
00235                       (AlgConfig *) gROOT->GetClass(cclnm,kTRUE)->New();
00236 
00237   return prototypicalalgconfig ? prototypicalalgconfig : 0;
00238 }
00239 
00240 //______________________________________________________________________
00241 void DynamicPluggableFactory::Register(DynAlgReg *dar)
00242 {
00243   fDynamicAlgRegistry.Add(dar);
00244 }

Generated on Mon Feb 15 11:06:38 2010 for loon by  doxygen 1.3.9.1