Public feed #528892

Miscellany

public cpp v1 · immutable
Raw Edit
·published 2007-06-01 18:38 UTC
rendered paste body
// gmtest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "gm/gmConfig_p.h"#include "gm/gmConfig.h"#include "gm/gmMachine.h"#include "gm/gmCall.h"gmType mytype;gmType ptype;gmFunctionObject * fu;class Unit{private:	int me;public:	void SetMe(int m) { me = m; }	int GetMe() { return me; }	static int GM_CDECL Unit_GetMe(gmThread * a_thread)	{		GM_CHECK_NUM_PARAMS(1);		GM_CHECK_FUNCTION_PARAM(func, 0);		GM_ASSERT(a_thread->GetThis()->m_type == mytype);		fu = func;		Unit * ptr = (Unit*)a_thread->ThisUser();		int res = ptr->GetMe();		a_thread->PushInt(res);		return GM_OK;	}};class Player{private:	int me;public:	void SetMe(int m) { me = m; }	int GetMe() { return me; }	static int GM_CDECL Player_GetMe(gmThread * a_thread)	{		GM_CHECK_NUM_PARAMS(0);		GM_ASSERT(a_thread->GetThis()->m_type == ptype);		Player * ptr = (Player*)a_thread->ThisUser();		int res = ptr->GetMe();		a_thread->PushInt(res);		return GM_OK;	}};int GM_CDECL int _tmain(int argc, _TCHAR* argv[]){	Unit me;	me.SetMe(100);	Player you;	you.SetMe(200);	gmMachine * m = new gmMachine();	mytype = m->CreateUserType("Unit");	ptype = m->CreateUserType("Player");	gmFunctionEntry e;	e.m_function = &Unit::Unit_GetMe;	e.m_name = "GetMe";	gmFunctionEntry e2;	e2.m_function = &Player::Player_GetMe;	e2.m_name = "GetPlr";	m->RegisterTypeLibrary(mytype, &e, 1);	m->RegisterTypeLibrary(ptype, &e2, 1);	gmVariable t;	gmVariable t2;	gmUserObject * mu = m->AllocUserObject(&me, mytype);	gmUserObject * mu2 = m->AllocUserObject(&you, ptype);	m->AddCPPOwnedGMObject(mu);	m->AddCPPOwnedGMObject(mu2);	t.SetUser(mu);	t2.SetUser(mu2);    	/* load file */	FILE * f = fopen("test.gm", "rb");	int size;	fseek(f, 0, SEEK_END);	size = ftell(f);	fseek(f, 0, SEEK_SET);	char * str = new char[size+1];	fread(str, size, 1, f);	str[size] = 0;	fclose(f);	int tid;	int errors = m->ExecuteString(str, &tid, true, "test.gm");	if(errors)	{		bool first = true;		const char * message;		while((message = m->GetLog().GetEntry(first)))			printf("Error: %s\n", message);	}	gmCall c;	int ret;	bool r ;	if(c.BeginGlobalFunction(m, "TestFunc", t, false))	{		c.AddParam(t2);		c.End();		r = c.GetReturnedInt(ret);	}	if(!r)	{		bool first = true;		const char * message;		while((message = m->GetLog().GetEntry(first)))			printf("%s", message);	}	// call the function	gmCall c2;	if(c2.BeginFunction(m, fu, t, false))	{		c2.End();		r = c.GetReturnedInt(ret);	}		return 0;}