AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
Tokenizer_exprtk.hpp
Go to the documentation of this file.
1/*
2 * This file is part of AQUAgpusph, a free CFD program based on SPH.
3 * Copyright (C) 2012 Jose Luis Cercos Pita <jl.cercos@upm.es>
4 *
5 * AQUAgpusph is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * AQUAgpusph is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with AQUAgpusph. If not, see <http://www.gnu.org/licenses/>.
17 */
18
23
24#ifndef TOKENIZER_H_INCLUDED
25#define TOKENIZER_H_INCLUDED
26
27#include <map>
28#include <string>
29#include <mutex>
30#include <typeinfo>
31#include <iostream>
32#include "aquagpusph/ext/exprtk.hpp"
35
36namespace Aqua {
37
38typedef long double tokenizer_t;
39
49{
50 public:
53
56
68 template<typename T>
69 void registerVariable(const std::string name, T value)
70 {
72 const std::lock_guard<std::mutex> lock(this->mutex);
73 removeVariable(name);
74 vars.add_variable(name, val);
75 }
76
81 std::vector<std::string> exprVariables(const std::string eq);
82
88 template<typename T=float>
89 T solve(const std::string eq)
90 {
91 const std::lock_guard<std::mutex> lock(this->mutex);
92 exprtk::parser<tokenizer_t> parser;
93 exprtk::expression<tokenizer_t> expr;
94
95 // First try straight number conversions
96 T res = 0;
97 size_t sz;
98 try {
99 res = narrow_cast<T>((int64_t)std::stoll(eq, &sz));
100 if (sz == eq.size()) {
101 return res;
102 }
103 } catch (...) {
104 }
105 try {
106 std::string::size_type sz;
107 res = narrow_cast<T>(std::stod(eq, &sz));
108 if (sz == eq.size()) {
109 return res;
110 }
111 } catch (...) {
112 }
113
114 expr.register_symbol_table(vars);
115 // expr.register_symbol_table(ivars);
116
117 if (!parser.compile(eq, expr))
118 {
119 LOG(L_ERROR, std::string("Error parsing \"") + eq + "\":\n");
120 LOG0(L_DEBUG, "\n");
121 LOG0(L_DEBUG, parser.error() + "\n");
122 LOG0(L_DEBUG, "\n");
123 throw std::runtime_error("Invalid expression");
124 }
125
126 tokenizer_t res_org;
127 try {
128 res_org = expr.value();
129 res = narrow_cast<T>(res_org);
130 } catch(std::out_of_range) {
131 LOG(L_ERROR, std::string("Error parsing \"") + eq + "\":\n");
132 LOG0(L_DEBUG, "\n");
133 LOG0(L_DEBUG, std::string("The result ") +
134 std::to_string(res_org) + " overflows the type \"" +
135 typeid(res).name() + "\"\n");
136 LOG0(L_DEBUG, "\n");
137 throw;
138 }
139 return res;
140 }
141
142 protected:
146 inline void removeVariable(const std::string name)
147 {
148 if (vars.is_variable(name))
149 vars.remove_variable(name);
150 }
151
152 private:
155 exprtk::symbol_table<tokenizer_t> vars;
157
159 static std::mutex mutex;
160}; // class Tokenizer
161
162} // Aqua::
163
164#endif // TOKENIZER_H_INCLUDED
Set of auxiliar functions.
Terminal output, with Log automatic copying. (See Aqua::InputOutput::Logger for details)
#define LOG0(level, log)
Definition Logger.hpp:64
#define LOG(level, log)
Definition Logger.hpp:55
#define T
Definition Sort.hcl.in:83
T solve(const std::string eq)
Solve a math expression.
Definition Tokenizer_exprtk.hpp:89
void removeVariable(const std::string name)
Remove a variable if it has been registered.
Definition Tokenizer_exprtk.hpp:146
Tokenizer_exprtk()
Constructor.
Definition Tokenizer_exprtk.cpp:36
void registerVariable(const std::string name, T value)
Register a variable.
Definition Tokenizer_exprtk.hpp:69
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
Tdst narrow_cast(Torg v)
Cast a value, checking that it is not overflowing.
Definition AuxiliarMethods.hpp:414
@ L_ERROR
Definition Logger.hpp:75
@ L_DEBUG
Definition Logger.hpp:72
long double tokenizer_t
Definition Tokenizer_exprtk.hpp:38
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition sphPrerequisites.hpp:65