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"
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
34namespace itk::simple::detail
35{
36
37// make hash function available in current name space to take priority
38
39template <typename T>
40struct hash : public std::hash<T>
41{};
42
44template <typename T>
45inline void
46hash_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
55template <typename S, typename T>
56struct 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
68template <class... TupleArgs>
69struct hash<std::tuple<TupleArgs...>>
70{
71
72public:
73 size_t
74 operator()(std::tuple<TupleArgs...> tupleValue) const
75 {
76 size_t seed = 0;
77 std::apply([&seed](auto... tupleElement) { (hash_combine(seed, tupleElement), ...); }, tupleValue);
78 return seed;
79 }
80};
81
82
83template <typename TMemberFunctionPointer, typename TKey>
85{
86protected:
87 using MemberFunctionType = TMemberFunctionPointer;
88 using ObjectType = typename ::detail::FunctionTraits<MemberFunctionType>::ClassType;
89 using MemberFunctionResultType = typename ::detail::FunctionTraits<MemberFunctionType>::ResultType;
90
91
93 : m_PFunction(3 * typelist2::length<InstantiatedPixelIDTypeList>::value)
94 {}
95
96public:
99 using FunctionObjectType = typename ::detail::FunctionTraits<MemberFunctionType>::FunctionObjectType;
100
101protected:
102 using KeyType = TKey;
103
104
109 template <typename... Args>
110 static FunctionObjectType
111 BindObject(MemberFunctionResultType (ObjectType ::*pfunc)(Args...), ObjectType * objectPointer)
112 {
113 return [pfunc, objectPointer](Args... args) -> MemberFunctionResultType {
114 return std::invoke(pfunc, objectPointer, std::forward<Args>(args)...);
115 };
116 }
117
118
119 using FunctionMapType = std::unordered_map<TKey, FunctionObjectType, hash<TKey>>;
120
121 // maps of Keys to pointers to member functions
123};
124
125} // namespace itk::simple::detail
126
127#endif // sitkMemberFunctionFactoryBase_h
std::unordered_map< std::tuple< unsigned int, int, unsigned int, int >, FunctionObjectType, hash< std::tuple< unsigned int, int, unsigned int, int > > > FunctionMapType
static FunctionObjectType BindObject(MemberFunctionResultType(ObjectType ::*pfunc)(Args...), ObjectType *objectPointer)
void hash_combine(std::size_t &seed, const T &val)
AllPixelIDTypeList InstantiatedPixelIDTypeList
STL namespace.
size_t operator()(const std::pair< S, T > &val) const
size_t operator()(std::tuple< TupleArgs... > tupleValue) const