C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_20.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 20 *
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 <cstdio>
21
#include <stdexcept>
22
#include <map>
23
#include <string>
24
25
#include "
exprtk.hpp
"
26
27
28
struct
vector_access_rtc
:
public
exprtk::vector_access_runtime_check
29
{
30
typedef
std::map<void*, std::string>
map_t
;
31
map_t
vector_map
;
32
33
bool
handle_runtime_violation
(
violation_context
& context)
34
{
35
const
map_t::iterator itr =
vector_map
.find(
static_cast<
void
*
>
(context.
base_ptr
));
36
const
std::string vector_name = (itr !=
vector_map
.end()) ?
37
itr->second :
"Unknown"
;
38
39
printf(
"Runtime vector access violation\n"
40
"Vector: %s base: %p end: %p access: %p typesize: %d\n"
,
41
vector_name.c_str(),
42
context.
base_ptr
,
43
context.
end_ptr
,
44
context.
access_ptr
,
45
static_cast<
unsigned
int
>
(context.
type_size
));
46
47
throw
std::runtime_error
48
(
"Runtime vector access violation. Vector: "
+ vector_name);
49
50
return
false
;
51
}
52
};
53
54
template
<
typename
T>
55
void
vector_overflow_example
()
56
{
57
typedef
exprtk::symbol_table<T>
symbol_table_t;
58
typedef
exprtk::expression<T>
expression_t;
59
typedef
exprtk::parser<T>
parser_t;
60
61
const
std::string expression_str =
62
" for (var i := 0; i < max(v0[],v1[]); i += 1) "
63
" { "
64
" v0[i] := (2 * v0[i]) + (v1[i] / 3); "
65
" } "
;
66
67
T v0[5 ] = { 0, 1, 2, 3, 4 };
68
T v1[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
69
70
vector_access_rtc
vec_rtc;
71
72
vec_rtc.
vector_map
[v0] =
"v0"
;
73
vec_rtc.
vector_map
[v1] =
"v1"
;
74
75
symbol_table_t symbol_table;
76
expression_t expression;
77
parser_t parser;
78
79
symbol_table.add_vector(
"v0"
, v0);
80
symbol_table.add_vector(
"v1"
, v1);
81
82
expression.register_symbol_table(symbol_table);
83
84
parser.register_vector_access_runtime_check(vec_rtc);
85
86
if
(!parser.compile(expression_str, expression))
87
{
88
printf(
"Error: %s\tExpression: %s\n"
,
89
parser.error().c_str(),
90
expression_str.c_str());
91
92
return
;
93
}
94
95
try
96
{
97
expression.value();
98
}
99
catch
(std::runtime_error& exception)
100
{
101
printf(
"Exception: %s\n"
,exception.what());
102
}
103
}
104
105
int
main
()
106
{
107
vector_overflow_example<double>
();
108
return
0;
109
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
vector_overflow_example
void vector_overflow_example()
Definition
exprtk_simple_example_20.cpp:55
main
int main()
Definition
exprtk_simple_example_20.cpp:105
exprtk::vector_access_runtime_check::violation_context
Definition
exprtk.hpp:2215
exprtk::vector_access_runtime_check::violation_context::type_size
std::size_t type_size
Definition
exprtk.hpp:2219
exprtk::vector_access_runtime_check::violation_context::access_ptr
void * access_ptr
Definition
exprtk.hpp:2218
exprtk::vector_access_runtime_check::violation_context::base_ptr
void * base_ptr
Definition
exprtk.hpp:2216
exprtk::vector_access_runtime_check::violation_context::end_ptr
void * end_ptr
Definition
exprtk.hpp:2217
exprtk::vector_access_runtime_check
Definition
exprtk.hpp:2213
vector_access_rtc
Definition
exprtk_loop_timeout_rtc.cpp:76
vector_access_rtc::vector_map
map_t vector_map
Definition
exprtk_simple_example_20.cpp:31
vector_access_rtc::map_t
std::map< void *, std::string > map_t
Definition
exprtk_simple_example_20.cpp:30
vector_access_rtc::handle_runtime_violation
bool handle_runtime_violation(violation_context &context)
Definition
exprtk_simple_example_20.cpp:33
Generated by
1.17.0