GDAL
vrtexpression.h
1/******************************************************************************
2 *
3 * Project: Virtual GDAL Datasets
4 * Purpose: Implementation of MathExpression
5 * Author: Daniel Baston
6 *
7 ******************************************************************************
8 * Copyright (c) 2024, ISciences LLC
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#pragma once
14
15#include "cpl_error.h"
16
17#include <string_view>
18#include <vector>
19
20namespace gdal
21{
22
26class MathExpression /* non final */
27{
28 public:
29 virtual ~MathExpression();
30
37 static std::unique_ptr<MathExpression> Create(const char *pszExpression,
38 const char *pszDialect);
39
51 virtual void RegisterVariable(std::string_view osVariable,
52 double *pdfLocation) = 0;
53
65 virtual void RegisterVector(std::string_view osVariable,
66 std::vector<double> *padfLocation) = 0;
67
79 virtual CPLErr Compile() = 0;
80
88 virtual CPLErr Evaluate() = 0;
89
99 virtual const std::vector<double> &Results() const = 0;
100};
101
103
104#if GDAL_VRT_ENABLE_EXPRTK
105
109class ExprtkExpression final : public MathExpression
110{
111 public:
112 explicit ExprtkExpression(std::string_view osExpression);
113
114 ~ExprtkExpression() override;
115
116 void RegisterVariable(std::string_view osVariable,
117 double *pdfLocation) override;
118
119 void RegisterVector(std::string_view osVariable,
120 std::vector<double> *padfLocation) override;
121
122 CPLErr Compile() override;
123
124 CPLErr Evaluate() override;
125
126 const std::vector<double> &Results() const override;
127
128 private:
129 class Impl;
130
131 std::unique_ptr<Impl> m_pImpl;
132};
133
134#endif
135
136#if GDAL_VRT_ENABLE_MUPARSER
137
141class MuParserExpression final : public MathExpression
142{
143 public:
144 explicit MuParserExpression(std::string_view osExpression);
145
146 ~MuParserExpression() override;
147
148 void RegisterVariable(std::string_view osVariable,
149 double *pdfLocation) override;
150
151 void RegisterVector(std::string_view osVariable,
152 std::vector<double> *padfLocation) override;
153
154 CPLErr Compile() override;
155
156 CPLErr Evaluate() override;
157
158 const std::vector<double> &Results() const override;
159
160 private:
161 class Impl;
162
163 std::unique_ptr<Impl> m_pImpl;
164};
165
166#endif
167
168inline std::unique_ptr<MathExpression>
169MathExpression::Create([[maybe_unused]] const char *pszExpression,
170 const char *pszDialect)
171{
172 if (EQUAL(pszDialect, "exprtk"))
173 {
174#if GDAL_VRT_ENABLE_EXPRTK
175 return std::make_unique<gdal::ExprtkExpression>(pszExpression);
176#else
177 CPLError(CE_Failure, CPLE_IllegalArg,
178 "Dialect '%s' is not supported by this GDAL build. A GDAL "
179 "build with ExprTk is needed.",
180 pszDialect);
181#endif
182 }
183 else if (EQUAL(pszDialect, "muparser"))
184 {
185#if GDAL_VRT_ENABLE_MUPARSER
186 return std::make_unique<gdal::MuParserExpression>(pszExpression);
187#else
188 CPLError(CE_Failure, CPLE_IllegalArg,
189 "Dialect '%s' is not supported by this GDAL build. A GDAL "
190 "build with muparser is needed.",
191 pszDialect);
192#endif
193 }
194 else
195 {
196 CPLError(CE_Failure, CPLE_IllegalArg, "Unknown expression dialect: %s",
197 pszDialect);
198 }
199 return nullptr;
200}
201
202bool MuParserHasDefineFunUserData();
203
205
206} // namespace gdal
Class to support evaluation of a mathematical expression.
Definition vrtexpression.h:27
virtual CPLErr Compile()=0
Compile the expression.
static std::unique_ptr< MathExpression > Create(const char *pszExpression, const char *pszDialect)
Create a MathExpression using a specified dialect.
virtual CPLErr Evaluate()=0
Evaluate the expression.
virtual void RegisterVariable(std::string_view osVariable, double *pdfLocation)=0
Register a variable to be used in the expression.
virtual void RegisterVector(std::string_view osVariable, std::vector< double > *padfLocation)=0
Register a vector to be used in the expression.
virtual const std::vector< double > & Results() const =0
Access the results from the last time the expression was evaluated.
CPL error handling services.
#define CPLE_IllegalArg
Illegal argument.
Definition cpl_error.h:93
CPLErr
Error category.
Definition cpl_error.h:37
#define EQUAL(a, b)
Alias for strcasecmp() == 0.
Definition cpl_port.h:541