<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kevinFarrelly.com &#187; scripting</title>
	<atom:link href="http://www.kevinfarrelly.com/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kevinfarrelly.com</link>
	<description>What's keeping my head spinning?</description>
	<lastBuildDate>Mon, 31 Jan 2011 22:59:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Lua / Luabind</title>
		<link>http://www.kevinfarrelly.com/development/programming/lua-luabind/</link>
		<comments>http://www.kevinfarrelly.com/development/programming/lua-luabind/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 10:27:06 +0000</pubDate>
		<dc:creator>kevin</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[luabind]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.kevinfarrelly.com/?p=67</guid>
		<description><![CDATA[The following code shows the small demo I created to test if the luabind-0.8.1 library worked for me. main.cpp script.lua output further reading Quick introduction to luabind]]></description>
			<content:encoded><![CDATA[<p>The following code shows the small demo I created to test if the luabind-0.8.1 library worked for me.</p>
<p><strong>main.cpp</strong></p>
<pre class="brush: cpp; title: ; notranslate">
extern &quot;C&quot;
{
	#include &lt;lua.h&gt;
	#include &lt;lualib.h&gt;
	#include &lt;lauxlib.h&gt;
}

#include &lt;luabind/luabind.hpp&gt;

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
using namespace std;

class Unit
{
private:
	string name;
	int maxHealth;
	int health;

public:
	Unit() :
	name(&quot;unit&quot;),
	maxHealth(10),
	health(10)
	{
	}

	Unit(const Unit&amp; unit) :
	name(name),
	maxHealth(maxHealth),
	health(health)
	{
	}

	const char* getName() const
	{
		return name.c_str();
	}

	void setName(const string&amp; name)
	{
		this-&gt;name = name;
	}

	int getHealth() const
	{
		return health;
	}

	void setHealth(int health)
	{
		this-&gt;health = health;
	}

};

class UnitManager
{
private:
	map&lt;string, Unit*&gt; unitMap;

public:
	virtual ~UnitManager()
	{
		map&lt;string, Unit*&gt;::iterator i = unitMap.begin();
		while (i != unitMap.end())
		{
			delete (*i++).second;
		}
	}

	void createUnit(const string&amp; name)
	{
		Unit* pUnit = new Unit();
		pUnit-&gt;setName(name);
		pUnit-&gt;setHealth(10);

		unitMap.insert(pair&lt;string, Unit*&gt;(
			pUnit-&gt;getName(),
			pUnit));
	}

	void deleteUnit(const string&amp; name)
	{
		map&lt;string, Unit*&gt;::iterator i = unitMap.find(name);
		if (i != unitMap.end())
		{
			delete (*i).second;
			unitMap.erase(name);
		}
	}

	Unit&amp; getUnit(const string&amp; name)
	{
		map&lt;string, Unit*&gt;::iterator i = unitMap.find(name);
		if (i == unitMap.end())
		{
			throw exception();
		}
		return (*((*i).second));
	}

	const vector&lt;string&gt; getKeys() const
	{
		vector&lt;string&gt; keys;
		map&lt;string, Unit*&gt;::const_iterator i = unitMap.begin();
		while (i != unitMap.end())
		{
			keys.push_back((*i).first);
			i++;
		}
		return keys;
	}

	void printUnits()
	{
		const vector&lt;string&gt; keys = getKeys();
		for (int i = 0; i &lt; keys.size(); i++)
		{
			cout &lt;&lt; keys[i] &lt;&lt; endl;
		}
	}

};

int main()
{
	lua_State *pState = lua_open();
	if(pState == NULL)
	{
		std::cout &lt;&lt; &quot;Error initializing &quot; &lt;&lt; LUA_RELEASE &lt;&lt; std::endl;
		return -1;
	}

	luaL_openlibs(pState);
	luabind::open(pState);

	luabind::module(pState)
	[
		luabind::class_&lt;UnitManager&gt;(&quot;UnitManager&quot;)
			.def(luabind::constructor&lt;&gt;())
			.def(&quot;createUnit&quot;, &amp;UnitManager::createUnit)
			.def(&quot;deleteUnit&quot;, &amp;UnitManager::deleteUnit)
			.def(&quot;printUnits&quot;, &amp;UnitManager::printUnits)
			.def(&quot;getUnit&quot;, &amp;UnitManager::getUnit)
	];

	luabind::module(pState)
	[
		luabind::class_&lt;Unit&gt;(&quot;Unit&quot;)
			.def(luabind::constructor&lt;&gt;())
			.def(&quot;getName&quot;, &amp;Unit::getName)
			.def(&quot;getHealth&quot;, &amp;Unit::getHealth)
			.def(&quot;setHealth&quot;, &amp;Unit::setHealth)
	];

	luaL_dofile(pState, &quot;script.lua&quot;);

	lua_close(pState);

	return 0;
}
</pre>
<p><strong>script.lua</strong></p>
<pre class="brush: plain; title: ; notranslate">
um = UnitManager();
um:createUnit('jack');
um:createUnit('john');
um:createUnit('jane');

print('Unit names:');
um:printUnits();

u = um:getUnit('jack');
print('Jack\'s health: ' .. u:getHealth());
u:setHealth(3);

u = um:getUnit('john');
print('John\'s health: ' .. u:getHealth());

u = um:getUnit('jack');
print('Jack\'s health: ' .. u:getHealth());
</pre>
<p><strong>output</strong></p>
<pre class="brush: plain; title: ; notranslate">
Unit names:
jack
jane
john
Jack's health: 10
John's health: 10
Jack's health: 3
</pre>
<p><strong>further reading</strong></p>
<ul>
<li><a href="http://www.nuclex.org/articles/quick-introduction-to-luabind" target="_blank">Quick introduction to luabind</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinfarrelly.com/development/programming/lua-luabind/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

