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;
66 ExprType _type;
67
68 private:
69 bool _threadSafe;
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>
85 Vec<double, d, true> inFp(int i) {
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>
93 Vec<double, d, true> outFpHandle() {
94 return Vec<double, d, true>(&outFp);
95 }
96
97 double& outFp;
98 char*& outStr;
99 ExprFuncNode::Data* data;
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 {
120 ExprFuncLocal() : ExprFuncX(true) {}
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
SeExpr2::ExprFuncX::prep
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &env) const =0
SeExpr2::ExprFuncSimple::ArgHandle::fp
double * fp
Definition: ExprFuncX.h:104
SeExpr2::Vec
Definition: Vec.h:98
SeExpr2::ExprFuncSimple::ArgHandle::nargs
int nargs() const
Definition: ExprFuncX.h:89
SeExpr2::ExprFuncSimple::ArgHandle::_nargs
int _nargs
Definition: ExprFuncX.h:102
SeExpr2::ExprFuncSimple::ArgHandle::outFpHandle
Vec< double, d, true > outFpHandle()
Return a vector handle which is easier to assign to.
Definition: ExprFuncX.h:93
SeExpr2::ExprFuncSimple
Definition: ExprFuncX.h:72
SeExpr2::ExprFuncSimple::ArgHandle
Definition: ExprFuncX.h:76
SeExpr2::ExprFuncSimple::ArgHandle::inFp
Vec< double, d, true > inFp(int i)
Definition: ExprFuncX.h:85
SeExpr2::ExprFuncX
Extension function spec, used for complicated argument custom functions.
Definition: ExprFuncX.h:35
SeExpr2::ExprFuncX::_isScalar
bool _isScalar
Definition: ExprFuncX.h:65
SeExpr2::ExprFuncSimple::ArgHandle::opData
int * opData
Definition: ExprFuncX.h:103
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::ExprFuncNode
Node that calls a function.
Definition: ExprNode.h:517
SeExpr2::ExprFuncSimple::ArgHandle::data
ExprFuncNode::Data * data
Definition: ExprFuncX.h:99
SeExpr2::ExprFuncX::_type
ExprType _type
Definition: ExprFuncX.h:66
SeExpr2::ExprVarEnv
Variable scope for tracking variable lookup.
Definition: ExprEnv.h:94
SeExpr2
Definition: Context.h:22
SeExpr2::ExprFuncX::~ExprFuncX
virtual ~ExprFuncX()
Definition: ExprFuncX.h:54
ExprType.h
SeExpr2::ExprFuncSimple::prep
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const =0
ExprNode.h
SeExpr2::ExprFuncX::ExprFuncX
ExprFuncX(const bool threadSafe)
Definition: ExprFuncX.h:43
Vec.h
SeExpr2::ExprFuncSimple::ArgHandle::outFp
double & outFp
Definition: ExprFuncX.h:97
SeExpr2::ExprFuncSimple::ArgHandle::outStr
char *& outStr
Definition: ExprFuncX.h:98
SeExpr2::ExprVarEnvBuilder
Variable scope builder is used by the type checking and code gen to track visiblity of variables and ...
Definition: ExprEnv.h:148
SeExpr2::ExprFuncSimple::ExprFuncSimple
ExprFuncSimple(const bool threadSafe)
Definition: ExprFuncX.h:74
SeExpr2::ExprFuncSimple::buildInterpreter
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
Definition: ExprFuncX.cpp:32
SeExpr2::ExprFuncX::isThreadSafe
bool isThreadSafe() const
Definition: ExprFuncX.h:56
SeExpr2::ExprFuncX::buildInterpreter
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const =0
Build an interpreter to evaluate the expression.
SeExpr2::ExprFuncSimple::ArgHandle::ArgHandle
ArgHandle(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.h:78
SeExpr2::ExprFuncX::sizeInBytes
virtual size_t sizeInBytes() const
Return memory usage of a funcX in bytes.
Definition: ExprFuncX.h:59
SeExpr2::ExprFuncSimple::eval
virtual void eval(ArgHandle args)=0
SeExpr2::ExprFuncLocal::buildInterpreter
virtual int buildInterpreter(const ExprFuncNode *node, Interpreter *interpreter) const
Build an interpreter to evaluate the expression.
SeExpr2::ExprFuncLocal::ExprFuncLocal
ExprFuncLocal()
Definition: ExprFuncX.h:120
SeExpr2::ExprFuncNode::Data
base class for custom instance data
Definition: ExprNode.h:570
SeExpr2::ExprFuncSimple::evalConstant
virtual ExprFuncNode::Data * evalConstant(const ExprFuncNode *node, ArgHandle args) const =0
SeExpr2::ExprFuncSimple::ArgHandle::inStr
char * inStr(int i)
Definition: ExprFuncX.h:88
SeExpr2::ExprFuncLocal::prep
virtual ExprType prep(ExprFuncNode *node, bool scalarWanted, ExprVarEnvBuilder &envBuilder) const
SeExpr2::ExprFuncSimple::ArgHandle::c
char ** c
Definition: ExprFuncX.h:105
SeExpr2::ExprFuncX::_threadSafe
bool _threadSafe
Definition: ExprFuncX.h:69
SeExpr2::Interpreter
Definition: Interpreter.h:40
SeExpr2::ExprFuncX::statistics
virtual void statistics(Statistics &) const
Give this function a chance to populate its statistics.
Definition: ExprFuncX.h:62
SeExpr2::Statistics
std::map< std::string, double > Statistics
Definition: ExprFuncX.h:27
SeExpr2::ExprFuncX::type
virtual ExprType type() const
Definition: ExprFuncX.h:47
SeExpr2::ExprFuncSimple::EvalOp
static int EvalOp(int *opData, double *fp, char **c, std::vector< int > &callStack)
Definition: ExprFuncX.cpp:24
SeExpr2::ExprFuncLocal
Definition: ExprFuncX.h:119