/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__COBJECT__
#define	__COBJECT__


		#include "script/script_engine_types.h"
		#include "script_squirrel/squirrel_vm.h"
		#include "thread/mutex.h"


namespace GS {
	namespace Script {
		struct EngineVM;

/*!
	@short	Weak/shared reference to native C/C++ objects for the Squirrel VM.
	@author	Emmanuel Julien (ejulien@gsworks.fr)
*/
class	CObject
{
		EngineVM		*vm;

public:

		/// Pop safe user pointer.
static	bool			Get(HSQUIRRELVM, int stack_index, void **, CObjectType = typetag_Undefined);
		/// Get safe user pointer type.
static	bool			GetType(HSQUIRRELVM, int idx, CObjectType &);
		// Get a base object from derived types.
static	bool			GetBase(HSQUIRRELVM, int idx, void **, CObjectType *derived_types);
		/// Push safe user pointer.
static	bool			Push(HSQUIRRELVM, void *, CObjectType, bool managed = false);

		/// Initialize reference to a native type.
		void			Initialize(CObjectType, void *, bool managed);
		/// Release reference to a native type.
		void			Release();

		bool			managed;
		List <CObject *> ::Item *list_item;		// Needs to be cached for fast deletion.

		CObjectType	type;
		void			*native;

		CObject(EngineVM *vm, CObjectType = typetag_Undefined, void *p = 0, bool managed = false);
		~CObject();
};

	} // Script
} // GS


#endif	// __COBJECT__
