Casting a user defined type from Boxed_Value


#1

I have a custom C++ class Rectangle that I’ve added to chaiscript. I also have a list of those rectangles in the chaiscript VM that I want to access and manipulate/access, but in C++, so:

// In chaiscript
var redRects = [];
vec.push_back(Rectangle(0, 0, 10, 10));
vec.push_back(Rectangle(5, 5, 20, 20));
// ...
DrawRectangles(redRects, Color.Red);
void DrawRectangles(const std::vector<Boxed_Value> &rectangles, int color)
{
    // Drawing code
}

I’m able to get this far where rectangles.size() in the function will return the correct number of rectangles in the vector. Doing rectangles[0].is_pointer() returns true, whereas rectangles[0].is_ref() returns false.

When I tried to do chaiscript::boxed_cast<Rectangle *>(rectangles[0]) an exception was throw.

So my question is, how do I accessa Boxed_Value of a user defined type?


#2

Without trying the full code out for myself, I’m guessing your problem is here:

 chaiscript::boxed_cast<Rectangle *>(rectangles[0])

The parameter rectangles is const, so anything you get out of it will be const too.

chaiscript::boxed_cast<const Rectangle *>(rectangles[0])
// or
chaiscript::boxed_cast<const Rectangle &>(rectangles[0])

Should work fine.