bbtkPackage.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkPackage.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:14 $
00006   Version:   $Revision: 1.11 $
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 
00040 #ifndef __bbtkPackage_h__
00041 #define __bbtkPackage_h__
00042 
00043 #include "bbtkBlackBox.h"
00044 #include "bbtkDynamicLibraryHandling.h"
00045 
00046 namespace bbtk
00047 {
00048 
00049   class Factory;
00050   BBTK_FORWARD_DECLARE_POINTER(Factory);
00051 
00052   class BBTK_EXPORT Package : public Object
00053   {
00054     BBTK_OBJECT_INTERFACE(Package);
00055     typedef Object Superclass;
00056   public:
00058     static Pointer New(const std::string& name,
00059                        const std::string& author,
00060                        const std::string& description,
00061                        const std::string& version,
00062                        const std::string& BBTKVersion);
00064     static Pointer CreateFromDynamicLibrary(const std::string& libname,
00065                                             const std::string& pkgname,
00066                                             const std::string& path);
00067 
00076     static void UnLoadDynamicLibrary(Package::WeakPointer p, bool doit = true);
00077 
00080     static void UnLoadReleasedDynamicallyLoadedPackages();
00081 
00087     static void Release(Package::WeakPointer p);
00094     static void ReleaseBlackBoxDescriptor(Package::WeakPointer p,
00095                                           BlackBoxDescriptor::WeakPointer d);
00096     
00097     
00098     typedef Package::Pointer (*DLGetPackageFunction)();
00099     typedef void (*DLDeletePackageFunction)();
00100     typedef const std::string& (*DLGetPackageBBTKVersionFunction)();
00101 
00109     static DynamicLibraryHandler OpenDynamicLibrary
00110     ( const std::string& dynamic_library_path,
00111       const std::string& package_name,
00112       DLGetPackageFunction&,
00113       DLDeletePackageFunction&);
00114 
00115  
00117     const std::string& GetName() const { return mName; }
00118 
00120     const std::string& GetAuthor() const { return mAuthor; }
00121 
00123     const std::string& GetCategory() const { return mCategory; }
00124 
00126     const std::string& GetDescription() const { return mDescription; }
00127 
00129     const std::string& GetVersion() const { return mVersion; }
00130 
00131     bool ContainsBlackBox(const std::string& boxname) const;
00132 
00133  
00134     BlackBox::Pointer NewBlackBox(const std::string& type,
00135                              const std::string& name) const;
00136   
00137     BlackBox::Pointer NewAdaptor(const DataInfo& typein,
00138                          const DataInfo& typeout,
00139                          const std::string& name) const;
00140     
00141     BlackBox::Pointer NewWidgetAdaptor(const DataInfo& typein,
00142                               const DataInfo& typeout,
00143                               const std::string& name) const;
00144     bool FindAdaptor(const DataInfo& typein,
00145                            const DataInfo& typeout,
00146                            std::string& adaptor) const;
00147     bool FindWidgetAdaptor(const DataInfo& typein,
00148                            const DataInfo& typeout,
00149                            std::string& adaptor) const;
00150 
00151     bool RegisterBlackBox(BlackBoxDescriptor::Pointer); 
00152 
00153     void PrintBlackBoxes(bool description = false, 
00154                          bool adaptors = false) const;
00155     void PrintAdaptors(bool description = false) const;
00156     void HelpBlackBox(const std::string& name, bool full=true) const;
00157     
00158     void CreateHtmlPage(const std::string& filename,
00159                         const std::string& caller = "?",
00160                         const std::string& source = "?",
00161                         const std::string& custom_header = "",
00162                         const std::string& custom_title = "",
00163                         int detail = 1, 
00164                         int level = 0,
00165                         bool relative_link = false ) const;
00166 
00167     void  SetDocURL(std::string url){ mDocURL=url; }
00168     const std::string& GetDocURL() const { return mDocURL; }
00169     
00170     void  SetDocRelativeURL(std::string url){ mDocRelativeURL=url; }
00171     const std::string& GetDocRelativeURL() const { return mDocRelativeURL; }
00172 
00173 
00174     unsigned int GetNumberOfBlackBoxes() const { return mBlackBoxMap.size(); }
00175     
00177     void ChangeBlackBoxName( const std::string& oldname, 
00178                              const std::string& newname );
00180     typedef std::map< std::string, BlackBoxDescriptor::Pointer> 
00181     BlackBoxMapType;
00182     const BlackBoxMapType& GetBlackBoxMap() const { return mBlackBoxMap; }
00183     BlackBoxMapType& GetBlackBoxMap() { return mBlackBoxMap; }
00184 
00186     class AdaptorKey 
00187     {
00188     public:
00189       AdaptorKey( const DataInfo& typein, const DataInfo& typeout, 
00190                   BlackBoxDescriptor::Kind kind ) 
00191         : mTypeIn(typein), mTypeOut(typeout), mKind(kind) {}
00192       
00193       bool operator< ( const AdaptorKey& k ) const
00194       {
00195         return ( ( mKind < k.mKind ) ||
00196                  ( ( mKind == k.mKind ) &&
00197                    ( ( mTypeIn < k.mTypeIn ) ||
00198                      ( ( mTypeIn == k.mTypeIn ) && 
00199                        ( mTypeOut < k.mTypeOut ) ) ) ) );
00200       }
00201       
00202       bool operator== ( const AdaptorKey& k ) const
00203       {
00204         return ( ( mKind == k.mKind ) && 
00205                  ( mTypeIn == k.mTypeIn ) && 
00206                  ( mTypeOut == k.mTypeOut ) );
00207       }
00208       DataInfo mTypeIn;
00209       DataInfo mTypeOut; 
00210       BlackBoxDescriptor::Kind mKind;
00211     };
00212     
00214     typedef std::map< AdaptorKey, BlackBoxDescriptor::WeakPointer> AdaptorMapType;
00215 
00216  
00217    const AdaptorMapType& GetAdaptorMap() const { return mAdaptorMap; }
00218 
00219     
00220     // Factories management
00222     void AddFactory(FactoryPointer f) { mFactorySet.insert(f); }
00224     void RemoveFactory(FactoryPointer f) { mFactorySet.erase(f); }
00225 
00226     
00227     typedef std::set<FactoryWeakPointer> FactorySet;
00229     FactorySet& GetFactorySet() { return mFactorySet; }
00231     const FactorySet& GetFactorySet() const { return mFactorySet; }
00232     
00233     void CheckBoxes() const;
00234 
00235   private:
00237     //    Package() {}
00239     //    Package(const Package&) {}
00241     Package(const std::string& name,
00242             const std::string& author,
00243             const std::string& description,
00244             const std::string& version,
00245             const std::string& BBTKVersion);
00247     static void UnLoad(Package::WeakPointer p);
00248 
00250     DynamicLibraryHandler mDynamicLibraryHandler;
00253     DLDeletePackageFunction mDLDeletePackageFunction;
00254 
00255 
00257     std::string mName;
00259     std::string mAuthor;
00261     std::string mCategory;    
00263     std::string mDescription;
00265     std::string mVersion;
00267     std::string mDocURL;
00270     std::string mDocRelativeURL;
00271 
00273     BlackBoxMapType mBlackBoxMap;
00274 
00276     AdaptorMapType mAdaptorMap;
00277 
00278 
00280     FactorySet mFactorySet;
00281 
00285     static std::set<Package::WeakPointer> 
00286     mReleasedDynamicallyLoadedPackages;
00287   };
00288   // EO class Package
00289   //====================================================================
00290 
00291 
00292 //====================================================================
00293 #if defined(_WIN32)
00294   #define BBTK_PACKAGE_EXPORT __declspec( dllexport )
00295 #else
00296   #define BBTK_PACKAGE_EXPORT
00297 #endif // defined(_WIN32) 
00298 //====================================================================
00299 
00300 #define BBTK_GET_PACKAGE_FUNCTION_NAME GetPackage
00301 #define BBTK_DEL_PACKAGE_FUNCTION_NAME DeletePackage
00302 #define BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME GetPackageBBTKVersion
00303   
00304 
00305 
00306 //====================================================================
00307 #define BBTK_DECLARE_PACKAGE(NAME)                                      \
00308   extern "C"                                                            \
00309   {                                                                     \
00310     bbtk::Package::Pointer& NAME ## GetPackagePointer();                \
00311     BBTK_PACKAGE_EXPORT                                                 \
00312     void BBTK_CDECL NAME ## DeletePackage ();           \
00313     BBTK_PACKAGE_EXPORT bbtk::Package::Pointer                          \
00314     BBTK_CDECL NAME ## GetPackage ();           \
00315     BBTK_PACKAGE_EXPORT const std::string&                              \
00316     BBTK_CDECL NAME ## GetPackageBBTKVersion ();        \
00317   }
00318 //==================================================================== 
00319 
00320 //==================================================================== 
00321 #define BBTK_IMPLEMENT_PACKAGE(NAME,AUTHOR,DESCRIPTION,VERSION)         \
00322   extern "C"                                                            \
00323   {                                                                     \
00324     bbtk::Package::Pointer& NAME ## GetPackagePointer()                 \
00325     {                                                                   \
00326       static bbtk::Package::Pointer u;                                  \
00327       return u;                                                         \
00328     }                                                                   \
00329     BBTK_PACKAGE_EXPORT                                                 \
00330     void BBTK_CDECL NAME ## DeletePackage ()                            \
00331     {                                                                   \
00332       NAME ## GetPackagePointer().reset();                              \
00333     }                                                                   \
00334     BBTK_PACKAGE_EXPORT                                                 \
00335     bbtk::Package::Pointer                                              \
00336     BBTK_CDECL NAME ## GetPackage()             \
00337     {                                                                   \
00338       if (!NAME ## GetPackagePointer())                                 \
00339         NAME ## GetPackagePointer() =                                   \
00340           bbtk::Package::New(#NAME,                                     \
00341                              AUTHOR,                                    \
00342                              DESCRIPTION,                               \
00343                              VERSION,                                   \
00344                              BBTK_STRINGIFY_SYMBOL(BBTK_VERSION)        \
00345                              );                                         \
00346       bbtk::Object::InsertInPackageList( NAME ## GetPackagePointer() ); \
00347       return NAME ## GetPackagePointer();                               \
00348     }                                                                   \
00349     BBTK_PACKAGE_EXPORT const std::string&                              \
00350     BBTK_CDECL NAME ## GetPackageBBTKVersion ()                         \
00351     { return bbtk::GetVersion(); }                                      \
00352     class NAME ## PackageAutodestructor                                 \
00353     {                                                                   \
00354     public:                                                             \
00355       NAME ## PackageAutodestructor() {}                                \
00356       ~NAME ## PackageAutodestructor()                                  \
00357       {                                                                 \
00358         if (NAME ## GetPackagePointer().use_count()>0)                  \
00359           {                                                             \
00360             bbtk::Package::WeakPointer p = NAME ## GetPackagePointer(); \
00361             bbtk::Package::Release(p);                                  \
00362           }                                                             \
00363       }                                                                 \
00364     };                                                                  \
00365     NAME ## PackageAutodestructor NAME ## PackageAutodestructorInstance; \
00366   }
00367 //====================================================================  
00368 
00369 //====================================================================
00370 #define BBTK_ADD_BLACK_BOX_TO_PACKAGE(NAME,CLASS)                       \
00371   bool bbDummy##NAME##CLASS = NAME ## GetPackage ()     \
00372     ->RegisterBlackBox(CLASS ## Descriptor::Instance());
00373   //====================================================================
00374   
00375   //====================================================================
00376 #define BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(NAME,CLASS,TEMPLATE_PARAM) \
00377   bool bbDummy##NAME##CLASS##TEMPLATE_PARAM = NAME ## GetPackage ()     \
00378     ->RegisterBlackBox(CLASS ## Descriptor <TEMPLATE_PARAM>::Instance());
00379   //====================================================================
00380   
00381   //====================================================================
00382 #define BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(NAME,CLASS,T1,T2)       \
00383   bool bbDummy##NAME##CLASS##T1##T2 = NAME ## GetPackage ()             \
00384     ->RegisterBlackBox(CLASS ## Descriptor <T1,T2>::Instance()); 
00385   //====================================================================
00386   
00387 
00388 }// namespace bbtk
00389 
00390 
00391 
00392 #endif
00393 

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