]> 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/07/23 11:46:10 $
7   Version:   $Revision: 1.3 $
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     /// Dtor
56     virtual ~AtomicBlackBoxGetFunctor() {}
57     /// Abstract method which applies the "Get" function of AtomicBlackBox o
58     virtual Data Get(AtomicBlackBox* o) = 0;
59     /// 
60     virtual TypeInfo GetTypeInfo() const = 0;
61     /// 
62     virtual std::string GetTypeName() const = 0;
63     /// 
64     virtual std::string GetHumanTypeName() const = 0;
65     /// 
66     virtual bool IsPointerType() const = 0;
67
68   };
69   //===========================================================================
70
71
72   //===========================================================================
73   class AtomicBlackBoxSetFunctor
74   {
75   public:
76     /// Default constructor
77     AtomicBlackBoxSetFunctor() {}
78     /// Dtor
79     virtual ~AtomicBlackBoxSetFunctor() {}
80     /// Abstract method which applies the "Set" function of AtomicBlackBox o
81     virtual void Set(AtomicBlackBox* o, const Data&) = 0;
82     /// 
83     virtual TypeInfo GetTypeInfo() const = 0;
84     /// 
85     virtual std::string GetTypeName() const = 0;
86     /// 
87     virtual std::string GetHumanTypeName() const = 0;
88     /// 
89     virtual bool IsPointerType() const = 0;
90     /// Abstract method which applies the "Set" function of AtomicBlackBox o
91     /// using brute force cast to the typed pointer required by the "Set" fun.
92     /// Only works if the param type of the "Set" function is a pointer 
93     /// (see template specialization below).
94     /// !!! Use with care !!!
95     virtual void BruteForceSetPointer(AtomicBlackBox* o, void* p) = 0;
96
97   };
98   //===========================================================================
99
100
101
102   //===========================================================================
103   template <class UBB, class T, class TRETURN> 
104   class AtomicBlackBoxTGetFunctor : public bbtk::AtomicBlackBoxGetFunctor
105   {
106   public:
107     /// Type of pointer on a UBB::Get method  
108     typedef TRETURN (UBB::*GetMethodPointerType)(void); //const
109
110     /// Construction with the pointer on the Get method
111     AtomicBlackBoxTGetFunctor(GetMethodPointerType g) :
112       mGetMethodPointer(g)
113       {
114         bbtkDebugMessage("Data",9,"AtomicBlackBoxTGetFunctor<"<<
115                          TypeName<UBB>()<<","<<
116                          TypeName<T>()<<","<<
117                          TypeName<TRETURN>()<<
118                          ">::AtomicBlackBoxTGetFunctor()"<<std::endl);
119       }
120       
121     /// Concrete application of the Get method of object o
122     Data Get(AtomicBlackBox* o) 
123     {
124       bbtkDebugMessage("Data",9,"AtomicBlackBoxTGetFunctor<"<<
125                        TypeName<UBB>()<<","<<
126                        TypeName<T>()<<","<<
127                        TypeName<TRETURN>()<<
128                        ">::Get()"<<std::endl);
129       return (((UBB*)o)->*mGetMethodPointer)();
130     }
131     /// 
132     TypeInfo GetTypeInfo() const { return typeid(T); }
133     std::string GetTypeName() const { return TypeName<T>(); }
134     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
135     /// 
136     virtual bool IsPointerType() const 
137     {
138       return boost::is_pointer<T>::value;
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 AtomicBlackBoxTSetFunctor : public AtomicBlackBoxSetFunctor
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     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
159       mSetMethodPointer(s) 
160       {
161         bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"<<
162                         TypeName<UBB>()<<","<<
163                         TypeName<T>()<<","<<
164                         TypeName<TACCESS>()<<
165                          ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
166       }
167       
168     /// Concrete application of the Set method of object o
169     void Set(AtomicBlackBox* o, const Data& d)
170     { 
171       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetfunctor<"<<
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("Kernel",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(AtomicBlackBox* b, void* p) 
189     {
190       bbtkInternalError("AtomicBlackBoxTSetFunctor<"
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 AtomicBlackBoxTSetFunctor for pointer types
210   template <class UBB, class T, class TACCESS> 
211   class AtomicBlackBoxTSetFunctor<UBB,T*,TACCESS*> 
212     : public AtomicBlackBoxSetFunctor
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     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
220       mSetMethodPointer(s) 
221     {
222       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"<<
223                        TypeName<UBB>()<<","<<
224                        TypeName<T*>()<<","<<
225                        TypeName<TACCESS*>()<<
226                        ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
227     }
228     
229     /// Concrete application of the Set method of object o
230     void Set(AtomicBlackBox* o, const Data& d)
231     { 
232       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetfunctor<"<<
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(AtomicBlackBox* o, void* p) 
250     {  
251       bbtkDebugMessage("Data",9,"AtomicBlackBoxTSetFunctor<"
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