SimpleITK  
sitkMemberFunctionFactoryBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 #include "sitkPixelIDTypes.h"
23 #include "sitkPixelIDTypeLists.h"
24 #include "sitkMacro.h"
25 #include "sitkNonCopyable.h"
26 
27 #include "Ancillary/type_list2.h"
28 #include "Ancillary/FunctionTraits.h"
29 
30 #include <unordered_map>
31 #include <functional>
32 #include <tuple>
33 
34 namespace itk::simple::detail
35 {
36 
37 // make hash function available in current name space to take priority
38 
39 template <typename T>
40 struct hash : public std::hash<T>
41 {};
42 
44 template <typename T>
45 inline void
46 hash_combine(std::size_t & seed, const T & val)
47 {
48  // Code from boost
49  // Reciprocal of the golden ratio helps spread entropy
50  // and handles duplicates.
51  std::hash<T> hasher;
52  seed ^= hasher(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
53 }
54 
55 template <typename S, typename T>
56 struct hash<std::pair<S, T>>
57 {
58  inline size_t
59  operator()(const std::pair<S, T> & val) const
60  {
61  size_t seed = 0;
62  hash_combine(seed, val.first);
63  hash_combine(seed, val.second);
64  return seed;
65  }
66 };
67 
68 template <class... TupleArgs>
69 struct hash<std::tuple<TupleArgs...>>
70 {
71 
72 public:
73  size_t
74  operator()(std::tuple<TupleArgs...> tupleValue) const
75  {
76  size_t seed = 0;
77  std::apply(
78  [&seed](auto... tupleElement) { (hash_combine(seed, tupleElement), ...); },tupleValue);
79  return seed;
80  }
81 };
82 
83 
84 template <typename TMemberFunctionPointer,
85  typename TKey>
87 {
88 protected:
89  using MemberFunctionType = TMemberFunctionPointer;
90  using ObjectType = typename ::detail::FunctionTraits<MemberFunctionType>::ClassType;
91  using MemberFunctionResultType = typename ::detail::FunctionTraits<MemberFunctionType>::ResultType;
92 
93 
95  : m_PFunction(3 * typelist2::length<InstantiatedPixelIDTypeList>::value)
96  {}
97 
98 public:
101  using FunctionObjectType = typename ::detail::FunctionTraits<MemberFunctionType>::FunctionObjectType;
102 
103  protected:
104  using KeyType = TKey;
105 
106 
111  template <typename... Args>
112  static FunctionObjectType BindObject(MemberFunctionResultType (ObjectType ::*pfunc)(Args...), ObjectType * objectPointer)
113  {
114  return [pfunc, objectPointer](Args... args) -> MemberFunctionResultType {
115  return std::invoke(pfunc, objectPointer, std::forward<Args>(args)...);
116  };
117  }
118 
119 
120 
121  using FunctionMapType = std::unordered_map<TKey, FunctionObjectType, hash<TKey>>;
122 
123  // maps of Keys to pointers to member functions
125 };
126 
127 } // namespace itk::simple::detail
128 
129 #endif // sitkMemberFunctionFactoryBase_h
itk::simple::detail::hash
Definition: sitkMemberFunctionFactoryBase.h:40
sitkNonCopyable.h
sitkPixelIDTypes.h
itk::simple::detail
Definition: sitkDetail.h:24
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::ObjectType
typename ::detail::FunctionTraits< MemberFunctionType >::ClassType ObjectType
Definition: sitkMemberFunctionFactoryBase.h:90
itk::simple::detail::hash< std::pair< S, T > >::operator()
size_t operator()(const std::pair< S, T > &val) const
Definition: sitkMemberFunctionFactoryBase.h:59
sitkMacro.h
itk::simple::detail::MemberFunctionFactoryBase::MemberFunctionFactoryBase
MemberFunctionFactoryBase()
Definition: sitkMemberFunctionFactoryBase.h:94
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::MemberFunctionType
MemberFunction1Type MemberFunctionType
Definition: sitkMemberFunctionFactoryBase.h:89
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::FunctionObjectType
typename ::detail::FunctionTraits< MemberFunctionType >::FunctionObjectType FunctionObjectType
Definition: sitkMemberFunctionFactoryBase.h:101
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::FunctionMapType
std::unordered_map< std::pair< unsigned int, int >, FunctionObjectType, hash< std::pair< unsigned int, int > > > FunctionMapType
Definition: sitkMemberFunctionFactoryBase.h:121
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::MemberFunctionResultType
typename ::detail::FunctionTraits< MemberFunctionType >::ResultType MemberFunctionResultType
Definition: sitkMemberFunctionFactoryBase.h:91
itk::simple::detail::MemberFunctionFactoryBase::m_PFunction
FunctionMapType m_PFunction
Definition: sitkMemberFunctionFactoryBase.h:124
itk::simple::InstantiatedPixelIDTypeList
AllPixelIDTypeList InstantiatedPixelIDTypeList
Definition: sitkPixelIDTypeLists.h:205
itk::simple::detail::hash_combine
void hash_combine(std::size_t &seed, const T &val)
Definition: sitkMemberFunctionFactoryBase.h:46
sitkConfigure.h
sitkPixelIDTypeLists.h
itk::simple::detail::hash< std::tuple< TupleArgs... > >::operator()
size_t operator()(std::tuple< TupleArgs... > tupleValue) const
Definition: sitkMemberFunctionFactoryBase.h:74
itk::simple::detail::MemberFunctionFactoryBase< MemberFunction1Type, std::pair< unsigned int, int > >::KeyType
std::pair< unsigned int, int > KeyType
Definition: sitkMemberFunctionFactoryBase.h:104
itk::simple::detail::MemberFunctionFactoryBase::BindObject
static FunctionObjectType BindObject(MemberFunctionResultType(ObjectType ::*pfunc)(Args...), ObjectType *objectPointer)
Definition: sitkMemberFunctionFactoryBase.h:112
itk::simple::detail::MemberFunctionFactoryBase
Definition: sitkMemberFunctionFactoryBase.h:86