Home » U++ Library support » U++ MT-multithreading and servers » How to call a C++ function in Witz template?
How to call a C++ function in Witz template? [message #39167] |
Thu, 21 February 2013 11:47  |
Peter
Messages: 16 Registered: October 2012
|
Promising Member |
|
|
Hi.
According to Witz manual it's possible to call a C++ function in Witz template. Unfortunately there's no example showing how to do that. In one of my source files I defined a trivial function:
void fun(int& i)
{
i++;
}
and Skylark handler:
SKYLARK(AAA, "aaa")
{
http("MYVAR", Value(2));
http("MYFUN", fun);
http.RenderResult("Project_name/aaa");
}
The corresponding aaa.witz template looks like this:
<html>
<body>
$MYVAR
$MYFUN($MYVAR)
$MYVAR
</body>
</html>
I expected to see the following output: 2 3 (old and new value of MYVAR). Instead I got the following error message:
"function nor link not found 'MYFUN'".
I assumed I just had to pass a pointer to my function as a value of shared variable MYFUN to make it visible in Witz template, but apparently that's not how it works. What's the correct way to do it?
|
|
|
Re: How to call a C++ function in Witz template? [message #39168 is a reply to message #39167] |
Thu, 21 February 2013 12:08  |
|
Hi Peter,
Peter wrote on Thu, 21 February 2013 11:47 | I assumed I just had to pass a pointer to my function as a value of shared variable MYFUN to make it visible in Witz template, but apparently that's not how it works. What's the correct way to do it?
|
It would be nice if it would work this way, but unfortunately it doesn't (at least not yet ). You have to register the function and it has to have correct signature. The simple increment function from your example can be done like this (not tested):
Value Fun(const Vector<Value>& arg, const Renderer *)
{
if(arg.GetCount() != 1 && !IsNumber(arg[0]))
return "ERROR";
return (int)arg[0] + 1;
}
INITBLOCK {
Compiler::Register("fun", Fun);
};
This code should be placed in .icpp file, to make sure it is linked correctly. Now you can call the function in witz using $fun(MYVAR), it does not have to be passed to the Http.
Is that clearer now? Further examples can be found in Skylark/StdLib.icpp.
Best regards,
Honza
|
|
|
Goto Forum:
Current Time: Sat May 10 06:45:27 CEST 2025
Total time taken to generate the page: 0.02633 seconds
|