]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.h
70e60e3c17c3b84beaf7fab0fe76689b8ee3b0cf
[bbtk.git] / kernel / src / bbtkFactory.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkFactory.h,v $
4   Language:  C++
5   Date:      $Date: 2010/09/12 14:52:25 $
6   Version:   $Revision: 1.18 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *\file
33  *\brief Class bbtk::Factory : can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded.
34  */
35 /**
36  *\class bbtk::Factory
37  *\brief Can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded.
38  *
39  */
40
41
42 #ifndef __bbtkFactory_h__
43 #define __bbtkFactory_h__
44
45 #include "bbtkPackage.h"
46
47 namespace bbtk
48 {
49
50   class Executer;
51   BBTK_FORWARD_DECLARE_POINTER(Executer);
52
53   class BBTK_EXPORT Factory : public Object
54   {
55     BBTK_OBJECT_INTERFACE(Factory);
56     typedef Object Superclass;
57   public:
58     static Pointer New();
59
60     // @name Packages management
61     //@{
62     /// Pushes back the names of the Package s which are in the Factory
63     void GetPackagesList(std::vector<std::string>&);
64     /// Loads a dynamic library which contains a Package 
65     void LoadPackage( const std::string& name );
66     /// Unloads a Package which was loaded from a dynamic library
67     void UnLoadPackage( const std::string& name );
68     /// Inserts a Package in the Factory
69     void InsertPackage( Package::Pointer );
70     /// Removess a Package from the Factory
71     void RemovePackage( Package::Pointer );
72     /// Returns the pointer on a Package provided by name
73     Package::Pointer GetPackage(const std::string& name) const;
74     /// The type of map of packages
75     typedef std::map< std::string, Package::Pointer > PackageMapType;
76     /// Returns the map of Packages
77     const PackageMapType& GetPackageMap() const { return mPackageMap; }
78     //@}
79
80     // @name Methods which print help
81     //@{
82     /// Prints the list of Package
83     void PrintHelpListPackages(bool details = true, 
84                                bool adaptors = false) const;
85     /// Prints help on a particular Package
86     void PrintHelpPackage(const std::string& name, 
87                           bool adaptors = false) const;
88     /// Prints help on a BlackBoxDescriptor
89     void PrintHelpDescriptor(const std::string& name, 
90                              std::string& package,
91                              bool full=true ) const;
92     
93     void ShowGraphTypes(const std::string& name) const;
94     // @}
95
96
97     /// @name Object creation methods
98     //@{
99     BlackBox::Pointer NewBlackBox(const std::string& type, 
100                           const std::string& name) const;
101     
102     BlackBox::Pointer NewAdaptor(const DataInfo& typein,
103                          const DataInfo& typeout,
104                          const std::string& name) const;
105
106     BlackBox::Pointer NewWidgetAdaptor(const DataInfo& typein,
107                                const DataInfo& typeout,
108                                const std::string& name) const;
109
110     Connection::Pointer NewConnection(BlackBox::Pointer from,
111                                       const std::string& output,
112                                       BlackBox::Pointer to,
113                                       const std::string& input) const;
114    //@}
115
116     /// @name Package inspection methods
117     /// Allow to test if an Adaptor is present in the packages WITHOUT instanciating it
118     //@{
119     bool FindAdaptor(const DataInfo& typein,
120                      const DataInfo& typeout,
121                      std::string& adaptor) const;
122
123     bool FindWidgetAdaptor(const DataInfo& typein,
124                            const DataInfo& typeout,
125                            std::string& adaptor) const;
126     
127     bool FindWidgetAdaptor2(const DataInfo& typein,
128                             const DataInfo& typeout,
129                             std::string& widget,
130                             std::string& adaptor) const;
131     //@}
132   
133     void WriteDotFilePackagesList(FILE *ff);
134
135     /// Releases all the packages of the Factory
136     void Reset();
137     
138     void Check() const;
139
140     typedef enum
141       {
142         Packages,
143         Categories,
144         Initials,
145         Adaptors
146       }
147       IndexEntryType;
148     void CreateHtmlIndex(IndexEntryType type, const std::string& filename);
149
150     /// Sets the executer who created the factory (if any)
151     void SetExecuter(ExecuterPointer e) { mExecuter = e; }
152     /// Gets the executer who created the factory (if any)
153     ExecuterPointer GetExecuter() { return mExecuter.lock(); }
154     /// Gets the executer who created the factory (if any) - const
155     //    const Executer::Pointer GetExecuter() const { return mExecuter.lock(); }
156
157           std::string GetPackageNameOfaBlackBox(std::string boxType);
158
159
160   private:
161
162
163     /// The map of packages
164     PackageMapType mPackageMap;
165
166     /// The executer which created the factory (if any)
167     ExecuterWeakPointer mExecuter;
168
169     bool DoLoadPackage(std::string libname,
170                        std::string pkgname,
171                        std::string path);
172     void CloseAllPackages();
173     void ClosePackage(PackageMapType::iterator& i);
174
175   };
176   // class Factory
177
178
179
180 }// namespace bbtk
181
182
183
184 #endif
185