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: 2010/09/12 14:52:25 $
00006   Version:   $Revision: 1.16 $
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);
00063     static Pointer CreateFromDynamicLibrary(const std::string& libname,
00064                                             const std::string& pkgname,
00065                                             const std::string& path);
00066 
00073 
00082     static void UnLoadDynamicLibrary(Package::WeakPointer p, bool doit = true);
00083 
00086     static void UnLoadReleasedDynamicallyLoadedPackages();
00087 
00094     static void Release(Package::WeakPointer p);
00095 
00096 
00098     bool Register(BlackBoxDescriptor::Pointer); 
00099 
00106     static void ReleaseBlackBoxDescriptor(Package::WeakPointer p,
00107                                           BlackBoxDescriptor::WeakPointer d);
00108     
00109     
00110     typedef Package::Pointer (*DLGetPackageFunction)();
00111     typedef void (*DLDeletePackageFunction)();
00112     typedef const std::string& (*DLGetPackageBBTKVersionFunction)();
00113 
00121     static DynamicLibraryHandler OpenDynamicLibrary
00122     ( const std::string& dynamic_library_path,
00123       const std::string& package_name,
00124       DLGetPackageFunction&,
00125       DLDeletePackageFunction&);
00126 
00127  
00129     const std::string& GetName() const { return mName; }
00130 
00132     const std::string& GetAuthor() const { return mAuthor; }
00133 
00135     const std::string& GetCategory() const { return mCategory; }
00136 
00138     const std::string& GetDescription() const { return mDescription; }
00139 
00141     const std::string& GetVersion() const { return mVersion; }
00142 
00145     bool ContainsDescriptor(const std::string& name) const;
00146 
00148     BlackBox::Pointer NewBlackBox(const std::string& type,
00149                              const std::string& name) const;
00150     
00153     BlackBox::Pointer NewAdaptor(const DataInfo& typein,
00154                                  const DataInfo& typeout,
00155                                  const std::string& name) const;
00156     
00160     BlackBox::Pointer NewWidgetAdaptor(const DataInfo& typein,
00161                                        const DataInfo& typeout,
00162                                        const std::string& name) const;
00163     bool FindAdaptor(const DataInfo& typein,
00164                      const DataInfo& typeout,
00165                      std::string& adaptor) const;
00166     bool FindWidgetAdaptor(const DataInfo& typein,
00167                            const DataInfo& typeout,
00168                            std::string& adaptor) const;
00169 
00170 
00172     void PrintHelpListDescriptors(bool description = false, 
00173                               bool adaptors = false) const;
00175     void PrintHelpListAdaptors(bool description = false) const;
00177     void PrintHelpDescriptor(const std::string& name, bool full=true) const;
00178     
00179 
00180 
00181     void CreateHtmlPage(const std::string& filename,
00182                         const std::string& caller = "?",
00183                         const std::string& source = "?",
00184                         const std::string& custom_header = "",
00185                         const std::string& custom_title = "",
00186                         int detail = 1, 
00187                         int level = 0,
00188                         bool relative_link = false ) const;
00189 
00190     void  SetDocURL(std::string url){ mDocURL=url; }
00191     const std::string& GetDocURL() const { return mDocURL; }
00192     
00193     void  SetDocRelativeURL(std::string url){ mDocRelativeURL=url; }
00194     const std::string& GetDocRelativeURL() const { return mDocRelativeURL; }
00195 
00196 
00197     unsigned int GetNumberOfDescriptors() const { return mDescriptorMap.size(); }
00198     
00200     void ChangeDescriptorName( const std::string& oldname, 
00201                                const std::string& newname );
00203     typedef std::map< std::string, BlackBoxDescriptor::Pointer> 
00204     DescriptorMapType;
00205     const DescriptorMapType& GetDescriptorMap() const { return mDescriptorMap; }
00206     DescriptorMapType& GetDescriptorMap() { return mDescriptorMap; }
00207 
00209     class AdaptorKey 
00210     {
00211     public:
00212       AdaptorKey( const DataInfo& typein, const DataInfo& typeout, 
00213                   BlackBoxDescriptor::Kind kind ) 
00214         : mTypeIn(typein), mTypeOut(typeout), mKind(kind) {}
00215       
00216       bool operator< ( const AdaptorKey& k ) const
00217       {
00218         return ( ( mKind < k.mKind ) ||
00219                  ( ( mKind == k.mKind ) &&
00220                    ( ( mTypeIn < k.mTypeIn ) ||
00221                      ( ( mTypeIn == k.mTypeIn ) && 
00222                        ( mTypeOut < k.mTypeOut ) ) ) ) );
00223       }
00224       
00225       bool operator== ( const AdaptorKey& k ) const
00226       {
00227         return ( ( mKind == k.mKind ) && 
00228                  ( mTypeIn == k.mTypeIn ) && 
00229                  ( mTypeOut == k.mTypeOut ) );
00230       }
00231       DataInfo mTypeIn;
00232       DataInfo mTypeOut; 
00233       BlackBoxDescriptor::Kind mKind;
00234     };
00235     
00237     typedef std::map< AdaptorKey, BlackBoxDescriptor::WeakPointer> AdaptorMapType;
00238 
00239  
00240    const AdaptorMapType& GetAdaptorMap() const { return mAdaptorMap; }
00241 
00242     
00243     // Factories management
00245     void AddFactory(FactoryPointer f) { mFactorySet.insert(f); }
00247     void RemoveFactory(FactoryPointer f) { mFactorySet.erase(f); }
00248 
00249     
00250     typedef std::set<FactoryWeakPointer> FactorySet;
00252     FactorySet& GetFactorySet() { return mFactorySet; }
00254     const FactorySet& GetFactorySet() const { return mFactorySet; }
00255     
00256     void Check() const;
00257           bool ifBoxExist( std::string boxType );
00258 
00259           
00260   private:
00262     //    Package() {}
00264     //    Package(const Package&) {}
00266     Package(const std::string& name,
00267             const std::string& author,
00268             const std::string& description,
00269             const std::string& version);
00271     static void UnLoad(Package::WeakPointer p);
00272 
00274     DynamicLibraryHandler mDynamicLibraryHandler;
00277     DLDeletePackageFunction mDLDeletePackageFunction;
00278 
00279 
00281     std::string mName;
00283     std::string mAuthor;
00285     std::string mCategory;    
00287     std::string mDescription;
00289     std::string mVersion;
00291     std::string mDocURL;
00294     std::string mDocRelativeURL;
00295 
00297     DescriptorMapType mDescriptorMap;
00298 
00300     AdaptorMapType mAdaptorMap;
00301 
00302 
00304     FactorySet mFactorySet;
00305 
00309     static std::set<Package::WeakPointer> 
00310     mReleasedDynamicallyLoadedPackages;
00311   };
00312   // EO class Package
00313   //====================================================================
00314 
00315 
00316 //====================================================================
00317 #if defined(_WIN32)
00318   #define BBTK_PACKAGE_EXPORT __declspec( dllexport )
00319 #else
00320   #define BBTK_PACKAGE_EXPORT
00321 #endif // defined(_WIN32) 
00322 //====================================================================
00323 
00324 #define BBTK_GET_PACKAGE_FUNCTION_NAME GetPackage
00325 #define BBTK_DEL_PACKAGE_FUNCTION_NAME DeletePackage
00326 #define BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME GetPackageBBTKVersion
00327   
00328 
00329 
00330 //====================================================================
00331 #define BBTK_DECLARE_PACKAGE(NAME)                                      \
00332   extern "C"                                                            \
00333   {                                                                     \
00334     bbtk::Package::Pointer& NAME ## GetPackagePointer();                \
00335     BBTK_PACKAGE_EXPORT                                                 \
00336     void BBTK_CDECL NAME ## DeletePackage ();           \
00337     BBTK_PACKAGE_EXPORT bbtk::Package::Pointer                          \
00338     BBTK_CDECL NAME ## GetPackage ();           \
00339     BBTK_PACKAGE_EXPORT const std::string&                              \
00340     BBTK_CDECL NAME ## GetPackageBBTKVersion ();        \
00341   }
00342 //==================================================================== 
00343 
00344 //==================================================================== 
00345 #define BBTK_IMPLEMENT_PACKAGE(NAME,AUTHOR,DESCRIPTION,VERSION)         \
00346   extern "C"                                                            \
00347   {                                                                     \
00348     bbtk::Package::Pointer& NAME ## GetPackagePointer()                 \
00349     {                                                                   \
00350       static bbtk::Package::Pointer u;                                  \
00351       return u;                                                         \
00352     }                                                                   \
00353     BBTK_PACKAGE_EXPORT                                                 \
00354     void BBTK_CDECL NAME ## DeletePackage ()                            \
00355     {                                                                   \
00356       NAME ## GetPackagePointer().reset();                              \
00357     }                                                                   \
00358     BBTK_PACKAGE_EXPORT                                                 \
00359     bbtk::Package::Pointer                                              \
00360     BBTK_CDECL NAME ## GetPackage()             \
00361     {                                                                   \
00362         if (!NAME ## GetPackagePointer())               {                       \
00363           NAME ## GetPackagePointer() =                                 \
00364               bbtk::Package::New(#NAME,                                 \
00365                              AUTHOR,                                    \
00366                              DESCRIPTION,                               \
00367                              VERSION    \
00368                              );                                         \
00369            bbtk::Object::InsertInPackageList( NAME ## GetPackagePointer() );  \
00370          }                                              \
00371       return NAME ## GetPackagePointer();                               \
00372     }                                                                   \
00373     BBTK_PACKAGE_EXPORT const std::string&                              \
00374     BBTK_CDECL NAME ## GetPackageBBTKVersion ()                         \
00375     { return bbtk::GetVersion(); }                                      \
00376     class NAME ## PackageAutodestructor                                 \
00377     {                                                                   \
00378     public:                                                             \
00379       NAME ## PackageAutodestructor() {}                                \
00380       ~NAME ## PackageAutodestructor()                                  \
00381       {                                                                 \
00382         if (NAME ## GetPackagePointer().use_count()>0)                  \
00383           {                                                             \
00384             bbtk::Package::WeakPointer p = NAME ## GetPackagePointer(); \
00385             bbtk::Package::Release(p);                                  \
00386           }                                                             \
00387       }                                                                 \
00388     };                                                                  \
00389     NAME ## PackageAutodestructor NAME ## PackageAutodestructorInstance; \
00390   }
00391 //====================================================================  
00392 
00393 //====================================================================
00394 #define BBTK_ADD_BLACK_BOX_TO_PACKAGE(NAME,CLASS)                       \
00395   bool bbDummy##NAME##CLASS = NAME ## GetPackage ()     \
00396     ->Register(CLASS ## Descriptor::Instance());
00397   //====================================================================
00398   
00399   //====================================================================
00400 #define BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(NAME,CLASS,TEMPLATE_PARAM) \
00401   bool bbDummy##NAME##CLASS##TEMPLATE_PARAM = NAME ## GetPackage ()     \
00402     ->Register(CLASS ## Descriptor <TEMPLATE_PARAM>::Instance());
00403   //====================================================================
00404   
00405   //====================================================================
00406 #define BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(NAME,CLASS,T1,T2)       \
00407   bool bbDummy##NAME##CLASS##T1##T2 = NAME ## GetPackage ()             \
00408     ->Register(CLASS ## Descriptor <T1,T2>::Instance()); 
00409   //====================================================================
00410   
00411 
00412 }// namespace bbtk
00413 
00414 
00415 
00416 #endif
00417 

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1