Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / MFC

C++ Class Mapping - An XML Parser example

Rate me:
Please Sign up or sign in to vote.
3.71/5 (8 votes)
21 Nov 20045 min read 82.5K   985   36  
How to use C++ macros to map class members for serialization or other purposes.
// XMLClassMapper.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "XMLClassMapper.h"
#include "MyXMLMessages.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;
void InitializeTestData( CMsgMediaCopy &copyMediaMsg );


using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// Simple test of XML Messages
	
		// Our XML Message
		CMsgMediaCopy copyMediaMsg;

		const int XML_BUFFER_SIZE = 2048;
		int size = XML_BUFFER_SIZE;
		TCHAR xmlBuffer[XML_BUFFER_SIZE];
		TCHAR* ptr = xmlBuffer;

		// Load some test data into the message object
		InitializeTestData( copyMediaMsg );

		// Deflate the object ( save as XML )
		if ( copyMediaMsg.Deflate( ptr, size ) != 0 )
		{
			// failed
			return 1;
		}
		
		// Reset to beginning of buffer and size of buffer.
		size = XML_BUFFER_SIZE;
		ptr  = xmlBuffer;

		// Inflate a new object from the XML buffer
		CMsgMediaCopy copyMediaMsg2;

		if ( copyMediaMsg2.Inflate( ptr, size ) != 0 )
		{
			// failed
			return 1;
		}

		// TODO: write a compare function in the base class ( CXMLMessage )
		// This should walk the entry list ( like deflate ), create pointers
		// to the source and destination element, and compare.
		// For now, Deflate 2nd object, and compare XML buffers.

		TCHAR xmlBuffer2[XML_BUFFER_SIZE];
		size = XML_BUFFER_SIZE;
		ptr  = xmlBuffer2;

		copyMediaMsg2.Deflate( ptr, size );

		if ( _tcscmp(  xmlBuffer, xmlBuffer2 ) != 0 )
		{
			FILE* fp = _tfopen( _T(".\\xmlMessage_1.txt"), _T("w"));
			_ftprintf( fp, _T("%s"), xmlBuffer );
			fclose( fp );

			fp = _tfopen( _T(".\\xmlMessage_2.txt"), _T("w"));
			_ftprintf( fp, _T("%s"), xmlBuffer2 );
			fclose( fp );

			return -1;
		}
	}

	return nRetCode;
}

void InitializeTestData( CMsgMediaCopy &copyMediaMsg)
{
	// Load test data. 
	CMsgDestinationInfo *destination;

	// Add two Destinations ( array of class pointers )
	destination = new CMsgDestinationInfo;

	destination->m_destination = _T("Server100");
	destination->m_filePathRoot = _T("Drive_D");

	copyMediaMsg.m_destinations.Add( destination );

	destination = new CMsgDestinationInfo;

	destination->m_destination = _T("Server200");
	destination->m_filePathRoot = _T("Drive_E");

	copyMediaMsg.m_destinations.Add( destination );

	copyMediaMsg.m_mediaName = _T("WTO Protest");
	copyMediaMsg.m_mediaType = _T("Documentary");
	copyMediaMsg.m_priorityLevel = 22;
	copyMediaMsg.m_securityLevel = 33;
	copyMediaMsg.m_requestNumber = 0;
	copyMediaMsg.m_copyOptions = _T("Quickly");

	// And some dummy members for testing all combinations of type and array
	// Note we do not support arrays of classes, just arrays of class pointers.

	// local class
	copyMediaMsg.m_class.m_destination  = _T("Server300");
	copyMediaMsg.m_class.m_filePathRoot = _T("Drive_F");

	// class pointer
	copyMediaMsg.m_classPtr = new CMsgDestinationInfo;
	copyMediaMsg.m_classPtr->m_destination  = _T("Server400");
	copyMediaMsg.m_classPtr->m_filePathRoot = _T("Drive_G");

	// integer pointer
	copyMediaMsg.m_intPtr = new int(1776);

	// integer array
	copyMediaMsg.m_intArray.Add( 2000 );
	copyMediaMsg.m_intArray.Add( 2001 );
	copyMediaMsg.m_intArray.Add( 2002 );
	copyMediaMsg.m_intArray.Add( 2003 );

	// integer pointer array
	copyMediaMsg.m_intPtrArray.Add( new int(44));
	copyMediaMsg.m_intPtrArray.Add( new int(55));
	copyMediaMsg.m_intPtrArray.Add( new int(66));

	// string array
	copyMediaMsg.m_stringArray.Add( CString( _T("Ontario")));
	copyMediaMsg.m_stringArray.Add( CString( _T("Manitoba")));
	copyMediaMsg.m_stringArray.Add( CString( _T("Alberta")));

	// string pointer
	copyMediaMsg.m_stringPtr = new CString( _T("Wizard of OZ"));

	// string pointer array
	copyMediaMsg.m_stringPtrArray.Add( new CString( _T("Dorothy")));
	copyMediaMsg.m_stringPtrArray.Add( new CString( _T("Toto")));
	copyMediaMsg.m_stringPtrArray.Add( new CString( _T("TinMan")));
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions