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 
30 class VarBlockCreator;
31 
33 class VarBlock {
34  private:
36  VarBlock(int size, bool makeThreadSafe) : indirectIndex(0), threadSafe(makeThreadSafe), _dataPtrs(size) {}
37 
38  public:
39  friend class VarBlockCreator;
40 
42  VarBlock(VarBlock&& other) {
43  threadSafe = other.threadSafe;
44  d = std::move(other.d);
45  s = std::move(other.s);
46  _dataPtrs = std::move(other._dataPtrs);
47  indirectIndex = other.indirectIndex;
48  }
49 
50  ~VarBlock() {}
51 
53  VarBlock(const VarBlock&) = delete;
54  VarBlock& operator=(const VarBlock&) = delete;
55 
57  double*& Pointer(uint32_t variableOffset) { return reinterpret_cast<double*&>(_dataPtrs[variableOffset]); }
58  char**& CharPointer(uint32_t variableOffset) { return reinterpret_cast<char**&>(_dataPtrs[variableOffset]); }
59 
61  // i.e. _dataPtrs[someAttributeOffset][indirectIndex]
63 
65  bool threadSafe;
66 
68  std::vector<double> d;
69 
71  std::vector<char*> s;
72 
74  char** data() { return _dataPtrs.data(); }
75 
76  private:
78  std::vector<char*> _dataPtrs;
79 };
80 
82 // This does not register actual data only types of the data. It can create
83 // a VarBlock which allows registering actual variable data
85  public:
87  class Ref : public ExprVarRef {
88  uint32_t _offset;
89  uint32_t _stride;
90 
91  public:
92  uint32_t offset() const { return _offset; }
93  uint32_t stride() const { return _stride; }
94  Ref(const ExprType& type, uint32_t offset, uint32_t stride)
96  void eval(double*) override { assert(false); }
97  void eval(const char**) override { assert(false); }
98  };
99 
101  int registerVariable(const std::string& name, const ExprType type) {
102  if (_vars.find(name) != _vars.end()) {
103  throw std::runtime_error("Already registered a variable named " + name);
104  } else {
105  int offset = _nextOffset;
106  _nextOffset += 1;
107  _vars.insert(std::make_pair(name, Ref(type, offset, type.dim())));
108  return offset;
109  }
110  }
111 
121  VarBlock create(bool makeThreadSafe = false) {
122  return VarBlock(_nextOffset, makeThreadSafe);
123  }
124 
126  ExprVarRef* resolveVar(const std::string& name) const {
127  auto it = _vars.find(name);
128  if (it != _vars.end()) return const_cast<Ref*>(&it->second);
129  return nullptr;
130  }
131 
132  private:
133  int _nextOffset = 0;
134  std::map<std::string, Ref> _vars;
135 };
136 
137 } // namespace
138 
139 #endif
SeExpr2::VarBlockCreator::Ref::Ref
Ref(const ExprType &type, uint32_t offset, uint32_t stride)
Definition: VarBlock.h:94
SeExpr2::VarBlockCreator::Ref::eval
void eval(const char **) override
Definition: VarBlock.h:97
SeExpr2::VarBlock
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:33
SeExpr2::VarBlockCreator::Ref::_stride
uint32_t _stride
Definition: VarBlock.h:89
SeExpr2::VarBlockCreator
A class that lets you register for the variables used by one or more expressions.
Definition: VarBlock.h:84
SeExpr2::VarBlockCreator::registerVariable
int registerVariable(const std::string &name, const ExprType type)
Register a variable and return a handle.
Definition: VarBlock.h:101
SeExpr2::VarBlockCreator::Ref::_offset
uint32_t _offset
Definition: VarBlock.h:88
SeExpr2::VarBlockCreator::Ref::stride
uint32_t stride() const
Definition: VarBlock.h:93
SeExpr2::VarBlock::VarBlock
VarBlock(int size, bool makeThreadSafe)
Allocate an VarBlock.
Definition: VarBlock.h:36
SeExpr2::ExprType::dim
int dim() const
Definition: ExprType.h:160
SeExpr2::ExprType
Definition: ExprType.h:39
SeExpr2::VarBlock::d
std::vector< double > d
copy of Interpreter's double data
Definition: VarBlock.h:68
SeExpr2::VarBlockCreator::create
VarBlock create(bool makeThreadSafe=false)
Definition: VarBlock.h:121
SeExpr2::VarBlockCreator::_vars
std::map< std::string, Ref > _vars
Definition: VarBlock.h:134
SeExpr2::VarBlockCreator::Ref::offset
uint32_t offset() const
Definition: VarBlock.h:92
SeExpr2::VarBlockCreator::Ref::eval
void eval(double *) override
returns this variable's value by setting result
Definition: VarBlock.h:96
SeExpr2
Definition: Context.h:22
SeExpr2::VarBlockCreator::_nextOffset
int _nextOffset
Definition: VarBlock.h:133
ExprType.h
Vec.h
SeExpr2::VarBlockCreator::resolveVar
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:126
SeExpr2::VarBlockCreator::Ref
Internally implemented var ref used by SeExpr.
Definition: VarBlock.h:87
SeExpr2::VarBlock::operator=
VarBlock & operator=(const VarBlock &)=delete
it
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10
SeExpr2::VarBlock::CharPointer
char **& CharPointer(uint32_t variableOffset)
Definition: VarBlock.h:58
SeExpr2::VarBlock::indirectIndex
int indirectIndex
indirect index to add to pointer based data
Definition: VarBlock.h:62
SeExpr2::VarBlock::Pointer
double *& Pointer(uint32_t variableOffset)
Get a reference to the data block pointer which can be modified.
Definition: VarBlock.h:57
SeExpr2::VarBlock::threadSafe
bool threadSafe
if true, interpreter's data will be copied to this instance before evaluation.
Definition: VarBlock.h:65
SeExpr2::VarBlock::VarBlock
VarBlock(VarBlock &&other)
Move semantics is the only allowed way to change the structure.
Definition: VarBlock.h:42
SeExpr2::VarBlock::s
std::vector< char * > s
copy of Interpreter's str data
Definition: VarBlock.h:71
SeExpr2::VarBlock::data
char ** data()
Raw data of the data block pointer (used by compiler)
Definition: VarBlock.h:74
SeExpr2::ExprVarRef
abstract class for implementing variable references
Definition: Expression.h:45
SeExpr2::ExprVarRef::type
virtual ExprType type() const
returns (current) type
Definition: Expression.h:59
SeExpr2::VarBlock::~VarBlock
~VarBlock()
Definition: VarBlock.h:50
Expression.h
SeExpr2::VarBlock::_dataPtrs
std::vector< char * > _dataPtrs
This stores double* or char** ptrs to variables.
Definition: VarBlock.h:78