Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Visual C++ 10.0

Linq-To-XML Style of Node Creation for C++

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
#include "StdAfx.h"
#include "Attribute.h"

using namespace Elmax;

RegexConverter Attribute::m_nConverter;

// Non-default constructor
Attribute::Attribute()
: m_pIConverter(NULL)
{
}

// Non-default constructor
Attribute::Attribute(
	MSXML2::IXMLDOMDocumentPtr& ptrDoc, 
	MSXML2::IXMLDOMNodePtr& ptrNode, 
	BaseConverter* pIConverter)
: m_pIConverter(pIConverter)
, m_ptrDoc(ptrDoc)
, m_ptrNode(ptrNode)
{
}

// Copy constructor
Attribute::Attribute(const Attribute& other)
: m_pIConverter(other.m_pIConverter) 
, m_strTemp(other.m_strTemp)
, m_asciiStrTemp(other.m_asciiStrTemp)
, m_ptrDoc(other.m_ptrDoc)
, m_ptrNode(other.m_ptrNode)
, m_strAttrName(other.m_strAttrName)
{

}

#if _HAS_CPP0X
// Move constructor
Attribute::Attribute(Attribute&& other)
: m_pIConverter(other.m_pIConverter) 
, m_strTemp(std::move(other.m_strTemp))
, m_asciiStrTemp(std::move(other.m_asciiStrTemp))
, m_ptrDoc(other.m_ptrDoc)
, m_ptrNode(other.m_ptrNode)
, m_strAttrName(std::move(other.m_strAttrName))
{
}
#endif // _HAS_CPP0X

// Assignment operator
Attribute& Attribute::operator=(const Attribute& other)
{
	m_pIConverter = other.m_pIConverter;
	m_strTemp = other.m_strTemp;
	m_asciiStrTemp = other.m_asciiStrTemp;
	m_ptrDoc = other.m_ptrDoc;
	m_ptrNode = other.m_ptrNode;
	m_strAttrName = other.m_strAttrName;

	return *this;
}

#if _HAS_CPP0X
// Move assignment operator
Attribute& Attribute::operator=(const Attribute&& other)
{
	m_pIConverter = other.m_pIConverter;
	m_strTemp = std::move(other.m_strTemp);
	m_asciiStrTemp = std::move(other.m_asciiStrTemp);
	m_ptrDoc = other.m_ptrDoc;
	m_ptrNode = other.m_ptrNode;
	m_strAttrName = std::move(other.m_strAttrName);

	return *this;
}
#endif // _HAS_CPP0X

Attribute::~Attribute(void)
{
}

bool Attribute::GetAttributeAt(const std::wstring& wstrAttrName, std::wstring& wstrValue, bool& bExists) const
{
	bExists = false;
	if(m_ptrNode)
	{
		MSXML2::IXMLDOMNamedNodeMapPtr attrList = m_ptrNode->Getattributes();

		if(attrList)
		{
			for(long i=0; i<attrList->length; ++i)
			{
				std::wstring name = (LPCWSTR)(attrList->item[i]->GetnodeName());
				if(wstrAttrName==name)
				{
					wstrValue = (LPCWSTR)(attrList->item[i]->Gettext());
					bExists = true;
					return true;
				}
			}
		}
	}

	return false;
}

bool Attribute::Exists() const
{
	if(!m_ptrDoc||!m_ptrNode)
		return false;

	std::wstring wstrValue;
	bool bExists = false;
	GetAttributeAt(m_strAttrName, wstrValue, bExists);

	return bExists;
}

void Attribute::SetParam(
	MSXML2::IXMLDOMDocumentPtr& ptrDoc, 
	MSXML2::IXMLDOMNodePtr& ptrNode, 
	const std::wstring& wstrAttrName,
	BaseConverter* pIConverter)
{
	m_ptrDoc = ptrDoc;
	m_ptrNode = ptrNode;
	m_strAttrName = wstrAttrName;
	m_pIConverter = pIConverter;
}


bool Attribute::Create(const std::wstring& namespaceUri)
{
	if(m_ptrDoc&&m_ptrNode)
	{
		bool bExists = false;
		std::wstring wstrValue;
		GetAttributeAt(m_strAttrName, wstrValue, bExists);
		if(false==bExists)
		{
			MSXML2::IXMLDOMNamedNodeMapPtr attrList = m_ptrNode->Getattributes();

			//MSXML2::IXMLDOMAttributePtr pAttr = m_ptrDoc->createAttribute(m_strAttrName.c_str());
			MSXML2::IXMLDOMAttributePtr pAttr = m_ptrDoc->createNode(MSXML2::NODE_ATTRIBUTE, m_strAttrName.c_str(), namespaceUri.c_str());

			if(attrList&&pAttr)
				attrList->setNamedItem(pAttr);

			return true;
		}
	}

	return false;
}

