root-project / cling

The cling C++ interpreter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cling API: Value cast

SAtacker opened this issue · comments

Value types: Type casting to any type

I am trying to integrate Cling into https://github.com/SAtacker/quick-ftxui/blob/cling_integration/include/quick-ftxui.hpp#L150-L152 .
The idea is that quick ftxui should be able to use any C++ function (possibly lambda) using cling and call it within the code.

For example:

  cling::Value res; // Will hold the result of the expression evaluation.
  interp.process("auto func1 = []() { return \"invoked\"; };;", &res);
  auto func1 = res.getAs<std::function<std::string()>>();
  std::cout << "func1 result is " << func1() << '\n';

Is something like this even possible?
P.S. I am aware that technically lamda is not a std::function, but it can be assigned.

To Reproduce

Clone the repository and checkout cling_integration and build.

Setup

Download latest ubuntu package of cling built

Additional context

Documentation for cling value class- https://cling.web.cern.ch/cling/doxygen/classcling_1_1Value.html

Yes, it should be possible. I believe this example does something quite similar:

func_t* pFunc = cling::utils::VoidToFunctionPtr<func_t*>(addr);

Seems clarified.