C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_compilation_timeout.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Compilation Timeout Check *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <chrono>
21#include <cstdio>
22#include <string>
23
24#include "exprtk.hpp"
25
26
28{
29 static constexpr std::size_t max_iters_per_check = 10000;
30
32 {
34 {
35 if (std::chrono::steady_clock::now() >= timeout_tp_)
36 {
37 context.error_message = "Compilation has timed-out";
38 return false;
39 }
40
41 iterations_ = 0;
42 }
43
44 return true;
45 }
46
47 using time_point_t = std::chrono::time_point<std::chrono::steady_clock>;
48
49 void set_timeout_time(const time_point_t& timeout_tp)
50 {
51 timeout_tp_ = timeout_tp;
52 }
53
56};
57
58template <typename T>
60{
61 typedef exprtk::symbol_table<T> symbol_table_t;
62 typedef exprtk::expression<T> expression_t;
63 typedef exprtk::parser<T> parser_t;
64
65 const std::string base_expression_string =
66 "x := "
67 "((1 /1) * sin( 2 * pi * f * t) + (1 /3) * sin( 6 * pi * f * t)+"
68 " (1 /5) * sin(10 * pi * f * t) + (1 /7) * sin(14 * pi * f * t)+"
69 " (1 /9) * sin(18 * pi * f * t) + (1/11) * sin(22 * pi * f * t)+"
70 " (1/13) * sin(26 * pi * f * t) + (1/15) * sin(30 * pi * f * t)+"
71 " (1/17) * sin(34 * pi * f * t) + (1/19) * sin(38 * pi * f * t)+"
72 " (1/21) * sin(42 * pi * f * t) + (1/23) * sin(46 * pi * f * t)+"
73 " (1/25) * sin(50 * pi * f * t) + (1/27) * sin(54 * pi * f * t));";
74
75 std::string large_expression_string = "var x := 0;";
76
77 for (std::size_t i = 0; i < 60000; ++i)
78 {
79 large_expression_string += base_expression_string;
80 }
81
82 static const T pi = T(3.141592653589793238462643383279502);
83
84 const T f = pi / T(10);
85 const T a = T(10);
86 T t = T(0);
87
88 symbol_table_t symbol_table;
89 symbol_table.add_variable("t",t);
90 symbol_table.add_constant("f",f);
91 symbol_table.add_constant("a",a);
92 symbol_table.add_constants();
93
94 expression_t expression;
95 expression.register_symbol_table(symbol_table);
96
97 compilation_timeout_check compilation_timeout_chck;
98
99 parser_t parser;
100 parser.register_compilation_timeout_check(compilation_timeout_chck);
101
102 const auto max_duration = std::chrono::seconds(8);
103 const auto timeout_tp = std::chrono::steady_clock::now() + max_duration;
104 compilation_timeout_chck.set_timeout_time(timeout_tp);
105
106 if (!parser.compile(large_expression_string, expression))
107 {
108 printf("Error: %s\t\n", parser.error().c_str());
109 return;
110 }
111}
112
113int main()
114{
115 compilation_timeout_check_example<double>();
116 return 0;
117}
const double pi
void compilation_timeout_check_example()
static constexpr std::size_t max_iters_per_check
std::chrono::time_point< std::chrono::steady_clock > time_point_t
void set_timeout_time(const time_point_t &timeout_tp)
bool continue_compilation(compilation_context &context) override