Jump to content
  • Advertisement
Sign in to follow this  
od1n

Exposing a Singleton Using Luabind

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I expose a singleton to lua using luabind? Here's what my singleton template looks like:
#ifndef __SINGLETON_H__
#define __SINGLETON_H__

template< typename T>
class Singleton
{
	private:
		static T* instance;		

	public:

		static T* getInstance()
		{
			if( instance == NULL) instance = new T;

			return instance;
		}

		static destroyInstance()
		{
			delete instance;
			instance = NULL;
		}
};

template <typename T> T* Singleton<T>::instance = NULL;

#endif



This is the class which inherits Singleton, and the binding code:
#include <iostream>
#include <string>
#include <cstdlib>

extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

#include <luabind/luabind.hpp>

#include "Singleton.h"

using namespace std;
using namespace luabind;

class A : public Singleton<A>
{
	public:
		A() {}
		~A() {}
};

int main( int argc, char* argv[])
{
	lua_State* luaVM = lua_open();

	lua_baselibopen( luaVM);
	lua_iolibopen( luaVM);
	lua_strlibopen( luaVM);
	lua_mathlibopen( luaVM);
	
	open( luaVM);

	module(luaVM)
	[
		class_<A>("A")
			.def("getInstance", &A::getInstance)
	];

	lua_dofile( luaVM, "startup.lua");

	lua_close( luaVM);

	system("PAUSE");

	return 0;
}



The problem with this code is that it won't compile. Heres the error I'm getting: error C2660: 'luabind::detail::overload_rep::overload_rep' : function does not take 2 arguments Which is in reference to: .def("getInstance", &A::getInstance) So how do I bind getInstance? Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You have to register the singleton class before registering class A eg.

module(luaVM)
[
class_ >("singleton"),
class_("A")
.def("getInstance", &A::getInstance)
];

I think this should fix it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!