]> Creatis software - bbtk.git/blob - kernel/src/bbtkAtomicBlackBoxGetSetFunctor.h
New widget pipeline : progressing ...
[bbtk.git] / kernel / src / bbtkAtomicBlackBoxGetSetFunctor.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkAtomicBlackBoxGetSetFunctor.h,v $
4   Language:  C++
5   Date:      $Date: 2008/11/25 11:17:13 $
6   Version:   $Revision: 1.5 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32 /**
33  *  \file 
34  *  \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.
35  */
36
37 /**
38  *  \class bbtk::AtomicBlackBoxGetFunctor
39  *  \brief Abstract functor of the Get accessors of the inputs and outputs of a AtomicBlackBox
40  *  \class bbtk::AtomicBlackBoxSetFunctor
41  *  \brief Abstract functor of the Set accessors of the inputs and outputs of a AtomicBlackBox
42  * \class bbtk::AtomicBlackBoxTGetFunctor
43  * \brief Template for concrete functors of the Get accessors of the inputs and outputs of a AtomicBlackBox (inherits from bbtk::AtomicBlackBoxGetFunctor).
44  * \class bbtk::AtomicBlackBoxTSetFunctor
45  * \brief Template for concrete functors of the Set accessors of the inputs and outputs of a AtomicBlackBox (inherits from bbtk::AtomicBlackBoxSetFunctor).
46  */
47  
48 #ifndef __bbtkAtomicBlackBoxGetSetFunctor_h__
49 #define __bbtkAtomicBlackBoxGetSetFunctor_h__
50
51 #include "bbtkData.h"
52 #include "bbtkMessageManager.h"
53
54 namespace bbtk
55 {
56
57    // Forward declaration
58   class AtomicBlackBox;
59
60
61   //===========================================================================
62   class BBTK_EXPORT AtomicBlackBoxGetFunctor
63   {
64   public:
65     /// Default constructor
66     AtomicBlackBoxGetFunctor() {}
67     /// Dtor
68     virtual ~AtomicBlackBoxGetFunctor() {}
69     /// Abstract method which applies the "Get" function of AtomicBlackBox o
70     virtual Data Get(AtomicBlackBox* o) = 0;
71     /// 
72     virtual TypeInfo GetTypeInfo() const = 0;
73     /// 
74     virtual std::string GetTypeName() const = 0;
75     /// 
76     virtual std::string GetHumanTypeName() const = 0;
77     /// 
78     virtual bool IsPointerType() const = 0;
79
80   };
81   //===========================================================================
82
83
84   //===========================================================================
85   class AtomicBlackBoxSetFunctor
86   {
87   public:
88     /// Default constructor
89     AtomicBlackBoxSetFunctor() {}
90     /// Dtor
91     virtual ~AtomicBlackBoxSetFunctor() {}
92     /// Abstract method which applies the "Set" function of AtomicBlackBox o
93     virtual void Set(AtomicBlackBox* o, const Data&) = 0;
94     /// 
95     virtual TypeInfo GetTypeInfo() const = 0;
96     /// 
97     virtual std::string GetTypeName() const = 0;
98     /// 
99     virtual std::string GetHumanTypeName() const = 0;
100     /// 
101     virtual bool IsPointerType() const = 0;
102     /// Abstract method which applies the "Set" function of AtomicBlackBox o
103     /// using brute force cast to the typed pointer required by the "Set" fun.
104     /// Only works if the param type of the "Set" function is a pointer 
105     /// (see template specialization below).
106     /// !!! Use with care !!!
107     virtual void BruteForceSetPointer(AtomicBlackBox* o, void* p) = 0;
108
109   };
110   //===========================================================================
111
112
113
114   //===========================================================================
115   template <class UBB, class T, class TRETURN> 
116   class AtomicBlackBoxTGetFunctor : public bbtk::AtomicBlackBoxGetFunctor
117   {
118   public:
119     /// Type of pointer on a UBB::Get method  
120     typedef TRETURN (UBB::*GetMethodPointerType)(void); //const
121
122     /// Construction with the pointer on the Get method
123     AtomicBlackBoxTGetFunctor(GetMethodPointerType g) :
124       mGetMethodPointer(g)
125       {
126         bbtkDebugMessage("data",9,"AtomicBlackBoxTGetFunctor<"<<
127                          TypeName<UBB>()<<","<<
128                          TypeName<T>()<<","<<
129                          TypeName<TRETURN>()<<
130                          ">::AtomicBlackBoxTGetFunctor()"<<std::endl);
131       }
132       
133     /// Concrete application of the Get method of object o
134     Data Get(AtomicBlackBox* o) 
135     {
136       bbtkDebugMessage("data",9,"AtomicBlackBoxTGetFunctor<"<<
137                        TypeName<UBB>()<<","<<
138                        TypeName<T>()<<","<<
139                        TypeName<TRETURN>()<<
140                        ">::Get()"<<std::endl);
141       return (((UBB*)o)->*mGetMethodPointer)();
142     }
143     /// 
144     TypeInfo GetTypeInfo() const { return typeid(T); }
145     std::string GetTypeName() const { return TypeName<T>(); }
146     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
147     /// 
148     virtual bool IsPointerType() const 
149     {
150       return boost::is_pointer<T>::value;
151     }
152
153   private:
154     ///  Pointer on the Get method  
155     GetMethodPointerType mGetMethodPointer;
156   };
157   //===========================================================================
158
159
160
161   //===========================================================================
162   template <class UBB, class T, class TACCESS> 
163   class AtomicBlackBoxTSetFunctor : public AtomicBlackBoxSetFunctor
164   {
165   public:
166     /// Type of pointer on a UBB::Set method  
167     typedef void (UBB::*SetMethodPointerType)(TACCESS);
168
169     /// Construction with the pointer on the Set method
170     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
171       mSetMethodPointer(s) 
172       {
173         bbtkDebugMessage("data",9,"AtomicBlackBoxTSetFunctor<"<<
174                         TypeName<UBB>()<<","<<
175                         TypeName<T>()<<","<<
176                         TypeName<TACCESS>()<<
177                          ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
178       }
179       
180     /// Concrete application of the Set method of object o
181     void Set(AtomicBlackBox* o, const Data& d)
182     { 
183       bbtkDebugMessage("data",9,"AtomicBlackBoxTSetfunctor<"<<
184                         TypeName<UBB>()<<","<<
185                         TypeName<T>()<<","<<
186                         TypeName<TACCESS>()<<
187                        ">::Set()"<<std::endl);
188       //      (((UBB*)o)->*mSetMethodPointer)(*(T*)d);
189       //      bbtkAssert( bbtkEqualTypes( d.type(), typeid(T) ) );
190       T t = d.unsafe_get<T>();
191       (((UBB*)o)->*mSetMethodPointer)(t);
192       //      bbtkDebugMessage("Kernel",9,"SetOK"<<std::endl);
193     }
194
195     /// 
196     TypeInfo GetTypeInfo() const { return typeid(T); }
197     std::string GetTypeName() const { return TypeName<T>(); }
198     std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
199     virtual bool IsPointerType() const { return false; }
200     virtual void BruteForceSetPointer(AtomicBlackBox* b, void* p) 
201     {
202       bbtkInternalError("AtomicBlackBoxTSetFunctor<"
203                         <<TypeName<UBB>()<<","
204                         <<TypeName<T>()<<","
205                         <<TypeName<TACCESS>()
206                         <<">::BruteForceSetPointer("
207                         <<b<<","<<p<<")"
208                         <<" called whereas type '"
209                         <<TypeName<T>()
210                         <<"' is not a pointer type"); 
211     }
212   private:
213     ///  Pointer on the Set method  
214     SetMethodPointerType mSetMethodPointer;
215   };
216   //===========================================================================
217
218
219
220   //===========================================================================
221   /// Template specialization of AtomicBlackBoxTSetFunctor for pointer types
222   template <class UBB, class T, class TACCESS> 
223   class AtomicBlackBoxTSetFunctor<UBB,T*,TACCESS*> 
224     : public AtomicBlackBoxSetFunctor
225   {
226   public:
227     /// Type of pointer on a UBB::Set method  
228     typedef void (UBB::*SetMethodPointerType)(TACCESS*);
229
230     /// Construction with the pointer on the Set method
231     AtomicBlackBoxTSetFunctor(SetMethodPointerType s) :
232       mSetMethodPointer(s) 
233     {
234       bbtkDebugMessage("data",9,"AtomicBlackBoxTSetFunctor<"<<
235                        TypeName<UBB>()<<","<<
236                        TypeName<T*>()<<","<<
237                        TypeName<TACCESS*>()<<
238                        ">::AtomicBlackBoxTSetFunctor()"<<std::endl);
239     }
240     
241     /// Concrete application of the Set method of object o
242     void Set(AtomicBlackBox* o, const Data& d)
243     { 
244       bbtkDebugMessage("data",9,"AtomicBlackBoxTSetfunctor<"<<
245                        TypeName<UBB>()<<","<<
246                        TypeName<T*>()<<","<<
247                        TypeName<TACCESS*>()<<
248                        ">::Set()"<<std::endl);
249       
250       (((UBB*)o)->*mSetMethodPointer)(d.unsafe_get<T*>());
251
252     }
253
254     /// 
255     TypeInfo GetTypeInfo() const { return typeid(T*); }
256     std::string GetTypeName() const { return TypeName<T*>(); }
257     std::string GetHumanTypeName() const { return HumanTypeName<T*>(); }
258     virtual bool IsPointerType() const { return true; }
259
260  
261     virtual void BruteForceSetPointer(AtomicBlackBox* o, void* p) 
262     {  
263       bbtkDebugMessage("data",9,"AtomicBlackBoxTSetFunctor<"
264                        <<TypeName<UBB>()<<","
265                        <<TypeName<T*>()<<","
266                        <<TypeName<TACCESS*>()
267                        <<">::BruteForceSetPointer() (pointer specialization)");
268
269       (((UBB*)o)->*mSetMethodPointer)((T*)p);
270
271     }
272   private:
273     ///  Pointer on the Set method  
274     SetMethodPointerType mSetMethodPointer;
275   };
276   //===========================================================================
277
278 }
279 // namespace bbtk
280 #endif