Show Changes Edit Print Recent Changes Subscriptions Lost and Found Find References Rename Administration Page Topic Locks

 Search

History

 2/22/2017 3:54:00 AM -92.222.238.172 2/22/2017 3:53:37 AM -92.222.238.172 4/13/2008 10:03:23 PM -203.127.30.82

Point Box Window Instance

Back to SqPlus

Summary

This example shows how to pass instances to Squirrel using two methods. The first method passes the instance to Squirrel via an accessor function, while the second example uses Squirrel to dynamically create a new instance on the root table using the given string name and C/C++ pointer. The example also shows instance assignment at any level. (http://www.liebeshoroskop2016.com)

```
// Test/example from a forum post

struct Pointf {
float X,Y;
Pointf() {}
Pointf(float _X, float _Y) : X(_X), Y(_Y) {}
};

struct Boxf {
Pointf UpperLeft,LowerRight;
Boxf() {}
Boxf(Pointf _UpperLeft,Pointf _LowerRight) : UpperLeft(_UpperLeft), LowerRight(_LowerRight) {}
void print(void) {
printf("UL.X %f UL.Y %f LR.X %f LR.Y %f\n",UpperLeft.X,UpperLeft.Y,LowerRight.X,LowerRight.Y);
}
};

struct Windowf {
int id;
Boxf box;
};

DECLARE_INSTANCE_TYPE(Pointf)
DECLARE_INSTANCE_TYPE(Boxf)
DECLARE_INSTANCE_TYPE(Windowf)

int constructPointf(float X,float Y,HSQUIRRELVM v) {
return PostConstruct<Pointf>(v,new Pointf(X,Y),ReleaseClassPtr<Pointf>::release);
} // constructPointf

// Must pass by reference or pointer (not copy)
int constructBoxf(Pointf & UpperLeft,Pointf & LowerRight,HSQUIRRELVM v) {
return PostConstruct<Boxf>(v,new Boxf(UpperLeft,LowerRight),ReleaseClassPtr<Boxf>::release);
} // constructBoxf

struct WindowHolder {
static Windowf * currentWindow;
static Windowf * getWindow(void) {
return currentWindow;
} // getWindow
};
Windowf * WindowHolder::currentWindow = 0;

void testPointfBoxf(void) {
SQClassDef<Pointf>("Pointf").
staticFunc(constructPointf,"constructor").
var(&Pointf::X,"X").
var(&Pointf::Y,"Y");

SQClassDef<Boxf>("Boxf").
staticFunc(constructBoxf,"constructor").
func(Boxf::print,"print").
var(&Boxf::UpperLeft,"UpperLeft").
var(&Boxf::LowerRight,"LowerRight");

SQClassDef<Windowf>("Windowf").
var(&Windowf::id,"Id").
var(&Windowf::box,"Box");

RegisterGlobal(WindowHolder::getWindow,"getWindow");
Windowf myWindow;
myWindow.id = 42;
myWindow.box = Boxf(Pointf(1.f,2.f),Pointf(3.f,4.f));
WindowHolder::currentWindow = &myWindow;

// The createWindow() function below creates a new instance on the root table.
// The instance data is a pointer to the C/C++ instance, and will not be freed
// or otherwise managed.

SquirrelObject test = SquirrelVM::CompileBuffer("\
local MyWindow = Windowf(); \n\
MyWindow.Box = Boxf(Pointf(11.,22.),Pointf(33.,44.)); \n\
print(MyWindow.Box.LowerRight.Y); \n\
MyWindow.Box.LowerRight.Y += 1.; \n\
local MyWindow2 = Windowf(); \n\
MyWindow2 = MyWindow; \n\
print(MyWindow2.Box.LowerRight.Y); \n\
local MyBox = Boxf(Pointf(10.,20.),Pointf(30.,40.)); \n\
MyBox.UpperLeft = Pointf(1000.,1000.); \n\
MyBox.UpperLeft.X = 5000. \n\
print(MyBox.UpperLeft.X) \n\
print(MyBox.UpperLeft.Y) \n\
MyWindow2.Box = MyBox; \n\
MyWindow2.Box.print(); \n\
MyWindow2 = getWindow(); \n\
print(\"MyWindow2: \"+MyWindow2.Id); \n\
MyWindow2.Box.print(); \n\
function createWindow(name,instance) { \n\
::rawset(name,instance); \n\
} \n\
");
SquirrelVM::RunScript(test);

Windowf window = myWindow;
window.id = 54;
window.box.UpperLeft.X  += 1;
window.box.UpperLeft.Y  += 1;
window.box.LowerRight.X += 1;
window.box.LowerRight.Y += 1;
// Create a new Window instance "NewWindow" on the root table.
SquirrelFunction<void>("createWindow")("NewWindow",&window);

SquirrelObject test2 = SquirrelVM::CompileBuffer("\
print(\"NewWindow: \"+NewWindow.Id); \n\
NewWindow.Box.print(); \n\
");
SquirrelVM::RunScript(test2);

} // testPointfBoxf

```