C++側クラスをXtalにバインドする

ヘッダーファイル

#include <xtal.h>
#include <xtal_macro.h>

class TestClass
{
public:
	//コンストラクタ
	TestClass();
	TestClass(int x);
	//デストラクタ
	~TestClass();
	//メソッド
	void testMethod(int x);
	//メンバ変数
	int testInt;
};


ソースファイル

//コンストラクタ
TestClass::TestClass()
{
    testInt = 0;
}

TestClass::TestClass(int x)
{
    testInt = x;
}

//デストラクタ
TestClass::~TestClass()
{
}

void TestClass::testMethod(int x)
{
    testInt = x;
}

// コンストラクタのバインド
XTAL_PREBIND(TestClass)
{
    // コンストラクタをバインドする
    it->def_ctor0<TestClass>();
    it->def_ctor1<TestClass, int>();
}

//メソッドやメンバ関数のバインド
XTAL_BIND(TestClass)
{
    // メンバ変数をバインドする(setter, getterを定義している)
    it->def_var(Xid(testInt), &TestClass::testInt);

    // メンバ関数をバインドする
    it->def_method(Xid(testMethod), &TestClass::testMethod);
}

コンストラクタのバインドは、引数の数に応じてメソッド(def_ctor0〜def_ctor6)が用意されているようで、コンストラクタの引数は6つまでに制限されている。
また、メンバ関数の引数も4つまでに制限されている。


実際に使う前にVMに定義する

xtal::lib()->def(Xid(TestClass), xtal::cpp_class<TestClass>());


スクリプト内部のみに定義する場合

xtal::CodePtr code = xtal::compile_file(filename);
code->def(Xid(TestClass), xtal::cpp_class<TestClass>());