]> Creatis software - bbtk.git/blob - kernel/src/bbtkAtomicBlackBoxGetSetFunctor.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkAtomicBlackBoxGetSetFunctor.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkAtomicBlackBoxGetSetFunctor.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/24 10:11:27 $
7   Version:   $Revision: 1.2 $
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::AtomicBlackBoxGetFunctor / Class bbtk::AtomicBlackBoxSetFunctor : abstract functors of the Get and Set accessors of the inputs and outputs of a AtomicBlackBox ; Concrete derivatives.
23  */
24
25 /**
26  *  \class bbtk::AtomicBlackBoxGetFunctor
27  *  \brief Abstract functor of the Get accessors of the inputs and outputs of a AtomicBlackBox
28  *  \class bbtk::AtomicBlackBoxSetFunctor
29  *  \brief Abstract functor of the Set accessors of the inputs and outputs of a AtomicBlackBox
30  * \class bbtk::AtomicBlackBoxTGetFunctor
31  * \brief Template for concrete functors of the Get accessors of the inputs and outputs of a AtomicBlackBox (inherits from bbtk::AtomicBlackBoxGetFunctor).
32  * \class bbtk::AtomicBlackBoxTSetFunctor
33  * \brief Template for concrete functors of the Set accessors of the inputs and outputs of a AtomicBlackBox (inherits from bbtk::AtomicBlackBoxSetFunctor).
34  */
35  
36 #ifndef __bbtkAtomicBlackBoxGetSetFunctor_h__
37 #define __bbtkAtomicBlackBoxGetSetFunctor_h__
38
39 #include "bbtkData.h"
40 #include "bbtkMessageManager.h"
41
42 namespace bbtk
43 {
44
45    // Forward declaration
46   class AtomicBlackBox;
47
48
49   //===========================================================================
50   class BBTK_EXPORT AtomicBlackBoxGetFunctor
51   {
52   public:
53     /// Default constructor
54     AtomicBlackBoxGetFunctor() {}
55     /// Abstract method which applies the "Get" function of AtomicBlackBox o
56     virtual Data Get(AtomicBlackBox* 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 AtomicBlackBoxSetFunctor
72   {
73   public:
74     /// Default constructor
75     AtomicBlackBoxSetFunctor() {}
76     /// Abstract method which applies the "Set" function of AtomicBlackBox o
77     virtual void Set(AtomicBlackBox* 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 AtomicBlackBox 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(AtomicBlackBox* o, void* p) = 0;
92
93   };
94   //===========================================================================
95
96
97
98   //===========================================================================
99   template <class UBB, class T, class TRETURN> 
100   class AtomicBlackBoxTGetFunctor : public bbtk::AtomicBlackBoxGetFunctor
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     AtomicBlackBoxTGetFunctor(GetMethodPointerType g) :
108       mGetMethodPointer(g)
109       {
110         bbtkDebugMessage("Data",9,"AtomicBlackBoxTGetFunctor<"<<
111                          TypeName<UBB>()<<","<<
112                          TypeName<T>()<<","<<
113                          TypeName<TRETURN>()<<
114                          ">::AtomicBlackBoxTGetFunctor()"<<std::endl);
115       }
116       
117     /// Concrete application of the Get method of object o
118     Data Get(AtomicBlackBox* o) 
119     {
120       bbtkDebugMessage("Data",9,"AtomicBlackBoxTGetFunctor<"<<
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       return boost::is_pointer<T>::value;
135     }
136
137   private:
138     ///  Pointer on the Get method  
139     GetMethodPointerType mGetMethodPointer;
140   };
141   //===========================================================================
142
143
144
145   //===========================================================================
146   template <class UBB, class T, class TACCESS> 
147   class AtomicBlackBoxTSetFunctor : public AtomicBlackBoxSetFunctor
148   {
149   public:
150     /// Type of pointer on a UBB::Set method  
151     typedef void (UBB::*SetMethodPointerType)(TACCESS);
152
153     /// Construction with the pointer on the Set method
154     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
155       mSetMethodPointer(s) 
156       {
157         bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"<<
158                         TypeName<UBB>()<<","<<
159                         TypeName<T>()<<","<<
160                         TypeName<TACCESS>()<<
161                          ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
162       }
163       
164     /// Concrete application of the Set method of object o
165     void Set(AtomicBlackBox* o, const Data& d)
166     { 
167       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetfunctor<"<<
168                         TypeName<UBB>()<<","<<
169                         TypeName<T>()<<","<<
170                         TypeName<TACCESS>()<<
171                        ">::Set()"<<std::endl);
172       //      (((UBB*)o)->*mSetMethodPointer)(*(T*)d);
173       //      bbtkAssert( bbtkEqualTypes( d.type(), typeid(T) ) );
174       T t = d.unsafe_get<T>();
175       (((UBB*)o)->*mSetMethodPointer)(t);
176       //      bbtkDebugMessage("Kernel",9,"SetOK"<<std::endl);
177     }
178
179     /// 
180     TypeInfo GetTypeInfo() const { return typeid(T); }
181     std::string GetTypeName() const { return TypeName<T>(); }
182     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
183     virtual bool IsPointerType() const { return false; }
184     virtual void BruteForceSetPointer(AtomicBlackBox* b, void* p) 
185     {
186       bbtkInternalError("AtomicBlackBoxTSetFunctor<"
187                         <<TypeName<UBB>()<<","
188                         <<TypeName<T>()<<","
189                         <<TypeName<TACCESS>()
190                         <<">::BruteForceSetPointer("
191                         <<b<<","<<p<<")"
192                         <<" called whereas type '"
193                         <<TypeName<T>()
194                         <<"' is not a pointer type"); 
195     }
196   private:
197     ///  Pointer on the Set method  
198     SetMethodPointerType mSetMethodPointer;
199   };
200   //===========================================================================
201
202
203
204   //===========================================================================
205   /// Template specialization of AtomicBlackBoxTSetFunctor for pointer types
206   template <class UBB, class T, class TACCESS> 
207   class AtomicBlackBoxTSetFunctor<UBB,T*,TACCESS*> 
208     : public AtomicBlackBoxSetFunctor
209   {
210   public:
211     /// Type of pointer on a UBB::Set method  
212     typedef void (UBB::*SetMethodPointerType)(TACCESS*);
213
214     /// Construction with the pointer on the Set method
215     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
216       mSetMethodPointer(s) 
217     {
218       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"<<
219                        TypeName<UBB>()<<","<<
220                        TypeName<T*>()<<","<<
221                        TypeName<TACCESS*>()<<
222                        ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
223     }
224     
225     /// Concrete application of the Set method of object o
226     void Set(AtomicBlackBox* o, const Data& d)
227     { 
228       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetfunctor<"<<
229                        TypeName<UBB>()<<","<<
230                        TypeName<T*>()<<","<<
231                        TypeName<TACCESS*>()<<
232                        ">::Set()"<<std::endl);
233       
234       (((UBB*)o)->*mSetMethodPointer)(d.unsafe_get<T*>());
235
236     }
237
238     /// 
239     TypeInfo GetTypeInfo() const { return typeid(T*); }
240     std::string GetTypeName() const { return TypeName<T*>(); }
241     std::string GetHumanTypeName() const { return HumanTypeName<T*>(); }
242     virtual bool IsPointerType() const { return true; }
243
244  
245     virtual void BruteForceSetPointer(AtomicBlackBox* o, void* p) 
246     {  
247       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"
248                        <<TypeName<UBB>()<<","
249                        <<TypeName<T*>()<<","
250                        <<TypeName<TACCESS*>()
251                        <<">::BruteForceSetPointer() (pointer specialization)");
252
253       (((UBB*)o)->*mSetMethodPointer)((T*)p);
254
255     }
256   private:
257     ///  Pointer on the Set method  
258     SetMethodPointerType mSetMethodPointer;
259   };
260   //===========================================================================
261
262 }
263 // namespace bbtk
264 #endif