Click here to Skip to main content
15,887,585 members
Articles / Desktop Programming / ATL

Docking ActiveX Controls: Principles and Implementation

Rate me:
Please Sign up or sign in to vote.
4.82/5 (7 votes)
28 Aug 200115 min read 183.7K   2.9K   82  
The article decribes how to implement docking ActiveX control using MFC and ATL
// This is a part of the Active Template Library. 
// Copyright (C) 1995-1999 Microsoft Corporation
// All rights reserved. 
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.

// VCUE_Copy.h
//
// This file contains ATL-style copy policy classes
// ATL uses copy policy classes in its enumerator and collection interface implementations
//
//////////////////////////////////////////////////////////////////////

#if !defined(_GENERICCOPY_H___36A49827_B15B_11D2_BA63_00C04F8EC847___INCLUDED_)
#define _GENERICCOPY_H___36A49827_B15B_11D2_BA63_00C04F8EC847___INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include <AtlCom.h>

namespace VCUE
{
	template <class DestinationType, class SourceType = DestinationType>
	class GenericCopy
	{
	public :
		typedef DestinationType	destination_type;
		typedef SourceType		source_type;

		static void init(destination_type* p)
		{
			_Copy<destination_type>::init(p);
		}
		static void destroy(destination_type* p)
		{
			_Copy<destination_type>::destroy(p);
		}
		static HRESULT copy(destination_type* pTo, const source_type* pFrom)
		{
			return _Copy<destination_type>::copy(pTo, const_cast<source_type*>(pFrom));
		}

	}; // class GenericCopy

	template <>
	class GenericCopy<BSTR>
	{
	public :
		typedef BSTR	destination_type;
		typedef BSTR	source_type;

		static void init(destination_type* p)
		{
			*p = NULL;
		}
		static void destroy(destination_type* p)
		{
			SysFreeString(*p);
			*p = NULL;
		}
		static HRESULT copy(destination_type* pTo, const source_type* pFrom)
		{
			*pTo = SysAllocStringByteLen(reinterpret_cast<const char*>(*pFrom), SysStringByteLen(*pFrom));
			if (*pTo)
				return S_OK;
			else
				return E_OUTOFMEMORY;
		}
	
	}; // class GenericCopy<BSTR>

	template <>
	class GenericCopy<VARIANT, BSTR>
	{
	public :
		typedef VARIANT	destination_type;
		typedef BSTR	source_type;

		static void init(destination_type* p)
		{
			GenericCopy<destination_type>::init(p);
		}
		static void destroy(destination_type* p)
		{
			GenericCopy<destination_type>::destroy(p);
		}
		static HRESULT copy(destination_type* pTo, const source_type* pFrom)
		{
			return CComVariant(*pFrom).Detach(pTo);
		}

	}; // class GenericCopy<VARIANT, BSTR>

	template <>
	class GenericCopy<BSTR, VARIANT>
	{
	public :
		typedef BSTR	destination_type;
		typedef VARIANT	source_type;

		static void init(destination_type* p)
		{
			GenericCopy<destination_type>::init(p);
		}
		static void destroy(destination_type* p)
		{
			GenericCopy<destination_type>::destroy(p);
		}
		static HRESULT copy(destination_type* pTo, const source_type* pFrom)
		{
			VARIANT var = {0};
			HRESULT hr = VariantChangeType(&var, const_cast<VARIANT*>(pFrom), 0, VT_BSTR);
			if (FAILED(hr))
				return hr;

			*pTo = var.bstrVal;
			return S_OK;
			// We deliberately do not free the variant.
		}

	}; // class GenericCopy<BSTR, VARIANT>

	template <class MapType, class DestinationType = MapType::referent_type>
	class MapCopy
	{
	public :
		typedef DestinationType			destination_type;
		typedef MapType::value_type		source_type;
		
		typedef MapType					map_type;
		typedef MapType::referent_type	pseudosource_type;

		static void init(destination_type* p)
		{
			GenericCopy<destination_type, pseudosource_type>::init(p);
		}
		static void destroy(destination_type* p)
		{
			GenericCopy<destination_type, pseudosource_type>::destroy(p);
		}
		static HRESULT copy(destination_type* pTo, const source_type* pFrom)
		{
			return GenericCopy<destination_type, pseudosource_type>::copy(pTo, &(pFrom->second));
		}

	}; // class MapCopy

}; // namespace VCUE

#endif // !defined(_GENERICCOPY_H___36A49827_B15B_11D2_BA63_00C04F8EC847___INCLUDED_)

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