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