#ifndef OPAL_SNull_HH #define OPAL_SNull_HH // ------------------------------------------------------------------------ // $RCSfile: SNull.h,v $ // ------------------------------------------------------------------------ // $Revision: 1.1.1.1 $ // ------------------------------------------------------------------------ // Copyright: see Copyright.readme // ------------------------------------------------------------------------ // // Template class: SNull // // ------------------------------------------------------------------------ // // $Date: 2000/03/27 09:33:42 $ // $Author: Andreas Adelmann $ // // ------------------------------------------------------------------------ #include "AbstractObjects/Expressions.h" #include "Expressions/TFunction0.h" #include "Utilities/DomainError.h" #include "Utilities/OverflowError.h" #include #include namespace Expressions { // Class SNull // ---------------------------------------------------------------------- /// A scalar expression without operands. // This expression evaluates a scalar function withoud operands // (e.g. a random generator) and returns the result. template class SNull: public Scalar { public: /// Constructor. // Use an operand-less scalar function. explicit SNull(const TFunction0 &function); SNull(const SNull &); virtual ~SNull(); /// Make clone. virtual Scalar *clone() const; /// Evaluate. virtual T evaluate() const; /// Make expression. // If the function is not a random generator, evaluate it and // store the result as a constant. static Scalar *make(const TFunction0 &function); /// Print expression. virtual void print(std::ostream &str, int precedence = 99) const; private: // Not implemented. SNull(); void operator=(const SNull &); // The operation function. const TFunction0 &fun; }; // Implementation // ------------------------------------------------------------------------ template inline SNull::SNull(const SNull &rhs): Scalar(rhs), fun(rhs.fun) {} template inline SNull::SNull(const TFunction0 &function): fun(function) {} template inline SNull::~SNull() {} template inline Scalar *SNull::clone() const { return new SNull(*this); } template inline T SNull::evaluate() const { errno = 0; T result = (*fun.function)(); // Test for run-time evaluation errors. switch(errno) { case EDOM: throw DomainError("SNull:evaluate()"); case ERANGE: // Ignore underflow. if(result == T(0)) return result; throw OverflowError("SNull:evaluate()"); default: return result; } } template inline Scalar *SNull::make(const TFunction0 &fun) { return new SNull(fun); } template inline void SNull::print(std::ostream &stream, int) const { stream << fun.name << "()"; } } #endif // OPAL_SNull_HH