bbtkFactory.cxx

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkFactory.cxx,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:13 $
00006   Version:   $Revision: 1.40 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00037 #include "bbtkFactory.h"
00038 #include "bbtkMessageManager.h"
00039 #include "bbtkConnection.h"
00040 #include "bbtkConfigurationFile.h"
00041 #include "bbtkUtilities.h"
00042 
00043 #include <sys/stat.h> // for struct stat stFileInfo
00044 
00045 #if defined(_WIN32)
00046 #include <direct.h> // for getcwd
00047 #endif
00048 
00049 #include <cctype>    // std::toupper
00050 
00051 #include <time.h>
00052 
00053 namespace bbtk
00054 {
00055 
00056   //===================================================================
00058   Factory::Pointer Factory::New()
00059   {
00060     bbtkDebugMessage("Kernel",9,"Factory::New()"<<std::endl);
00061     return MakePointer(new Factory());
00062   }
00063   //===================================================================
00064 
00065   //===================================================================
00067   Factory::Factory()
00068     : mExecuter()
00069   {
00070     bbtkDebugMessage("Kernel",7,"Factory::Factory()"<<std::endl);
00071   }
00072   //===================================================================
00073 
00074   //===================================================================
00076   Factory::~Factory()
00077   {
00078     bbtkDebugMessageInc("Kernel",7,"Factory::~Factory()"<<std::endl);
00079     CloseAllPackages();
00080     bbtkDebugDecTab("Kernel",7);
00081   }
00082   //===================================================================
00083 
00084 
00085 
00086   //===================================================================
00087   void Factory::Reset()
00088   {
00089     bbtkDebugMessageInc("Kernel",7,"Factory::Reset()"<<std::endl);
00090     CloseAllPackages();
00091     bbtkDebugDecTab("Kernel",7);
00092   }
00093   //===================================================================
00094 
00095 
00096   // ===================================================================
00097   bool Factory::DoLoadPackage(std::string libname,
00098                               std::string pkgname,
00099                               std::string path)
00100   {
00101     
00102     Package::Pointer p = Package::CreateFromDynamicLibrary(libname,
00103                                                            pkgname,
00104                                                            path);
00105     if (p!=0)
00106       {
00107         //===================================================================
00108         bbtkMessage("Output",2,p->GetName()<<" "
00109                     <<p->GetVersion()
00110                     <<" "
00111                     <<p->GetAuthor() << " Category(s) :"
00112                     <<p->GetCategory()
00113                     <<std::endl);
00114         bbtkMessage("Output",2,p->GetDescription()<<std::endl);
00115         //===================================================================
00116         p->AddFactory(GetThisPointer<Factory>());
00117         mPackageMap[pkgname] = p;
00118         return true;
00119       }
00120     return false;
00121     
00122   }
00123   
00124   //===================================================================
00134   
00137   
00138 
00139   
00140   void Factory::LoadPackage( const std::string& name )
00141   {
00142   // Note : in the following :
00143   // name : the user supplied name
00144   //      - abreviated name    e.g.       pkg   pkg.so   libbpkg   libbbpkg.so
00145   //      - relative full name e.g.       ./libbbpkg.so   ../../libbbpkg.so
00146   //      - absolute full name e.g.       /home/usrname/proj/lib/libbbpkg.so
00147   //          same for Windows, with      c:, d: ...
00148   //
00149   // lastname : string before the last / (if any), or user supplied name
00150 
00151     bbtkDebugMessageInc("Kernel",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
00152     bbtkMessage("debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
00153 
00154     std::vector<std::string> package_paths;
00155     std::string libname;  // full path library name
00156     std::string pkgname;  // e.g. libbb<pkgname>.so
00157 
00158     std::string upath;
00159     pkgname = Utilities::ExtractPackageName(name,upath);
00160 
00161     bbtkMessage("debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
00162     bbtkMessage("debug",1,"Package path ["<<upath<<"]"<<std::endl);
00163 
00164     // no loading package if already loaded
00165     PackageMapType::iterator iUnload;
00166     iUnload = mPackageMap.find(pkgname);
00167     if (iUnload != mPackageMap.end())
00168     {
00169       bbtkMessage("Output",2,"["<< pkgname <<"] already loaded" << std::endl);
00170       return;
00171     }
00172 
00173 // =================================================
00174 // The following structure was checked to work
00175 // with any type of relative/absolute path.
00176 // Please don't modify it without checking
00177 // *all* the cases. JP
00178 //==================================================
00179 
00180 //std::cout << "upath [" << upath << "]" << std::endl;
00181 
00182     bool ok = false;
00183     bool foundFile = false;
00184 
00185     // If path provided by user will be the first scanned :
00186     // push it into vector of paths
00187     if (upath.length()>0)   // ------------------------------------- check user supplied location
00188     {
00189        if (name[0] != '.' && name[0] != '/' && name[1]!= ':')
00190        {
00191           bbtkError("Use absolute or relative path name! ["<<name<<"] is an illegal name");
00192           return;
00193        }
00194 
00195       // std::string path = Utilities::ExpandLibName(upath, false);
00196        std::string path = Utilities::ExpandLibName(name,false); // keep last item, here.
00197          
00198        if (path != "")
00199        {
00200           std::string p2;
00201           Utilities::ExtractPackageName(path,p2);
00202           //libname = Utilities::MakeLibnameFromPath(path, pkgname);
00203           libname = Utilities::MakeLibnameFromPath(p2, pkgname); // remove last item
00204           // Check if library exists
00205           if ( !Utilities::FileExists(libname) )
00206           {
00207           // The following is *NOT* a debug time message :
00208           // It's a user intended message.
00209           // Please don't remove it.
00210             bbtkMessage("Output",3,"   [" <<libname 
00211                         <<"] : doesn't exist" <<std::endl);
00212           }
00213           else
00214           {
00215              ok = DoLoadPackage( libname, pkgname, path);         
00216           }
00217        }
00218        else
00219        {
00220           bbtkError("Path ["<<upath<<"] doesn't exist");
00221           return;
00222        }
00223     }
00224     else     // ----------------------------------------------------- iterate on the paths  
00225     {
00226 
00227     std::string path;
00228     package_paths = ConfigurationFile::GetInstance().Get_package_paths();
00229     std::vector<std::string>::iterator i;
00230     for (i=package_paths.begin();i!=package_paths.end();++i)
00231     {
00232         foundFile = false;
00233         path = *i;
00234 
00235         // we *really* want '.' to be the current working directory
00236         if (path == ".")
00237         {
00238           char buf[2048]; // for getcwd
00239           char * currentDir = getcwd(buf, 2048);
00240           std::string cwd(currentDir);
00241           path = currentDir;
00242         }
00243 
00244         libname = Utilities::MakeLibnameFromPath(path, pkgname);
00245 
00246         bbtkMessage("debug",2,"-> Trying to load [" << libname << "]" <<std::endl);
00247 
00248       // Check if library exists           
00249         if ( !Utilities::FileExists(libname) )
00250         {
00251         // The following is *NOT* a debug time message :
00252         // It's a user intended message.
00253         // Please don't remove it.
00254           bbtkMessage("Output",3,
00255                       "   [" <<libname <<"] : doesn't exist" <<std::endl);
00256            continue;  // try next path
00257         }
00258         foundFile = true; 
00259 
00260       // Try to Load the library
00261 
00262         ok = DoLoadPackage( libname, pkgname, path);
00263         if (ok)
00264         {
00265            bbtkMessage("debug",2,"   OK"<<std::endl);
00266         }
00267         break; // we stop iterating even if error : have to signal it to user
00268     } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
00269 
00270 }
00271 
00272     if( !ok )  // nothing was loaded
00273     {
00274       if (!foundFile)
00275       {
00276         bbtkError("Could not find package ["<<pkgname<< "]");
00277       }
00278       else
00279       {
00280 #if defined(__GNUC__)
00281         bbtkError("Could not load package ["<< pkgname
00282                   <<"] :" << std::endl 
00283                   << "  Opening "<<libname<<" failed"
00284                   << "  Reason: "<< dlerror());
00285 #elif defined(_WIN32)
00286         bbtkError("Could not load package ["<<pkgname
00287                   <<"] :"<< std::endl << "   Error loading " <<libname);
00288 
00289     // look how to get the error message on win
00290     //<<dlerror());
00291     // it is the bordel !! (the bloody fucking bordel, you mean?)
00292     // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
00293 #endif
00294       }
00295     }
00296     bbtkMessage("Output",2,"[" << libname << "] loaded" << std::endl);
00297 
00298   }
00299 
00300   //===================================================================
00305  void Factory::UnLoadPackage( const std::string& name )
00306  {
00307     bbtkDebugMessageInc("Kernel",7,"Factory::UnLoadPackage(\""
00308                        <<name<<"\")"<<std::endl);
00309   
00310     PackageMapType::iterator i;
00311     i = mPackageMap.find(name);
00312     if (i == mPackageMap.end()) 
00313     {
00314       bbtkError("cannot unload package \""<<name
00315                 <<"\" : package not loaded !");
00316     }
00317     ClosePackage(i);
00318     bbtkDebugDecTab("Kernel",7);
00319   }
00320   //===================================================================
00321 
00322 
00323   //===================================================================
00324   void Factory::CloseAllPackages()
00325   {
00326     bbtkDebugMessageInc("Kernel",7,"Factory::CloseAllPackages()"<<std::endl);
00327     while (mPackageMap.begin() != mPackageMap.end())
00328       {
00329         PackageMapType::iterator i = mPackageMap.begin();
00330         ClosePackage(i);
00331       }
00332     bbtkDebugDecTab("Kernel",7);
00333   }
00334   //===================================================================
00335 
00336   //===================================================================
00348  void Factory::ClosePackage(PackageMapType::iterator& i) 
00349   {   
00350      bbtkDebugMessageInc("Kernel",7,"Factory::ClosePackage(\""
00351                          <<i->second->GetName()
00352                         <<"\")"<<std::endl);
00353 
00354      
00355      // Removes this from the set of factories which use the package
00356      i->second->RemoveFactory(GetThisPointer<Factory>());
00357      Package::WeakPointer p = i->second;
00358      // remove the entry in the map
00359      mPackageMap.erase(i);
00360      // Release the package if not already destroyed
00361      if (p.lock()) Package::Release(p);
00362 
00363 
00364      bbtkDebugDecTab("Kernel",7);
00365   }
00366   //===================================================================
00367   
00368 
00369 
00370   //===================================================================  
00372   void Factory::PrintPackages(bool details, bool adaptors) const
00373   {
00374     bbtkDebugMessageInc("Kernel",9,"Factory::PrintPackages"<<std::endl);
00375 
00376     PackageMapType::const_iterator i;
00377     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00378     {
00379       bbtkMessage("Help",1, i->first << std::endl);
00380       if (details) {
00381          i->second->PrintBlackBoxes(false,adaptors);
00382       }
00383     }
00384 
00385     bbtkDebugDecTab("Kernel",9);
00386   }
00387   //===================================================================
00388 
00389   //===================================================================  
00391   void Factory::HelpPackage(const std::string& name, bool adaptors) const
00392   {
00393     bbtkDebugMessageInc("Kernel",9,"Factory::HelpPackage(\""<<name<<"\")"
00394                         <<std::endl);
00395 
00396     PackageMapType::const_iterator i = mPackageMap.find(name);
00397     if ( i != mPackageMap.end() ) 
00398       {
00399       bbtkMessage("Help",1, "Package "<<i->first<<" ");
00400       
00401       if (i->second->GetVersion().length()>0)
00402         bbtkMessageCont("Help",1,"v" <<i->second->GetVersion());
00403         
00404       if (i->second->GetAuthor().length()>0)
00405         bbtkMessageCont("Help",1,"- "<<i->second->GetAuthor());
00406         
00407       if (i->second->GetCategory().length()>0)
00408         bbtkMessageCont("Help",1,"- "<<i->second->GetCategory());        
00409         
00410       bbtkMessageCont("Help",1,std::endl);
00411       bbtkIncTab("Help",1);
00412       bbtkMessage("Help",1,i->second->GetDescription()<<std::endl);
00413       if (i->second->GetNumberOfBlackBoxes()>0) 
00414         {
00415           bbtkMessage("Help",1, "Black boxes : "<<std::endl);
00416           i->second->PrintBlackBoxes(true,adaptors);
00417         }
00418       else 
00419         {
00420           bbtkMessage("Help",1, "No black boxes"<<std::endl);
00421         }
00422       bbtkDecTab("Help",1);
00423       }
00424     else 
00425       {
00426       bbtkDebugDecTab("Kernel",9);
00427       bbtkError("package \""<<name<<"\" unknown");
00428       }
00429     
00430     bbtkDebugDecTab("Kernel",9);
00431   }
00432   //===================================================================
00433 
00434   //===================================================================
00437   void Factory::HelpBlackBox(const std::string& name, 
00438                              std::string& package,
00439                              bool full) const
00440   {
00441     bbtkDebugMessageInc("Kernel",9,"Factory::HelpBlackBox(\""<<name<<"\")"
00442                         <<std::endl);
00443 
00444     bool found = false;
00445     PackageMapType::const_iterator i;
00446     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00447       {
00448       if (i->second->ContainsBlackBox(name)) 
00449         {
00450           i->second->HelpBlackBox(name,full);
00451               package = i->second->GetName();
00452           found = true;
00453         }
00454       }
00455     
00456     bbtkDebugDecTab("Kernel",9);
00457     if (!found) 
00458       {
00459       bbtkError("No package of the factory contains any black box <"
00460                  <<name<<">");
00461       }
00462   }  
00463   //===================================================================
00464 
00465 
00466   //=================================================================== 
00468   void Factory::InsertPackage( Package::Pointer p )
00469   {
00470     bbtkDebugMessageInc("Kernel",9,"Factory::InsertPackage(\""<<
00471                         p->GetName()<<"\")"<<std::endl);
00472 
00473     p->AddFactory(GetThisPointer<Factory>());
00474     mPackageMap[p->GetName()] = p;
00475 
00476     bbtkDebugDecTab("Kernel",9);
00477   }
00478   //===================================================================
00479   
00480   //=================================================================== 
00482   void Factory::RemovePackage( Package::Pointer p )
00483   {
00484     bbtkDebugMessageInc("Kernel",9,"Factory::RemovePackage(\""<<
00485                         p->GetName()<<"\")"<<std::endl);
00486 
00487     PackageMapType::iterator i;
00488     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00489       {
00490          if (i->second == p) break;
00491       };
00492     
00493     if (i!=mPackageMap.end())
00494       {
00495         ClosePackage(i);
00496       }
00497     else 
00498       {
00499         bbtkError("Factory::RemovePackage(\""<<
00500                   p->GetName()<<"\") : package absent from factory");
00501       }
00502 
00503     bbtkDebugDecTab("Kernel",9);
00504   }
00505   //===================================================================
00506   
00507 
00508   //===================================================================
00510   BlackBox::Pointer Factory::NewBlackBox(const std::string& type, 
00511                                  const std::string& name) const
00512   {
00513     bbtkDebugMessageInc("Kernel",7,"Factory::NewBlackBox(\""
00514                         <<type<<"\",\""<<name<<"\")"<<std::endl);
00515 
00516     BlackBox::Pointer b; 
00517     PackageMapType::const_iterator i;
00518     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00519       {
00520       b = i->second->NewBlackBox(type,name);
00521       if (b) break; 
00522       }
00523     if (!b) 
00524       {
00525        bbtkError("black box type \""<<type<<"\" unknown");
00526       } 
00527 
00528     bbtkDebugDecTab("Kernel",7);
00529     return b;
00530   }
00531   //===================================================================
00532 
00533   //===================================================================
00535   BlackBox::Pointer Factory::NewAdaptor(const DataInfo& typein,
00536                                 const DataInfo& typeout,
00537                                 const std::string& name) const
00538   {
00539     bbtkDebugMessageInc("Kernel",8,"Factory::NewAdaptor("
00540                         <<typein<<","
00541                         <<typeout<<",\""
00542                         <<name<<"\")"<<bbtkendl);
00543 
00544 
00545     BlackBox::Pointer b; 
00546     PackageMapType::const_iterator i;
00547     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00548       {
00549       b = i->second->NewAdaptor(typein,typeout,name);
00550       if (b) break; 
00551       }
00552     if (!b) 
00553       {
00554         bbtkError("no "<<typein<<" to "<<typeout
00555                   <<" adaptor available");
00556       } 
00557     
00558     bbtkDebugDecTab("Kernel",7);
00559     return b; 
00560   }
00561   //===================================================================
00562 
00563 
00564   //===================================================================
00566   BlackBox::Pointer Factory::NewWidgetAdaptor(const DataInfo& typein,
00567                                       const DataInfo& typeout,
00568                                       const std::string& name) const
00569   {
00570     bbtkDebugMessageInc("Kernel",8,"Factory::NewWidgetAdaptor(<"
00571                         <<typein<<">,<"
00572                         <<typeout<<">,\""
00573                         <<name<<"\")"<<bbtkendl);
00574 
00575 
00576     BlackBox::Pointer b; 
00577     PackageMapType::const_iterator i;
00578     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00579       {
00580         b = i->second->NewWidgetAdaptor(typein,
00581                                                  typeout,
00582                                                  name);
00583       if (b) break; 
00584       }
00585     if (!b) 
00586       {
00587         bbtkError("no "<<typein<<" to "<<typeout
00588                   <<"> widget adaptor available");
00589       } 
00590     
00591     bbtkDebugDecTab("Kernel",7);
00592     return b; 
00593   }
00594   //===================================================================
00595 
00596   //===================================================================
00598   bool Factory::FindAdaptor(const DataInfo& typein,
00599                                   const DataInfo& typeout,
00600                                   std::string& adaptor) const
00601   {
00602     bbtkDebugMessageInc("Kernel",8,"Factory::FindAdaptor(<"
00603                         <<typein<<">,<"
00604                         <<typeout<<">)"<<bbtkendl);
00605     
00606     bool b = false;
00607     PackageMapType::const_iterator i;
00608     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00609       {
00610         b = i->second->FindAdaptor(typein,
00611                                             typeout,
00612                                             adaptor);
00613         if (b) break; 
00614       }
00615     /*
00616     if (!b) 
00617       {
00618         bbtkError("no "<<typein<<" to "<<typeout
00619                   <<"> widget adaptor available");
00620       } 
00621     */
00622 
00623     bbtkDebugDecTab("Kernel",7);
00624     return b; 
00625   }
00626   //===================================================================
00627 
00628   //===================================================================
00630   bool Factory::FindWidgetAdaptor(const DataInfo& typein,
00631                                   const DataInfo& typeout,
00632                                   std::string& adaptor) const
00633   {
00634     bbtkDebugMessageInc("Kernel",8,"Factory::FindWidgetAdaptor(<"
00635                         <<typein<<">,<"
00636                         <<typeout<<">)"<<bbtkendl);
00637     
00638     bool b = false;
00639     PackageMapType::const_iterator i;
00640     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00641       {
00642         b = i->second->FindWidgetAdaptor(typein,
00643                                                   typeout,
00644                                                   adaptor);
00645         if (b) break; 
00646       }
00647     bbtkDebugDecTab("Kernel",7);
00648     return b; 
00649   }
00650   //===================================================================
00651 
00652   //===================================================================
00654   bool Factory::FindWidgetAdaptor2(const DataInfo& typein,
00655                                   const DataInfo& typeout,
00656                                   std::string& widget,
00657                                   std::string& adaptor) const
00658   {
00659     bbtkDebugMessageInc("Kernel",8,"Factory::FindWidgetAdaptor(<"
00660                         <<typein<<">,<"
00661                         <<typeout<<">)"<<bbtkendl);
00662     
00663     bool b = false;
00664     adaptor = widget = "";
00665     PackageMapType::const_iterator i;
00666     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00667       {
00668         b = i->second->FindWidgetAdaptor(typein,
00669                                                   typeout,
00670                                                   widget);
00671         if (b) break; 
00672       }
00673     if (!b) 
00674       {
00675         // Look for a widget adaptor with good nature out
00676         bbtkMessage("Kernel",5,
00677                     "*** Looking for a two pieces widget adaptor for : "
00678                     << typein << "->"<<typeout<<std::endl);
00679         for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00680           {
00681             Package::AdaptorMapType::const_iterator j;
00682             for (j=i->second->GetAdaptorMap().begin();
00683                  j!=i->second->GetAdaptorMap().end();
00684                  ++j)
00685               {
00686                 if ( ( j->first.mKind ==  
00687                        BlackBoxDescriptor::DEFAULT_GUI) &&
00688                      //(j->first.mTypeIn == typein) &&
00689                      (j->first.mTypeOut.GetNature() == typeout.GetNature() ) 
00690                      )
00691                   {
00692                     widget = j->second.lock()->GetTypeName();
00693                     bbtkMessage("Kernel",5,
00694                                 "===> Found first part : "<<widget
00695                                 << " "<<j->first.mTypeIn<<"->"
00696                                 <<j->first.mTypeOut<<std::endl);
00697                     DataInfo ti( j->first.mTypeOut.GetType(), "");
00698                     DataInfo to( typeout.GetType(), "");
00699                     b = FindAdaptor( ti, to, adaptor );
00700                     if (b) 
00701                       {
00702                         bbtkMessage("Kernel",5,
00703                                     "===> Found second part : "<<adaptor
00704                                     <<std::endl);
00705                         break;
00706                       }
00707                     else
00708                       {
00709                         bbtkMessage("Kernel",5,
00710                                     "===> No second part found"<<std::endl);
00711                       }
00712                   }
00713               }
00714             if (b) break;
00715           }
00716       }
00717     bbtkDebugDecTab("Kernel",7);
00718     return b; 
00719   }
00720   //===================================================================
00721 
00722   //===================================================================
00724   Connection::Pointer Factory::NewConnection(BlackBox::Pointer from,
00725                                              const std::string& output,
00726                                              BlackBox::Pointer to,
00727                                              const std::string& input) const
00728   {
00729     bbtkDebugMessage("Kernel",7,"Factory::NewConnection(\""
00730                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
00731                       <<to->bbGetName()<<"\",\""<<input
00732                       <<"\")"<<std::endl);
00733     
00734     return Connection::New(from,output,to,input,
00735                            GetThisPointer<Factory>());
00736   }
00737   //===================================================================
00738 
00739 
00740 
00741   //===================================================================
00742   Package::Pointer Factory::GetPackage(const std::string& name) const
00743   {
00744     bbtkDebugMessageInc("Kernel",9,"Factory::GetPackage(\""<<name<<"\")"
00745                          <<std::endl);
00746 
00747     PackageMapType::const_iterator i = mPackageMap.find(name);
00748     if ( i != mPackageMap.end() ) 
00749     {
00750       bbtkDebugDecTab("Kernel",9); 
00751       return i->second;
00752     }
00753     else 
00754     {
00755        bbtkDebugDecTab("Kernel",9);
00756        bbtkError("package \""<<name<<"\" unknown");
00757     }
00758     
00759     bbtkDebugDecTab("Kernel",9);  
00760   }
00761   //===================================================================
00762   
00763 
00764   //===================================================================
00765   void Factory::CheckPackages() const
00766   {
00767     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
00768                  <<std::endl);
00769     PackageMapType::const_iterator i;
00770     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00771       {
00772         i->second->CheckBoxes();
00773       }
00774     bbtkMessage("debug",1,"****** Checking Factory "<<(void*)this
00775                 <<" ... OK"<<std::endl);
00776   }
00777   //===================================================================
00778 
00779   //===================================================================
00780   void Factory::WriteDotFilePackagesList(FILE *ff)
00781   {
00782 
00783     bbtkDebugMessageInc("Kernel",9,"Factory::WriteDotFilePackagesList()"
00784                          <<std::endl);
00785 
00786     fprintf( ff , "\n");
00787     fprintf( ff , "subgraph cluster_FACTORY {\n");
00788     fprintf( ff , "  label = \"PACKAGES\"%s\n",  ";");
00789     fprintf( ff , "  style=filled%s\n",";");
00790     fprintf( ff , "  color=lightgrey%s\n",";");
00791     fprintf( ff , "  rankdir=TB%s\n",";");
00792 
00793     std::string url;
00794     PackageMapType::const_iterator i;
00795     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00796     {
00797        url=GetPackage(i->first)->GetDocURL();
00798        fprintf(ff,"  %s [shape=ellipse, URL=\"%s\"]%s\n",i->first.c_str(),url.c_str(),";" );
00799     }
00800     fprintf( ff , "}\n\n");
00801     bbtkDebugDecTab("Kernel",9);
00802   }
00803   //===================================================================
00804 
00805 
00806  void Factory::ShowGraphTypes(const std::string& name) const
00807  {
00808 
00809    bool found = false;
00810    PackageMapType::const_iterator i;
00811    for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00812    {
00813       if (i->second->ContainsBlackBox(name)) 
00814       {
00815          std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
00816 
00817             // Don't pollute the file store with  "temp_dir" directories ...    
00818          std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00819          std::string directory = "\"" + default_doc_dir + separator + "temp_dir"  +separator + "\"";
00820          std::string filename2 =  default_doc_dir + separator + "temp_dir" + separator + "tmp.html"; 
00821 
00822 #if defined(_WIN32)  
00823         std::string command("start \"Titre\" /D ");
00824 #else 
00825         std::string command("gnome-open ");
00826 #endif
00827         command=command + directory +" tmp.html";
00828         FILE *ff;
00829         ff=fopen(filename2.c_str(),"w");
00830 
00831         fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
00832         fprintf(ff,"  window.location=\"%s#%s\";\n" , i->second->GetDocURL().c_str(),name.c_str() );
00833         fprintf(ff,"//--></script></head><body></body></html>\n");
00834 
00835 
00836         //fprintf(ff, "<a  href=\"%s#%s\">Link</a>\n", i->second->GetDocURL().c_str(),name.c_str() );
00837         fclose(ff);
00838         system( command.c_str() );      
00839         found = true;
00840      }
00841    }
00842     
00843    bbtkDebugDecTab("Kernel",9);
00844    if (!found) 
00845    {
00846       bbtkError("No package of the factory contains any black box <"
00847                 <<name<<">");
00848    }
00849  }
00850     
00851 
00852 
00853 
00854   void Factory::CreateHtmlIndex(IndexEntryType type, 
00855                                 const std::string& filename)
00856   {
00857     bbtkDebugMessageInc("Kernel",9,"Factory::CreateHtmlIndex(\""
00858                         <<filename<<"\")"<<bbtkendl);
00859     
00860     std::string title;
00861 
00862     typedef std::map<std::string, 
00863       std::vector<BlackBoxDescriptor::Pointer> > IndexType;
00864     IndexType index;
00865     // Builds the index map
00866     PackageMapType::const_iterator i;
00867     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
00868       {
00869         Package::Pointer pack = i->second;
00870         if (pack->GetName()=="user") continue;
00871         Package::BlackBoxMapType::const_iterator j;
00872         for (j = pack->GetBlackBoxMap().begin(); 
00873              j!= pack->GetBlackBoxMap().end(); 
00874              ++j)
00875           {
00876             
00877             // Skip adaptors 
00878             if ( type==Adaptors )
00879               {  
00880                 if (j->second->GetKind() == BlackBoxDescriptor::STANDARD )
00881                   continue;
00882               }
00883             else 
00884               if (j->second->GetKind() != BlackBoxDescriptor::STANDARD )
00885                 continue;
00886 
00887             std::vector<std::string> keys;
00888             if (type==Packages)
00889               {
00890                 std::string k("");
00891                 k += pack->GetName();
00892                 keys.push_back(k);
00893                 title = "Boxes by package";
00894               }
00895             else if ((type==Initials) || (type==Adaptors))
00896               {
00897                 std::string init(" ");
00898                 init[0] =  std::toupper(j->second->GetTypeName()[0]);
00899                 keys.push_back(init);
00900                 title = "Alphabetical list";
00901               }
00902             else if (type==Categories)
00903               {
00904                 // Split the category string 
00905                 std::string delimiters = ";,";
00906                 Utilities::SplitString(j->second->GetCategory(),
00907                                        delimiters,keys);
00908                 if (keys.size()==0) 
00909                   keys.push_back(" NONE");
00910                 title = "Boxes by category";
00911               }
00912     
00913             
00914             std::vector<std::string>::const_iterator k;
00915             for (k=keys.begin(); k!=keys.end(); ++k )
00916               {
00917                 IndexType::iterator p;
00918                 p = index.find(*k);
00919                 if (p != index.end()) 
00920                   {
00921                     p->second.push_back(j->second);
00922                   }
00923                 else 
00924                   {
00925                     std::vector<BlackBoxDescriptor::Pointer> v;
00926                     v.push_back(j->second);
00927                     index[*k] = v;
00928                   }
00929               }
00930             
00931           }
00932       }   
00933     // Creates the file 
00934     //---------------------
00935     // Open output file
00936     std::ofstream s;
00937     s.open(filename.c_str());
00938     if (!s.good()) 
00939     {
00940        bbtkError("Factory::CreateHtmlIndex : could not open file '"
00941                  <<filename<<"'");
00942     }
00943     
00944     //----------------------
00945     // Html head
00946     s << "<html lang=\"en\">\n";
00947     s << "<head>\n";
00948     s << "<title>"<<title<<"</title>\n";
00949     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
00950     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
00951     s << "<meta name=\"generator\" content=\"\">\n";
00952     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
00953     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
00954     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
00955     s << "pre.display { font-family:inherit }\n";
00956     s << "pre.format  { font-family:inherit }\n";
00957     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
00958     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
00959     s << "pre.smallexample { font-size:smaller }\n";
00960     s << "pre.smalllisp    { font-size:smaller }\n";
00961     s << "span.sc    { font-variant:small-caps }\n";
00962     s << "span.roman { font-family:serif; font-weight:normal; } \n";
00963     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
00964     s << "--></style>\n";
00965     s << "</head>\n";
00966     //----------------------
00967 
00968     //----------------------
00969     // Html body
00970     s << "<body>\n";
00971     s << "<a name=\"Top\"></a>\n"; 
00972     s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
00973     s << "<p>\n";
00974     IndexType::iterator ii;
00975     for (ii=index.begin();ii!=index.end();++ii)
00976       {
00977         s << "<a href=\"#"<<ii->first<<"\">"<<ii->first<<"</a>&nbsp;&nbsp;";    
00978       }
00979 
00980     for (ii=index.begin();ii!=index.end();++ii)
00981       {
00982         s << "<p><hr>\n";
00983         s << "<p><a href=\"#Top\">Top</a>";
00984         if (type==Packages)
00985           {
00986             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
00987             s << "<p><a href=\""<<ii->first<<"/index.html\">"
00988               << ii->first<<"</a>\n"; 
00989 
00990             s << "&nbsp;&nbsp;-&nbsp;&nbsp;\n"; 
00991 
00992             s << "<a name=\"doxygen\"></a>\n"; 
00993             s << "<a href=..\\doxygen\\" << ii->first << "/main.html>(Doxygen documentation of the source)</a>\n"; 
00994           }
00995         else 
00996           {
00997             s << "<a name=\""<<ii->first<<"\"></a>\n"; 
00998             s << "<p><b>"<<ii->first<<"</b>\n";
00999           }
01000         s << "<ul>\n";
01001 
01002         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
01003 
01004         std::vector<BlackBoxDescriptor::Pointer>::iterator di;
01005         for (di=ii->second.begin();di!=ii->second.end();++di)
01006           {
01007             std::string pack = (*di)->GetPackage()->GetName();
01008             std::string name = (*di)->GetTypeName();
01009             Utilities::html_format(name);
01010             std::string descr = (*di)->GetDescription();
01011             Utilities::html_format(descr);
01012             s << "<TR>";
01013             s << "<TD style='vertical-align: top;'>";
01014             s << "&nbsp;&nbsp;&nbsp;<a href=\""<<pack
01015               <<"/index.html#"<<name<<"\">"
01016               <<pack<<"::"<<name<<"</a>";
01017             s << "</TD> ";
01018             s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
01019             s << "</TR>\n";
01020           }    
01021         s << "</TABLE>\n";
01022         s << "</ul>\n";
01023         s << "</div>\n";
01024       }
01025     //----------------------
01026     // Footer 
01027     time_t rawtime;
01028     tm * ptm;
01029     time ( &rawtime );
01030     ptm = gmtime ( &rawtime );
01031 
01032     s << "<p><hr>\n";
01033     s << "Automatically generated by <b>bbtk</b> on "
01034       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
01035       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
01036     s << "</body></html>\n"; 
01037     s.close();
01038     //----------------------
01039 
01040     // End
01041     bbtkDebugDecTab("Kernel",9);
01042   }
01043 
01044  //==========================================================================
01045   std::string Factory::GetObjectName() const
01046   {
01047     return std::string("Factory");
01048   }
01049   //==========================================================================
01050   
01051   //==========================================================================
01052   std::string  Factory::GetObjectInfo() const 
01053   {
01054     std::stringstream i;
01055     return i.str();
01056   }
01057   //==========================================================================
01058 
01059   //==========================================================================
01060 size_t  Factory::GetObjectSize() const 
01061 {
01062   size_t s = Superclass::GetObjectSize();
01063   s += Factory::GetObjectInternalSize();
01064   return s;
01065   }
01066   //==========================================================================
01067   //==========================================================================
01068 size_t  Factory::GetObjectInternalSize() const 
01069 {
01070   size_t s = sizeof(Factory);
01071   return s;
01072   }
01073   //==========================================================================
01074   //==========================================================================
01075   size_t  Factory::GetObjectRecursiveSize() const 
01076   {
01077     size_t s = Superclass::GetObjectRecursiveSize();
01078     s += Factory::GetObjectInternalSize();
01079 
01080     PackageMapType::const_iterator i;
01081     for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
01082     {
01083       s += i->second->GetObjectRecursiveSize();
01084     }
01085     return s;
01086   }
01087   //==========================================================================
01088 
01089 }
01090   

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6