Can I return a struct from ChaiScript to C++


#1

Totally new to ChaiScript, trying to evaluate it for a project that requires some hard real time characteristics. I’m also very rusty with newer versions of C++ (sigh), been years since I needed to work with that language. Finally figured out how to do such things as put high level wrappers around eval so that I can use my own classes to get ChaiScript to load scripts, evaluate them, give me addresses of functions for future evaluation etc. However, still struggling with a few topics for which it seems hard to find documentation

  1. I’d like to call a ChaiScript function, passing it some parameters and have that function return a struct so that I can get back multiple values in one go. So for example, on the C++ side, I might have something like
typedef struct {int i, int j, int k} MyTuple;
auto f = eval<std::function<MyTuple (int, int, int)>>("MyFuncReturningTuple")
MyTuple result = f(1,2,3);
  1. How would MyFuncReturningTuple be defined in ChaiScript, in particular, how would I return values as a struct?

Is this possible?

Thanks

P.S. I observe after posting this that some of the angle brackets are being lost. A bit surprised that this editor is not properly converting them so they stick. Trying again, replacing the angle brackets with square brackets (sigh)

eval<std::function<MyTuple (int, int, int)>>("MyFuncReturningTuple")

#2

Whoever fixed that code, thanks — I couldn’t find a “code” option like there is in StackOverflow


#3

(I edited your post for you, you were using “blockquote” syntax (>) instead of “code” syntax. The editor has an icon for it that looks like </>)

Since you say you haven’t used C++ in a while, I’ll try to point out things to consider along the way.

The first note is that doing typedef struct... is pre-C++98… or older. Users today expect to see:

struct MyTuple { int i; int j; int k };

Now, if you literally want just a tuple, you would probably just want to use std::tuple<int, int, int> which was introduced in C++11. However, std::tuple doesn’t give you any way to name the elements, so I’m going to assume that you want the elements named and I’ll proceed from there.

A complete (but untested) example might be:

struct MyTuple { 
  int i; int j; int k; 
  MyTuple(int t_i, int t_j, int t_k)
    : i(t_i), j(t_j), k(t_k) 
  {
    // add a constructor that initializes these values
  } 
};

int main()
{
  // make a chaiscript object somehow and name it `chai`

  // add the constructor
  chai.add(chaiscript::constructor<MyTuple (int, int, int)>(), "MyTuple");

  // add a function in chaiscript land that makes a MyTuple
  // and have it do something (multiply the params) just for the fun of it
  chai.eval("def MyFuncReturningTuple(int i, int j, int k) { return MyTuple(i * 2 ,j * 3, k * 4); }");

  // get a reference to our newly defined ChaiScript function back in C++ land
  auto f = chai.eval<std::function<MyTuple (int, int, int)>>("MyFunctionReturningTuple");
  
  // then call it:
  MyTuple result = f(1,2,3);

  // result.i should be 2
  // result.j should be 6
  // result.k should be 12
}

If you haven’t already, check out the cheatsheet, which I believe covers all of these topics.

-Jason


#4

Ha Ha ha — first of all, thank you for providing me updated syntax. I’m chuckling because there’s a quote of mine that has been circling the internet for some years that says “Every new feature added to C++ is designed to fix the previously new features added to C++”. I didn’t know that ‘typedef’ was no longer needed there, although there’s something to be said for it to be obvious that a new type is being defined.
I had looked at the cheatsheet but hadn’t noticed an example quite like what I wanted. I’ll study your response (which I really appreciate). I did not realize I’d need to use a constructor to fill in the tuple. Was hoping that I could just directly assign values to the fields from a script.
As for the code syntax, I had noticed the angle brackets but it looked like those required me to explicitly use html markup and I was looking for something like the stackoverflow { } approach that presumably replaces symbols automatically with their escape sequences.


#5

Jason, thanks so much — not only does that example work perfectly for me, but it starts to become clear exactly how this API works (I had forgotten that a struct is essentially just a class with everything public) and I see exactly how to extend your example for other objects, something I was getting ready to ask about.

Cheers,
D