Click here to Skip to main content
15,896,481 members
Articles / Programming Languages / C++11

Implementing COM Interfaces with C++0x Variadic Templates

Rate me:
Please Sign up or sign in to vote.
4.92/5 (25 votes)
4 Sep 2011CPOL11 min read 43.1K   662   35  
A step-by-step illustration of a minimalistic pattern for implementing a series of COM interfaces with little code
#pragma once

/*
 * Copyright (c) 2011, KO Software (official@ko-sw.com)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     - All original and modified versions of this source code must include the
 *       above copyright notice, this list of conditions and the following
 *       disclaimer.
 *
 *     - The name of KO Software may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KO SOFTWARE ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT WILL KO SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "sdk_ComObject.h"

//! If Visual Studio 2010 supported variadic templates and we wanted to use
//! it in our works instead of GCC, the below line would need to be changed to
//! #include <msxml2.h>
//! where everything is already declared properly.
#if _MSC_VER > 1100
#  include <msxml2.h>
#else
#  include "msxml.h"
#endif


////////////////////////////////////////////////////////////////////////////////

class XmlHandler :

#if (SUPPORTED_CPP0X != 0)
	public ComEntryV<ISAXContentHandler>
#else
	public ComEntry1<ISAXContentHandler>
#endif

{
private:
	std::vector<std::wstring> myStack;	//!< Stack of tag names being parsed
	std::wstring myCurVersion;


// Interface
public:
  const std::wstring & GetParsedCurrentVersion() const
    { return myCurVersion; }


// ISAXContentHandler members
protected:
	STDMETHODIMP putDocumentLocator
		(
			ISAXLocator *    /* theVal               */
		)
	{ return S_OK; }


	STDMETHODIMP startPrefixMapping
		(
			LPCWSTR          /* thePrefix            */,
			int              /* theNumCharsPrefix    */,
			LPCWSTR          /* theUri               */,
			int              /* theNumCharsUri       */
		)
	{ return S_OK; }


	STDMETHODIMP endPrefixMapping
		(
			LPCWSTR          /* thePrefix            */,
			int              /* theNumCharsPrefix    */
		)
	{ return S_OK; }


	STDMETHODIMP ignorableWhitespace
		(
			LPCWSTR          /* theWhsp              */,
			int              /* theNumChars          */
		)
	{ return S_OK; }


	STDMETHODIMP processingInstruction
		(
			LPCWSTR          /* theTarget            */,
			int              /* theNumCharsTarget    */,
			LPCWSTR          /* theData              */,
			int              /* theNumCharsData      */
		)
	{ return S_OK; }


	STDMETHODIMP skippedEntity
		(
			LPCWSTR          /* theName              */,
			int              /* theNumCharsName      */
		)
	{ return S_OK; }


	STDMETHODIMP startDocument()
	{ return S_OK; }


	STDMETHODIMP endDocument()
	{ return S_OK; }


	STDMETHODIMP startElement
		(
			LPCWSTR          /* theUri               */,
			int              /* theNumCharsUri       */,
			LPCWSTR             theLocalName           ,
			int                 theNumCharsLocalName   ,
			LPCWSTR          /* theQualName          */,
			int              /* theNumCharsQualName  */,
			ISAXAttributes * /* theAttribs           */
		)
	{
		std::wstring aName(theLocalName, theLocalName + theNumCharsLocalName);
		myStack.push_back(aName);

		return S_OK;
	}


	STDMETHODIMP endElement
		(
			LPCWSTR          /* theUri               */,
			int              /* theNumCharsUri       */,
			LPCWSTR             theLocalName           ,
			int                 theNumCharsLocalName   ,
			LPCWSTR          /* theQualName          */,
			int              /* theNumCharsQualName  */
		)
	{
		std::wstring aName(theLocalName, theLocalName + theNumCharsLocalName);

		if (myStack.back() != aName)
			return E_FAIL;

		myStack.pop_back();
		return S_OK;
	}


	STDMETHODIMP characters
		(
			LPCWSTR             theString              ,
			int                 theNumChars
		)
	{
		if (myStack.size() == 3)
			if ( myStack[0] == L"XML_DIZ_INFO" &&
					 myStack[1] == L"Program_Info" &&
					 myStack[2] == L"Program_Version" )
			{
				myCurVersion += std::wstring(theString, theString + theNumChars);
			}

		return S_OK;
	}
};

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 Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
Kirill Osipov constantly lives in Petrozavodsk, Russia. He enjoys listening to music, playing the guitar, learning German, and, occasionally, drinking beer and whisky.

He is extremely passionate about software development and has recently realized he can be most creative in this particular field.

He also maintains his part time business at http://www.ko-sw.com.

Comments and Discussions