//-------------------------------------------------------------------------------------------------
// <copyright file="LWLocationTable.h" company="LiquidRobotics">
//  Copyright (c) Liquid Robotics Corporation, confidential and proprietary.  All rights reserved. 
// </copyright>
//
// <summary>
//	Inline implementation of PushLocationTable class, the constructor of which can
//	be used semantically as a function call which causes the constituent parts of
//	a GPS sample to be pushed as a lua table (valid location) or nil (invalid
//	location)
// </summary>
//
// <owner>Mike Cookson</owner>
//-------------------------------------------------------------------------------------------------

#ifndef __LWLocationTable_h__
#define __LWLocationTable_h__

#include "LqrSMACLuaInstance.h"
#include "LqrSMACSharedLib.h"
#include <string>
#include <stdio.h>

namespace LiquidRobotics {
namespace LuaWrapper {

class SHARED_LIB_PUBLIC PushLocationTable
{
public:

	typedef LiquidRobotics::SMACFramework::LuaInstance LuaInstance;
	typedef std::string string;

	PushLocationTable(LuaInstance& _L, bool valid, double latitude, double longitude, const char* varName = NULL)
	: L(_L)
	{
		if (valid)
		{
			L.newtable();
			SetNamedNumericField("latitude", latitude);
			SetNamedNumericField("longitude", longitude);
		}
		else
			L.pushnil();

		if (varName && *varName)
			L.setglobal(varName);
	}

private:

	void SetNamedNumericField(const string& fieldName, double numVal, int stackOffset = -3)
	{
		L.pushlstring(fieldName);
		L.pushnumber(numVal);
		L.settable(stackOffset);
	}

	LuaInstance& L;
};

}
}

#endif
