Jpp
 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::JException;
22  using JLANG::JNullType;
23  using JLANG::JVoid;
25 
26 
27  /**
28  * Template definition of function object interface.
29  */
30  template<class JArgument_t = JNullType, class JResult_t = JNullType>
31  class JFunctional;
32 
33 
34  /**
35  * Template specialisation of compilable function object.
36  */
37  template<>
39  {
40  protected:
41  /**
42  * Function compilation.
43  */
44  virtual void do_compile() = 0;
45 
46 
47  public:
48  /**
49  * Virtual destructor.
50  */
51  virtual ~JFunctional()
52  {}
53 
54 
55  /**
56  * Function compilation.
57  */
58  void compile()
59  {
60  do_compile();
61  }
62  };
63 
64 
65  /**
66  * Template definition of recursive function value evaluation.
67  */
68  template<class JArgument_t, class JResult_t>
69  class JFunctional :
70  public virtual JFunctional<JNullType, JNullType>
71  {
72  protected:
73  /**
74  * Default constructor.
75  */
78  supervisor(JSupervisor::getInstance())
79  {}
80 
81  public:
82 
83  class JSupervisor;
84 
85  typedef JArgument_t argument_type;
86  typedef JResult_t result_type;
88  typedef JSupervisor supervisor_type;
89 
90 
91  /**
92  * Recursive function value evaluation.
93  *
94  * \param pX pointer to abscissa values
95  * \return function value
96  */
97  virtual result_type evaluate(const argument_type* pX) const = 0;
98 
99 
100  /**
101  * Recursive function value evaluation.
102  *
103  * \param function function
104  * \param pX pointer to abscissa values
105  */
106  static result_type getValue(const JFunctional& function,
107  const argument_type* pX)
108  {
109  return function.evaluate(pX);
110  }
111 
112 
113  /**
114  * Termination of recursive function value evaluation.
115  *
116  * \param y result
117  * \param pX pointer to abscissa values
118  */
120  const argument_type* pX)
121  {
122  return y;
123  }
124 
125 
126  /**
127  * Exception handler for functional object.
128  */
130  {
131  /**
132  * Default constructor.
133  */
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  * \param error error
150  */
151  virtual result_type action(const JException& error) const
152  {
153  throw error;
154  }
155  };
156 
157 
158  /**
159  * Exception handler for functional object using default result.
160  */
161  struct JDefaultResult :
162  public JExceptionHandler
163  {
164  /**
165  * Constructor.
166  *
167  * \param value default result in case of exception
168  */
171  defaultResult(value)
172  {}
173 
174 
175  /**
176  * Constructor.
177  *
178  * \param value default result in case of exception
179  */
180  JDefaultResult(const JMATH::JZero& value) :
182  defaultResult()
183  {}
184 
185 
186  /**
187  * Implementation of exception handler.
188  * This implementation returns the default value.
189  *
190  * \param error error
191  * \return default value
192  */
193  virtual result_type action(const JException& error) const
194  {
195  return defaultResult;
196  }
197 
198  private:
200  };
201 
202 
203  /**
204  * Place holder for exception handler.
205  */
206  class JSupervisor :
207  public JSharedPointer<JExceptionHandler>
208  {
209 
211 
212  public:
213  /**
214  * Default constructor.
215  */
218  {}
219 
220 
221  /**
222  * Constructor
223  *
224  * \param exception_handler pointer to exception handler
225  */
226  JSupervisor(JExceptionHandler* exception_handler) :
227  supervisor_type(exception_handler)
228  {}
229 
230 
231  /**
232  * Get reference to unique instance of this class object.
233  *
234  * \return supervisor
235  */
236  static const JSupervisor& getInstance()
237  {
238  static const JSupervisor supervisor;
239 
240  return supervisor;
241  }
242 
243 
244  /**
245  * Set exception handler of given functional object.
246  *
247  * \param function function
248  * \return this supervisor
249  */
250  const JSupervisor& operator()(functional_type& function) const
251  {
252  function.setExceptionHandler(*this);
253 
254  return *this;
255  }
256  };
257 
258 
259  /**
260  * Get supervisor.
261  *
262  * \return supervisor
263  */
264  JSupervisor getSupervisor() const
265  {
266  return supervisor;
267  }
268 
269 
270  /**
271  * Get exception handler.
272  *
273  * \return exception handler
274  */
275  const JExceptionHandler& getExceptionHandler() const
276  {
277  return *supervisor;
278  }
279 
280 
281  /**
282  * Set the supervisor for handling of exceptions.
283  *
284  * \param supervisor supervisor
285  */
286  void setExceptionHandler(const JSupervisor& supervisor)
287  {
288  this->supervisor = supervisor;
289  }
290 
291 
292  protected:
293  JSupervisor supervisor;
294  };
295 
296 
297  /**
298  * Template definition of function object interface in multidimensions.
299  */
300  template<class JArgument_t, class JResult_t>
301  class JFunction :
302  public virtual JFunctional<JArgument_t, JResult_t>
303  {
304  public:
305 
309  };
310 
311 
312  /**
313  * Template definition of function object interface in one dimension.
314  * This class provides for the JFunction<>::() operator.
315  */
316  template<class JArgument_t, class JResult_t>
317  struct JFunction1D :
318  public virtual JFunctional<JArgument_t, JResult_t>
319  {
320  enum { NUMBER_OF_DIMENSIONS = 1 };
321 
325 
326 
327  /**
328  * Function value evaluation.
329  *
330  * \param x argument value
331  * \return function value
332  */
334  {
335  return this->evaluate(&x);
336  }
337  };
338 
339 
340  /**
341  * Functional object compiler.
342  */
343  struct JCompiler {
344  /**
345  * Default constructor.
346  */
348  {}
349 
350 
351  /**
352  * Compile function.
353  *
354  * \param function function
355  * \return this compiler
356  */
357  const JCompiler& operator()(JFunctional<>& function) const
358  {
359  function.compile();
360 
361  return *this;
362  }
363  };
364 
365 
366  /**
367  * Function object for functional object compilation.
368  */
369  static const JCompiler compiler;
370 
371 
372  /**
373  * Auxiliary class to evaluate result type.
374  * The result type is the actual data type.
375  */
376  template<class JClass_t, class JResultType_t = void>
377  struct JResultType {
378 
379  typedef JClass_t result_type;
380  };
381 
382 
383  /**
384  * Auxiliary class to evaluate result type.
385  * The result type is the result type of the function object.
386  */
387  template<class JClass_t>
388  struct JResultType<JClass_t, typename JVoid<typename JClass_t::result_type>::type> {
389 
390  typedef typename JClass_t::result_type result_type;
391  };
392 }
393 
394 #endif
virtual ~JFunctional()
Virtual destructor.
Definition: JFunctional.hh:51
JFunctional()
Default constructor.
Definition: JFunctional.hh:76
General exception.
Definition: JException.hh:40
const JCompiler & operator()(JFunctional<> &function) const
Compile function.
Definition: JFunctional.hh:357
virtual result_type action(const JException &error) const
Implementation of exception handler.
Definition: JFunctional.hh:151
Exceptions.
JDefaultResult(const result_type value)
Constructor.
Definition: JFunctional.hh:169
Functional object compiler.
Definition: JFunctional.hh:343
JCompiler()
Default constructor.
Definition: JFunctional.hh:347
JExceptionHandler()
Default constructor.
Definition: JFunctional.hh:134
result_type operator()(const argument_type x) const
Function value evaluation.
Definition: JFunctional.hh:333
functional_type::argument_type argument_type
Definition: JFunctional.hh:323
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:306
static const JCompiler compiler
Function object for functional object compilation.
Definition: JFunctional.hh:369
JSupervisor()
Default constructor.
Definition: JFunctional.hh:216
static result_type getValue(typename JClass< result_type >::argument_type y, const argument_type *pX)
Termination of recursive function value evaluation.
Definition: JFunctional.hh:119
Definition of zero value for any class.
JSupervisor supervisor
Definition: JFunctional.hh:293
static const JSupervisor & getInstance()
Get reference to unique instance of this class object.
Definition: JFunctional.hh:236
Place holder for exception handler.
Definition: JFunctional.hh:206
Template definition of function object interface in one dimension.
Definition: JFunctional.hh:317
virtual result_type evaluate(const argument_type *pX) const =0
Recursive function value evaluation.
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:106
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:301
Auxiliary class to assign zero value.
Definition: JZero.hh:70
The template JSharedPointer class can be used to share a pointer to an object.
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:322
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
JArgument_t argument_type
Definition: JFunctional.hh:83
virtual result_type action(const JException &error) const
Implementation of exception handler.
Definition: JFunctional.hh:193
Template definition of function object interface.
Definition: JFunctional.hh:31
Auxiliary class for no type definition.
Definition: JNullType.hh:19
functional_type::result_type result_type
Definition: JFunctional.hh:324
JSupervisor(JExceptionHandler *exception_handler)
Constructor.
Definition: JFunctional.hh:226
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
virtual ~JExceptionHandler()
Virtual destructor.
Definition: JFunctional.hh:141
const JSupervisor & operator()(functional_type &function) const
Set exception handler of given functional object.
Definition: JFunctional.hh:250
Exception handler for functional object.
Definition: JFunctional.hh:129
void setExceptionHandler(const JSupervisor &supervisor)
Set the supervisor for handling of exceptions.
Definition: JFunctional.hh:286
JSharedPointer< JExceptionHandler > supervisor_type
Definition: JFunctional.hh:210
JFunctional< argument_type, result_type > functional_type
Definition: JFunctional.hh:87
const JExceptionHandler & getExceptionHandler() const
Get exception handler.
Definition: JFunctional.hh:275
JSupervisor getSupervisor() const
Get supervisor.
Definition: JFunctional.hh:264
JResult_t result_type
Definition: JFunctional.hh:86
functional_type::argument_type argument_type
Definition: JFunctional.hh:307
Auxiliary class for void type definition.
Definition: JVoid.hh:20
JDefaultResult(const JMATH::JZero &value)
Constructor.
Definition: JFunctional.hh:180
functional_type::result_type result_type
Definition: JFunctional.hh:308
Exception handler for functional object using default result.
Definition: JFunctional.hh:161
JSupervisor supervisor_type
Definition: JFunctional.hh:88
void compile()
Function compilation.
Definition: JFunctional.hh:58