Jpp
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  {
133  /**
134  * Default constructor.
135  */
137  {}
138 
139 
140  /**
141  * Virtual destructor.
142  */
144  {}
145 
146 
147  /**
148  * Implementation of exception handler.
149  * This implementation throws the exception.
150  *
151  * \param error error
152  */
153  virtual result_type action(const JException& error) const
154  {
155  throw error;
156  }
157  };
158 
159 
160  /**
161  * Exception handler for functional object using default result.
162  */
163  struct JDefaultResult :
164  public JExceptionHandler
165  {
166  /**
167  * Constructor.
168  *
169  * \param value default result in case of exception
170  */
173  defaultResult(value)
174  {}
175 
176 
177  /**
178  * Constructor.
179  *
180  * \param value default result in case of exception
181  */
182  JDefaultResult(const JMATH::JZero& value) :
184  defaultResult()
185  {}
186 
187 
188  /**
189  * Implementation of exception handler.
190  * This implementation returns the default value.
191  *
192  * \param error error
193  * \return default value
194  */
195  virtual result_type action(const JException& error) const
196  {
197  return defaultResult;
198  }
199 
200  private:
202  };
203 
204 
205  /**
206  * Place holder for exception handler.
207  */
208  class JSupervisor :
209  public JSharedPointer<JExceptionHandler>
210  {
211 
213 
214  public:
215  /**
216  * Default constructor.
217  */
220  {}
221 
222 
223  /**
224  * Constructor
225  *
226  * \param exception_handler pointer to exception handler
227  */
228  JSupervisor(JExceptionHandler* exception_handler) :
229  supervisor_type(exception_handler)
230  {}
231 
232 
233  /**
234  * Get reference to unique instance of this class object.
235  *
236  * \return supervisor
237  */
238  static const JSupervisor& getInstance()
239  {
240  static const JSupervisor supervisor;
241 
242  return supervisor;
243  }
244 
245 
246  /**
247  * Set exception handler of given functional object.
248  *
249  * \param function function
250  * \return this supervisor
251  */
252  const JSupervisor& operator()(functional_type& function) const
253  {
254  function.setExceptionHandler(*this);
255 
256  return *this;
257  }
258  };
259 
260 
261  /**
262  * Get supervisor.
263  *
264  * \return supervisor
265  */
266  JSupervisor getSupervisor() const
267  {
268  return supervisor;
269  }
270 
271 
272  /**
273  * Get exception handler.
274  *
275  * \return exception handler
276  */
277  const JExceptionHandler& getExceptionHandler() const
278  {
279  return *supervisor;
280  }
281 
282 
283  /**
284  * Set the supervisor for handling of exceptions.
285  *
286  * \param supervisor supervisor
287  */
288  void setExceptionHandler(const JSupervisor& supervisor)
289  {
290  this->supervisor = supervisor;
291  }
292 
293 
294  protected:
295  JSupervisor supervisor;
296  };
297 
298 
299  /**
300  * Template definition of function object interface in multidimensions.
301  */
302  template<class JArgument_t, class JResult_t>
303  struct JFunction :
304  public virtual JFunctional<JArgument_t, JResult_t>
305  {
309  };
310 
311 
312  /**
313  * Template definition of function object interface in one dimension.
314  * This class provides for the standard function operator <tt>()</tt>.
315  */
316  template<class JArgument_t, class JResult_t>
317  struct JFunction1D :
318  public JFunction<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
JTOOLS::JFunctional::supervisor
JSupervisor supervisor
Definition: JFunctional.hh:295
JException.hh
JTOOLS::JFunctional::JSupervisor::getInstance
static const JSupervisor & getInstance()
Get reference to unique instance of this class object.
Definition: JFunctional.hh:238
JTOOLS::compiler
static const JCompiler compiler
Function object for functional object compilation.
Definition: JFunctional.hh:369
JTOOLS::JFunctional::JSupervisor::operator()
const JSupervisor & operator()(functional_type &function) const
Set exception handler of given functional object.
Definition: JFunctional.hh:252
JTOOLS::pX
pX
Definition: JPolint.hh:625
JZero.hh
JLANG::getInstance
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
JTOOLS::JFunctional::JDefaultResult::JDefaultResult
JDefaultResult(const JMATH::JZero &value)
Constructor.
Definition: JFunctional.hh:182
JTOOLS::JFunction
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:303
JTOOLS::JFunction1D::NUMBER_OF_DIMENSIONS
Definition: JFunctional.hh:320
JSharedPointer.hh
JLANG::JNullType
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
JTOOLS::JResultType
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
JTOOLS::JFunctional::result_type
JResult_t result_type
Definition: JFunctional.hh:87
JLANG::JClass
Template for generic class types.
Definition: JClass.hh:80
JVoid.hh
JTOOLS::JFunctional::evaluate
virtual result_type evaluate(const argument_type *pX) const =0
Recursive function value evaluation.
JTOOLS::JFunctional::JDefaultResult::defaultResult
result_type defaultResult
Definition: JFunctional.hh:201
JTOOLS::JFunctional::functional_type
JFunctional< argument_type, result_type > functional_type
Definition: JFunctional.hh:88
JTOOLS::JFunctional::getValue
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
JTOOLS::JFunctional::argument_type
JArgument_t argument_type
Definition: JFunctional.hh:84
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JFunctional::JExceptionHandler::JExceptionHandler
JExceptionHandler()
Default constructor.
Definition: JFunctional.hh:136
JTOOLS::JFunctional::supervisor_type
JSupervisor supervisor_type
Definition: JFunctional.hh:89
JTOOLS::JFunctional::getExceptionHandler
const JExceptionHandler & getExceptionHandler() const
Get exception handler.
Definition: JFunctional.hh:277
JTOOLS::JFunctional::JDefaultResult::JDefaultResult
JDefaultResult(const result_type value)
Constructor.
Definition: JFunctional.hh:171
JTOOLS::JFunction1D
Template definition of function object interface in one dimension.
Definition: JFunctional.hh:317
JTOOLS::JFunctional::JSupervisor::supervisor_type
JSharedPointer< JExceptionHandler > supervisor_type
Definition: JFunctional.hh:212
JTOOLS::JResultType::result_type
JClass_t result_type
Definition: JFunctional.hh:379
JTOOLS::JFunctional::JSupervisor::JSupervisor
JSupervisor(JExceptionHandler *exception_handler)
Constructor.
Definition: JFunctional.hh:228
JTOOLS::JCompiler
Functional object compiler.
Definition: JFunctional.hh:343
JTOOLS::JFunctional::JSupervisor
Place holder for exception handler.
Definition: JFunctional.hh:208
JMATH::JZero
Auxiliary class to assign zero value.
Definition: JZero.hh:70
JTOOLS::JFunctional::JDefaultResult
Exception handler for functional object using default result.
Definition: JFunctional.hh:163
JTOOLS::JFunction::argument_type
functional_type::argument_type argument_type
Definition: JFunctional.hh:307
JTOOLS::JFunction1D::operator()
result_type operator()(const argument_type x) const
Function value evaluation.
Definition: JFunctional.hh:333
JTOOLS::JFunctional< JNullType, JNullType >::compile
void compile()
Function compilation.
Definition: JFunctional.hh:59
JTOOLS::JFunction::result_type
functional_type::result_type result_type
Definition: JFunctional.hh:308
JTOOLS::JFunctional< JNullType, JNullType >::~JFunctional
virtual ~JFunctional()
Virtual destructor.
Definition: JFunctional.hh:52
JTOOLS::JFunctional::JSupervisor::JSupervisor
JSupervisor()
Default constructor.
Definition: JFunctional.hh:218
JLANG::JVoid
Auxiliary class for void type definition.
Definition: JVoid.hh:20
JTOOLS::JFunction::functional_type
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:306
JNullType.hh
JClass.hh
JTOOLS::JFunctional::JExceptionHandler::action
virtual result_type action(const JException &error) const
Implementation of exception handler.
Definition: JFunctional.hh:153
JTOOLS::JCompiler::operator()
const JCompiler & operator()(JFunctional<> &function) const
Compile function.
Definition: JFunctional.hh:357
JTOOLS::do_compile
virtual void do_compile()
Function compilation.
Definition: JPolint.hh:702
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JFunctional::JExceptionHandler::~JExceptionHandler
virtual ~JExceptionHandler()
Virtual destructor.
Definition: JFunctional.hh:143
JTOOLS::JFunctional::getSupervisor
JSupervisor getSupervisor() const
Get supervisor.
Definition: JFunctional.hh:266
JTOOLS::JFunctional
Template definition of function object interface.
Definition: JFunctional.hh:32
JTOOLS::JFunction1D::argument_type
functional_type::argument_type argument_type
Definition: JFunctional.hh:323
JTOOLS::JFunctional::setExceptionHandler
void setExceptionHandler(const JSupervisor &supervisor)
Set the supervisor for handling of exceptions.
Definition: JFunctional.hh:288
JTOOLS::JFunction1D::result_type
functional_type::result_type result_type
Definition: JFunctional.hh:324
JLANG::JException
General exception.
Definition: JException.hh:40
JTOOLS::JFunctional::JFunctional
JFunctional()
Default constructor.
Definition: JFunctional.hh:77
JTOOLS::JFunction1D::functional_type
JFunctional< JArgument_t, JResult_t > functional_type
Definition: JFunctional.hh:322
JTOOLS::JFunctional::JExceptionHandler
Exception handler for functional object.
Definition: JFunctional.hh:131
JLANG::JSharedPointer
The template JSharedPointer class can be used to share a pointer to an object.
Definition: JSharedPointer.hh:28
JTOOLS::JFunctional::getValue
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
JTOOLS::JFunctional::JDefaultResult::action
virtual result_type action(const JException &error) const
Implementation of exception handler.
Definition: JFunctional.hh:195
JTOOLS::JCompiler::JCompiler
JCompiler()
Default constructor.
Definition: JFunctional.hh:347
JTOOLS::JResultType< JClass_t, typename JVoid< typename JClass_t::result_type >::type >::result_type
JClass_t::result_type result_type
Definition: JFunctional.hh:390