#include "shmvar.h"
#include <QDebug>

#define _IS_VALID_VT(vt)		((vt > CShmStringVariable::VT_Invalid) && (vt < CShmStringVariable::VT_Last))

CShmStringVariable::CShmStringVariable(void *pData, size_t nCChData, VT vt, const std::type_info &rti, HSHM hShm, const char *pszName, int nIndex, QObject *pParent)
{
	if(!pData || !hShm || !nCChData || !_IS_VALID_VT(vt))
	{
		Q_ASSERT_X(false, "CShmStringVariable::CShmStringVariable", "Invalid parameter!");
		return;
	}

    m_data.pVoid = pData;
    m_hShm = hShm;
    setObjectName(QStringLiteral("CShmVariable"));
    if((m_nIndex = nIndex) >= 0)
	{
		m_varName += QString("%1%2%3").arg('[').arg(nIndex).arg(']');
	}

    if((rti == typeid(signed char)) || (rti == typeid(unsigned char)))
    {
    }
    else if(rti == typeid(wchar_t))
	{
	}
    else
	{
		Q_ASSERT_X(false, "CShmStringVariable::CShmStringVariable", "Unknown data type!");
	}
}

CShmStringVariable::~CShmStringVariable(void)
{
}

void CShmStringVariable::valRaw(QString &v)
{
}

QString CShmStringVariable::val(void)
{
}

void CShmStringVariable::setVal(const QString &val)
{
}

unsigned long long CShmStringVariable::CheckUpdateShm(bool fLock)
{
	return 0;
}


void CShmStringVariable::Lock(void)
{
	::GfaIpcLockSHM(m_hShm);
//	qDebug() << "CShmStringVariable::Lock";
}

void CShmStringVariable::Unlock(void)
{
//	qDebug() << "CShmStringVariable::Unlock";
	::GfaIpcUnlockSHM(m_hShm);
}

void CShmStringVariable::emitChanged(bool fLock)
{
//    qDebug() << "CShmStringVariable: val changed!";
	if(fLock)
	    emit valChanged(val());
	else
	{
	    QString v;
	    valRaw(v);
	    emit valChanged(v);
	}
}
