#include "varinst.h"
#include "messageclass.h"
#include "imodule.h"

const char* VarInstListener::type="VarInstListener";

const char* VarInstListener::getType() const
{
	return type;
}

void VarInst::notify()
{
	if (!notifyOn) return;

	// should be notification global ?
	MesgVarChange varChange(this);
	module->receiveMessage(&varChange);

	// notify listeners (if any)
	for(t_listeners::iterator it=listeners.begin();it!=listeners.end();it++)
		(*it)->valueUpdated(this);
}


const char* VarDouble::type="VarDouble";

const char* VarDouble::getType() const
{
	return type;
}

const string& VarDouble::toString() const
{
	char buf[128];
	sprintf(buf,"%f",value);
	return *(new string(buf));
}

const char* VarInteger::type="VarInt";

const char* VarInteger::getType() const
{
	return type;
}

const string& VarInteger::toString() const
{
	char buf[128];
	sprintf(buf,"%d",value);
	return *(new string(buf));
}

const char* VarChar::type="VarChar";

const char* VarChar::getType() const
{
	return type;
}

const string& VarChar::toString() const
{
	char buf[2];
	buf[0]=value;
	buf[1]='\0';
	return *(new string(buf));
}

const char* VarString::type="VarString";

const char* VarString::getType() const
{
	return type;
}

const string& VarString::toString() const
{
	return value;
}

const char* VarBool::type="VarBool";

const char* VarBool::TRUE_VALUE="true";
const char* VarBool::FALSE_VALUE="false";

const char* VarBool::getType() const
{
	return type;
}

const string& VarBool::toString() const
{
	return *(new string(value?TRUE_VALUE:FALSE_VALUE));
}


double toDouble(const VarInst* varInst)
{
	return ((VarDouble*)varInst)->get();
}

int toInt(const VarInst* varInst)
{
	return ((VarInteger*)varInst)->get();
}

char toChar(const VarInst* varInst)
{
	return ((VarChar*)varInst)->get();
}

const char* toString(const VarInst* varInst)
{
	return ((VarString*)varInst)->get().c_str();
}

bool toBool(const VarInst* varInst)
{
	return ((VarBool*)varInst)->get();
}
