00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00036 #include "bbtkPackage.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkConfigurationFile.h"
00039 #include <fstream>
00040 #include <time.h>
00041 #include "bbtkUtilities.h"
00042
00043 namespace bbtk
00044 {
00045
00046
00047
00048
00050 Package::Pointer Package::New(const std::string& name,
00051 const std::string& author,
00052 const std::string& description,
00053 const std::string& version,
00054 const std::string& BBTKVersion)
00055 {
00056 bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
00057 <<bbtkendl);
00058 Package::Pointer p = MakePointer(new Package(name,
00059 author,
00060 description,
00061 version,
00062 BBTKVersion));
00063 bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
00064 <<bbtkendl);
00065 return p;
00066 }
00067
00068
00069
00071 Package::Package(const std::string& name,
00072 const std::string& author,
00073 const std::string& description,
00074 const std::string& version,
00075 const std::string& BBTKVersion)
00076 :
00077 mDynamicLibraryHandler(0),
00078 mName(name),
00079 mAuthor(author),
00080 mDescription(description),
00081 mVersion(version)
00082 {
00083 bbtkDebugMessage("object",2,"==> Package::Package('"<<name<<"',...)"
00084 <<bbtkendl);
00085 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00086 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
00087 std::string url = default_doc_dir;
00088 if (c != '/' && c !='\\') url = url + "/";
00089 url = url + "temp_dir/" + name + "/index.html";
00090
00091 SetDocURL(url);
00092 SetDocRelativeURL("Relative url not set");
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
00106 <<bbtkendl);
00107
00108 }
00109
00110
00111
00112
00113
00115 Package::~Package()
00116 {
00117 bbtkDebugMessage("object",2,"==> Package::~Package(\""<<mName<<"\")"<<bbtkendl);
00118 bbtkDebugMessage("object",2,"<== Package::~Package(\""<<mName<<"\")"<<bbtkendl);
00119 }
00120
00121
00122
00123
00124 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
00125 const std::string& descname)
00126 {
00127
00128 std::string packname = pack.lock()->GetName();
00129
00130 bbtkDebugMessage("package",5,"--- Releasing descriptor '"
00131 <<packname<<"::"<<descname<<"'"<<bbtkendl);
00132
00133
00134 Package::BlackBoxMapType::iterator desc =
00135 pack.lock()->GetBlackBoxMap().find(descname);
00136 if (desc == pack.lock()->GetBlackBoxMap().end())
00137 {
00138 bbtkDebugMessage("package",5,
00139 " Descriptor has already been released"
00140 <<bbtkendl);
00141 return;
00142 }
00143
00144
00145 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
00146 desc->second.reset();
00147
00148 if (pdesc.expired())
00149 {
00150 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
00151 if (pack.expired())
00152 {
00153 bbtkDebugMessage("package",2,
00154 " ... and caused its package death"
00155 <<bbtkendl);
00156 return;
00157 }
00158 desc = pack.lock()->GetBlackBoxMap().find(descname);
00159 if (desc != pack.lock()->GetBlackBoxMap().end())
00160 pack.lock()->GetBlackBoxMap().erase(desc);
00161 }
00162 else
00163 {
00164 bbtkDebugMessage("package",5," ... Descriptor still alive ("
00165 <<pdesc.use_count()<<" refs)"
00166 <<bbtkendl);
00167 pack.lock()->GetBlackBoxMap()[descname] = pdesc.lock();
00168 }
00169 }
00170
00171
00172
00173
00175 void Package::Release(Package::WeakPointer pack)
00176 {
00177 std::string packname = pack.lock()->mName;
00178 bbtkDebugMessage("package",1,"==> Package::Release('"<<
00179 packname<<"')"<<bbtkendl);
00180
00181 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
00182 long ndesc = pack.lock()->GetBlackBoxMap().size();
00183 long nrefs = pack.use_count();
00184
00185 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
00186 <<ndesc<<" descr / dyn="
00187 <<dyn<<std::endl);
00188
00189
00190
00191
00192
00193
00194
00195
00196 if (nrefs == ndesc + dyn)
00197 {
00198 bbtkDebugMessage("package",5,
00199 " -> No more external ref : checking descriptors"
00200 <<bbtkendl);
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 std::vector<std::string> descnamelist;
00216 BlackBoxMapType::iterator i;
00217 for (i=pack.lock()->mBlackBoxMap.begin();
00218 i!= pack.lock()->mBlackBoxMap.end();
00219 ++i)
00220 descnamelist.push_back(i->first);
00221
00222
00223 std::vector<std::string>::iterator descname;
00224 for (descname=descnamelist.begin();
00225 descname!=descnamelist.end();
00226 ++descname)
00227 {
00228
00229 if (pack.expired())
00230 {
00231 bbtkDebugMessage("package",1,"--- Package::Release('"<<
00232 packname
00233 <<"') : package expired during release : bailing out"<<bbtkendl);
00234 break;
00235 }
00236
00237 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
00238
00239 }
00240
00241
00242 UnLoadDynamicLibrary(pack);
00243
00244 Package::UnLoadReleasedDynamicallyLoadedPackages();
00245
00246
00247
00248
00249 }
00250
00251 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
00252
00253 bbtkDebugMessage("package",2,"<== Package::Release('"<<
00254 packname<<"')"<<bbtkendl);
00255
00256 if (!pack.expired())
00257 {
00258 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
00259 long ndesc = pack.lock()->GetBlackBoxMap().size();
00260 long nrefs = pack.use_count();
00261
00262 bbtkDebugMessage("package",1," ... Package still alive ("
00263 <<nrefs<<" refs / "
00264 <<ndesc<<" descr / dyn="
00265 <<dyn<<")"<<std::endl);
00266 }
00267 else
00268 {
00269 bbtkDebugMessage("package",1," ... Package has been released"
00270 <<std::endl);
00271 }
00272 #endif
00273 }
00274
00275
00276
00282 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
00283 BlackBoxDescriptor::WeakPointer descr)
00284 {
00285 std::string packname = pack.lock()->mName;
00286 std::string dname = descr.lock()->GetTypeName();
00287 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
00288 packname<<"','"<<dname<<"') : refs="
00289 <<descr.use_count()<<bbtkendl);
00290
00291 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
00292 long ndesc = pack.lock()->GetBlackBoxMap().size();
00293 long nrefs = pack.use_count();
00294
00295 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
00296 <<ndesc<<" descr / dynamically loaded = "
00297 <<dyn<<std::endl);
00298
00299
00300
00301
00302
00303
00304
00305
00306 if (nrefs == ndesc + dyn)
00307 {
00308 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
00309 }
00310
00311
00312
00313 UnLoadDynamicLibrary(pack,false);
00314
00315 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
00316 packname<<"','"<<dname<<"'): refs="
00317 <<descr.use_count()<<bbtkendl);
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 }
00337
00338
00339
00347 DynamicLibraryHandler Package::OpenDynamicLibrary
00348 ( const std::string& libname,
00349 const std::string& package_name,
00350 DLGetPackageFunction& getpack,
00351 DLDeletePackageFunction& delpack)
00352 {
00353 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
00354 <<libname<<")"<<std::endl);
00355 #if defined(__GNUC__)
00356
00357
00358 void *handler;
00359 handler = dlopen(libname.c_str(),
00360 BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
00361 if (!handler)
00362 {
00363 bbtkMessage("package",2,
00364 "Could not open shared library [" <<libname<<"] : "
00365 <<dlerror() << std::endl);
00366 return 0;
00367 }
00368
00369 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
00370
00371
00372 std::string getvername(package_name);
00373 getvername +=
00374 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
00375 DLGetPackageBBTKVersionFunction getbbtkversion
00376 = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
00377 if (!getbbtkversion)
00378 {
00379 bbtkDebugMessage("package",3,"***"<<std::endl);
00380 bbtkMessage("package",2,
00381 "Shared library ["<<libname
00382 <<"] is not a valid bbtk package."
00383 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
00384 dlclose(handler);
00385 return 0;
00386 }
00387
00388 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
00389 <<"] found"<<std::endl);
00390
00391 if (getbbtkversion() != bbtk::GetVersion())
00392 {
00393 bbtkMessage("package",2,
00394 "Shared library ["<<libname
00395 <<"] was build with bbtk version "
00396 <<getbbtkversion()
00397 <<" but the current program runs with version "
00398 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
00399 dlclose(handler);
00400 return 0;
00401
00402 }
00403
00404 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
00405
00406 std::string getpackname(package_name);
00407 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
00408 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
00409 if (!getpack)
00410 {
00411 bbtkMessage("package",2,
00412 "Shared library ["<<libname
00413 <<"] is not a valid bbtk package."
00414 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
00415 dlclose(handler);
00416 return 0;
00417 }
00418
00419 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
00420
00421
00422 std::string delpackname(package_name);
00423 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
00424 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
00425 if (!delpack)
00426 {
00427 bbtkMessage("package",2,
00428 "Shared library ["<<libname
00429 <<"] is not a valid bbtk package."
00430 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
00431 dlclose(handler);
00432 return 0;
00433 }
00434 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
00435 #elif defined(_WIN32)
00436
00437 HINSTANCE handler;
00438
00439 SetErrorMode(0);
00440
00441 handler = LoadLibrary(libname.c_str());
00442 if (!handler)
00443 {
00444 bbtkMessage("package",2,
00445 "Could not open shared library [" <<libname<<"]"
00446 << std::endl);
00447 DWORD dwErrorCode = 0;
00448 dwErrorCode = GetLastError();
00449 bbtkMessage("package",2,
00450 "Windows Error: [" << dwErrorCode <<"]"
00451 << std::endl);
00452
00453 return 0;
00454 }
00455
00456
00457 std::string getvername(package_name);
00458 getvername +=
00459 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
00460 DLGetPackageBBTKVersionFunction getbbtkversion
00461 = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler,
00462 getvername.c_str()));
00463 if (!getbbtkversion)
00464 {
00465 FreeLibrary(handler);
00466 bbtkMessage("package",2,
00467 "Shared library ["<<libname
00468 <<"] is not a valid bbtk package."
00469 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
00470 return 0;
00471 }
00472
00473
00474 if (getbbtkversion() != bbtk::GetVersion())
00475 {
00476 FreeLibrary(handler);
00477 bbtkMessage("package",2,
00478 "Shared library ["<<libname
00479 <<"] was build with bbtk version "
00480 <<getbbtkversion()
00481 <<" but the current program runs with version "
00482 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
00483 return 0;
00484
00485 }
00486
00487
00488 std::string getpackname(package_name);
00489 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
00490 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
00491 if (!getpack)
00492 {
00493 FreeLibrary(handler);
00494 bbtkMessage("package",2,
00495 "Shared library ["<<libname
00496 <<"] is not a valid bbtk package."
00497 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
00498 return 0;
00499 }
00500
00501
00502 std::string delpackname(package_name);
00503 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
00504 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
00505 if (!delpack)
00506 {
00507 FreeLibrary(handler);
00508 bbtkMessage("package",2,
00509 "Shared library ["<<libname
00510 <<"] is not a valid bbtk package."
00511 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
00512 return 0;
00513 }
00514
00515 #else
00516 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
00517 #endif
00518
00519 return handler;
00520 }
00521
00522
00523
00525 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
00526 const std::string& pkgname,
00527 const std::string& path)
00528 {
00529 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
00530 <<libname<<")"<<std::endl);
00531
00532 DLGetPackageFunction gf;
00533 DLDeletePackageFunction df;
00534 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
00535 pkgname,
00536 gf,df);
00537 if (h==0) return Package::Pointer();
00538 Package::Pointer p = gf();
00539 p->mDynamicLibraryHandler = h;
00540 p->mDLDeletePackageFunction = df;
00541
00542 std::string separator =
00543 ConfigurationFile::GetInstance().Get_file_separator ();
00544
00545 std::string docreldoc =
00546 separator + "bbdoc" + separator + pkgname + separator + "index.html";
00547 std::string reldoc =
00548 ".." + separator + ".." + docreldoc;
00549 std::string doc = path + separator + ".." + separator
00550 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
00551 + docreldoc;
00552
00553 p->SetDocURL(doc);
00554 p->SetDocRelativeURL(reldoc);
00555
00556 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
00557 <<libname<<") .. OK"<<std::endl);
00558 return p;
00559 }
00560
00561
00562
00563
00564
00565
00567 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
00568 {
00569 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
00570 return;
00571
00572
00573 std::string packname = pack.lock()->GetName();
00574 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
00575 <<packname<<"')"
00576 <<std::endl);
00577
00578 if (!pack.lock()->GetBlackBoxMap().empty())
00579 {
00580
00581 bbtkDebugMessage("package",5," Package not empty ... abort"
00582 <<std::endl);
00583 return;
00584
00585
00586
00587
00588
00589
00590 }
00591
00592 if (pack.use_count()!=1)
00593 {
00594 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
00595 <<"empty dl package with external refs"
00596 <<BBTK_INTERNAL_ERROR_MESSAGE);
00597 }
00598
00599 if (doit)
00600 {
00601 UnLoad(pack);
00602 bbtkDebugMessage("package",5,"==> dynamic library for package '"
00603 <<packname<<"' closed"
00604 <<std::endl);
00605 }
00606 else
00607 {
00608 mReleasedDynamicallyLoadedPackages.insert(pack);
00609 bbtkDebugMessage("package",1,"==> package '"<<packname
00610 <<"' put in the 'to unload' list"
00611 <<std::endl);
00612
00613 }
00614
00615 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
00616 <<packname<<"')"
00617 <<std::endl);
00618
00619 }
00620
00621
00622
00625 void Package::UnLoadReleasedDynamicallyLoadedPackages()
00626 {
00627 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
00628
00629 std::set<Package::WeakPointer>::iterator i;
00630 for (i=mReleasedDynamicallyLoadedPackages.begin();
00631 i!=mReleasedDynamicallyLoadedPackages.end();
00632 ++i)
00633 {
00634 if (!i->expired()) UnLoad(*i);
00635 }
00636 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
00637 }
00638
00639
00640
00641 void Package::UnLoad(Package::WeakPointer pack)
00642 {
00643 std::string packname = pack.lock()->GetName();
00644 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
00645
00646 Package* p = pack.lock().get();
00647
00648 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
00649
00650
00651 p->mDLDeletePackageFunction();
00652
00653
00654 #if defined(__GNUC__)
00655 dlclose(h);
00656 #elif defined(_WIN32)
00657 FreeLibrary(h);
00658 #endif
00659
00660 bbtkDebugMessage("package",1,"==> dynamic library for package '"
00661 <<packname<<"' closed"
00662 <<std::endl);
00663 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
00664 }
00665
00666
00667
00669 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
00670 const std::string& name) const
00671 {
00672 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
00673
00674 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
00675 if (i == mBlackBoxMap.end())
00676 {
00677 bbtkDebugDecTab("Kernel",8);
00678 return BlackBox::Pointer();
00679 }
00680 BlackBox::Pointer bb =i->second->NewBlackBox(name);
00681 bbtkDebugDecTab("Kernel",8);
00682 return bb;
00683
00684 }
00685
00686
00687
00688
00689
00692 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
00693 const DataInfo& typeout,
00694 const std::string& name) const
00695 {
00696 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
00697 ">::NewAdaptor("
00698 <<typein<<","
00699 <<typeout<<",\""
00700 <<name<<"\")"<<bbtkendl);
00701
00702 AdaptorKey key(typein,typeout,
00703 BlackBoxDescriptor::DEFAULT_ADAPTOR);
00704 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
00705 if (i == mAdaptorMap.end())
00706 {
00707 bbtkDebugDecTab("Kernel",8);
00708 return BlackBox::Pointer();
00709 }
00710 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
00711 bbtkDebugDecTab("Kernel",8);
00712 return bb;
00713
00714 }
00715
00716
00717
00720 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
00721 const DataInfo& typeout,
00722 const std::string& name) const
00723 {
00724 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
00725 ">::NewWidgetAdaptor("
00726 <<typein<<","
00727 <<typeout<<",\""
00728 <<name<<"\")"<<bbtkendl);
00729
00730 AdaptorKey key(typein,typeout,
00731 BlackBoxDescriptor::DEFAULT_GUI);
00732 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
00733 if (i == mAdaptorMap.end())
00734 {
00735 bbtkDebugDecTab("Kernel",8);
00736 return BlackBox::Pointer();
00737 }
00738 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
00739 bbtkDebugDecTab("Kernel",8);
00740 return bb;
00741
00742 }
00743
00744
00745
00746
00747
00752 bool Package::FindWidgetAdaptor(const DataInfo& typein,
00753 const DataInfo& typeout,
00754 std::string& adaptor) const
00755 {
00756 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
00757 ">::FindWidgetAdaptor("
00758 <<typein<<","
00759 <<typeout<<")"<<bbtkendl);
00760
00761 AdaptorKey key(
00762 DataInfo(typeid(void),""),
00763 typeout,
00764 BlackBoxDescriptor::DEFAULT_GUI);
00765
00766 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
00767 if (i == mAdaptorMap.end())
00768 {
00769 bbtkDebugDecTab("Kernel",8);
00770 return false;
00771 }
00772 adaptor = i->second.lock()->GetTypeName();
00773 bbtkDebugDecTab("Kernel",8);
00774 return true;
00775
00776 }
00777
00778
00779
00780
00781
00786 bool Package::FindAdaptor(const DataInfo& typein,
00787 const DataInfo& typeout,
00788 std::string& adaptor) const
00789 {
00790 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
00791 ">::FindAdaptor("
00792 <<typein<<","
00793 <<typeout<<")"<<bbtkendl);
00794
00795 AdaptorKey key(typein,typeout,
00796 BlackBoxDescriptor::DEFAULT_ADAPTOR);
00797 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
00798 if (i == mAdaptorMap.end())
00799 {
00800 bbtkDebugDecTab("Kernel",8);
00801 return false;
00802 }
00803 adaptor = i->second.lock()->GetTypeName();
00804 bbtkDebugDecTab("Kernel",8);
00805 return true;
00806
00807 }
00808
00809
00810
00811
00813 bool Package::RegisterBlackBox(BlackBoxDescriptor::Pointer d)
00814 {
00815 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
00816
00817 BlackBoxMapType::iterator i = mBlackBoxMap.find(d->GetTypeName());
00818 if (i!=mBlackBoxMap.end())
00819 {
00820 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
00821 <<d->GetTypeName()<<"> which is already in the package");
00822 return false;
00823 }
00824
00825 mBlackBoxMap[d->GetTypeName()] = d;
00826
00827 d->SetPackage(GetThisPointer<Package>());
00828
00829
00830 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
00831 {
00832 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
00833
00834 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
00835 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
00836 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
00837 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
00838 AdaptorKey key(infoin,infoout,d->GetKind());
00839
00840 AdaptorMapType::const_iterator i;
00841 i = mAdaptorMap.find(key);
00842 if (i == mAdaptorMap.end())
00843 {
00844 mAdaptorMap[key] = d;
00845 }
00846
00847 else
00848 {
00849 if (i->second.lock()->GetTypeName() != d->GetTypeName())
00850 {
00851 bbtkError("Package <"<<GetName()<<
00852 "> : trying to register black box <"
00853 <<d->GetTypeName()
00854 <<"> as default adaptor but there is already a default adaptor registered (<"
00855 <<i->second.lock()->GetTypeName()<<">)");
00856 }
00857 }
00858 }
00859
00860 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
00861 {
00862 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
00863
00864 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
00865 DataInfo infoin(typeid(void),"");
00866 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
00867 AdaptorKey key(infoin,infoout,d->GetKind());
00868
00869 AdaptorMapType::const_iterator i;
00870 i = mAdaptorMap.find(key);
00871 if (i == mAdaptorMap.end())
00872 {
00873 mAdaptorMap[key] = d;
00874 }
00875
00876 else
00877 {
00878 if (i->second.lock()->GetTypeName() != d->GetTypeName())
00879 {
00880 bbtkError("Package <"<<GetName()<<
00881 "> : trying to register black box <"
00882 <<d->GetTypeName()
00883 <<"> as default widget adaptor but there is already a default adaptor registered (<"
00884 <<i->second.lock()->GetTypeName()<<">)");
00885 }
00886 }
00887 }
00888
00889
00890 bbtkDebugDecTab("Kernel",8);
00891
00892 return true;
00893 }
00894
00895
00896
00897 void Package::CheckBoxes() const
00898 {
00899 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
00900 <<" ["<<GetName()<<"]"<<std::endl);
00901 BlackBoxMapType::const_iterator i;
00902 for (i=mBlackBoxMap.begin();
00903 i!=mBlackBoxMap.end();
00904 ++i)
00905 {
00906 i->second->Check(true);
00907 }
00908 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
00909 <<" ["<<GetName()<<"] ... OK"<<std::endl);
00910 }
00911
00912
00913
00914
00916 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
00917 {
00918 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
00919
00920 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
00921 if (i == mBlackBoxMap.end())
00922 {
00923 bbtkDebugDecTab("Kernel",8);
00924 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
00925 }
00926
00927 i->second->SetTypeName(newname);
00928 mBlackBoxMap[newname] = i->second;
00929 mBlackBoxMap.erase(i);
00930
00931 bbtkDebugDecTab("Kernel",8);
00932 }
00933
00934
00935
00936
00937
00939 void Package::PrintBlackBoxes(bool description, bool adaptors) const
00940 {
00941 unsigned int lmax = 0;
00942 std::vector<std::string> names;
00943 std::vector<std::string> kinds;
00944 std::vector<std::string> descrs;
00945
00946 BlackBoxMapType::const_iterator i;
00947 for (i=mBlackBoxMap.begin();
00948 i!=mBlackBoxMap.end();
00949 ++i)
00950 {
00951 if ( adaptors ||
00952 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
00953 {
00954 std::string name(" ");
00955 name += i->second->GetTypeName();
00956 names.push_back(name);
00957
00958 std::string kind;
00959 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
00960 {
00961 kind = std::string("[A]");
00962 }
00963 else if ( i->second->GetKind() ==
00964 BlackBoxDescriptor::DEFAULT_ADAPTOR )
00965 {
00966 kind = std::string("[DA]");
00967 }
00968 kinds.push_back(kind);
00969
00970 unsigned int l = name.size()+kind.size();
00971 if (l>lmax) lmax = l;
00972
00973 std::string descr;
00974 if (description)
00975 {
00976 descr += " : ";
00977 descr += i->second->GetDescription();
00978 }
00979 descrs.push_back(descr);
00980 }
00981 }
00982
00983
00984 std::string offs;
00985 offs.append(lmax+3,' ');
00986 std::vector<std::string>::iterator ni,ci,di;
00987 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
00988 ni != names.end(); ++ni, ++ci, ++di)
00989 {
00990 std::string space;
00991 space.append(lmax - ni->size() - ci->size(),' ');
00992 bbtkMessage("Help",1,*ni << space << *ci );
00993 std::string d(*di);
00994 unsigned int dmax = 75 - lmax;
00995
00996
00997 if (d.size()>dmax)
00998 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
00999 else
01000 bbtkMessage("Help",1,d << std::endl);
01001
01002
01003 }
01004
01005 }
01006
01007
01008
01010 void Package::PrintAdaptors(bool description) const
01011 {
01012 BlackBoxMapType::const_iterator i;
01013 for (i=mBlackBoxMap.begin();
01014 i!=mBlackBoxMap.end();
01015 ++i)
01016 {
01017 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
01018 {
01019 bbtkMessage("Help",1,
01020 " "<<i->second->GetTypeName());
01021 if ( i->second->GetKind() ==
01022 BlackBoxDescriptor::DEFAULT_ADAPTOR )
01023 {
01024 bbtkMessage("Help",1,
01025 " [default]");
01026 }
01027 if (description)
01028 {
01029 bbtkMessage("Help",1,
01030 " : "<<i->second->GetDescription());
01031
01032 }
01033 bbtkMessage("Help",1,std::endl);
01034 }
01035 }
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053 }
01054
01055
01056
01058 void Package::HelpBlackBox(const std::string& name, bool full) const
01059 {
01060 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
01061 <<name<<"\")"<<bbtkendl);
01062
01063 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
01064 if (i == mBlackBoxMap.end())
01065 {
01066 bbtkDebugDecTab("Kernel",8);
01067 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
01068 }
01069
01070 i->second->GetHelp(full);
01071 bbtkDebugDecTab("Kernel",8);
01072
01073 }
01074
01075
01076
01077
01079 bool Package::ContainsBlackBox(const std::string& name) const
01080 {
01081 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
01082 <<name<<"\")"<<bbtkendl);
01083
01084 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
01085 if (i == mBlackBoxMap.end())
01086 {
01087 bbtkDebugDecTab("Kernel",8);
01088 return false;
01089 }
01090 bbtkDebugDecTab("Kernel",8);
01091 return true;
01092 }
01093
01094
01095
01096
01097
01098 void Package::CreateHtmlPage(const std::string& filename,
01099 const std::string& caller,
01100 const std::string& source,
01101 const std::string& custom_header,
01102 const std::string& custom_title,
01103 int detail,
01104 int level,
01105 bool relative_link ) const
01106 {
01107 bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
01108 <<filename<<"\")"<<bbtkendl);
01109
01110
01111
01112 std::ofstream s;
01113 s.open(filename.c_str());
01114 if (!s.good())
01115 {
01116 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
01117 }
01118
01119
01120
01121 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
01122
01123 if (custom_title.length() != 0) title = custom_title;
01124
01125 s << "<html lang=\"en\">\n";
01126 s << "<head>\n";
01127 s << "<title>" << title << "</title>\n";
01128 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
01129 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
01130 s << "<meta name=\"generator\" content=\"\">\n";
01131 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
01132
01133 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
01134 s << "pre.display { font-family:inherit }\n";
01135 s << "pre.format { font-family:inherit }\n";
01136 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
01137 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
01138 s << "pre.smallexample { font-size:smaller }\n";
01139 s << "pre.smalllisp { font-size:smaller }\n";
01140 s << "span.sc { font-variant:small-caps }\n";
01141 s << "span.roman { font-family:serif; font-weight:normal; } \n";
01142 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
01143 s << "--></style>\n";
01144 s << "</head>\n";
01145
01146
01147
01148
01149 s << "<body>\n";
01150 s << "<a name=\"Top\"></a>\n";
01151
01152
01153
01154 if ( custom_header.length() != 0)
01155 {
01156 if ( custom_header != "none" )
01157 {
01158 std::ifstream in;
01159 in.open(custom_header.c_str());
01160 if (!in.good())
01161 {
01162 bbtkError("Could not open file \""<<custom_header<<"\"");
01163 }
01164 char buffer[512];
01165 while (!in.eof())
01166 {
01167 in.getline(buffer,512);
01168 std::string line(buffer);
01169 s << line;
01170 }
01171 in.close();
01172 s << "<hr>\n";
01173
01174
01175
01176
01177
01178
01179
01180
01181 }
01182 }
01183
01184 else
01185 {
01186 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
01187 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
01188 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
01189 << GetDescription() << "</TD></TR>\n";
01190 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
01191 << GetAuthor() << "</TD></TR>\n";
01192 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
01193 << GetCategory() << "</TD></TR>\n";
01194 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
01195 << GetVersion() << "</TD></TR>\n";
01196 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
01197 << bbtk::GetVersion() << "</TD></TR>\n";
01198 s << "</TABLE>\n";
01199 }
01200
01201
01202
01203
01204
01205 s << "<p><b> Black Boxes : </b>\n";
01206 s << "<ul>\n";
01207
01208 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
01209
01210 BlackBoxMapType::const_iterator i;
01211 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
01212 {
01213 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
01214 continue;
01215
01216 std::string name = i->second->GetTypeName();
01217 Utilities::html_format(name);
01218 std::string descr = i->second->GetDescription();
01219
01220
01221 s << "<TR>";
01222 s << "<TD style='vertical-align: top;'>";
01223 s << " <a name=\"toc_"<<name
01224 <<"\" href=\"#"<<name<<"\">"
01225 <<name<<"</a>";
01226 s << "</TD> ";
01227 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
01228 s << "</TR>\n";
01229 }
01230 s << "</TABLE>\n";
01231
01232
01233 s << "</ul>\n";
01234 s << "</div>\n";
01235
01236
01237
01238 if (mAdaptorMap.size()>0)
01239 {
01240
01241 s << "<p><b> Adaptors : </b>\n";
01242 s << "<ul>\n";
01243
01244
01245 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
01246 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
01247 {
01248 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
01249 continue;
01250
01251 std::string name = i->second->GetTypeName();
01252 Utilities::html_format(name);
01253 std::string descr = i->second->GetDescription();
01254
01255 s << "<TR>";
01256 s << "<TD style='vertical-align: top;'>";
01257 s << " <a name=\"toc_"<<name
01258 <<"\" href=\"#"<<name<<"\">"
01259 <<name<<"</a>";
01260 s << "</TD> ";
01261 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
01262 s << "</TR>\n";
01263 }
01264 s << "</TABLE>\n";
01265
01266 s << "</ul>\n";
01267 s << "</div>\n";
01268 }
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287 std::string dir;
01288
01289 std::string::size_type slash_position = filename.find_last_of("/\\");
01290
01291
01292 if (slash_position != std::string::npos) {
01293 if (slash_position == 0)
01294 slash_position = 1;
01295 dir = filename.substr(0,slash_position);
01296 }
01297
01298 for (i=mBlackBoxMap.begin();
01299 i!=mBlackBoxMap.end();
01300 ++i)
01301 {
01302 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
01303 }
01304
01305
01306
01307 time_t rawtime;
01308 tm * ptm;
01309 time ( &rawtime );
01310 ptm = gmtime ( &rawtime );
01311
01312 s << "<p><hr>\n";
01313 s << "Automatically generated by <b>"<<caller<<"</b> "
01314
01315 <<"on "
01316 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
01317 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
01318 s << "</body></html>\n";
01319 s.close();
01320
01321
01322
01323 bbtkDebugDecTab("Kernel",9);
01324 }
01325
01326
01327
01328 std::string Package::GetObjectName() const
01329 {
01330 return std::string("Package '")+mName+std::string("'");
01331 }
01332
01333
01334
01335 std::string Package::GetObjectInfo() const
01336 {
01337 std::stringstream i;
01338 i << " - "<<mBlackBoxMap.size() << " boxes" << std::endl;
01339 if (mDynamicLibraryHandler)
01340 {
01341 i<< " - Loaded from dynamic library"<<std::endl;
01342 }
01343 return i.str();
01344 }
01345
01346
01347
01348
01349 size_t Package::GetObjectSize() const
01350 {
01351 size_t s = Superclass::GetObjectSize();
01352 s += Package::GetObjectInternalSize();
01353 return s;
01354 }
01355
01356
01357 size_t Package::GetObjectInternalSize() const
01358 {
01359 size_t s = sizeof(Package);
01360 return s;
01361 }
01362
01363
01364 size_t Package::GetObjectRecursiveSize() const
01365 {
01366 size_t s = Superclass::GetObjectRecursiveSize();
01367 s += Package::GetObjectInternalSize();
01368
01369 BlackBoxMapType::const_iterator i;
01370 for (i = mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i )
01371 {
01372 s += i->second->GetObjectRecursiveSize();
01373 }
01374 return s;
01375 }
01376
01377
01378
01379 std::set<Package::WeakPointer>
01380 Package::mReleasedDynamicallyLoadedPackages;
01381
01382 }
01383