SimpleITK  1.3.0.dev466
sitkMemberFunctionFactoryBase.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 #ifndef sitkMemberFunctionFactoryBase_h
19 #define sitkMemberFunctionFactoryBase_h
20 
21 #include "sitkConfigure.h"
22 
23 #include "nsstd/functional.h"
24 
25 #include "sitkPixelIDTypes.h"
26 #include "sitkPixelIDTypeLists.h"
27 #include "sitkMacro.h"
28 #include "sitkNonCopyable.h"
29 
30 #include "Ancillary/TypeList.h"
31 #include "Ancillary/FunctionTraits.h"
32 
33 #if defined SITK_HAS_UNORDERED_MAP
34 #include "nsstd/unordered_map.h"
35 #else
36 #include <map>
37 #endif
38 
39 namespace itk
40 {
41 namespace simple
42 {
43 
44 // this namespace is internal classes not part of the external simple ITK interface
45 namespace detail {
46 
47 
48 #if defined SITK_HAS_UNORDERED_MAP
49 
50 template <typename T> struct hash : public nsstd::hash<T>{};
51 
54 template <>
55 struct hash< std::pair<int, int> >
56  : public std::unary_function<std::pair<int,int>, std::size_t> {
57  std::size_t operator()( const std::pair<int, int > &p ) const
58  { return nsstd::hash<size_t>()( size_t(p.first) * prime + p.second ); }
59 private:
60  static const std::size_t prime = 16777619u;
61 };
62 #endif
63 
64 template< typename TMemberFunctionPointer,
65  typename TKey,
66  unsigned int TArity = ::detail::FunctionTraits<TMemberFunctionPointer>::arity>
68 
69 
76 template< typename TMemberFunctionPointer, typename TKey>
77 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 0> :
78  protected NonCopyable
79 {
80 protected:
81 
82  typedef TMemberFunctionPointer MemberFunctionType;
83  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
84  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
85 
86 
88 #if defined SITK_HAS_UNORDERED_MAP
89  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
90  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
91  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
92 #endif
93  { }
94 
95 public:
96 
99  typedef nsstd::function< MemberFunctionResultType ( ) > FunctionObjectType;
100 
101 
102 protected:
103 
104  typedef TKey KeyType;
105 
110  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
111  {
112 
113  // this is really only needed because std::bind1st does not work
114  // with tr1::function... that is with tr1::bind, we need to
115  // specify the other arguments, and can't just bind the first
116  return nsstd::bind( pfunc,objectPointer );
117  }
118 
119  // maps of Keys to pointers to member functions
120 #if defined SITK_HAS_UNORDERED_MAP
121  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
122  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
123  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
124 #else
125  std::map<TKey, FunctionObjectType> m_PFunction4;
126  std::map<TKey, FunctionObjectType> m_PFunction3;
127  std::map<TKey, FunctionObjectType> m_PFunction2;
128 #endif
129 
130 };
131 
132 
139 template< typename TMemberFunctionPointer, typename TKey>
140 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 1> :
141  protected NonCopyable
142 {
143 protected:
144 
145  typedef TMemberFunctionPointer MemberFunctionType;
146  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
147  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
148  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument0Type MemberFunctionArgumentType;
149 
150 
152 #if defined SITK_HAS_UNORDERED_MAP
153  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
154  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
155  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
156 #endif
157  { }
158 
159 public:
160 
163  typedef nsstd::function< MemberFunctionResultType ( MemberFunctionArgumentType ) > FunctionObjectType;
164 
165 
166 protected:
167 
168  typedef TKey KeyType;
169 
174  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
175  {
176  // needed for _1 place holder
177  using namespace nsstd::placeholders;
178 
179  // this is really only needed because std::bind1st does not work
180  // with tr1::function... that is with tr1::bind, we need to
181  // specify the other arguments, and can't just bind the first
182  return nsstd::bind( pfunc,objectPointer, _1 );
183  }
184 
185 
186  // maps of Keys to pointers to member functions
187 #if defined SITK_HAS_UNORDERED_MAP
188  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
189  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
190  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
191 #else
192  std::map<TKey, FunctionObjectType> m_PFunction4;
193  std::map<TKey, FunctionObjectType> m_PFunction3;
194  std::map<TKey, FunctionObjectType> m_PFunction2;
195 #endif
196 
197 
198 };
199 
200 
201 template< typename TMemberFunctionPointer, typename TKey>
202 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 2> :
203  protected NonCopyable
204 {
205 protected:
206 
207  typedef TMemberFunctionPointer MemberFunctionType;
208  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
209  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument0Type MemberFunctionArgument0Type;
210  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument1Type MemberFunctionArgument1Type;
211  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
212 
213 
215 #if defined SITK_HAS_UNORDERED_MAP
216  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
217  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
218  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
219 #endif
220  { }
221 
222 public:
223 
227  typedef nsstd::function< MemberFunctionResultType ( MemberFunctionArgument0Type, MemberFunctionArgument1Type) > FunctionObjectType;
228 
229 
230 protected:
231 
232  typedef TKey KeyType;
233 
238  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
239  {
240  // needed for _1 place holder
241  using namespace nsstd::placeholders;
242 
243  // this is really only needed because std::bind1st does not work
244  // with tr1::function... that is with tr1::bind, we need to
245  // specify the other arguments, and can't just bind the first
246  return nsstd::bind( pfunc, objectPointer, _1, _2 );
247  }
248 
249 
250  // maps of Keys to pointers to member functions
251 #if defined SITK_HAS_UNORDERED_MAP
252  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
253  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
254  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
255 #else
256  std::map<TKey, FunctionObjectType> m_PFunction4;
257  std::map<TKey, FunctionObjectType> m_PFunction3;
258  std::map<TKey, FunctionObjectType> m_PFunction2;
259 #endif
260 
261 
262 };
263 
264 
265 template< typename TMemberFunctionPointer, typename TKey>
266 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 3> :
267  protected NonCopyable
268 {
269 protected:
270 
271  typedef TMemberFunctionPointer MemberFunctionType;
272  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
273  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument0Type MemberFunctionArgument0Type;
274  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument1Type MemberFunctionArgument1Type;
275  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument2Type MemberFunctionArgument2Type;
276  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
277 
278 
280 #if defined SITK_HAS_UNORDERED_MAP
281  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
282  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
283  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
284 #endif
285  { }
286 
287 public:
288 
291  typedef nsstd::function< MemberFunctionResultType ( MemberFunctionArgument0Type, MemberFunctionArgument1Type, MemberFunctionArgument2Type) > FunctionObjectType;
292 
293 
294 protected:
295 
296  typedef TKey KeyType;
297 
302  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
303  {
304  // needed for _1 place holder
305  using namespace nsstd::placeholders;
306 
307  // this is really only needed because std::bind1st does not work
308  // with tr1::function... that is with tr1::bind, we need to
309  // specify the other arguments, and can't just bind the first
310  return nsstd::bind( pfunc, objectPointer, _1, _2, _3 );
311  }
312 
313 
314  // maps of Keys to pointers to member functions
315 #if defined SITK_HAS_UNORDERED_MAP
316  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
317  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
318  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
319 #else
320  std::map<TKey, FunctionObjectType> m_PFunction4;
321  std::map<TKey, FunctionObjectType> m_PFunction3;
322  std::map<TKey, FunctionObjectType> m_PFunction2;
323 #endif
324 
325 };
326 
327 
328 template< typename TMemberFunctionPointer, typename TKey>
329 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 4> :
330  protected NonCopyable
331 {
332 protected:
333 
334  typedef TMemberFunctionPointer MemberFunctionType;
335  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
336  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument0Type MemberFunctionArgument0Type;
337  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument1Type MemberFunctionArgument1Type;
338  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument2Type MemberFunctionArgument2Type;
339  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument3Type MemberFunctionArgument3Type;
340  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
341 
342 
344 #if defined SITK_HAS_UNORDERED_MAP
345  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
346  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
347  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
348 #endif
349  { }
350 
351 public:
352 
355  typedef nsstd::function< MemberFunctionResultType ( MemberFunctionArgument0Type, MemberFunctionArgument1Type, MemberFunctionArgument2Type, MemberFunctionArgument3Type) > FunctionObjectType;
356 
357 
358 protected:
359 
360  typedef TKey KeyType;
361 
366  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
367  {
368  // needed for _1 place holder
369  using namespace nsstd::placeholders;
370 
371  // this is really only needed because std::bind1st does not work
372  // with tr1::function... that is with tr1::bind, we need to
373  // specify the other arguments, and can't just bind the first
374  return nsstd::bind( pfunc, objectPointer, _1, _2, _3, _4 );
375  }
376 
377 
378  // maps of Keys to pointers to member functions
379 #if defined SITK_HAS_UNORDERED_MAP
380  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
381  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
382  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
383 #else
384  std::map<TKey, FunctionObjectType> m_PFunction4;
385  std::map<TKey, FunctionObjectType> m_PFunction3;
386  std::map<TKey, FunctionObjectType> m_PFunction2;
387 #endif
388 
389 };
390 
391 template< typename TMemberFunctionPointer, typename TKey>
392 class MemberFunctionFactoryBase<TMemberFunctionPointer, TKey, 5> :
393  protected NonCopyable
394 {
395 protected:
396 
397  typedef TMemberFunctionPointer MemberFunctionType;
398  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ResultType MemberFunctionResultType;
399  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument0Type MemberFunctionArgument0Type;
400  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument1Type MemberFunctionArgument1Type;
401  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument2Type MemberFunctionArgument2Type;
402  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument3Type MemberFunctionArgument3Type;
403  typedef typename ::detail::FunctionTraits<MemberFunctionType>::Argument4Type MemberFunctionArgument4Type;
404  typedef typename ::detail::FunctionTraits<MemberFunctionType>::ClassType ObjectType;
405 
406 
408 #if defined SITK_HAS_UNORDERED_MAP
409  : m_PFunction4( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
410  m_PFunction3( typelist::Length<InstantiatedPixelIDTypeList>::Result ),
411  m_PFunction2( typelist::Length<InstantiatedPixelIDTypeList>::Result )
412 #endif
413  { }
414 
415 public:
416 
419  typedef nsstd::function< MemberFunctionResultType (
420  MemberFunctionArgument0Type,
421  MemberFunctionArgument1Type,
422  MemberFunctionArgument2Type,
423  MemberFunctionArgument3Type,
424  MemberFunctionArgument4Type ) >
426 
427 
428 protected:
429 
430  typedef TKey KeyType;
431 
436  static FunctionObjectType BindObject( MemberFunctionType pfunc, ObjectType *objectPointer)
437  {
438  // needed for _1 place holder
439  using namespace nsstd::placeholders;
440 
441  // this is really only needed because std::bind1st does not work
442  // with tr1::function... that is with tr1::bind, we need to
443  // specify the other arguments, and can't just bind the first
444  return nsstd::bind( pfunc, objectPointer, _1, _2, _3, _4, _5 );
445  }
446 
447 
448  // maps of Keys to pointers to member functions
449 #if defined SITK_HAS_UNORDERED_MAP
450  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction4;
451  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction3;
452  nsstd::unordered_map< TKey, FunctionObjectType, hash<TKey> > m_PFunction2;
453 #else
454  std::map<TKey, FunctionObjectType> m_PFunction4;
455  std::map<TKey, FunctionObjectType> m_PFunction3;
456  std::map<TKey, FunctionObjectType> m_PFunction2;
457 #endif
458 
459 };
460 
461 } // end namespace detail
462 } // end namespace simple
463 } // end namespace itk
464 
465 #endif // sitkMemberFunctionFactoryBase_h
nsstd::function< MemberFunctionResultType(MemberFunctionArgument0Type, MemberFunctionArgument1Type, MemberFunctionArgument2Type, MemberFunctionArgument3Type, MemberFunctionArgument4Type) > FunctionObjectType
::detail::FunctionTraits< MemberFunctionType >::Argument0Type MemberFunctionArgument0Type
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
::detail::FunctionTraits< MemberFunctionType >::Argument0Type MemberFunctionArgumentType
STL namespace.
::detail::FunctionTraits< MemberFunctionType >::Argument0Type MemberFunctionArgument0Type
::detail::FunctionTraits< MemberFunctionType >::Argument1Type MemberFunctionArgument1Type
::detail::FunctionTraits< MemberFunctionType >::Argument1Type MemberFunctionArgument1Type
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
::detail::FunctionTraits< MemberFunctionType >::Argument2Type MemberFunctionArgument2Type
nsstd::function< MemberFunctionResultType(MemberFunctionArgument0Type, MemberFunctionArgument1Type, MemberFunctionArgument2Type) > FunctionObjectType
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
::detail::FunctionTraits< MemberFunctionType >::Argument3Type MemberFunctionArgument3Type
::detail::FunctionTraits< MemberFunctionType >::Argument1Type MemberFunctionArgument1Type
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
nsstd::function< MemberFunctionResultType(MemberFunctionArgumentType) > FunctionObjectType
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
nsstd::function< MemberFunctionResultType(MemberFunctionArgument0Type, MemberFunctionArgument1Type) > FunctionObjectType
::detail::FunctionTraits< MemberFunctionType >::Argument0Type MemberFunctionArgument0Type
::detail::FunctionTraits< MemberFunctionType >::Argument3Type MemberFunctionArgument3Type
A base class for the MemberFunctionFactory.
::detail::FunctionTraits< MemberFunctionType >::Argument2Type MemberFunctionArgument2Type
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
nsstd::function< MemberFunctionResultType(MemberFunctionArgument0Type, MemberFunctionArgument1Type, MemberFunctionArgument2Type, MemberFunctionArgument3Type) > FunctionObjectType
::detail::FunctionTraits< MemberFunctionType >::Argument4Type MemberFunctionArgument4Type
An inheratable class to disable copying of a class.
::detail::FunctionTraits< MemberFunctionType >::Argument1Type MemberFunctionArgument1Type
static FunctionObjectType BindObject(MemberFunctionType pfunc, ObjectType *objectPointer)
::detail::FunctionTraits< MemberFunctionType >::Argument0Type MemberFunctionArgument0Type
::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
::detail::FunctionTraits< MemberFunctionType >::Argument2Type MemberFunctionArgument2Type