<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx</link><description>After programming in C++ for a while, you will inevitably be introduced to the concept of &amp;#8220;smart pointers&amp;#8221; (or you will discover them on your own). These are pointers that know to automatically release the objects they point to when they (the</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210161</link><pubDate>Fri, 06 Aug 2004 20:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210161</guid><dc:creator>Vatsan Madhavan</dc:creator><description>I like making the common case easy. Writing a Dispose() function (a Global one at that!! eeeks!) is a pain. So this is what I'd do different:&lt;br&gt;&lt;br&gt;//Provide a default for the Dispose() function&lt;br&gt;template&amp;lt; typename T, void (__stdcall *Dispose)( T )=0, const T NullResource = 0&amp;gt;&lt;br&gt;class CAutoResource&lt;br&gt;{&lt;br&gt;........&lt;br&gt;~CAutoResource()&lt;br&gt;{&lt;br&gt;    if ( NullResource != m_Resource )&lt;br&gt;    {&lt;br&gt;	if (Dispose) &lt;br&gt;	   Dispose( m_Resource );&lt;br&gt;	else delete m_Resource; //default to delete&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;........&lt;br&gt;};&lt;br&gt;&lt;br&gt;&lt;br&gt;I tried to do something like this:&lt;br&gt;&lt;br&gt;template&amp;lt; typename T, void (__stdcall *Dispose)( T )=operator delete, const T NullResource = 0&amp;gt;&lt;br&gt;&lt;br&gt;but this wouldn't compile. Can this be made to work ?&lt;br&gt;</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210173</link><pubDate>Fri, 06 Aug 2004 21:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210173</guid><dc:creator>the1</dc:creator><description>Vatson,&lt;br&gt;&lt;br&gt;CAutoResource is not the right place to have a default delete function, as the resource may not be a pointer at all.  CAutoPtrEx is what you want.&lt;br&gt;&lt;br&gt;'operator delete' is not accepted as a function pointer.  You can define a template function like this:&lt;br&gt;&lt;br&gt;template &amp;lt; typename T &amp;gt;&lt;br&gt;void Delete( T * p )&lt;br&gt;{&lt;br&gt;delete p;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Then you can do:&lt;br&gt;&lt;br&gt;template &amp;lt; typename T, void (* Dispose)( T * ) = Delete &amp;gt;&lt;br&gt;class CAutoPtrEx&lt;br&gt;{&lt;br&gt;...&lt;br&gt;};&lt;br&gt;</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210176</link><pubDate>Fri, 06 Aug 2004 21:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210176</guid><dc:creator>Simon Cooke [exMSFT]</dc:creator><description>Problem with your generic handle implementation;&lt;br&gt;&lt;br&gt;It doesn't work for File handles. &lt;br&gt;&lt;br&gt;Easily fixed though; just replace the Null value with INVALID_HANDLE_VALUE instead of 0.</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210183</link><pubDate>Fri, 06 Aug 2004 21:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210183</guid><dc:creator>the1</dc:creator><description>Simon,&lt;br&gt;&lt;br&gt;There is no problem for file handles.  I made the value denoting null resource a *template parameter* just for this purpose.  You just need to do something like:&lt;br&gt;&lt;br&gt;typedef CAutoResource&amp;lt; int, close, INVALID_HANDLE_VALUE &amp;gt; CAutoFileHandle;&lt;br&gt;&lt;br&gt;assuming close() closes a file handle, and the concrete type for file handle is int.</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210252</link><pubDate>Fri, 06 Aug 2004 22:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210252</guid><dc:creator>Rick Schaut</dc:creator><description>Rather than use a pointer to a function for releasing the resource, use functors:&lt;br&gt;&lt;br&gt;// Virtual base class for release functor&lt;br&gt;class CAutoRelease&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;    virtual void operator()(void *) = NULL;&lt;br&gt;};&lt;br&gt;&lt;br&gt;// Changes to CAutoResource:&lt;br&gt;template&amp;lt;typename T, const T NullResource = 0&amp;gt;&lt;br&gt;class CAutoResource&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;&amp;#160;&amp;#160;&amp;#160; typedef T ResourceType;&lt;br&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CAutoResource( CAutoRelease &amp;amp;Release, T Resource = NullResource )&lt;br&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : m_Resource( Resource ),&lt;br&gt;              m_Release(Release) {};&lt;br&gt;    ~CAutoResource()&lt;br&gt;        {&lt;br&gt;        if (NullResource != m_Resource)&lt;br&gt;            m_Release(m_Resource);&lt;br&gt;        };&lt;br&gt;&lt;br&gt;[...]&lt;br&gt;&lt;br&gt;private:&lt;br&gt;    T m_Resource;&lt;br&gt;   CAutoRelease &amp;amp;m_Release;&lt;br&gt;};&lt;br&gt;&lt;br&gt;// Templated class for pointer to a type&lt;br&gt;template &amp;lt;class T&amp;gt;&lt;br&gt;class CAutoReleasePtr&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;    virtual void operator()(void *p)&lt;br&gt;        {&lt;br&gt;        T *pt = static_cast&amp;lt;T*&amp;gt;(p);&lt;br&gt;        delete pt;&lt;br&gt;        ];&lt;br&gt;};&lt;br&gt;&lt;br&gt;// Templated class for pointer to a COM interface&lt;br&gt;template &amp;lt;class T&amp;gt;&lt;br&gt;class CAutoReleaseInterface&lt;br&gt;{&lt;br&gt;public:&lt;br&gt;    virtual void operator()(void *p)&lt;br&gt;        {&lt;br&gt;        T *pi = static_cast&amp;lt;T *&amp;gt;(p);&lt;br&gt;        pi-&amp;gt;Release();&lt;br&gt;        };&lt;br&gt;};&lt;br&gt;&lt;br&gt;While CAutoResource desn't have a default constructor, any subclass can have a default constructor that passes in the appropriate release functor:&lt;br&gt;&lt;br&gt;// Template CAutoResource class for COM interfaces&lt;br&gt;template &amp;lt;class T&amp;gt;&lt;br&gt;class CAutoCOMPtr : public CAutoResource&lt;br&gt;{&lt;br&gt;    typedef T *PInterface;&lt;br&gt;    CAutoCOMPtr() :   CAutoResource(CAutoReleaseInterface&amp;lt;PInterface&amp;gt;, NULL) {};&lt;br&gt;&lt;br&gt;[...]&lt;br&gt;};&lt;br&gt;&lt;br&gt;Of course, caveats apply since I've not compiled or run any of the above code, but it shouldn't be difficult to get it to work.&lt;br&gt;&lt;br&gt;Rick&lt;br&gt;</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210281</link><pubDate>Sat, 07 Aug 2004 00:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210281</guid><dc:creator>Rick Schaut</dc:creator><description>Actually, there's a problem with the type conflict between the typedef given in CAutoResource and the operator() in the CAutoRelease virtual base class, so the idea needs a bit more work to be fleshed out completely.  Nonetheless, I'd still look for a functor-based solution rather than use a pointer to function.&lt;br&gt;&lt;br&gt;Rick</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#210501</link><pubDate>Sat, 07 Aug 2004 11:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210501</guid><dc:creator>indranil banerjee</dc:creator><description>There is one feature that std::auto_ptr has that your class doesn't. And that is the ability to return auto_ptr from a function.&lt;br&gt;&lt;br&gt;You'd have to implement the copy constructor for that.</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#211045</link><pubDate>Mon, 09 Aug 2004 03:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:211045</guid><dc:creator>David A. Mellis</dc:creator><description>What's the advantage of rewriting std::auto_ptr (and, when you decide you need more functionality, boost::shared_ptr) when you can simply wrap your resource in a class and use the existing smart pointers?  &lt;br&gt;&lt;br&gt;Or, what's the advantage of making the Dispose function a template parameter instead of defining its behavior with a virtual destructor?</description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#211179</link><pubDate>Mon, 09 Aug 2004 10:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:211179</guid><dc:creator>Dee Zsombor</dc:creator><description>With boost::shared_ptr you can get the same functionality and even better as you will use a class soon to be part of the new C++ standard!&lt;br&gt;&lt;br&gt;Copy paste from shared_ptr documentation:&lt;br&gt;&lt;br&gt;shared_ptr&amp;lt;IWhatever&amp;gt; make_shared_from_COM(IWhatever * p)&lt;br&gt;{&lt;br&gt;    p-&amp;gt;AddRef();&lt;br&gt;    shared_ptr&amp;lt;IWhatever&amp;gt; pw(p, mem_fn(&amp;amp;IWhatever::Release));&lt;br&gt;    return pw;&lt;br&gt;}&lt;br&gt;&lt;br&gt;Note how the resource is returned from a function ... </description></item><item><title>re: Generalize Smart Pointers in C++</title><link>http://blogs.msdn.com/the1/archive/2004/08/06/210131.aspx#217592</link><pubDate>Fri, 20 Aug 2004 06:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:217592</guid><dc:creator>the1</dc:creator><description>Rick,&lt;br&gt;&lt;br&gt;Good point.  I'll consider using functor as opposed to function pointer.&lt;br&gt;&lt;br&gt;David &amp;amp; Dee,&lt;br&gt;&lt;br&gt;Thanks for pointing me to shared_ptr.  Why do we need CAutoResource when we can wrap the resource in a class and use existing smart pointers?  The answer is that we don't want the resource to behave like a pointer.  i.e. we want to say 'resource' instead of '*resource'.</description></item></channel></rss>