]> Creatis software - bbtk.git/blob - kernel/src/bbtkUserBlackBoxGetSetFunctor.h
Recreated the complete cvs tree because the project architecture deeply changed
[bbtk.git] / kernel / src / bbtkUserBlackBoxGetSetFunctor.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkUserBlackBoxGetSetFunctor.h,v $
5   Language:  C++
6   Date:      $Date: 2008/01/22 15:02:00 $
7   Version:   $Revision: 1.1.1.1 $
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
20 /**
21  *  \file 
22  *  \brief Class bbtk::UserBlackBoxGetFunctor / Class bbtk::UserBlackBoxSetFunctor : abstract functors of the Get and Set accessors of the inputs and outputs of a UserBlackBox ; Concrete derivatives.
23  */
24
25 /**
26  *  \class bbtk::UserBlackBoxGetFunctor
27  *  \brief Abstract functor of the Get accessors of the inputs and outputs of a UserBlackBox
28  *  \class bbtk::UserBlackBoxSetFunctor
29  *  \brief Abstract functor of the Set accessors of the inputs and outputs of a UserBlackBox
30  * \class bbtk::UserBlackBoxTGetFunctor
31  * \brief Template for concrete functors of the Get accessors of the inputs and outputs of a UserBlackBox (inherits from bbtk::UserBlackBoxGetFunctor).
32  * \class bbtk::UserBlackBoxTSetFunctor
33  * \brief Template for concrete functors of the Set accessors of the inputs and outputs of a UserBlackBox (inherits from bbtk::UserBlackBoxSetFunctor).
34  */
35  
36 #ifndef __bbtkUserBlackBoxGetSetFunctor_h__
37 #define __bbtkUserBlackBoxGetSetFunctor_h__
38
39 #include "bbtkData.h"
40 #include "bbtkMessageManager.h"
41
42 namespace bbtk
43 {
44
45    // Forward declaration
46   class UserBlackBox;
47
48
49   //===========================================================================
50   class BBTK_EXPORT UserBlackBoxGetFunctor
51   {
52   public:
53     /// Default constructor
54     UserBlackBoxGetFunctor() {}
55     /// Abstract method which applies the "Get" function of UserBlackBox o
56     virtual Data Get(UserBlackBox* o) = 0;
57     /// 
58     virtual TypeInfo GetTypeInfo() const = 0;
59     /// 
60     virtual std::string GetTypeName() const = 0;
61     /// 
62     virtual std::string GetHumanTypeName() const = 0;
63     /// 
64     virtual bool IsPointerType() const = 0;
65
66   };
67   //===========================================================================
68
69
70   //===========================================================================
71   class UserBlackBoxSetFunctor
72   {
73   public:
74     /// Default constructor
75     UserBlackBoxSetFunctor() {}
76     /// Abstract method which applies the "Set" function of UserBlackBox o
77     virtual void Set(UserBlackBox* o, const Data&) = 0;
78     /// 
79     virtual TypeInfo GetTypeInfo() const = 0;
80     /// 
81     virtual std::string GetTypeName() const = 0;
82     /// 
83     virtual std::string GetHumanTypeName() const = 0;
84     /// 
85     virtual bool IsPointerType() const = 0;
86     /// Abstract method which applies the "Set" function of UserBlackBox o
87     /// using brute force cast to the typed pointer required by the "Set" fun.
88     /// Only works if the param type of the "Set" function is a pointer 
89     /// (see template specialization below).
90     /// !!! Use with care !!!
91     virtual void BruteForceSetPointer(UserBlackBox* o, void* p) = 0;
92
93   };
94   //===========================================================================
95
96
97
98   //===========================================================================
99   template <class UBB, class T, class TRETURN> 
100   class UserBlackBoxTGetFunctor : public bbtk::UserBlackBoxGetFunctor
101   {
102   public:
103     /// Type of pointer on a UBB::Get method  
104     typedef TRETURN (UBB::*GetMethodPointerType)(void); //const
105
106     /// Construction with the pointer on the Get method
107     UserBlackBoxTGetFunctor(GetMethodPointerType g) :
108       mGetMethodPointer(g)
109       {
110         bbtkDebugMessage("Data",9,"UserBlackBoxTGetFunctor<"<<
111                          TypeName<UBB>()<<","<<
112                          TypeName<T>()<<","<<
113                          TypeName<TRETURN>()<<
114                          ">::UserBlackBoxTGetFunctor()"<<std::endl);
115       }
116       
117     /// Concrete application of the Get method of object o
118     Data Get(UserBlackBox* o) 
119     {
120       bbtkDebugMessage("Data",9,"UserBlackBoxTGetFunctor<"<<
121                        TypeName<UBB>()<<","<<
122                        TypeName<T>()<<","<<
123                        TypeName<TRETURN>()<<
124                        ">::Get()"<<std::endl);
125       return (((UBB*)o)->*mGetMethodPointer)();
126     }
127     /// 
128     TypeInfo GetTypeInfo() const { return typeid(T); }
129     std::string GetTypeName() const { return TypeName<T>(); }
130     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
131     /// 
132     virtual bool IsPointerType() const 
133     {
134 #ifdef _USE_BOOST_
135         return boost::is_pointer<T>::value;
136 #else
137         return false;
138 #endif
139     }
140
141   private:
142     ///  Pointer on the Get method  
143     GetMethodPointerType mGetMethodPointer;
144   };
145   //===========================================================================
146
147
148
149   //===========================================================================
150   template <class UBB, class T, class TACCESS> 
151   class UserBlackBoxTSetFunctor : public UserBlackBoxSetFunctor
152   {
153   public:
154     /// Type of pointer on a UBB::Set method  
155     typedef void (UBB::*SetMethodPointerType)(TACCESS);
156
157     /// Construction with the pointer on the Set method
158     UserBlackBoxTSetFunctor(SetMethodPointerType s) :
159       mSetMethodPointer(s) 
160       {
161         bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"<<
162                         TypeName<UBB>()<<","<<
163                         TypeName<T>()<<","<<
164                         TypeName<TACCESS>()<<
165                          ">::UserBlackBoxTSetFunctor()"<<std::endl);
166       }
167       
168     /// Concrete application of the Set method of object o
169     void Set(UserBlackBox* o, const Data& d)
170     { 
171       bbtkDebugMessage("Data",9,"UserBlackBoxTSetfunctor<"<<
172                         TypeName<UBB>()<<","<<
173                         TypeName<T>()<<","<<
174                         TypeName<TACCESS>()<<
175                        ">::Set()"<<std::endl);
176       //      (((UBB*)o)->*mSetMethodPointer)(*(T*)d);
177       //      bbtkAssert( bbtkEqualTypes( d.type(), typeid(T) ) );
178       T t = d.unsafe_get<T>();
179       (((UBB*)o)->*mSetMethodPointer)(t);
180       //      bbtkDebugMessage("Core",9,"SetOK"<<std::endl);
181     }
182
183     /// 
184     TypeInfo GetTypeInfo() const { return typeid(T); }
185     std::string GetTypeName() const { return TypeName<T>(); }
186     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
187     virtual bool IsPointerType() const { return false; }
188     virtual void BruteForceSetPointer(UserBlackBox* b, void* p) 
189     {
190       bbtkInternalError("UserBlackBoxTSetFunctor<"
191                         <<TypeName<UBB>()<<","
192                         <<TypeName<T>()<<","
193                         <<TypeName<TACCESS>()
194                         <<">::BruteForceSetPointer("
195                         <<b<<","<<p<<")"
196                         <<" called whereas type '"
197                         <<TypeName<T>()
198                         <<"' is not a pointer type"); 
199     }
200   private:
201     ///  Pointer on the Set method  
202     SetMethodPointerType mSetMethodPointer;
203   };
204   //===========================================================================
205
206
207
208   //===========================================================================
209   /// Template specialization of UserBlackBoxTSetFunctor for pointer types
210   template <class UBB, class T, class TACCESS> 
211   class UserBlackBoxTSetFunctor<UBB,T*,TACCESS*> 
212     : public UserBlackBoxSetFunctor
213   {
214   public:
215     /// Type of pointer on a UBB::Set method  
216     typedef void (UBB::*SetMethodPointerType)(TACCESS*);
217
218     /// Construction with the pointer on the Set method
219     UserBlackBoxTSetFunctor(SetMethodPointerType s) :
220       mSetMethodPointer(s) 
221     {
222       bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"<<
223                        TypeName<UBB>()<<","<<
224                        TypeName<T*>()<<","<<
225                        TypeName<TACCESS*>()<<
226                        ">::UserBlackBoxTSetFunctor()"<<std::endl);
227     }
228     
229     /// Concrete application of the Set method of object o
230     void Set(UserBlackBox* o, const Data& d)
231     { 
232       bbtkDebugMessage("Data",9,"UserBlackBoxTSetfunctor<"<<
233                        TypeName<UBB>()<<","<<
234                        TypeName<T*>()<<","<<
235                        TypeName<TACCESS*>()<<
236                        ">::Set()"<<std::endl);
237       
238       (((UBB*)o)->*mSetMethodPointer)(d.unsafe_get<T*>());
239
240     }
241
242     /// 
243     TypeInfo GetTypeInfo() const { return typeid(T*); }
244     std::string GetTypeName() const { return TypeName<T*>(); }
245     std::string GetHumanTypeName() const { return HumanTypeName<T*>(); }
246     virtual bool IsPointerType() const { return true; }
247
248  
249     virtual void BruteForceSetPointer(UserBlackBox* o, void* p) 
250     {  
251       bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"
252                        <<TypeName<UBB>()<<","
253                        <<TypeName<T*>()<<","
254                        <<TypeName<TACCESS*>()
255                        <<">::BruteForceSetPointer() (pointer specialization)");
256
257       (((UBB*)o)->*mSetMethodPointer)((T*)p);
258
259     }
260   private:
261     ///  Pointer on the Set method  
262     SetMethodPointerType mSetMethodPointer;
263   };
264   //===========================================================================
265
266 }
267 // namespace bbtk
268 #endif