Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
JFunctional.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JFUNCTIONAL__
2#define __JTOOLS__JFUNCTIONAL__
3
5#include "JLang/JNullType.hh"
6#include "JLang/JException.hh"
7#include "JLang/JVoid.hh"
8#include "JLang/JClass.hh"
9#include "JMath/JZero.hh"
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JTOOLS {}
17namespace JPP { using namespace JTOOLS; }
18
19namespace JTOOLS {
20
21 using JLANG::JClass;
23 using JLANG::JNullType;
24 using JLANG::JVoid;
26
27
28 /**
29 * Template definition of function object interface.
30 */
31 template<class JArgument_t = JNullType, class JResult_t = JNullType>
32 class JFunctional;
33
34
35 /**
36 * Template specialisation of compilable function object.
37 */
38 template<>
40 {
41 protected:
42 /**
43 * Function compilation.
44 */
45 virtual void do_compile() = 0;
46
47
48 public:
49 /**
50 * Virtual destructor.
51 */
52 virtual ~JFunctional()
53 {}
54
55
56 /**
57 * Function compilation.
58 */
59 void compile()
60 {
61 do_compile();
62 }
63 };
64
65
66 /**
67 * Template definition of recursive function value evaluation.
68 */
69 template<class JArgument_t, class JResult_t>
70 class JFunctional :
71 public virtual JFunctional<JNullType, JNullType>
72 {
73 protected:
74 /**
75 * Default constructor.
76 */
79 supervisor(JSupervisor::getInstance())
80 {}
81
82 public:
83
84 class JSupervisor;
85
86 typedef JArgument_t argument_type;
87 typedef JResult_t result_type;
90
91
92 /**
93 * Recursive function value evaluation.
94 *
95 * \param pX pointer to abscissa values
96 * \return function value
97 */
98 virtual result_type evaluate(const argument_type* pX) const = 0;
99
100
101 /**
102 * Recursive function value evaluation.
103 *
104 * \param function function
105 * \param pX pointer to abscissa values
106 */
107 static result_type getValue(const JFunctional& function,
108 const argument_type* pX)
109 {
110 return function.evaluate(pX);
111 }
112
113
114 /**
115 * Termination of recursive function value evaluation.
116 *
117 * \param value result
118 * \param pX pointer to abscissa values
119 */
122 const argument_type* pX)
123 {
124 return value;
125 }
126
127
128 /**
129 * Exception handler for functional object.
130 */
132 private JException
133 {
134 /**
135 * Default constructor.
136 */
138 JException("unknown")
139 {}
140
141
142 /**
143 * Virtual destructor.
144 */
146 {}
147
148
149 /**
150 * Implementation of exception handler.
151 * This implementation throws the exception.
152 *
153 * \return value
154 */
156 {
157 return this->action(static_cast<const JException&>(*this));
158 }
159
160
161 /**
162 * Implementation of exception handler.
163 * This implementation throws the exception.
164 *
165 * \param error error
166 * \return value
167 */
168 virtual result_type action(const JException& error) const
169 {
170 throw error;
171 }
172 };
173
174
175 /**
176 * Exception handler for functional object using default result.
177 */
179 public JExceptionHandler
180 {
181 /**
182 * Constructor.
183 *
184 * \param value default result in case of exception
185 */
188 defaultResult(value)
189 {}
190
191
192 /**
193 * Constructor.
194 *
195 * \param value default result in case of exception
196 */
200 {}
201
202
203 /**
204 * Implementation of exception handler.
205 * This implementation returns the default value.
206 *
207 * \param error error
208 * \return default value
209 */
210 virtual result_type action(const JException& error) const override
211 {
212 return defaultResult;
213 }
214
215 private:
217 };
218
219
220 /**
221 * Place holder for exception handler.
222 */
224 public JSharedPointer<JExceptionHandler>
225 {
226
228
229 public:
230 /**
231 * Default constructor.
232 */
236
237
238 /**
239 * Constructor
240 *
241 * \param exception_handler pointer to exception handler
242 */
243 JSupervisor(JExceptionHandler* exception_handler) :
244 supervisor_type(exception_handler)
245 {}
246
247
248 /**
249 * Get reference to unique instance of this class object.
250 *
251 * \return supervisor
252 */
253 static const JSupervisor& getInstance()
254 {
255 static const JSupervisor supervisor;
256
257 return supervisor;
258 }
259
260
261 /**
262 * Set exception handler of given functional object.
263 *
264 * \param function function
265 * \return this supervisor
266 */
267 const JSupervisor& operator()(functional_type& function) const
268 {
269 function.setExceptionHandler(*this);
270
271 return *this;
272 }
273 };
274
275
276 /**
277 * Get supervisor.
278 *
279 * \return supervisor
280 */
282 {
283 return supervisor;
284 }
285
286
287 /**
288 * Get exception handler.
289 *
290 * \return exception handler
291 */
293 {
294 return *supervisor;
295 }
296
297
298 /**
299 * Set the supervisor for handling of exceptions.
300 *
301 * \param supervisor supervisor
302 */
304 {
305 this->supervisor = supervisor;
306 }
307
308
309 protected:
311 };
312
313
314 /**
315 * Template definition of function object interface in multidimensions.
316 */
317 template<class JArgument_t, class JResult_t>
318 struct JFunction :
319 public virtual JFunctional<JArgument_t, JResult_t>
320 {
324 };
325
326
327 /**
328 * Template definition of function object interface in one dimension.
329 * This class provides for the standard function operator <tt>()</tt>.
330 */
331 template<class JArgument_t, class JResult_t>
332 struct JFunction1D :
333 public virtual JFunctional<JArgument_t, JResult_t>
334 {
336
340
341
342 /**
343 * Function value evaluation.
344 *
345 * \param x argument value
346 * \return function value
347 */
349 {
350 return this->evaluate(&x);
351 }
352 };
353
354
355 /**
356 * Functional object compiler.
357 */
358 struct JCompiler {
359 /**
360 * Default constructor.
361 */
363 {}
364
365
366 /**
367 * Compile function.
368 *
369 * \param function function
370 * \return this compiler
371 */
372 const JCompiler& operator()(JFunctional<>& function) const
373 {
374 function.compile();
375
376 return *this;
377 }
378 };
379
380
381 /**
382 * Function object for functional object compilation.
383 */
384 static const JCompiler compiler;
385
386
387 /**
388 * Auxiliary class to evaluate result type.
389 * The result type is the actual data type.
390 */
391 template<class JClass_t, class JResultType_t = void>
392 struct JResultType {
393
394 typedef JClass_t result_type;
395 };
396
397
398 /**
399 * Auxiliary class to evaluate result type.
400 * The result type is the result type of the function object.
401 */
402 template<class JClass_t>
403 struct JResultType<JClass_t, typename JVoid<typename JClass_t::result_type>::type> {
404
405 typedef typename JClass_t::result_type result_type;
406 };
407}
408
409#endif
Exceptions.
Definition of zero value for any class.
General exception.
Definition JException.hh:24
The template JSharedPointer class can be used to share a pointer to an object.
Place holder for exception handler.
JSupervisor()
Default constructor.
static const JSupervisor & getInstance()
Get reference to unique instance of this class object.
JSharedPointer< JExceptionHandler > supervisor_type
JSupervisor(JExceptionHandler *exception_handler)
Constructor.
const JSupervisor & operator()(functional_type &function) const
Set exception handler of given functional object.
virtual ~JFunctional()
Virtual destructor.
void compile()
Function compilation.
virtual void do_compile()=0
Function compilation.
Template definition of function object interface.
JArgument_t argument_type
JSupervisor supervisor
JSupervisor supervisor_type
const JExceptionHandler & getExceptionHandler() const
Get exception handler.
JFunctional< argument_type, result_type > functional_type
static JClass< result_type >::argument_type getValue(typename JClass< result_type >::argument_type value, const argument_type *pX)
Termination of recursive function value evaluation.
virtual result_type evaluate(const argument_type *pX) const =0
Recursive function value evaluation.
void setExceptionHandler(const JSupervisor &supervisor)
Set the supervisor for handling of exceptions.
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
JSupervisor getSupervisor() const
Get supervisor.
JFunctional()
Default constructor.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
static const JCompiler compiler
Function object for functional object compilation.
virtual void do_compile() override
Function compilation.
Definition JPolint.hh:860
Template for generic class types.
Definition JClass.hh:80
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary class for no type definition.
Definition JNullType.hh:19
Auxiliary class for void type definition.
Definition JVoid.hh:21
Auxiliary class to assign zero value.
Definition JZero.hh:81
Functional object compiler.
const JCompiler & operator()(JFunctional<> &function) const
Compile function.
JCompiler()
Default constructor.
Template definition of function object interface in one dimension.
result_type operator()(const argument_type x) const
Function value evaluation.
functional_type::result_type result_type
functional_type::argument_type argument_type
JFunctional< JArgument_t, JResult_t > functional_type
Template definition of function object interface in multidimensions.
functional_type::result_type result_type
JFunctional< JArgument_t, JResult_t > functional_type
functional_type::argument_type argument_type
Exception handler for functional object using default result.
JDefaultResult(const JMATH::JZero &value)
Constructor.
JDefaultResult(const result_type value)
Constructor.
virtual result_type action(const JException &error) const override
Implementation of exception handler.
Exception handler for functional object.
result_type action() const
Implementation of exception handler.
JExceptionHandler()
Default constructor.
virtual ~JExceptionHandler()
Virtual destructor.
virtual result_type action(const JException &error) const
Implementation of exception handler.
Auxiliary class to evaluate result type.