bool Attribute::Delete()
{
	if(m_ptrNode)
	{
		bool bExists = false;
		std::wstring wstrValue;
		GetAttributeAt(m_strAttrName, wstrValue, bExists);
		if(bExists)
		{
			MSXML2::IXMLDOMNamedNodeMapPtr attrList = m_ptrNode->Getattributes();

			if(attrList)
				attrList->removeNamedItem(m_strAttrName.c_str());
		}
		else
			return false;
	}
	else
		throw std::exception("No valid node in this Attribute!");

	return true;
}

Attribute::operator char () const
{
	return GetChar('\0');
}

Attribute::operator short () const
{
	return GetShort(0);
}

Attribute::operator int () const
{
	return GetInt32(0);
}

Attribute::operator __int64 () const
{
	return GetInt64(0L);
}

Attribute::operator unsigned char () const
{
	return GetUChar(0);
}

Attribute::operator unsigned short () const
{
	return GetUShort(0);
}

Attribute::operator unsigned int () const
{
	return GetUInt32(0);
}

Attribute::operator unsigned __int64 () const
{
	return GetUInt64(0L);
}

Attribute::operator float () const
{
	return GetFloat(0.0f);
}

Attribute::operator double () const
{
	return GetDouble(0.0);
}

Attribute::operator std::string () const
{
	return GetString("");
}

Attribute::operator std::wstring () const
{
	return GetString(L"");
}

Attribute::operator const std::string () const
{
	return GetString("");
}

Attribute::operator const std::wstring () const
{
	return GetString(L"");
}

Attribute::operator const CString () const
{
	return GetCString(L"");
}

Attribute::operator LPCWSTR ()
{
	m_strTemp = GetString(L"");
	return m_strTemp.c_str();
}

Attribute::operator LPCSTR ()
{
	m_asciiStrTemp = GetString("");
	return m_asciiStrTemp.c_str();
}

Attribute::operator GUID () const
{
	GUID defaultGUID;
	memset(&defaultGUID, 0, sizeof(defaultGUID));
	return GetGUID(defaultGUID);
}

Attribute::operator Elmax::Date () const
{
	Elmax::Date date;
	return GetDate(date);
}

Attribute::operator Elmax::DateAndTime () const
{
	Elmax::DateAndTime datetime;
	return GetDateTime(datetime);
}

void Attribute::operator=(char val)
{
	SetChar(val);
}

void Attribute::operator=(short val)
{
	SetShort(val);
}

void Attribute::operator=(int val)
{
	SetInt32(val);
}

void Attribute::operator=(__int64 val)
{
	SetInt64(val);
}

void Attribute::operator=(unsigned char val)
{
	SetUChar(val);
}

void Attribute::operator=(unsigned short val)
{
	SetUShort(val);
}

void Attribute::operator=(unsigned int val)
{
	SetUInt32(val);
}

void Attribute::operator=(unsigned __int64 val)
{
	SetUInt64(val);
}

void Attribute::operator=(float val)
{
	SetFloat(val);
}

void Attribute::operator=(double val)
{
	SetDouble(val);
}

void Attribute::operator=(const std::wstring& val)
{
	SetString(val);
}

void Attribute::operator=(const std::string& val)
{
	SetString(val);
}

void Attribute::operator=(const CString& val)
{
	SetCString(val);
}

void Attribute::operator=(LPCWSTR val)
{
	SetString(std::wstring(val));
}

void Attribute::operator=(LPCSTR val)
{
	SetString(std::string(val));
}

void Attribute::operator=(const GUID& val)
{
	SetGUID(val);
}

void Attribute::operator=(const Elmax::Date& val)
{
	SetDate(val);
}

void Attribute::operator=(const Elmax::DateAndTime& val)
{
	SetDateTime(val);
}

