Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Dynamic expressions</strong></p> <p>If you want to receive a string from the user and built an expression from that, maybe the <a href="http://partow.net/programming/exprtk/index.html" rel="noreferrer">C++ Mathematical Expression Library</a> fits your bill?</p> <pre><code>template&lt;typename T&gt; void trig_function() { std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)"; T x; exprtk::symbol_table&lt;T&gt; symbol_table; symbol_table.add_variable("x",x); symbol_table.add_constants(); exprtk::expression&lt;T&gt; expression; expression.register_symbol_table(symbol_table); exprtk::parser&lt;T&gt; parser; parser.compile(expression_string,expression); for (x = T(-5.0); x &lt;= T(+5.0); x += 0.001) { T y = expression.value(); printf("%19.15f\t%19.15f\n",x,y); } } </code></pre> <p>There are also the possibility embed a scripting language, such as <a href="http://www.lua.org" rel="noreferrer">Lua</a> or <a href="http://www.python.org" rel="noreferrer">Python</a>, which will give you (even) more power. This is something to consider if you're writing a game, since you'll likely want to script large parts of it.</p> <p>If you're using <a href="http://qt-project.org/" rel="noreferrer">Qt</a>, you can use <a href="http://qt-project.org/doc/qt-4.8/qtscript.html" rel="noreferrer">QtScript</a> (Javascript-ish) to run expressions that read (static or dynamic) properties from your QObject-derived objects.</p> <p>Using one of the above keeps you from having to write your own parser, AST and evaluator, however for a small set of operators it shouldn't be too hard to hack together something if you use <a href="http://boost-spirit.com/home/" rel="noreferrer">Boost.Spirit</a> or some other decent parsing library.</p> <p><strong>Static expressions</strong></p> <p>For selecting between a set of predefined expressions (i.e. known at compile time), you should store the expression in a polymorphic function object. </p> <p>For C++11, if that's available to you, use <code>std::function</code> and lambda expressions. </p> <pre><code>std::function&lt;bool (int, int)&gt; expr = [](int a, int b) { a*2 &lt; b }; </code></pre> <p>For earlier compilers, I recommend function and bind, either in <a href="http://www.boost.org" rel="noreferrer">Boost</a> (boost::) or C++0x TR1 (std::), depending on your compiler. Also, Boost.Lambda will be of help here, as it allows you to construct and store expressions for later evaluation. However, if you're not familiar with C++ and templates (or functional programming), it will likely scare you quite a bit.</p> <p>With that you could write</p> <pre><code>using namespace boost::lambda; boost::function&lt;bool (int, int)&gt; myexpr1 = (_1 + _2) &gt; 20; boost::function&lt;bool (int, int)&gt; myexpr2 = (_1 * _2) &gt; 42; std::cout &lt;&lt; myexpr1(4,7) &lt;&lt; " " &lt;&lt; myexpr2(2,5); </code></pre> <p>with bind, it'd look like:</p> <pre><code>boost::function&lt;bool (Player&amp;)&gt; check = bind(&amp;Player::getHealth, _1) &gt; 20; Player p1; if (check(p1)) { dostuff(); } check = bind(&amp;Player::getGold, _1) &lt; 42; if (check(p1)) { doOtherStuff(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload