KSeExpr  4.0.4.0
VarBlock.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 "Expression.h"
10 #include "Vec.h"
11 
12 namespace KSeExpr
13 {
14 class ExprNode;
15 class ExprVarNode;
16 class ExprFunc;
17 
18 class VarBlockCreator;
19 
21 class VarBlock
22 {
23 private:
25  VarBlock(int size, bool makeThreadSafe)
26  : indirectIndex(0)
27  , threadSafe(makeThreadSafe)
28  , _dataPtrs(size)
29  {
30  }
31 
32 public:
33  friend class VarBlockCreator;
34 
36  VarBlock(VarBlock &&other) noexcept
37  {
38  threadSafe = other.threadSafe;
39  d = std::move(other.d);
40  s = std::move(other.s);
41  _dataPtrs = std::move(other._dataPtrs);
42  indirectIndex = other.indirectIndex;
43  }
44 
45  ~VarBlock() = default;
46 
48  VarBlock(const VarBlock &) = delete;
49  VarBlock &operator=(const VarBlock &) = delete;
50  VarBlock &operator=(VarBlock &&other) = delete;
51 
53  double *&Pointer(uint32_t variableOffset)
54  {
55  return reinterpret_cast<double *&>(_dataPtrs[variableOffset]);
56  }
57  char **&CharPointer(uint32_t variableOffset)
58  {
59  return reinterpret_cast<char **&>(_dataPtrs[variableOffset]);
60  }
61 
63  // i.e. _dataPtrs[someAttributeOffset][indirectIndex]
65 
67  bool threadSafe;
68 
70  std::vector<double> d;
71 
73  std::vector<char *> s;
74 
76  char **data()
77  {
78  return _dataPtrs.data();
79  }
80 
81 private:
83  std::vector<char *> _dataPtrs;
84 };
85 
87 // This does not register actual data only types of the data. It can create
88 // a VarBlock which allows registering actual variable data
90 {
91 public:
93  class Ref : public ExprVarRef
94  {
95  uint32_t _offset;
96  uint32_t _stride;
97 
98  public:
99  uint32_t offset() const
100  {
101  return _offset;
102  }
103  uint32_t stride() const
104  {
105  return _stride;
106  }
107  Ref(const ExprType &type, uint32_t offset, uint32_t stride)
108  : ExprVarRef(type)
109  , _offset(offset)
110  , _stride(stride)
111  {
112  }
113  void eval(double *) override
114  {
115  assert(false);
116  }
117  void eval(const char **) override
118  {
119  assert(false);
120  }
121  };
122 
124  int registerVariable(const std::string &name, const ExprType &type)
125  {
126  if (_vars.find(name) != _vars.end()) {
127  throw std::runtime_error("Already registered a variable named " + name);
128  } else {
129  int offset = _nextOffset;
130  _nextOffset += 1;
131  _vars.insert(std::make_pair(name, Ref(type, offset, type.dim())));
132  return offset;
133  }
134  }
135 
145  VarBlock create(bool makeThreadSafe = false) const
146  {
147  return VarBlock(_nextOffset, makeThreadSafe);
148  }
149 
151  ExprVarRef *resolveVar(const std::string &name) const
152  {
153  auto it = _vars.find(name);
154  if (it != _vars.end())
155  return const_cast<Ref*>(&it->second);
156  return nullptr;
157  }
158 
159 private:
160  int _nextOffset {};
161  std::map<std::string, Ref> _vars;
162 };
163 
164 } // namespace KSeExpr
int dim() const
Definition: ExprType.h:180
abstract class for implementing variable references
Definition: Expression.h:36
virtual ExprType type() const
returns (current) type
Definition: Expression.h:50
Internally implemented var ref used by SeExpr.
Definition: VarBlock.h:94
uint32_t stride() const
Definition: VarBlock.h:103
Ref(const ExprType &type, uint32_t offset, uint32_t stride)
Definition: VarBlock.h:107
uint32_t offset() const
Definition: VarBlock.h:99
void eval(double *) override
returns this variable's value by setting result
Definition: VarBlock.h:113
void eval(const char **) override
Definition: VarBlock.h:117
A class that lets you register for the variables used by one or more expressions.
Definition: VarBlock.h:90
VarBlock create(bool makeThreadSafe=false) const
Definition: VarBlock.h:145
int registerVariable(const std::string &name, const ExprType &type)
Register a variable and return a handle.
Definition: VarBlock.h:124
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:151
std::map< std::string, Ref > _vars
Definition: VarBlock.h:161
A thread local evaluation context. Just allocate and fill in with data.
Definition: VarBlock.h:22
VarBlock(int size, bool makeThreadSafe)
Allocate an VarBlock.
Definition: VarBlock.h:25
VarBlock & operator=(VarBlock &&other)=delete
VarBlock(VarBlock &&other) noexcept
Move semantics is the only allowed way to change the structure.
Definition: VarBlock.h:36
int indirectIndex
indirect index to add to pointer based data
Definition: VarBlock.h:64
VarBlock(const VarBlock &)=delete
Don't allow copying and operator='ing'.
std::vector< char * > s
copy of Interpreter's str data
Definition: VarBlock.h:73
std::vector< char * > _dataPtrs
This stores double* or char** ptrs to variables.
Definition: VarBlock.h:83
std::vector< double > d
copy of Interpreter's double data
Definition: VarBlock.h:70
char ** data()
Raw data of the data block pointer (used by compiler)
Definition: VarBlock.h:76
VarBlock & operator=(const VarBlock &)=delete
~VarBlock()=default
bool threadSafe
if true, interpreter's data will be copied to this instance before evaluation.
Definition: VarBlock.h:67
double *& Pointer(uint32_t variableOffset)
Get a reference to the data block pointer which can be modified.
Definition: VarBlock.h:53
char **& CharPointer(uint32_t variableOffset)
Definition: VarBlock.h:57