Jpp 19.3.0-rc.2
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
4#include <memory>
5
6#include "JLang/JNullType.hh"
7#include "JLang/JException.hh"
8#include "JLang/JVoid.hh"
9#include "JLang/JClass.hh"
10#include "JMath/JZero.hh"
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JTOOLS {}
18namespace JPP { using namespace JTOOLS; }
19
20namespace JTOOLS {
21
22 using JLANG::JClass;
24 using JLANG::JNullType;
25 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<>
72 {
73 protected:
74 /**
75 * Default constructor.
76 */
78 JFunctional<>()
79 {}
80
81 public:
82
83 typedef JArgument_t argument_type;
84 typedef JResult_t result_type;
86
87
88 /**
89 * Recursive function value evaluation.
90 *
91 * \param pX pointer to abscissa values
92 * \return function value
93 */
94 virtual result_type evaluate(const argument_type* pX) const = 0;
95
96
97 /**
98 * Recursive function value evaluation.
99 *
100 * \param function function
101 * \param pX pointer to abscissa values
102 */
103 static result_type getValue(const JFunctional& function,
104 const argument_type* pX)
105 {
106 return function.evaluate(pX);
107 }
108
109
110 /**
111 * Termination of recursive function value evaluation.
112 *
113 * \param value result
114 * \param pX pointer to abscissa values
115 */
118 const argument_type* pX)
119 {
120 return value;
121 }
122
123
124 /**
125 * Exception handler for functional object.
126 */
128 private JException
129 {
130 /**
131 * Default constructor.
132 */
134 JException("unknown")
135 {}
136
137
138 /**
139 * Virtual destructor.
140 */
142 {}
143
144
145 /**
146 * Implementation of exception handler.
147 * This implementation throws the exception.
148 *
149 * \return value
150 */
152 {
153 return this->action(static_cast<const JException&>(*this));
154 }
155
156
157 /**
158 * Implementation of exception handler.
159 * This implementation throws the exception.
160 *
161 * \param error error
162 * \return value
163 */
164 virtual result_type action(const JException& error) const
165 {
166 throw error;
167 }
168 };
169
170
171 /**
172 * Exception handler for functional object using default result.
173 */
175 public JExceptionHandler
176 {
177 /**
178 * Constructor.
179 *
180 * \param value default result in case of exception
181 */
186
187
188 /**
189 * Constructor.
190 *
191 * \param value default result in case of exception
192 */
197
198
199 /**
200 * Implementation of exception handler.
201 * This implementation returns the default value.
202 *
203 * \param error error
204 * \return default value
205 */
206 virtual result_type action(const JException& error) const override
207 {
208 return value;
209 }
210
211 private:
213 };
214
215
216 /**
217 * Get exception handler.
218 *
219 * \return exception handler
220 */
221 virtual const JExceptionHandler& getExceptionHandler() const = 0;
222 };
223
224
225 /**
226 * Template definition of function object interface in multidimensions.
227 */
228 template<class JArgument_t, class JResult_t>
229 struct JFunction :
230 public virtual JFunctional<JArgument_t, JResult_t>
231 {
236 class JSupervisor;
238
239
240 /**
241 * Place holder for exception handler.
242 */
244 {
245 public:
246
247 friend JFunction;
248
249 /**
250 * Default constructor.
251 */
254 {}
255
256
257 /**
258 * Constructor
259 *
260 * \param handler pointer to exception handler
261 */
265
266
267 /**
268 * Set exception handler of given functional object.
269 *
270 * \param function function
271 * \return this supervisor
272 */
273 const JSupervisor& operator()(JFunction& function) const
274 {
275 function.setExceptionHandler(*this);
276
277 return *this;
278 }
279
280 private:
281 std::shared_ptr<JExceptionHandler> handler;
282 };
283
284
285 /**
286 * Get supervisor.
287 *
288 * \return supervisor
289 */
291 {
292 return __supervisor;
293 }
294
295
296 /**
297 * Get exception handler.
298 *
299 * \return exception handler
300 */
302 {
303 return *__supervisor.handler;
304 }
305
306
307 /**
308 * Set the supervisor for handling of exceptions.
309 *
310 * \param supervisor supervisor
311 */
312 void setExceptionHandler(const JSupervisor& supervisor)
313 {
314 this->__supervisor = supervisor;
315 }
316
317 protected:
319 };
320
321
322 /**
323 * Template definition of function object interface in one dimension.
324 * This class provides for the standard function operator <tt>()</tt>.
325 */
326 template<class JArgument_t, class JResult_t>
327 struct JFunction1D :
328 public JFunction<JArgument_t, JResult_t>
329 {
331
335
336
337 /**
338 * Function value evaluation.
339 *
340 * \param x argument value
341 * \return function value
342 */
344 {
345 return this->evaluate(&x);
346 }
347 };
348
349
350 /**
351 * Functional object compiler.
352 */
353 struct JCompiler {
354 /**
355 * Default constructor.
356 */
358 {}
359
360
361 /**
362 * Compile function.
363 *
364 * \param function function
365 * \return this compiler
366 */
367 const JCompiler& operator()(JFunctional<>& function) const
368 {
369 function.compile();
370
371 return *this;
372 }
373 };
374
375
376 /**
377 * Function object for functional object compilation.
378 */
379 static const JCompiler compiler;
380
381
382 /**
383 * Auxiliary class to evaluate result type.
384 * The result type is the actual data type.
385 */
386 template<class JClass_t, class JResultType_t = void>
387 struct JResultType {
388
389 typedef JClass_t result_type;
390 };
391
392
393 /**
394 * Auxiliary class to evaluate result type.
395 * The result type is the result type of the function object.
396 */
397 template<class JClass_t>
398 struct JResultType<JClass_t, typename JVoid<typename JClass_t::result_type>::type> {
399
400 typedef typename JClass_t::result_type result_type;
401 };
402}
403
404#endif
Exceptions.
Definition of zero value for any class.
General exception.
Definition JException.hh:24
Place holder for exception handler.
JSupervisor(JExceptionHandler *handler)
Constructor.
std::shared_ptr< JExceptionHandler > handler
const JSupervisor & operator()(JFunction &function) const
Set exception handler of given functional object.
JSupervisor()
Default constructor.
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
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.
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
virtual const JExceptionHandler & getExceptionHandler() const =0
Get exception handler.
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:869
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
const JSupervisor & getSupervisor() const
Get supervisor.
JFunctional< JArgument_t, JResult_t > functional_type
JSupervisor supervisor_type
functional_type::argument_type argument_type
JSupervisor __supervisor
functional_type::JExceptionHandler JExceptionHandler
void setExceptionHandler(const JSupervisor &supervisor)
Set the supervisor for handling of exceptions.
const JExceptionHandler & getExceptionHandler() const override
Get exception handler.
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.