Click here to Skip to main content
11,429,154 members (61,762 online)
Click here to Skip to main content
Articles » Languages » C++ / CLI » C++/CLI » Downloads
Add your own
alternative version

CAutoNativePtr - A managed smart pointer for using native objects in managed code

, 20 Jan 2006 Ms-PL
CAutoNativePtr is a managed template class that acts as a smart pointer, and is handy for using native objects in managed code.
autonative.zip
/***
	CAutoNativePtr - A smart pointer for using native objects in managed code.

	Author	:	Nishant Sivakumar
	Email	:	voidnish@gmail.com	
	Blog	:	http://blog.voidnish.com
	Web		:	http://www.voidnish.com 	

	You may freely use this class as long as you include
	this copyright. 
	
	You may freely modify and use this class as long
	as you include this copyright in your modified version. 

	This code is provided "as is" without express or implied warranty. 
	
	Copyright � Nishant Sivakumar, 2006.
	All Rights Reserved.
***/

#pragma once

template<typename T> ref class CAutoNativePtr
{
private:
	T* _ptr;

public:
	CAutoNativePtr() : _ptr(nullptr)
	{
	}

	CAutoNativePtr(T* t) : _ptr(t)
	{
	}

	CAutoNativePtr(CAutoNativePtr<T>% an) : _ptr(an.Detach())
	{
	}

	template<typename TDERIVED> 
		CAutoNativePtr(CAutoNativePtr<TDERIVED>% an) : _ptr(an.Detach())
	{
	}

	!CAutoNativePtr()
	{	
		delete _ptr;
	}

	~CAutoNativePtr()
	{
		this->!CAutoNativePtr();
	}

	CAutoNativePtr<T>% operator=(T* t)
	{
		Attach(t);
		return *this;
	}

	CAutoNativePtr<T>% operator=(CAutoNativePtr<T>% an)
	{
		if(this != %an)
			Attach(an.Detach());
		return *this;
	}

	template<typename TDERIVED> 
		CAutoNativePtr<T>% operator=(CAutoNativePtr<TDERIVED>% an)
	{
		Attach(an.Detach());
		return *this;
	}

	static T* operator->(CAutoNativePtr<T>% an)
	{
		return an._ptr;
	}

	static operator T*(CAutoNativePtr<T>% an)
	{
		return an._ptr;
	}

	T* Detach()
	{
		T* t = _ptr;
		_ptr = nullptr;
		return t;
	}

	void Attach(T* t)
	{
		if(t)
		{	
			if(_ptr != t)
			{
				delete _ptr;
				_ptr = t;
			}
		}
		else
		{
#ifdef _DEBUG
			throw gcnew Exception(
				"Attempting to Attach(...) a nullptr!");
#endif
		}		
	}

	void Destroy()
	{
		delete _ptr;
		_ptr = nullptr;
	}
};

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)

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 20 Jan 2006
Article Copyright 2006 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid