CMS 3D CMS Logo

Expression.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Expression_h
2 #define PhysicsTools_Utilities_Expression_h
4 #include <ostream>
5 #include <memory>
6 
7 namespace funct {
8 
9  struct AbsExpression {
10  virtual ~AbsExpression() { }
11  virtual double operator()() const = 0;
12  virtual AbsExpression * clone() const = 0;
13  virtual std::ostream& print(std::ostream& cout) const = 0;
14  };
15 
16  template<typename F>
17  struct ExpressionT : public AbsExpression {
18  inline ExpressionT(const F& f) : _f(f) {}
19  virtual ~ExpressionT() { }
20  virtual double operator()() const { return _f(); }
21  virtual AbsExpression * clone() const { return new ExpressionT<F>(_f); }
22  virtual std::ostream& print(std::ostream& cout) const { return cout << _f; }
23  private:
24  F _f;
25  };
26 
27  struct Expression {
28  inline Expression() { }
29  template<typename F>
30  inline Expression(const F& f) : _f(new ExpressionT<F>(f)) { }
31  inline Expression(const Expression& e) : _f(e._f->clone()) { }
32  inline Expression& operator=(const Expression& e) { _f.reset(e._f->clone()); return *this; }
33  inline double operator()() const { return (*_f)(); }
34  inline std::ostream& print(std::ostream& cout) const { return _f->print(cout); }
35  private:
36  std::auto_ptr<AbsExpression> _f;
37  };
38 
39  inline std::ostream& operator<<(std::ostream& cout, const Expression& e) {
40  e.print(cout); return cout;
41  }
42 
44  virtual ~AbsFunctExpression() { }
45  virtual double operator()(double x) const = 0;
46  virtual AbsFunctExpression * clone() const = 0;
47  };
48 
49  template<typename F>
51  inline FunctExpressionT(const F& f) : _f(f) {}
52  virtual ~FunctExpressionT() { }
53  virtual double operator()(double x) const { return _f(x); }
54  virtual AbsFunctExpression * clone() const { return new FunctExpressionT<F>(_f); }
55  private:
56  F _f;
57  };
58 
59  struct FunctExpression {
60  inline FunctExpression() { }
61  template<typename F>
62  inline FunctExpression(const F& f) : _f(new FunctExpressionT<F>(f)) { }
63  inline FunctExpression(const FunctExpression& e) : _f(e._f->clone()) { }
64  inline FunctExpression& operator=(const FunctExpression& e) { _f.reset(e._f->clone()); return *this; }
65  inline double operator()(double x) const { return (*_f)(x); }
66  private:
67  std::auto_ptr<AbsFunctExpression> _f;
68  };
69 
70 }
71 
72 #endif
virtual std::ostream & print(std::ostream &cout) const
Definition: Expression.h:22
Expression(const F &f)
Definition: Expression.h:30
Definition: Abs.h:5
Expression & operator=(const Expression &e)
Definition: Expression.h:32
std::auto_ptr< AbsExpression > _f
Definition: Expression.h:36
virtual double operator()() const
Definition: Expression.h:20
virtual ~AbsFunctExpression()
Definition: Expression.h:44
virtual AbsExpression * clone() const
Definition: Expression.h:21
Expression(const Expression &e)
Definition: Expression.h:31
virtual ~FunctExpressionT()
Definition: Expression.h:52
virtual AbsExpression * clone() const =0
FunctExpression & operator=(const FunctExpression &e)
Definition: Expression.h:64
virtual ~ExpressionT()
Definition: Expression.h:19
FunctExpression(const FunctExpression &e)
Definition: Expression.h:63
double f[11][100]
virtual ~AbsExpression()
Definition: Expression.h:10
virtual double operator()() const =0
double operator()() const
Definition: Expression.h:33
virtual double operator()(double x) const
Definition: Expression.h:53
std::ostream & print(std::ostream &cout) const
Definition: Expression.h:34
virtual std::ostream & print(std::ostream &cout) const =0
FunctExpressionT(const F &f)
Definition: Expression.h:51
FunctExpression(const F &f)
Definition: Expression.h:62
double operator()(double x) const
Definition: Expression.h:65
std::ostream & operator<<(std::ostream &cout, const Expression &e)
Definition: Expression.h:39
std::auto_ptr< AbsFunctExpression > _f
Definition: Expression.h:67
virtual AbsFunctExpression * clone() const
Definition: Expression.h:54
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
ExpressionT(const F &f)
Definition: Expression.h:18