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 double eval(std::string_view expression, double defaultValue);
67
68 Expression * compile(std::string_view expression, std::string_view variable);
69 void free(Expression * expression);
70
71 double update(const Expression * expression, double defaultValue);
72 float update(const Expression * expression, float defaultValue);
73 int update(const Expression * expression, int defaultValue);
74 size_t update(const Expression * expression, size_t defaultValue);
75
76 std::span<const ExpressionVariable> getValues() const;
77 std::span<ExpressionVariable> getValues();
78
79 ExpressionContext * getParent() { return parent; }
80
81 void link(ExpressionContext * source, std::string_view sourceName, std::string_view destName);
82
83 void clear();
84
85 private:
86 void reserve();
87
88 ExpressionContext * parent;
89
90 std::unordered_map<size_t, Expression> expressions;
91 std::vector<ExpressionVariable> values;
92
93 te_scope scope;
94 std::vector<te_variable> variables;
95 };
96
97 template<typename T>
99 {
100 ExpressionValue() = default;
101 ExpressionValue(T t) : value(t), hasValue(true) {}
102 ExpressionValue(ExpressionContext * expressionContext, std::string_view name, std::string_view expression, T value) :
103 expressionContext(expressionContext),
104 value(value)
105 {
106 this->expression = expressionContext->compile(expression, name);
107 }
108
110 {
111 if (expression && expressionContext) {
112 expressionContext->free(expression);
113 }
114 }
115
116 operator T() const { return getValue(); }
117
118 T getValue() const
119 {
120 if (!hasValue) {
121 value = expression ? get(T()) : T();
122 }
123
124 return value;
125 }
126
127 T get(T defaultValue) const
128 {
129 return expressionContext->update(expression, defaultValue);
130 }
131
132 ExpressionContext * expressionContext = nullptr;
133 Expression * expression = nullptr;
134 mutable T value;
135 bool hasValue = false;
136 };
137
138 struct ParsedValue;
139
140 void readSize_t(ExpressionValue<size_t> & value, ExpressionContext * ev, const std::string & prefix, const ParsedValue & parsedValue);
141 }
142}
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:40