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