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  ~ExpressionT() override {}
20  double operator()() const override { return _f(); }
21  AbsExpression* clone() const override { return new ExpressionT<F>(_f); }
22  std::ostream& print(std::ostream& cout) const override { 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  ~FunctExpressionT() override {}
59  double operator()(double x) const override { return _f(x); }
60  AbsFunctExpression* clone() const override { 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
double operator()(double x) const override
Definition: Expression.h:59
AbsFunctExpression * clone() const override
Definition: Expression.h:60
Expression(const F &f)
Definition: Expression.h:31
Definition: Abs.h:5
Expression & operator=(const Expression &e)
Definition: Expression.h:33
double operator()(double x) const
Definition: Expression.h:75
virtual AbsExpression * clone() const =0
std::unique_ptr< AbsFunctExpression > _f
Definition: Expression.h:78
virtual AbsFunctExpression * clone() const =0
virtual ~AbsFunctExpression()
Definition: Expression.h:50
std::ostream & print(std::ostream &cout) const override
Definition: Expression.h:22
Expression(const Expression &e)
Definition: Expression.h:32
virtual double operator()(double x) const =0
FunctExpression & operator=(const FunctExpression &e)
Definition: Expression.h:71
~FunctExpressionT() override
Definition: Expression.h:58
std::ostream & print(std::ostream &cout) const
Definition: Expression.h:38
FunctExpression(const FunctExpression &e)
Definition: Expression.h:70
double f[11][100]
virtual ~AbsExpression()
Definition: Expression.h:10
virtual std::ostream & print(std::ostream &cout) const =0
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
virtual double operator()() 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
float x
std::ostream & operator<<(std::ostream &cout, const Expression &e)
Definition: Expression.h:44
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
double operator()() const
Definition: Expression.h:37
double operator()() const override
Definition: Expression.h:20
ExpressionT(const F &f)
Definition: Expression.h:18
AbsExpression * clone() const override
Definition: Expression.h:21
~ExpressionT() override
Definition: Expression.h:19