SeExpr
VarBlock.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 VarBlock_h
18 #define VarBlock_h
19 
20 #include "Expression.h"
21 #include "ExprType.h"
22 #include "Vec.h"
23 
24 namespace SeExpr2 {
25 
26 class ExprNode;
27 class ExprVarNode;
28 class ExprFunc;
29 class Expression;
30 class Interpreter;
31 
32 class VarBlockCreator;
33 
35 class VarBlock {
36  private:
38  VarBlock(int size) : indirectIndex(0) { _dataPtrs.resize(size); }
39 
40  public:
41  friend class VarBlockCreator;
42 
44  VarBlock(VarBlock&& other) { _dataPtrs = std::move(other._dataPtrs); }
45 
46  ~VarBlock() {}
47 
49  VarBlock(const VarBlock&) = delete;
50  VarBlock& operator=(const VarBlock&) = delete;
51 
53  double*& Pointer(uint32_t variableOffset) { return reinterpret_cast<double*&>(_dataPtrs[variableOffset]); }
54  char**& CharPointer(uint32_t variableOffset) { return reinterpret_cast<char**&>(_dataPtrs[variableOffset]); }
55 
57  // i.e. _dataPtrs[someAttributeOffset][indirectIndex]
59 
61  char** data() { return _dataPtrs.data(); }
62 
63  private:
65  std::vector<char*> _dataPtrs;
66 };
67 
69 // This does not register actual data only types of the data. It can create
70 // a VarBlock which allows registering actual variable data
72 public:
74  class Ref : public ExprVarRef {
75  uint32_t _offset;
76  uint32_t _stride;
77 
78  public:
79  uint32_t offset() const { return _offset; }
80  uint32_t stride() const { return _stride; }
81  Ref(const ExprType& type, uint32_t offset, uint32_t stride)
82  : ExprVarRef(type), _offset(offset), _stride(stride) {}
83  void eval(double*) override { assert(false); }
84  void eval(const char**) override { assert(false); }
85  };
86 
88  int registerVariable(const std::string& name, const ExprType type) {
89  if (_vars.find(name) != _vars.end()) {
90  throw std::runtime_error("Already registered a variable named " + name);
91  } else {
92  int offset = _nextOffset;
93  _nextOffset += 1;
94  _vars.insert(std::make_pair(name, Ref(type, offset, type.dim())));
95  return offset;
96  }
97  }
98 
101 
103  ExprVarRef* resolveVar(const std::string& name) const {
104  auto it = _vars.find(name);
105  if (it != _vars.end()) return const_cast<Ref*>(&it->second);
106  return nullptr;
107  }
108 
109  private:
110  int _nextOffset = 0;
111  std::map<std::string, Ref> _vars;
112 };
113 
114 } // namespace
115 
116 #endif
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:35
VarBlock & operator=(const VarBlock &)=delete
int dim() const
Definition: ExprType.h:160
int registerVariable(const std::string &name, const ExprType type)
Register a variable and return a handle.
Definition: VarBlock.h:88
char ** data()
Raw data of the data block pointer (used by compiler)
Definition: VarBlock.h:61
uint32_t stride() const
Definition: VarBlock.h:80
double *& Pointer(uint32_t variableOffset)
Get a reference to the data block pointer which can be modified.
Definition: VarBlock.h:53
ExprVarRef * resolveVar(const std::string &name) const
Resolve the variable using anything in the data block (call from resolveVar in Expr subclass) ...
Definition: VarBlock.h:103
VarBlock(VarBlock &&other)
Move semantics is the only allowed way to change the structure.
Definition: VarBlock.h:44
std::vector< char * > _dataPtrs
This stores double* or char** ptrs.
Definition: VarBlock.h:65
Ref(const ExprType &type, uint32_t offset, uint32_t stride)
Definition: VarBlock.h:81
std::map< std::string, Ref > _vars
Definition: VarBlock.h:111
A class that lets you register for the variables used by one or more expressions. ...
Definition: VarBlock.h:71
Internally implemented var ref used by SeExpr.
Definition: VarBlock.h:74
VarBlock(int size)
Allocate an VarBlock.
Definition: VarBlock.h:38
int indirectIndex
indirect index to add to pointer based data
Definition: VarBlock.h:58
void eval(double *) override
returns this variable&#39;s value by setting result
Definition: VarBlock.h:83
virtual ExprType type() const
returns (current) type
Definition: Expression.h:59
uint32_t offset() const
Definition: VarBlock.h:79
void eval(const char **) override
Definition: VarBlock.h:84
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
char **& CharPointer(uint32_t variableOffset)
Definition: VarBlock.h:54
abstract class for implementing variable references
Definition: Expression.h:45
VarBlock create()
Get an evaluation handle (one needed per thread)
Definition: VarBlock.h:100