#ifndef __JFUNCTIONAL__ #define __JFUNCTIONAL__ #include "JLang/JSharedPointer.hh" #include "JLang/JClass.hh" #include "JLang/JException.hh" namespace JTOOLS { namespace { using JLANG::JSharedPointer; using JLANG::JNullType; using JLANG::JException; } /** * Template definition of function object interface. */ template class JFunction; /** * Template specialisation of compilable function object. */ template<> class JFunction { public: /** * Virtual destructor. */ virtual ~JFunction() {} /** * Function compilation. */ virtual void compile() = 0; }; /** * Template definition of recursive function value evaluation. */ template class JFunction : public virtual JFunction { public: typedef JArgument_t argument_type; typedef JResult_t result_type; /** * Default constructor. */ JFunction() : JFunction(), supervisor(getSupervisor()) {} /** * Function value evaluation. * * \param pX pointer to argument value * \return function value */ virtual result_type evaluate(const argument_type* pX) const = 0; /** * Exception handler for function object. */ class JExceptionHandler { public: /** * Default constructor. */ JExceptionHandler() {} /** * Virtual destructor. */ virtual ~JExceptionHandler() {} /** * Implementation of exception handler. */ virtual result_type action(const JException& exception) const { throw exception; } }; /** * Exception handler for template function object using default result. */ class JDefaultResult : public JExceptionHandler { public: /** * Constructor. * * \param defaultResult default result in case of exception */ JDefaultResult(const result_type& defaultResult) : JExceptionHandler(), default_result(defaultResult) {} /** * Implementation of exception handler. */ virtual result_type action(const JException& exception) const { return default_result; } private: result_type default_result; }; /** * Place holder for exception handler. */ class JSupervisor : public JSharedPointer { public: typedef JSharedPointer JSupervisor_t; /** * Default constructor. */ JSupervisor() : JSupervisor_t(new JExceptionHandler()) {} /** * Constructor * * \param exception_handler pointer to exception handler */ template JSupervisor(JExceptionHandler_t* exception_handler) : JSupervisor_t(exception_handler) {} }; /** * Get exception handler. * * \return exception handler */ const JExceptionHandler& getExceptionHandler() const { return *supervisor; } /** * Set exception handler. * * \param exceptionHandler [supervisor of] exception handler */ void setExceptionHandler(const JSupervisor& exceptionHandler) { supervisor = exceptionHandler; } protected: JSupervisor supervisor; /** * Get default supervisor. */ static const JSupervisor& getSupervisor() { static const JSupervisor* const supervisor = new JSupervisor(); return *supervisor; } }; /** * Template definition of function object interface in one dimension. * This class implements the JFunction<>::evaluate() method. */ template class JFunction1D : public JFunction { public: typedef JFunction JFunction_t; typedef typename JFunction_t::argument_type argument_type; typedef typename JFunction_t::result_type result_type; /** * Function value evaluation. * * \param x argument value * \return function value */ virtual result_type operator()(const argument_type& x) const = 0; /** * Function value evaluation. * * \param pX pointer to argument value * \return function value */ virtual result_type evaluate(const argument_type* pX) const { return (*this)(*pX); } }; /** * Helper methods to convert JFunction1D result to function value. */ inline double get_value(const double value) { return value; } } #endif