Cogs.Core
Expressions.h
1#pragma once
2
3#include "tinyexpr.h"
4
5#include <span>
6#include <string>
7#include <string_view>
8#include <vector>
9#include <unordered_map>
10
11namespace Cogs
12{
13 namespace Core
14 {
19 {
20 double value = 0;
21
22 double min = 0;
23 double max = 0;
24 double step = 1;
25
26 std::string name;
27 int sequenceNumber = 0;
28 };
29
35 {
36 std::string expressionString;
37 mutable ExpressionVariable * resultVariable = nullptr;
38 te_expr * expression = nullptr;
39
40 size_t hash = 0;
41 };
42
53 struct COGSCORE_DLL_API ExpressionContext
54 {
56 const size_t kMaxVariables = 1024;
57
59 ExpressionContext(const ExpressionContext & other) = delete;
60 ExpressionContext & operator=(const ExpressionContext & other) = delete;
61
62 void inherit(ExpressionContext * other);
63
64 ExpressionVariable & add(std::string_view name, double value);
65
66 float eval(std::string_view expression, float defaultValue);
67 double eval(std::string_view expression, double defaultValue);
68 int eval(std::string_view expression, int defaultValue);
69
70 Expression * compile(std::string_view expression, std::string_view variable);
71 void free(Expression * expression);
72
73 double update(const Expression * expression, double defaultValue);
74 float update(const Expression * expression, float defaultValue);
75 int update(const Expression * expression, int defaultValue);
76 size_t update(const Expression * expression, size_t defaultValue);
77
78 std::span<const ExpressionVariable> getValues() const;
79 std::span<ExpressionVariable> getValues();
80
81 ExpressionContext * getParent() { return parent; }
82
83 void link(ExpressionContext * source, std::string_view sourceName, std::string_view destName);
84
85 void clear();
86
87 private:
88 void reserve();
89
90 ExpressionContext * parent;
91
92 std::unordered_map<size_t, Expression> expressions;
93 std::vector<ExpressionVariable> values;
94
95 te_scope scope;
96 std::vector<te_variable> variables;
97 };
98
100 {
101 ExpressionValueInit() {} // = default gives warning for LINUX build.
102 ExpressionValueInit(ExpressionContext * expressionContext, std::string_view name, std::string_view expression) :
103 expressionContext(expressionContext),
104 name(name),
105 expression(expression)
106 {}
107
108 ExpressionContext * expressionContext;
109 const std::string_view name;
110 const std::string_view expression;
111 };
112
113 template<typename T>
115 {
116 ExpressionValue() = default;
117 ExpressionValue(T t) : value(t), hasValue(true) {}
118 ExpressionValue(ExpressionContext * expressionContext, std::string_view name, std::string_view expression, T value) :
119 expressionContext(expressionContext),
120 value(value)
121 {
122 this->expression = expressionContext->compile(expression, name);
123 }
124
126 {
127 if (expression && expressionContext) {
128 expressionContext->free(expression);
129 }
130 }
131
132 ExpressionValue & operator=(const ExpressionValueInit& expressionValue)
133 {
134 this->expressionContext = expressionValue.expressionContext;
135 this->expression = expressionContext->compile(expressionValue.expression, expressionValue.name);
136
137 hasValue = expression == nullptr;
138
139 return *this;
140 }
141
142 operator T() const { return getValue(); }
143
144 T getValue() const
145 {
146 if (!hasValue) {
147 value = expression ? get(T()) : T();
148 }
149
150 return value;
151 }
152
153 T get(T defaultValue) const
154 {
155 return expressionContext->update(expression, defaultValue);
156 }
157
158 ExpressionContext * expressionContext = nullptr;
159 Expression * expression = nullptr;
160 mutable T value;
161 bool hasValue = false;
162 };
163
164 struct ParsedValue;
165
166 void readSize_t(ExpressionValue<size_t> & value, ExpressionContext * ev, const std::string & prefix, const ParsedValue & parsedValue);
167 }
168}
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Provides a context for evaluation of expressions.
Definition: Expressions.h:54
Defines a single named expression variable.
Definition: Expressions.h:19
Defines a string expression, to be parsed and evaluated by an expression context.
Definition: Expressions.h:35
Stores the parsed output of a key/value pair.
Definition: Parsing.h:32