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
24namespace SeExpr2 {
25
26class ExprNode;
27class ExprVarNode;
28class ExprFunc;
29
30class VarBlockCreator;
31
33class 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
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
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
int dim() const
Definition: ExprType.h:160
abstract class for implementing variable references
Definition: Expression.h:45
virtual ExprType type() const
returns (current) type
Definition: Expression.h:59
Internally implemented var ref used by SeExpr.
Definition: VarBlock.h:87
void eval(const char **) override
Definition: VarBlock.h:97
Ref(const ExprType &type, uint32_t offset, uint32_t stride)
Definition: VarBlock.h:94
void eval(double *) override
returns this variable's value by setting result
Definition: VarBlock.h:96
uint32_t stride() const
Definition: VarBlock.h:93
uint32_t offset() const
Definition: VarBlock.h:92
A class that lets you register for the variables used by one or more expressions.
Definition: VarBlock.h:84
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
VarBlock create(bool makeThreadSafe=false)
Definition: VarBlock.h:121
int registerVariable(const std::string &name, const ExprType type)
Register a variable and return a handle.
Definition: VarBlock.h:101
std::map< std::string, Ref > _vars
Definition: VarBlock.h:134
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:33
std::vector< double > d
copy of Interpreter's double data
Definition: VarBlock.h:68
int indirectIndex
indirect index to add to pointer based data
Definition: VarBlock.h:62
char ** data()
Raw data of the data block pointer (used by compiler)
Definition: VarBlock.h:74
double *& Pointer(uint32_t variableOffset)
Get a reference to the data block pointer which can be modified.
Definition: VarBlock.h:57
std::vector< char * > _dataPtrs
This stores double* or char** ptrs to variables.
Definition: VarBlock.h:78
char **& CharPointer(uint32_t variableOffset)
Definition: VarBlock.h:58
VarBlock & operator=(const VarBlock &)=delete
bool threadSafe
if true, interpreter's data will be copied to this instance before evaluation.
Definition: VarBlock.h:65
std::vector< char * > s
copy of Interpreter's str data
Definition: VarBlock.h:71
VarBlock(VarBlock &&other)
Move semantics is the only allowed way to change the structure.
Definition: VarBlock.h:42
VarBlock(int size, bool makeThreadSafe)
Allocate an VarBlock.
Definition: VarBlock.h:36
VarBlock(const VarBlock &)=delete
Don't allow copying and operator='ing'.
you may not use this file except in compliance with the License and the following modification to it
Definition: license.txt:10