]> Creatis software - bbtk.git/blob - kernel/src/bbtkPackage.h
4c0b70534cf890d37f22f4f0a43c7b73bcfc3372
[bbtk.git] / kernel / src / bbtkPackage.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkPackage.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/09 11:16:57 $
7   Version:   $Revision: 1.7 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See doc/license.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  * \file
20  * \brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
21  */
22 /**
23  * \class bbtk::Package
24  * \brief registers black boxes descriptors and is able to create instances of the black boxes registered.
25  */
26
27 #ifndef __bbtkPackage_h__
28 #define __bbtkPackage_h__
29
30 #include "bbtkBlackBox.h"
31
32 namespace bbtk
33 {
34
35   class BBTK_EXPORT Package
36   {
37   public:
38     Package(const std::string& name,
39             const std::string& author,
40             const std::string& description,
41             const std::string& version,
42             const std::string& BBTKVersion);
43     ~Package();
44     /// Returns the name of the package
45     const std::string& GetName() const { return mName; }
46
47     /// Returns the author of the package
48     const std::string& GetAuthor() const { return mAuthor; }
49
50     /// Returns the category of the package
51     const std::string& GetCategory() const { return mCategory; }
52
53     /// Returns the description of the package
54     const std::string& GetDescription() const { return mDescription; }
55
56     /// Returns the version of the package
57     const std::string& GetVersion() const { return mVersion; }
58
59     /// Returns the version of bbtk used to build the package
60     const std::string& GetBBTKVersion() const { return mBBTKVersion; }
61
62     bool ContainsBlackBox(const std::string& boxname) const;
63
64  
65     BlackBox* NewBlackBox(const std::string& type,
66                              const std::string& name) const;
67   
68     BlackBox* NewAdaptor(const DataInfo& typein,
69                          const DataInfo& typeout,
70                          const std::string& name) const;
71     
72    BlackBox* NewWidgetAdaptor(const DataInfo& typein,
73                               const DataInfo& typeout,
74                               const std::string& name) const;
75     bool FindAdaptor(const DataInfo& typein,
76                            const DataInfo& typeout,
77                            std::string& adaptor) const;
78     bool FindWidgetAdaptor(const DataInfo& typein,
79                            const DataInfo& typeout,
80                            std::string& adaptor) const;
81
82     bool RegisterBlackBox(BlackBoxDescriptor*); 
83     void UnRegisterBlackBox(const std::string& name); 
84
85     //   bool RegisterAdaptor(BlackBoxDescriptor*); 
86
87     void PrintBlackBoxes(bool description = false, 
88                          bool adaptors = false) const;
89     void PrintAdaptors(bool description = false) const;
90     void HelpBlackBox(const std::string& name, bool full=true) const;
91     
92     void CreateHtmlPage(const std::string& filename,
93                         const std::string& caller = "?",
94                         const std::string& source = "?",
95                         const std::string& custom_header = "",
96                         const std::string& custom_title = "",
97                         int detail = 1, 
98                         int level = 0,
99                         bool relative_link = false ) const;
100
101     void  SetDocURL(std::string url){ mDocURL=url; }
102     const std::string& GetDocURL() const { return mDocURL; }
103     
104     void  SetDocRelativeURL(std::string url){ mDocRelativeURL=url; }
105     const std::string& GetDocRelativeURL() const { return mDocRelativeURL; }
106
107
108     unsigned int GetNumberOfBlackBoxes() const { return mBlackBoxMap.size(); }
109     
110     /// Changes the name of a black box type
111     void ChangeBlackBoxName( const std::string& oldname, 
112                              const std::string& newname );
113     /// The type of map of descriptors
114     typedef std::map< std::string, BlackBoxDescriptor*> BlackBoxMapType;
115     const BlackBoxMapType& GetBlackBoxMap() const { return mBlackBoxMap; }
116     BlackBoxMapType& GetBlackBoxMap() { return mBlackBoxMap; }
117
118     /// The type of key in the map of adaptor descriptors
119     class AdaptorKey 
120     {
121     public:
122       AdaptorKey( const DataInfo& typein, const DataInfo& typeout, 
123                   BlackBoxDescriptor::Kind kind ) 
124         : mTypeIn(typein), mTypeOut(typeout), mKind(kind) {}
125       
126       bool operator< ( const AdaptorKey& k ) const
127       {
128         return ( ( mKind < k.mKind ) ||
129                  ( ( mKind == k.mKind ) &&
130                    ( ( mTypeIn < k.mTypeIn ) ||
131                      ( ( mTypeIn == k.mTypeIn ) && 
132                        ( mTypeOut < k.mTypeOut ) ) ) ) );
133       }
134       
135       bool operator== ( const AdaptorKey& k ) const
136       {
137         return ( ( mKind == k.mKind ) && 
138                  ( mTypeIn == k.mTypeIn ) && 
139                  ( mTypeOut == k.mTypeOut ) );
140       }
141       DataInfo mTypeIn;
142       DataInfo mTypeOut; 
143       BlackBoxDescriptor::Kind mKind;
144     };
145     
146     /// The type of map of adaptor descriptors
147     typedef std::map< AdaptorKey, BlackBoxDescriptor*> AdaptorMapType;
148
149  
150    const AdaptorMapType& GetAdaptorMap() const { return mAdaptorMap; }
151
152
153     // Factories management
154     /// Adds the factory to the set of factories which use the package
155     void AddFactory(Factory* f) { mFactorySet.insert(f); }
156     /// Removes the factory from the set of factories which use the package
157     void RemoveFactory(Factory* f) { mFactorySet.erase(f); }
158
159     /// Gets the set of factories which use the package
160     std::set<Factory*>& GetFactorySet() { return mFactorySet; }
161     /// Gets the set of factories which use the package (const)
162     const std::set<Factory*>& GetFactorySet() const { return mFactorySet; }
163     
164     void CheckBoxes() const;
165
166   private:
167
168     /// The name of the package
169     std::string mName;
170     /// The author of the package
171     std::string mAuthor;
172     /// The categories of the package
173     std::string mCategory;    
174     /// The description of the package
175     std::string mDescription;
176     /// The version of the package
177     std::string mVersion;
178     /// The version of the library bbtk used to build the package
179     std::string mBBTKVersion;
180     /// URL of the documentation of the Package (absolute path)
181     std::string mDocURL;
182     /// URL of the documentation of the Package 
183     /// (path relative to bbtk doc root)
184     std::string mDocRelativeURL;
185
186     /// The map of black boxes descriptors
187     BlackBoxMapType mBlackBoxMap;
188
189
190   public:
191
192
193   private:
194     /// The map of adaptors descriptors
195     AdaptorMapType mAdaptorMap;
196
197
198     /// The set of factories which contain the package 
199     std::set<Factory*> mFactorySet;
200     
201   };
202   // EO class Package
203   //====================================================================
204
205
206 //====================================================================
207 #if defined(_WIN32)
208   #define BBTK_PACKAGE_EXPORT __declspec( dllexport )
209 #else
210   #define BBTK_PACKAGE_EXPORT
211 #endif // defined(_WIN32) 
212 //====================================================================
213
214 //====================================================================
215 #define BBTK_DECLARE_PACKAGE(NAME)                                      \
216   extern "C"                                                            \
217   {                                                                     \
218     bbtk::Package*& NAME ## GetPackagePointer();                        \
219     BBTK_PACKAGE_EXPORT void BBTK_CDECL NAME ## DeletePackage();        \
220     BBTK_PACKAGE_EXPORT bbtk::Package* BBTK_CDECL NAME ## GetPackage(); \
221   }
222 //==================================================================== 
223
224 //==================================================================== 
225 #define BBTK_IMPLEMENT_PACKAGE(NAME,AUTHOR,DESCRIPTION,VERSION)         \
226   extern "C"                                                            \
227   {                                                                     \
228     bbtk::Package*& NAME ## GetPackagePointer()                         \
229     {                                                                   \
230       static bbtk::Package* u = 0;                                      \
231       return u;                                                         \
232     }                                                                   \
233     BBTK_PACKAGE_EXPORT void BBTK_CDECL NAME ## DeletePackage()         \
234     {                                                                   \
235       if (NAME ## GetPackagePointer())                                  \
236         delete NAME ## GetPackagePointer();                             \
237       NAME ## GetPackagePointer() = 0;                                  \
238     }                                                                   \
239     BBTK_PACKAGE_EXPORT bbtk::Package* BBTK_CDECL NAME ## GetPackage()  \
240     {                                                                   \
241       if (!NAME ## GetPackagePointer())                                 \
242         NAME ## GetPackagePointer() =                                   \
243           new bbtk::Package(#NAME,                                      \
244                             AUTHOR,     \
245                             DESCRIPTION, \
246                             VERSION,    \
247                             BBTK_STRINGIFY_SYMBOL(BBTK_VERSION)         \
248                             );                                          \
249       return NAME ## GetPackagePointer();                               \
250     }                                                                   \
251   }
252 //====================================================================  
253
254 //====================================================================
255 #define BBTK_ADD_BLACK_BOX_TO_PACKAGE(NAME,CLASS)                       \
256   bool bbDummy##NAME##CLASS = NAME ## GetPackage()->RegisterBlackBox(CLASS::bbDescriptor());
257   //====================================================================
258   
259 //====================================================================
260 #define BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(NAME,CLASS,TEMPLATE_PARAM) \
261   bool bbDummy##NAME##CLASS##TEMPLATE_PARAM = NAME ## GetPackage()->RegisterBlackBox(CLASS<TEMPLATE_PARAM>::bbDescriptor());
262   //====================================================================
263
264 //====================================================================
265 #define BBTK_ADD_TEMPLATE2_BLACK_BOX_TO_PACKAGE(NAME,CLASS,T1,T2)       \
266   bool bbDummy##NAME##CLASS##T1##T2 = NAME ## GetPackage()->RegisterBlackBox(CLASS<T1,T2>::bbDescriptor()); 
267   //====================================================================
268   
269 //====================================================================
270 //#define BBTK_ADD_ADAPTOR_TO_PACKAGE(NAME,CLASS)                       \
271 //  bool bbDummy##NAME##CLASS = NAME ## GetPackage()->RegisterBlackBox(CLASS::bbDescriptor()); \
272 //  bool bbDummyAdaptor##NAME##CLASS = NAME ## GetPackage()->RegisterAdaptor(CLASS::bbDescriptor());
273 //====================================================================
274
275 //====================================================================
276 //#define BBTK_ADD_TEMPLATE_ADAPTOR_TO_PACKAGE(NAME,CLASS,TEMPLATE_PARAM) \
277 //  bool bbDummy##NAME##CLASS##TEMPLATE_PARAM = NAME ## GetPackage()->RegisterBlackBox(CLASS<TEMPLATE_PARAM>::bbDescriptor()); \
278 //  bool bbDummyAdaptor##NAME##CLASS##TEMPLATE_PARAM = NAME ## GetPackage()->RegisterAdaptor(CLASS<TEMPLATE_PARAM>::bbDescriptor());
279 //====================================================================
280
281 //====================================================================
282 //#define BBTK_ADD_TEMPLATE2_ADAPTOR_TO_PACKAGE(NAME,CLASS,T1,T2)       \
283 //  bool bbDummy##NAME##CLASS##T1##T2 = NAME ## GetPackage()->RegisterBlackBox(CLASS<T1,T2>::bbDescriptor()); \
284 //    bool bbDummyAdaptor##NAME##CLASS##T1##T2 = NAME ## GetPackage()->RegisterAdaptor(CLASS<T1,T2>::bbDescriptor());
285   //====================================================================
286
287 }// namespace bbtk
288
289
290
291 #endif
292