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 
24  private:
25  F _f;
26  };
27 
28  struct Expression {
29  inline Expression() {}
30  template <typename F>
31  inline Expression(const F& f) : _f(new ExpressionT<F>(f)) {}
32  inline Expression(const Expression& e) : _f(e._f->clone()) {}
33  inline Expression& operator=(const Expression& e) {
34  _f.reset(e._f->clone());
35  return *this;
36  }
37  inline double operator()() const { return (*_f)(); }
38  inline std::ostream& print(std::ostream& cout) const { return _f->print(cout); }
39 
40  private:
41  std::unique_ptr<AbsExpression> _f;
42  };
43 
44  inline std::ostream& operator<<(std::ostream& cout, const Expression& e) {
45  e.print(cout);
46  return cout;
47  }
48 
50  virtual ~AbsFunctExpression() {}
51  virtual double operator()(double x) const = 0;
52  virtual AbsFunctExpression* clone() const = 0;
53  };
54 
55  template <typename F>
57  inline FunctExpressionT(const F& f) : _f(f) {}
58  virtual ~FunctExpressionT() {}
59  virtual double operator()(double x) const { return _f(x); }
60  virtual AbsFunctExpression* clone() const { return new FunctExpressionT<F>(_f); }
61 
62  private:
63  F _f;
64  };
65 
66  struct FunctExpression {
67  inline FunctExpression() {}
68  template <typename F>
69  inline FunctExpression(const F& f) : _f(new FunctExpressionT<F>(f)) {}
70  inline FunctExpression(const FunctExpression& e) : _f(e._f->clone()) {}
72  _f.reset(e._f->clone());
73  return *this;
74  }
75  inline double operator()(double x) const { return (*_f)(x); }
76 
77  private:
78  std::unique_ptr<AbsFunctExpression> _f;
79  };
80 
81 } // namespace funct
82 
83 #endif
virtual std::ostream & print(std::ostream &cout) const
Definition: Expression.h:22
Expression(const F &f)
Definition: Expression.h:31
Definition: Abs.h:5
Expression & operator=(const Expression &e)
Definition: Expression.h:33
std::unique_ptr< AbsFunctExpression > _f
Definition: Expression.h:78
virtual double operator()() const
Definition: Expression.h:20
virtual ~AbsFunctExpression()
Definition: Expression.h:50
virtual AbsExpression * clone() const
Definition: Expression.h:21
Expression(const Expression &e)
Definition: Expression.h:32
virtual ~FunctExpressionT()
Definition: Expression.h:58
virtual AbsExpression * clone() const =0
FunctExpression & operator=(const FunctExpression &e)
Definition: Expression.h:71
virtual ~ExpressionT()
Definition: Expression.h:19
FunctExpression(const FunctExpression &e)
Definition: Expression.h:70
double f[11][100]
virtual ~AbsExpression()
Definition: Expression.h:10
virtual double operator()() const =0
double operator()() const
Definition: Expression.h:37
virtual double operator()(double x) const
Definition: Expression.h:59
std::ostream & print(std::ostream &cout) const
Definition: Expression.h:38
virtual std::ostream & print(std::ostream &cout) const =0
FunctExpressionT(const F &f)
Definition: Expression.h:57
FunctExpression(const F &f)
Definition: Expression.h:69
std::unique_ptr< AbsExpression > _f
Definition: Expression.h:41
double operator()(double x) const
Definition: Expression.h:75
std::ostream & operator<<(std::ostream &cout, const Expression &e)
Definition: Expression.h:44
virtual AbsFunctExpression * clone() const
Definition: Expression.h:60
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
ExpressionT(const F &f)
Definition: Expression.h:18