Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
16 namespace JTOOLS {}
17 namespace JPP { using namespace JTOOLS; }
18 
19 namespace JTOOLS {
20 
21  using JLANG::JClass;
22  using JLANG::JException;
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;
89  typedef JSupervisor supervisor_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  */
120  static typename JClass<result_type>::argument_type
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  */
178  struct JDefaultResult :
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  */
197  JDefaultResult(const JMATH::JZero& value) :
199  defaultResult()
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  */
223  class JSupervisor :
224  public JSharedPointer<JExceptionHandler>
225  {
226 
228 
229  public:
230  /**
231  * Default constructor.
232  */
235  {}
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  */
281  JSupervisor getSupervisor() const
282  {
283  return supervisor;
284  }
285 
286 
287  /**
288  * Get exception handler.
289  *
290  * \return exception handler
291  */
292  const JExceptionHandler& getExceptionHandler() const
293  {
294  return *supervisor;
295  }
296 
297 
298  /**
299  * Set the supervisor for handling of exceptions.
300  *
301  * \param supervisor supervisor
302  */
303  void setExceptionHandler(const JSupervisor& supervisor)
304  {
305  this->supervisor = supervisor;
306  }
307 
308 
309  protected:
310  JSupervisor supervisor;
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  {
335  enum { NUMBER_OF_DIMENSIONS = 1 };
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
virtual ~JFunctional()
Virtual destructor.
Definition: JFunctional.hh:52
JFunctional()
Default constructor.
Definition: JFunctional.hh:77
General exception.
Definition: JException.hh:24
const JCompiler & operator()(JFunctional<> &function) const
Compile function.
Definition: JFunctional.hh:372
virtual result_type action(const JException &error) const
Implementation of exception handler.
Definition: JFunctional.hh:168
Exceptions.
JDefaultResult(const result_type value)
Constructor.
Definition: JFunctional.hh:186
Functional object compiler.
Definition: JFunctional.hh:358
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:321
JCompiler()
Default constructor.
Definition: JFunctional.hh:362
JExceptionHandler()
Default constructor.
Definition: JFunctional.hh:137
result_type operator()(const argument_type x) const
Function value evaluation.
Definition: JFunctional.hh:348
functional_type::argument_type argument_type
Definition: JFunctional.hh:338
static const JCompiler compiler
Function object for functional object compilation.
Definition: JFunctional.hh:384
JSupervisor()
Default constructor.
Definition: JFunctional.hh:233
virtual void do_compile() override
Function compilation.
Definition: JPolint.hh:860
Definition of zero value for any class.
JSupervisor supervisor
Definition: JFunctional.hh:310
virtual result_type action(const JException &error) const override
Implementation of exception handler.
Definition: JFunctional.hh:210
static const JSupervisor & getInstance()
Get reference to unique instance of this class object.
Definition: JFunctional.hh:253
Place holder for exception handler.
Definition: JFunctional.hh:223
Template definition of function object interface in one dimension.
Definition: JFunctional.hh:332
virtual result_type evaluate(const argument_type *pX) const =0
Recursive function value evaluation.
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
Auxiliary class to assign zero value.
Definition: JZero.hh:81
The template JSharedPointer class can be used to share a pointer to an object.
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:337
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
JArgument_t argument_type
Definition: JFunctional.hh:84
Template definition of function object interface.
Definition: JFunctional.hh:32
Auxiliary class for no type definition.
Definition: JNullType.hh:19
functional_type::result_type result_type
Definition: JFunctional.hh:339
JSupervisor(JExceptionHandler *exception_handler)
Constructor.
Definition: JFunctional.hh:243
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:392
virtual ~JExceptionHandler()
Virtual destructor.
Definition: JFunctional.hh:145
const JSupervisor & operator()(functional_type &function) const
Set exception handler of given functional object.
Definition: JFunctional.hh:267
Exception handler for functional object.
Definition: JFunctional.hh:131
void setExceptionHandler(const JSupervisor &supervisor)
Set the supervisor for handling of exceptions.
Definition: JFunctional.hh:303
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:318
JSharedPointer< JExceptionHandler > supervisor_type
Definition: JFunctional.hh:227
JFunctional< argument_type, result_type > functional_type
Definition: JFunctional.hh:88
Template for generic class types.
Definition: JClass.hh:80
functional_type::result_type result_type
Definition: JFunctional.hh:323
static JClass< result_type >::argument_type getValue(typename JClass< result_type >::argument_type value, const argument_type *pX)
Termination of recursive function value evaluation.
Definition: JFunctional.hh:121
const JExceptionHandler & getExceptionHandler() const
Get exception handler.
Definition: JFunctional.hh:292
JSupervisor getSupervisor() const
Get supervisor.
Definition: JFunctional.hh:281
JResult_t result_type
Definition: JFunctional.hh:87
Auxiliary class for void type definition.
Definition: JVoid.hh:20
JDefaultResult(const JMATH::JZero &value)
Constructor.
Definition: JFunctional.hh:197
functional_type::argument_type argument_type
Definition: JFunctional.hh:322
Exception handler for functional object using default result.
Definition: JFunctional.hh:178
result_type action() const
Implementation of exception handler.
Definition: JFunctional.hh:155
JSupervisor supervisor_type
Definition: JFunctional.hh:89
void compile()
Function compilation.
Definition: JFunctional.hh:59