SeExpr
ExprFuncX.h
Go to the documentation of this file.
1 /*
2  Copyright Disney Enterprises, Inc. All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License
6  and the following modification to it: Section 6 Trademarks.
7  deleted and replaced with:
8 
9  6. Trademarks. This License does not grant permission to use the
10  trade names, trademarks, service marks, or product names of the
11  Licensor and its affiliates, except as required for reproducing
12  the content of the NOTICE file.
13 
14  You may obtain a copy of the License at
15  http://www.apache.org/licenses/LICENSE-2.0
16 */
17 #ifndef _ExprFuncX_h_
18 #define _ExprFuncX_h_
19 
20 #include "ExprType.h"
21 #include "Vec.h"
22 #include "ExprNode.h"
23 
24 namespace SeExpr2 {
25 class ExprFuncNode;
26 class Interpreter;
27 class ExprVarEnv;
28 typedef std::map<std::string, double> Statistics;
29 
31 
35 class ExprFuncX {
36  public:
43  ExprFuncX(const bool threadSafe) : _threadSafe(threadSafe) {}
44 
46  virtual ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& env) const = 0;
47  virtual ExprType type() const { return _type; }
48 
51  // virtual void eval(const ExprFuncNode* node, Vec3d& result) const = 0;
53  virtual int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const = 0;
54  virtual ~ExprFuncX() {}
55 
56  bool isThreadSafe() const { return _threadSafe; }
57 
59  virtual size_t sizeInBytes() const { return 0; }
60 
62  virtual void statistics(Statistics& /*statistics*/) const {}
63 
64  protected:
65  bool _isScalar;
67 
68  private:
70 };
71 
72 class ExprFuncSimple : public ExprFuncX {
73  public:
74  ExprFuncSimple(const bool threadSafe) : ExprFuncX(threadSafe) {}
75 
76  class ArgHandle {
77  public:
78  ArgHandle(int* opData, double* fp, char** c, std::vector<int>& callStack)
79  : outFp(fp[opData[2]]), outStr(c[opData[2]]), data(reinterpret_cast<ExprFuncNode::Data*>(c[opData[1]])),
80  // TODO: put the value in opData rather than fp
81  _nargs((int)fp[opData[3]]), // TODO: would be good not to have to convert to int!
82  opData(opData + 4), fp(fp), c(c) {}
83 
84  template <int d>
86  return Vec<double, d, true>(&fp[opData[i]]);
87  }
88  char* inStr(int i) { return c[opData[i]]; }
89  int nargs() const { return _nargs; }
90 
92  template<int d>
94  return Vec<double, d, true>(&outFp);
95  }
96 
97  double& outFp;
98  char*& outStr;
100 
101  private:
102  int _nargs;
103  int* opData;
104  double* fp;
105  char** c;
106  // std::stack<int>& callStack;
107  };
108 
109  virtual int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const;
110 
111  virtual ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& envBuilder) const = 0;
112  virtual ExprFuncNode::Data* evalConstant(const ExprFuncNode* node, ArgHandle args) const = 0;
113  virtual void eval(ArgHandle args) = 0;
114 
115  private:
116  static int EvalOp(int* opData, double* fp, char** c, std::vector<int>& callStack);
117 };
118 
119 class ExprFuncLocal : public ExprFuncX {
121 
123  virtual ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& envBuilder) const;
125  virtual int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const;
126 };
127 }
128 
129 #endif
ArgHandle(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.h:78
virtual void eval(ArgHandle args)=0
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const =0
Build an interpreter to evaluate the expression.
virtual void statistics(Statistics &) const
Give this function a chance to populate its statistics.
Definition: ExprFuncX.h:62
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const =0
Vec< double, d, true > outFpHandle()
Return a vector handle which is easier to assign to.
Definition: ExprFuncX.h:93
ExprFuncNode::Data * data
Definition: ExprFuncX.h:99
Vec< double, d, true > inFp(int i)
Definition: ExprFuncX.h:85
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
Node that calls a function.
Definition: ExprNode.h:514
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:97
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:74
virtual ExprType type() const
Definition: ExprFuncX.h:47
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const =0
virtual size_t sizeInBytes() const
Return memory usage of a funcX in bytes.
Definition: ExprFuncX.h:59
ExprType _type
Definition: ExprFuncX.h:66
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &env) const =0
base class for custom instance data
Definition: ExprNode.h:564
virtual ~ExprFuncX()
Definition: ExprFuncX.h:54
bool isThreadSafe() const
Definition: ExprFuncX.h:56
static int EvalOp(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.cpp:24
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
Definition: ExprFuncX.cpp:32
ExprFuncX(const bool threadSafe)
Definition: ExprFuncX.h:43
Extension function spec, used for complicated argument custom functions.
Definition: ExprFuncX.h:35
std::map< std::string, double > Statistics
Definition: ExprFuncX.h:27
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:152