1 /*=========================================================================
4 Module: $RCSfile: bbtkUserBlackBoxGetSetFunctor.h,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
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.
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).
36 #ifndef __bbtkUserBlackBoxGetSetFunctor_h__
37 #define __bbtkUserBlackBoxGetSetFunctor_h__
40 #include "bbtkMessageManager.h"
45 // Forward declaration
49 //===========================================================================
50 class BBTK_EXPORT UserBlackBoxGetFunctor
53 /// Default constructor
54 UserBlackBoxGetFunctor() {}
55 /// Abstract method which applies the "Get" function of UserBlackBox o
56 virtual Data Get(UserBlackBox* o) = 0;
58 virtual TypeInfo GetTypeInfo() const = 0;
60 virtual std::string GetTypeName() const = 0;
62 virtual std::string GetHumanTypeName() const = 0;
64 virtual bool IsPointerType() const = 0;
67 //===========================================================================
70 //===========================================================================
71 class UserBlackBoxSetFunctor
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;
79 virtual TypeInfo GetTypeInfo() const = 0;
81 virtual std::string GetTypeName() const = 0;
83 virtual std::string GetHumanTypeName() const = 0;
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;
94 //===========================================================================
98 //===========================================================================
99 template <class UBB, class T, class TRETURN>
100 class UserBlackBoxTGetFunctor : public bbtk::UserBlackBoxGetFunctor
103 /// Type of pointer on a UBB::Get method
104 typedef TRETURN (UBB::*GetMethodPointerType)(void); //const
106 /// Construction with the pointer on the Get method
107 UserBlackBoxTGetFunctor(GetMethodPointerType g) :
110 bbtkDebugMessage("Data",9,"UserBlackBoxTGetFunctor<"<<
111 TypeName<UBB>()<<","<<
113 TypeName<TRETURN>()<<
114 ">::UserBlackBoxTGetFunctor()"<<std::endl);
117 /// Concrete application of the Get method of object o
118 Data Get(UserBlackBox* o)
120 bbtkDebugMessage("Data",9,"UserBlackBoxTGetFunctor<"<<
121 TypeName<UBB>()<<","<<
123 TypeName<TRETURN>()<<
124 ">::Get()"<<std::endl);
125 return (((UBB*)o)->*mGetMethodPointer)();
128 TypeInfo GetTypeInfo() const { return typeid(T); }
129 std::string GetTypeName() const { return TypeName<T>(); }
130 std::string GetHumanTypeName() const { return HumanTypeName<T>(); }
132 virtual bool IsPointerType() const
135 return boost::is_pointer<T>::value;
142 /// Pointer on the Get method
143 GetMethodPointerType mGetMethodPointer;
145 //===========================================================================
149 //===========================================================================
150 template <class UBB, class T, class TACCESS>
151 class UserBlackBoxTSetFunctor : public UserBlackBoxSetFunctor
154 /// Type of pointer on a UBB::Set method
155 typedef void (UBB::*SetMethodPointerType)(TACCESS);
157 /// Construction with the pointer on the Set method
158 UserBlackBoxTSetFunctor(SetMethodPointerType s) :
161 bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"<<
162 TypeName<UBB>()<<","<<
164 TypeName<TACCESS>()<<
165 ">::UserBlackBoxTSetFunctor()"<<std::endl);
168 /// Concrete application of the Set method of object o
169 void Set(UserBlackBox* o, const Data& d)
171 bbtkDebugMessage("Data",9,"UserBlackBoxTSetfunctor<"<<
172 TypeName<UBB>()<<","<<
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);
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)
190 bbtkInternalError("UserBlackBoxTSetFunctor<"
191 <<TypeName<UBB>()<<","
193 <<TypeName<TACCESS>()
194 <<">::BruteForceSetPointer("
196 <<" called whereas type '"
198 <<"' is not a pointer type");
201 /// Pointer on the Set method
202 SetMethodPointerType mSetMethodPointer;
204 //===========================================================================
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
215 /// Type of pointer on a UBB::Set method
216 typedef void (UBB::*SetMethodPointerType)(TACCESS*);
218 /// Construction with the pointer on the Set method
219 UserBlackBoxTSetFunctor(SetMethodPointerType s) :
222 bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"<<
223 TypeName<UBB>()<<","<<
224 TypeName<T*>()<<","<<
225 TypeName<TACCESS*>()<<
226 ">::UserBlackBoxTSetFunctor()"<<std::endl);
229 /// Concrete application of the Set method of object o
230 void Set(UserBlackBox* o, const Data& d)
232 bbtkDebugMessage("Data",9,"UserBlackBoxTSetfunctor<"<<
233 TypeName<UBB>()<<","<<
234 TypeName<T*>()<<","<<
235 TypeName<TACCESS*>()<<
236 ">::Set()"<<std::endl);
238 (((UBB*)o)->*mSetMethodPointer)(d.unsafe_get<T*>());
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; }
249 virtual void BruteForceSetPointer(UserBlackBox* o, void* p)
251 bbtkDebugMessage("Data",9,"UserBlackBoxTSetFunctor<"
252 <<TypeName<UBB>()<<","
253 <<TypeName<T*>()<<","
254 <<TypeName<TACCESS*>()
255 <<">::BruteForceSetPointer() (pointer specialization)");
257 (((UBB*)o)->*mSetMethodPointer)((T*)p);
261 /// Pointer on the Set method
262 SetMethodPointerType mSetMethodPointer;
264 //===========================================================================