C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-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 <cstdio>
21#include <stdexcept>
22#include <map>
23#include <string>
24
25#include "exprtk.hpp"
26
27
29{
30 typedef std::map<void*, std::string> map_t;
32
34 {
35 const map_t::iterator itr = vector_map.find(static_cast<void*>(context.base_ptr));
36 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
54template <typename T>
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 try
87 {
88 if (!parser.compile(expression_str, expression))
89 {
90 printf("Error: %s\tExpression: %s\n",
91 parser.error().c_str(),
92 expression_str.c_str());
93
94 return;
95 }
96
97 expression.value();
98 }
99 catch(std::runtime_error& exception)
100 {
101 printf("Exception: %s\n",exception.what());
102 }
103}
104
105int main()
106{
107 vector_overflow_example<double>();
108 return 0;
109}
void vector_overflow_example()
std::map< void *, std::string > map_t
bool handle_runtime_violation(violation_context &context)