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