c++exprtk

Get list of available functions from exprtk::symbol_table


I would like to query all available function names from a symbol_table. I can see that symbol_table has methods for getting variables, vectors, and stringvars (i.e. symbol_table::get_variable_list). However, I don't see a corresponding symbol_table::get_function_list. Is there some other way that I can get this information?


Solution

  • exprtk::symbol_table now supports obtaining the names of all registered user defined functions.

    template <typename T>
    struct myfunc final : public exprtk::ifunction<T>
    {
       using exprtk::ifunction<T>::operator();
    
       myfunc()
       : exprtk::ifunction<T>(2)
       { exprtk::disable_has_side_effects(*this); }
    
       inline T operator()(const T& v1, const T& v2)
       {
          return T(1) + (v1 * v2) / T(3);
       }
    };
    
    template <typename T>
    inline T myotherfunc(T v0, T v1, T v2)
    {
       return std::abs(v0 - v1) * v2;
    }
     .
     .
     .
    using symbol_table_t = exprtk::symbol_table<T>;
    using expression_t   = exprtk::expression<T>;
    using parser_t       = exprtk::parser<T>;
    
    symbol_table_t symbol_table;
    
    myfunc<T> mf;
    
    symbol_table.add_function("f1",[](T v0) -> T { return v0;});
    symbol_table.add_function("f2",[](T v0, T v1) -> T{ return v0 / v1;});
    
    symbol_table.add_function("myfunc"   , mf         );
    symbol_table.add_function("otherfunc", myotherfunc);
    
    std::vector<std::string> function_list;
    
    symbol_table.get_function_list(function_list);
    
    for (const auto& func : function_list)
    {
       printf("function: %s\n",func.c_str());
    }
    

    Note: Thanks for the suggestion.