[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

functortraits.hxx
1/************************************************************************/
2/* */
3/* Copyright 1998-2005 by Ullrich Koethe */
4/* */
5/* This file is part of the VIGRA computer vision library. */
6/* The VIGRA Website is */
7/* http://hci.iwr.uni-heidelberg.de/vigra/ */
8/* Please direct questions, bug reports, and contributions to */
9/* ullrich.koethe@iwr.uni-heidelberg.de or */
10/* vigra@informatik.uni-hamburg.de */
11/* */
12/* Permission is hereby granted, free of charge, to any person */
13/* obtaining a copy of this software and associated documentation */
14/* files (the "Software"), to deal in the Software without */
15/* restriction, including without limitation the rights to use, */
16/* copy, modify, merge, publish, distribute, sublicense, and/or */
17/* sell copies of the Software, and to permit persons to whom the */
18/* Software is furnished to do so, subject to the following */
19/* conditions: */
20/* */
21/* The above copyright notice and this permission notice shall be */
22/* included in all copies or substantial portions of the */
23/* Software. */
24/* */
25/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32/* OTHER DEALINGS IN THE SOFTWARE. */
33/* */
34/************************************************************************/
35
36
37#ifndef VIGRA_FUNCTORTRAITS_HXX
38#define VIGRA_FUNCTORTRAITS_HXX
39
40#include <functional>
41#include "metaprogramming.hxx"
42
43namespace vigra {
44
45struct InitializerTag {};
46struct UnaryFunctorTag {};
47struct BinaryFunctorTag {};
48struct TernaryFunctorTag {};
49struct UnaryAnalyserTag {};
50struct BinaryAnalyserTag {};
51struct TernaryAnalyserTag {};
52
53struct UnaryReduceFunctorTag
54: public InitializerTag, public UnaryAnalyserTag
55{};
56
57struct BinaryReduceFunctorTag
58: public InitializerTag, public BinaryAnalyserTag
59{};
60
61typedef UnaryFunctorTag UnaryExpandFunctorTag;
62typedef BinaryFunctorTag BinaryExpandFunctorTag;
63
64template <class T>
65class FunctorTraitsBase
66{
67 public:
68 typedef T type;
69
70 typedef typename IsDerivedFrom<T, InitializerTag>::result isInitializer;
71
72 typedef typename IsDerivedFrom<T, UnaryFunctorTag>::result isUnaryFunctor;
73 typedef typename IsDerivedFrom<T, BinaryFunctorTag>::result isBinaryFunctor;
74 typedef typename IsDerivedFrom<T, TernaryFunctorTag>::result isTernaryFunctor;
75
76 typedef typename IsDerivedFrom<T, UnaryAnalyserTag>::result isUnaryAnalyser;
77 typedef typename IsDerivedFrom<T, BinaryAnalyserTag>::result isBinaryAnalyser;
78 typedef typename IsDerivedFrom<T, TernaryAnalyserTag>::result isTernaryAnalyser;
79};
80
81
82
83/** \addtogroup Functors
84*/
85//@{
86/** \brief Export associated information for a functor.
87
88 The FunctorTraits class contains the following fields:
89
90 \code
91 template <class T>
92 struct FunctorTraits
93 {
94 typedef T type;
95
96 typedef ... isInitializer;
97
98 typedef ... isUnaryFunctor;
99 typedef ... isBinaryFunctor;
100 typedef ... isTernaryFunctor;
101
102 typedef ... isUnaryAnalyser;
103 typedef ... isBinaryAnalyser;
104 typedef ... isTernaryAnalyser;
105 };
106 \endcode
107
108 where the dots are either <tt>VigraTrueType</tt> or <tt>VigraFalseType</tt>
109 depending on whether the functor supports the respective functionality or not.
110 Note that these traits are automatically defined correctly when your functor is derived
111 from the appropriate functor tag classes:
112
113 \code
114 struct InitializerTag {};
115 struct UnaryFunctorTag {};
116 struct BinaryFunctorTag {};
117 struct TernaryFunctorTag {};
118 struct UnaryAnalyserTag {};
119 struct BinaryAnalyserTag {};
120 struct TernaryAnalyserTag {};
121 struct UnaryReduceFunctorTag : public InitializerTag, public UnaryAnalyserTag {};
122 struct BinaryReduceFunctorTag : public InitializerTag, public BinaryAnalyserTag {};
123 \endcode
124
125 If a functor <tt>f</tt> is a model of these categories, it supports the following
126 calls (<tt>v</tt> is a variable such that the result type of the functor
127 calls can be converted into <tt>v</tt>'s type, and <tt>a1, a2, a3</tt> are
128 variables convertible into the functor's argument types):
129
130 <DL>
131 <DT><b>Initializer</b>
132 <DD> <tt>v = f()</tt> (used with initImageWithFunctor())
133 <DT><b>UnaryFunctor</b>
134 <DD> <tt>v = f(a1)</tt> (used with transformImage())
135 <DT><b>BinaryFunctor</b>
136 <DD> <tt>v = f(a1, a2)</tt> (used with combineTwoImages())
137 <DT><b>TernaryFunctor</b>
138 <DD> <tt>v = f(a1, a2, a3)</tt> (used with combineThreeImages())
139 <DT><b>UnaryAnalyser</b>
140 <DD> <tt>f(a1)</tt> (return type <tt>void</tt>, used with inspectImage())
141 <DT><b>BinaryAnalyser</b>
142 <DD> <tt>f(a1, a2)</tt> (return type <tt>void</tt>, used with inspectTwoImages())
143 <DT><b>TernaryAnalyser</b>
144 <DD> <tt>f(a1, a2, a3)</tt> (return type <tt>void</tt>)
145 </DL>
146
147 It should be noted that the functor's argument and result types are not contained
148 in the traits class: Since the function calls are often member template functions in
149 VIGRA, many functors do not have fixed argument types. Neither are the result
150 types fixed in this case because they are computed (via a template meta-program)
151 from the argument types.
152
153 <b>\#include</b> <vigra/functortraits.hxx> <br/>
154 Namespace: vigra
155*/
156template <class T>
158: public FunctorTraitsBase<T>
159{};
160
161#define VIGRA_DEFINE_STL_FUNCTOR(name, unary, binary) \
162template <class T> \
163class FunctorTraits<name<T> > \
164{ \
165 public: \
166 typedef T type; \
167 \
168 typedef VigraFalseType isInitializer; \
169 \
170 typedef unary isUnaryFunctor; \
171 typedef binary isBinaryFunctor; \
172 typedef VigraFalseType isTernaryFunctor; \
173 \
174 typedef VigraFalseType isUnaryAnalyser; \
175 typedef VigraFalseType isBinaryAnalyser; \
176 typedef VigraFalseType isTernaryAnalyser; \
177};
178
179// ???TODO: these should also be specialized for the ptr_fun and mem_fun_ptr wrappers
180VIGRA_DEFINE_STL_FUNCTOR(std::plus, VigraFalseType, VigraTrueType)
181VIGRA_DEFINE_STL_FUNCTOR(std::minus, VigraFalseType, VigraTrueType)
182VIGRA_DEFINE_STL_FUNCTOR(std::multiplies, VigraFalseType, VigraTrueType)
183VIGRA_DEFINE_STL_FUNCTOR(std::divides, VigraFalseType, VigraTrueType)
184VIGRA_DEFINE_STL_FUNCTOR(std::modulus, VigraFalseType, VigraTrueType)
185VIGRA_DEFINE_STL_FUNCTOR(std::equal_to, VigraFalseType, VigraTrueType)
186VIGRA_DEFINE_STL_FUNCTOR(std::not_equal_to, VigraFalseType, VigraTrueType)
187VIGRA_DEFINE_STL_FUNCTOR(std::greater, VigraFalseType, VigraTrueType)
188VIGRA_DEFINE_STL_FUNCTOR(std::less, VigraFalseType, VigraTrueType)
189VIGRA_DEFINE_STL_FUNCTOR(std::greater_equal, VigraFalseType, VigraTrueType)
190VIGRA_DEFINE_STL_FUNCTOR(std::less_equal, VigraFalseType, VigraTrueType)
191VIGRA_DEFINE_STL_FUNCTOR(std::logical_and, VigraFalseType, VigraTrueType)
192VIGRA_DEFINE_STL_FUNCTOR(std::logical_or, VigraFalseType, VigraTrueType)
193
194VIGRA_DEFINE_STL_FUNCTOR(std::negate, VigraTrueType, VigraFalseType)
195VIGRA_DEFINE_STL_FUNCTOR(std::logical_not, VigraTrueType, VigraFalseType)
196
197#if !( (__cplusplus >= 202002L) || (defined(_MSC_VER) && _MSVC_LANG >= 202002L) )
198VIGRA_DEFINE_STL_FUNCTOR(std::binary_negate, VigraFalseType, VigraTrueType)
199VIGRA_DEFINE_STL_FUNCTOR(std::unary_negate, VigraTrueType, VigraFalseType)
200#endif
201
202#undef VIGRA_DEFINE_STL_FUNCTOR
203
204template <class R>
205class FunctorTraits<R (*)()>
206{
207 public:
208 typedef R (*type)();
209
210 typedef VigraTrueType isInitializer;
211 typedef VigraFalseType isUnaryFunctor;
212 typedef VigraFalseType isBinaryFunctor;
213 typedef VigraFalseType isTernaryFunctor;
214 typedef VigraFalseType isUnaryAnalyser;
215 typedef VigraFalseType isBinaryAnalyser;
216 typedef VigraFalseType isTernaryAnalyser;
217};
218
219template <class R, class T>
220class FunctorTraits<R (*)(T)>
221{
222 public:
223 typedef R (*type)(T);
224
225 typedef VigraFalseType isInitializer;
226 typedef VigraTrueType isUnaryFunctor;
227 typedef VigraFalseType isBinaryFunctor;
228 typedef VigraFalseType isTernaryFunctor;
229 typedef VigraFalseType isUnaryAnalyser;
230 typedef VigraFalseType isBinaryAnalyser;
231 typedef VigraFalseType isTernaryAnalyser;
232};
233
234template <class R, class T1, class T2>
235class FunctorTraits<R (*)(T1, T2)>
236{
237 public:
238 typedef R (*type)(T1, T2);
239
240 typedef VigraFalseType isInitializer;
241 typedef VigraFalseType isUnaryFunctor;
242 typedef VigraTrueType isBinaryFunctor;
243 typedef VigraFalseType isTernaryFunctor;
244 typedef VigraFalseType isUnaryAnalyser;
245 typedef VigraFalseType isBinaryAnalyser;
246 typedef VigraFalseType isTernaryAnalyser;
247};
248
249template <class R, class T1, class T2, class T3>
250class FunctorTraits<R (*)(T1, T2, T3)>
251{
252 public:
253 typedef R (*type)(T1, T2, T3);
254
255 typedef VigraFalseType isInitializer;
256 typedef VigraFalseType isUnaryFunctor;
257 typedef VigraFalseType isBinaryFunctor;
258 typedef VigraTrueType isTernaryFunctor;
259 typedef VigraFalseType isUnaryAnalyser;
260 typedef VigraFalseType isBinaryAnalyser;
261 typedef VigraFalseType isTernaryAnalyser;
262};
263
264//@}
265
266} // namespace vigra
267
268#endif // VIGRA_FUNCTORTRAITS_HXX
Export associated information for a functor.
Definition functortraits.hxx:159

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.12.1