Click here to Skip to main content
15,886,137 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.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// XMLMessage.h
//
// XML Message base class. All classes that map to XML must be derived from this class.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "afxwin.h"
#include "afxtempl.h"
#include "msg\XMLMacros.h"

// Put your company namespace here.
static const TCHAR* _xmlNameSpace    = _T("http://www.yourcompany.com");
static const TCHAR* _targetNameSpace = _T("http://www.yourcompany.com");

class CXMLMessage
{
	public:

	CXMLMessage();
	virtual ~CXMLMessage();

	virtual int CXMLMessage::Deflate( TCHAR* &a_buffer, int &a_size, int a_alloc = 0, TCHAR* a_memberTag = 0 );
	// TODO: virtual int CXMLMessage::Deflate( TCHAR* a_filename );
	// TODO: virtual int CXMLMessage::Deflate( FILE*  a_fp );

   virtual int CXMLMessage::Inflate( TCHAR* a_buffer, int a_limit );
	virtual int CXMLMessage::Inflate( TCHAR* a_buffer, int a_limit, int &a_size, TCHAR* a_memberTag = 0 );
	virtual int CXMLMessage::Inflate( TCHAR* a_filename );
	virtual int CXMLMessage::Inflate( HANDLE a_handle );

	virtual int CXMLMessage::GenerateDTD( TCHAR* a_memberTag = 0, FILE* a_fp = 0 );
	virtual int CXMLMessage::GenerateXSD( TCHAR* a_memberTag = 0, FILE* a_fp = 0, int a_flags = 0, int a_nestDepth = 0 );

   virtual int CXMLMessage::Copy( CXMLMessage& a_srcMessage);

	// Map related functions, defined by macro BEGIN_XML_MAP

	virtual const MSG_XMLMAP* GetXMLMap() = 0;
	virtual const TCHAR* GetXMLTag() = 0;

	private: 
	int m_wrapperValid;

	// If Deflate() caller does not specify a valid pointer, buffer is 
	// allocated, and destroyed automatically.
	TCHAR* m_xmlBuffer;

   protected:

	virtual void CXMLMessage::Initialize();
	virtual void CXMLMessage::Cleanup();

	void CXMLMessage::Cleanup( const MSG_XMLMAP* a_xmlMap );
	void CXMLMessage::Initialize( const MSG_XMLMAP* a_xmlMap );
	int  CXMLMessage::DeflateMap( const MSG_XMLMAP* a_xmlMap, TCHAR* a_buffer, int &a_size, int a_alloc );
	int  CXMLMessage::GenerateDTDElement( const MSG_XMLMAP* a_xmlMap, FILE* a_fp, int &a_count );
   int  CXMLMessage::GenerateDTDChildren( const MSG_XMLMAP* a_xmlMap, FILE* a_fp );
	int  CXMLMessage::GenerateXSD( const MSG_XMLMAP* a_xmlMap, FILE* a_fp, int a_flags, int a_indent );

	int CXMLMessage::TypeCheck( CXMLMessage& a_msg1, CXMLMessage& a_msg2 );

	// Supports for classes
	int CXMLMessage::DeflateClass( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int &a_size, int a_alloc );
	int CXMLMessage::InflateClass( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int limit );
	int CXMLMessage::InflateClass( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int limit, int &a_size );

	// Supported atomic data types
	int CXMLMessage::DeflateInteger( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int &a_size, int a_alloc );
	int CXMLMessage::DeflateString( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int &a_size, int a_alloc );

	int CXMLMessage::InflateInteger( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int limit, int &a_size );
	int CXMLMessage::InflateString( MSG_XMLMAP_ENTRY* a_entry, TCHAR* a_buffer, int limit, int &a_size );

	// Lookup map entry associated with an XML tag
	MSG_XMLMAP_ENTRY* CXMLMessage::FindEntry( CString a_tag );
	MSG_XMLMAP_ENTRY* CXMLMessage::FindEntry( const MSG_XMLMAP* a_xmlMap, CString a_tag );

	// Support functions
	int CXMLMessage::ParseTag( TCHAR* a_buffer, int a_limit, int &a_size, CString &a_tag, int &a_direction );
	int CXMLMessage::ParseEndTag( TCHAR* a_buffer, int a_limit, int &a_size, CString &a_tag, CString &a_data );
	int CXMLMessage::CheckTag( TCHAR* a_buffer, int a_limit, int &a_size, CString &a_tag  );

};

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