C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_import_packages.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Import Packages Examples *
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 <map>
22#include <string>
23
24#include "exprtk.hpp"
25
26
27template <typename T>
29{
30public:
31
38 typedef std::function<bool(symbol_table_t&)> import_func_t;
39 typedef std::map<std::string,import_func_t> package_map_t;
40
41 using igfun_t::operator();
42
44 : igfun_t("S")
45 , symbol_table_(symbol_table)
46 {
48
49 package_map_.emplace(
50 "exprtk.rtl.io" ,
51 [&](symbol_table_t& symbtab) -> bool { return symbtab.add_package(io_package); });
52
53 package_map_.emplace(
54 "exprtk.rtl.io.file",
55 [&](symbol_table_t& symbtab) -> bool { return symbtab.add_package(file_package); });
56
57 package_map_.emplace(
58 "exprtk.rtl.vecops" ,
59 [&](symbol_table_t& symbtab) -> bool { return symbtab.add_package(vector_package); });
60 }
61
62 inline T operator()(parameter_list_t parameters) override
63 {
64 const string_t package(parameters[0]);
65
66 const auto itr = package_map_.find(exprtk::to_str(package));
67
68 if (package_map_.end() == itr)
69 {
70 return T(0);
71 }
72
73 return itr->second(symbol_table_) ? T(1) : T(0);
74 }
75
76private:
77
83};
84
85template <typename T>
87{
88 typedef exprtk::symbol_table<T> symbol_table_t;
89 typedef exprtk::expression<T> expression_t;
90 typedef exprtk::parser<T> parser_t;
91
92 symbol_table_t symbol_table;
93
94 import_packages<T> import_package(symbol_table);
95
96 symbol_table.add_function("import",import_package);
97
98 expression_t expression;
99 expression.register_symbol_table(symbol_table);
100
101 parser_t parser;
102
103 const std::string import_packages_program =
104 " import('exprtk.rtl.io' ); "
105 " import('exprtk.rtl.io.file'); "
106 " import('exprtk.rtl.vecops' ); "
107 " "
108 " var v[7] := [1:1]; "
109 " "
110 " println('v: ', v); "
111 " "
112 " reverse(v); "
113 " "
114 " println('reversed v: ', v); "
115 " ";
116
117 parser.compile(import_packages_program, expression);
118
119 expression.value();
120}
121
122int main()
123{
124 import_package_example<double>();
125 return 0;
126}
bool add_package(Package &package)
Definition exprtk.hpp:21363
generic_type::scalar_view scalar_t
symbol_table_t & symbol_table_
T operator()(parameter_list_t parameters) override
exprtk::rtl::io::file::package< T > file_package
std::map< std::string, import_func_t > package_map_t
std::function< bool(symbol_table_t &)> import_func_t
import_packages(symbol_table_t &symbol_table)
igfun_t::parameter_list_t parameter_list_t
exprtk::igeneric_function< T > igfun_t
exprtk::rtl::vecops::package< T > vector_package
generic_type::string_view string_t
igfun_t::generic_type generic_type
exprtk::symbol_table< T > symbol_table_t
exprtk::rtl::io::package< T > io_package
void import_package_example()
std::string to_str(const StringView &view)
Definition exprtk.hpp:4996