KSeExpr  4.0.4.0
ExprFuncX.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
2 // SPDX-License-Identifier: LicenseRef-Apache-2.0
3 // SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #pragma once
7 
8 #include "ExprType.h"
9 #include "Vec.h"
10 #include "ExprNode.h"
11 
12 namespace KSeExpr {
13 class ExprFuncNode;
14 class Interpreter;
15 class ExprVarEnv;
16 using Statistics = std::map<std::string, double>;
17 
19 
23 class ExprFuncX {
24  public:
31  ExprFuncX(const bool threadSafe) : _threadSafe(threadSafe) {}
32  ExprFuncX(const ExprFuncX &) = default;
33  ExprFuncX(ExprFuncX &&) = default;
34  ExprFuncX& operator=(const ExprFuncX &) = default;
35  ExprFuncX& operator=(ExprFuncX &&) = default;
36 
38  virtual ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& env) const = 0;
39  virtual ExprType type() const { return _type; }
40 
43  // virtual void eval(const ExprFuncNode* node, Vec3d& result) const = 0;
45  virtual int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const = 0;
46  virtual ~ExprFuncX() = default;
47 
48  bool isThreadSafe() const { return _threadSafe; }
49 
51  virtual size_t sizeInBytes() const { return 0; }
52 
54  virtual void statistics(Statistics& /*statistics*/) const {}
55 
56  protected:
57  bool _isScalar{};
59 
60  private:
62 };
63 
64 class ExprFuncSimple : public ExprFuncX {
65  public:
66  ExprFuncSimple(const bool threadSafe) : ExprFuncX(threadSafe) {}
67 
68  class ArgHandle {
69  public:
70  ArgHandle(int* opData, double* fp, char** c, std::vector<int>&)
71  : outFp(fp[opData[2]]), outStr(c[opData[2]]), data(reinterpret_cast<ExprFuncNode::Data*>(c[opData[1]])),
72  // TODO: put the value in opData rather than fp
73  _nargs((int)fp[opData[3]]), // TODO: would be good not to have to convert to int!
74  opData(opData + 4), fp(fp), c(c) {}
75 
76  template <int d>
78  return Vec<double, d, true>(&fp[opData[i]]);
79  }
80  char* inStr(int i) { return c[opData[i]]; }
81  int nargs() const { return _nargs; }
82 
84  template <int d>
86  return Vec<double, d, true>(&outFp);
87  }
88 
89  double& outFp;
90  char*& outStr;
92 
93  private:
94  int _nargs;
95  int* opData;
96  double* fp;
97  char** c;
98  // std::stack<int>& callStack;
99  };
100 
101  int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const override;
102 
103  ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& envBuilder) const override = 0;
104  virtual ExprFuncNode::Data* evalConstant(const ExprFuncNode* node, ArgHandle args) const = 0;
105  virtual void eval(ArgHandle args) = 0;
106 
107  private:
108  static int EvalOp(int* opData, double* fp, char** c, std::vector<int>& callStack);
109 };
110 
111 class ExprFuncLocal : public ExprFuncX {
113 
115  ExprType prep(ExprFuncNode* node, bool scalarWanted, ExprVarEnvBuilder& envBuilder) const override;
117  int buildInterpreter(const ExprFuncNode* node, Interpreter* interpreter) const override;
118 };
119 } // namespace KSeExpr
ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const override
int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const override
Build an interpreter to evaluate the expression.
Node that calls a function.
Definition: ExprNode.h:654
Vec< double, d, true > outFpHandle()
Return a vector handle which is easier to assign to.
Definition: ExprFuncX.h:85
ExprFuncNode::Data * data
Definition: ExprFuncX.h:91
Vec< double, d, true > inFp(int i)
Definition: ExprFuncX.h:77
ArgHandle(int *opData, double *fp, char **c, std::vector< int > &)
Definition: ExprFuncX.h:70
virtual void eval(ArgHandle args)=0
static int EvalOp(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.cpp:14
int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const override
Build an interpreter to evaluate the expression.
Definition: ExprFuncX.cpp:22
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:66
ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const override=0
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const =0
Extension function spec, used for complicated argument custom functions.
Definition: ExprFuncX.h:23
ExprFuncX & operator=(ExprFuncX &&)=default
bool isThreadSafe() const
Definition: ExprFuncX.h:48
ExprFuncX(ExprFuncX &&)=default
ExprFuncX(const ExprFuncX &)=default
ExprType _type
Definition: ExprFuncX.h:58
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const =0
Build an interpreter to evaluate the expression.
virtual size_t sizeInBytes() const
Return memory usage of a funcX in bytes.
Definition: ExprFuncX.h:51
virtual void statistics(Statistics &) const
Give this function a chance to populate its statistics.
Definition: ExprFuncX.h:54
ExprFuncX & operator=(const ExprFuncX &)=default
virtual ExprType type() const
Definition: ExprFuncX.h:39
virtual ~ExprFuncX()=default
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &env) const =0
ExprFuncX(const bool threadSafe)
Definition: ExprFuncX.h:31
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:181
std::map< std::string, double > Statistics
Definition: ExprFuncX.h:16
base class for custom instance data
Definition: ExprNode.h:723