C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
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-2025) *
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
27
struct
compilation_timeout_check
final :
public
exprtk::compilation_check
28
{
29
static
constexpr
std::size_t
max_iters_per_check
= 10000;
30
31
bool
continue_compilation
(
compilation_context
& context)
override
32
{
33
if
(++
iterations_
>=
max_iters_per_check
)
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
54
std::size_t
iterations_
=
max_iters_per_check
;
55
time_point_t
timeout_tp_
;
56
};
57
58
template
<
typename
T>
59
void
compilation_timeout_check_example
()
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
113
int
main
()
114
{
115
compilation_timeout_check_example<double>
();
116
return
0;
117
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
pi
const double pi
Definition
exprtk_benchmark.cpp:186
compilation_timeout_check_example
void compilation_timeout_check_example()
Definition
exprtk_compilation_timeout.cpp:59
main
int main()
Definition
exprtk_compilation_timeout.cpp:113
compilation_timeout_check
Definition
exprtk_compilation_timeout.cpp:28
compilation_timeout_check::max_iters_per_check
static constexpr std::size_t max_iters_per_check
Definition
exprtk_compilation_timeout.cpp:29
compilation_timeout_check::iterations_
std::size_t iterations_
Definition
exprtk_compilation_timeout.cpp:54
compilation_timeout_check::time_point_t
std::chrono::time_point< std::chrono::steady_clock > time_point_t
Definition
exprtk_compilation_timeout.cpp:47
compilation_timeout_check::set_timeout_time
void set_timeout_time(const time_point_t &timeout_tp)
Definition
exprtk_compilation_timeout.cpp:49
compilation_timeout_check::continue_compilation
bool continue_compilation(compilation_context &context) override
Definition
exprtk_compilation_timeout.cpp:31
compilation_timeout_check::timeout_tp_
time_point_t timeout_tp_
Definition
exprtk_compilation_timeout.cpp:55
exprtk::compilation_check::compilation_context
Definition
exprtk.hpp:2258
exprtk::compilation_check::compilation_context::error_message
std::string error_message
Definition
exprtk.hpp:2259
exprtk::compilation_check
Definition
exprtk.hpp:2256
Generated by
1.17.0