bool Attribute::SetBool(bool val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetBool(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetChar(char val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetChar(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetShort(short val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetShort(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetInt32(int val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetInt32(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetInt64(__int64 val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetInt64(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetUChar(unsigned char val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetUChar(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetUShort(unsigned short val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetUShort(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetUInt32(unsigned int val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetUInt32(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetUInt64(unsigned __int64 val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetUInt64(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetFloat(float val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetFloat(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetDouble(double val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetDouble(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetString(const std::wstring& val)
{
	if(!m_ptrDoc||!m_ptrNode)
		throw std::exception("Invalid element");

	bool bExists = false;
	std::wstring wstrValue;
	GetAttributeAt(m_strAttrName, wstrValue, bExists);
	if(false==bExists)
		Create();

	MSXML2::IXMLDOMNamedNodeMapPtr attrList = m_ptrNode->Getattributes();

	if(attrList)
	{
		for(long i=0; i<attrList->length; ++i)
		{
			std::wstring name = (LPCWSTR)(attrList->item[i]->GetnodeName());
			if(m_strAttrName==name)
			{
				attrList->item[i]->Puttext(val.c_str());
				return true;
			}
		}

		MSXML2::IXMLDOMAttributePtr pAttr = m_ptrDoc->createAttribute(m_strAttrName.c_str());

		if(attrList&&pAttr)
		{
			pAttr->Puttext(val.c_str());
			attrList->setNamedItem(pAttr);
			return true;
		}
	}

	return false;
}

bool Attribute::SetString(const std::string& val)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetString(strDest, val) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetCString(const CString& val)
{
	return SetString((LPCWSTR)(val));
}

bool Attribute::SetGUID(const GUID& val, bool bRemoveBraces)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetGUID(strDest, val, bRemoveBraces) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::SetDate(const Elmax::Date& val)
{
	std::wstring strDest = val.GetString();
	if(SetString(strDest))
		return true;

	return false;
}

bool Attribute::SetDateTime(const Elmax::DateAndTime& val)
{
	std::wstring strDest = val.GetString();
	if(SetString(strDest))
		return true;

	return false;
}

bool Attribute::SetHex(unsigned int val, bool bAddPrefix)
{
	if(NULL==m_pIConverter)
		m_pIConverter = &m_nConverter;

	std::wstring strDest;
	if( m_pIConverter->SetHex(strDest, val, bAddPrefix) )
	{
		if(SetString(strDest))
			return true;
	}

	return false;
}

bool Attribute::GetString(const std::wstring& defaultVal, std::wstring& val) const
{
	if(!m_ptrDoc||!m_ptrNode)
		throw std::exception("Invalid element!");

	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring wstrValue;
	bool bExists = false;
	GetAttributeAt(m_strAttrName, wstrValue, bExists);
	if(false==bExists||wstrValue.empty())
	{
		val = defaultVal;
		return false;
	}

	return pConv->GetString(wstrValue, defaultVal, val);
}

bool Attribute::GetBool(bool defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	bool val = defaultVal;
	pConv->GetBool(src, defaultVal, val);
	return val;
}

char Attribute::GetChar(char defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	char val = defaultVal;
	pConv->GetChar(src, defaultVal, val);
	return val;
}

short Attribute::GetShort(short defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	short val = defaultVal;
	pConv->GetShort(src, defaultVal, val);
	return val;
}

int Attribute::GetInt32(int defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	int val = defaultVal;
	pConv->GetInt32(src, defaultVal, val);
	return val;
}

__int64 Attribute::GetInt64(__int64 defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	__int64 val = defaultVal;
	pConv->GetInt64(src, defaultVal, val);
	return val;
}

unsigned char Attribute::GetUChar(unsigned char defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	unsigned char val = defaultVal;
	pConv->GetUChar(src, defaultVal, val);
	return val;
}

unsigned short Attribute::GetUShort(unsigned short defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	unsigned short val = defaultVal;
	pConv->GetUShort(src, defaultVal, val);
	return val;
}

unsigned int Attribute::GetUInt32(unsigned int defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	unsigned int val = defaultVal;
	pConv->GetUInt32(src, defaultVal, val);
	return val;
}

unsigned __int64 Attribute::GetUInt64(unsigned __int64 defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	unsigned __int64 val = defaultVal;
	pConv->GetUInt64(src, defaultVal, val);
	return val;
}

float Attribute::GetFloat(float defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	float val = defaultVal;
	pConv->GetFloat(src, defaultVal, val);
	return val;
}

double Attribute::GetDouble(double defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	double val = defaultVal;
	pConv->GetDouble(src, defaultVal, val);
	return val;
}

std::wstring Attribute::GetString(const std::wstring& defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	std::wstring val = defaultVal;
	pConv->GetString(src, defaultVal, val);
	return val;
}

std::string Attribute::GetString(const std::string& defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	std::string val = defaultVal;
	pConv->GetString(src, defaultVal, val);
	return val;
}

CString Attribute::GetCString(const CString& defaultVal) const
{
	return GetString((LPCWSTR)(defaultVal)).c_str();
}

GUID Attribute::GetGUID(const GUID& defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	GUID val = defaultVal;
	pConv->GetGUID(src, defaultVal, val);
	return val;
}

Elmax::Date Attribute::GetDate(const Elmax::Date& defaultVal) const
{
	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	Elmax::Date val;
	try
	{
		val.SetString(src);
	}
	catch (...)
	{
		return defaultVal;
	}
	return val;
}

Elmax::DateAndTime Attribute::GetDateTime(const Elmax::DateAndTime& defaultVal) const
{
	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	Elmax::DateAndTime val;
	try
	{
		val.SetString(src);
	}
	catch (...)
	{
		return defaultVal;
	}
	return val;
}

unsigned int Attribute::ReadHex(unsigned int defaultVal) const
{
	BaseConverter* pConv = m_pIConverter;
	if(NULL==pConv)
		pConv = &m_nConverter;

	std::wstring src;
	if(false==GetString(L"", src))
		return defaultVal;

	unsigned int val = defaultVal;
	pConv->ReadHex(src, defaultVal, val);
	return val;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions