src/ai/squirrel/engine.hpp
author truelight
Thu, 15 Mar 2007 09:09:45 +0000
branchnoai
changeset 9399 84a3d84c19b2
parent 9398 a9479d5aa957
child 9404 ef9e171617a3
permissions -rw-r--r--
(svn r9194) [NoAI] -Fix r9193: compile before commit :( :(
/* $Id$ */

/** @file engine.hpp defines the SquirrelEngine class */

#ifndef SQUIRREL_CORE_HPP
#define SQUIRREL_CORE_HPP

class SquirrelEngine {
private:
	HSQUIRRELVM vm;        ///< The VirtualMachine instnace for squirrel

	/**
	 * The internal RunError handler. It looks up the real error and calls RunError with it.
	 */
	static SQInteger _RunError(HSQUIRRELVM vm);

protected:
	/**
	 * The CompileError handler.
	 */
	static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);

	/**
	 * The RunError handler.
	 */
	static void RunError(HSQUIRRELVM vm, const char *error);

	/**
	 * If a user runs 'print' inside a script, this function gets the params.
	 */
	static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);

public:
	SquirrelEngine();
	~SquirrelEngine();

	/**
	 * Load a script.
	 * @param script The full script-name to load.
	 * @return False if loading failed.
	 */
	bool LoadScript(const char *script);

	/**
	 * Adds a function to the stack. Depending on the current state this means
	 *  either a method or a global function.
	 */
	void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params);
	void AddMethod(const char *method_name, SQFUNCTION proc);
	void AddMethod(const char *method_name, SQFUNCTION proc, void *userdata, int size);

	/**
	 * Adds a class to the global scope. Make sure to call AddClassEnd when you
	 *  are done adding methods.
	 */
	void AddClassBegin(const char *class_name);

	/**
	 * Finishes adding a class to the global scope. If this isn't called, no
	 *  class is really created.
	 */
	void AddClassEnd();

	/**
	 * Call a method of an instance.
	 */
	void CallMethod(HSQOBJECT instance, const char *method_name);

	/**
	 * Creates a class instance.
	 * @param class_name The name of the class of which we create an instance.
	 * @param real_instance The instance to the real class, if it represents a real class.
	 * @param instance Returning value with the pointer to the instance.
	 * @return False if creating failed.
	 */
	bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);

	/**
	 * Get the real-instance pointer.
	 * @note This will only work just after a function-call from within Squirrel
	 *  to your C++ function.
	 */
	static bool GetInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }

	HSQUIRRELVM GetVM() {
#warning Please stop using this function ASAP
		return this->vm;
	}
};

#endif /* SQUIRREL_CORE_HPP */