IXxxx* vs. CAdapt < CComPtr < IXxxx > > in STL Collections 
Author Message
 IXxxx* vs. CAdapt < CComPtr < IXxxx > > in STL Collections

Hello again,

What, if any, is the real advantage of using CAdapt < CComPtr < IXxxx > >
over simple IXxxx* element types in an STL-based collection
(ICollectionOnSTL)?  It seems the code would be slightly cleaner without the
use of the CAdapt < CComPtr < > > templates and it personally causes me some
confusion regarding the references counting of the contained objects in the
custom _Copy policy classes:  (BTW, it seems that the _CopyXXXX::destroy()
functions are never getting called so my contained objects aren't being
released?!)

typedef CComQIPtr<IThumb>      SPThumb;

class _CopyIThumbFromIThumbAdapter
{
public:
    static HRESULT copy(IThumb** p1, CAdapt<SPThumb>* p2)
 {
  *p1 = p2->m_T;
  if (*p1)
   (*p1)->AddRef();

  return (p1 ? S_OK : E_NOINTERFACE);
    }

    static void init(SPThumb *p) {;}
    static void destroy(SPThumb *p) { if (*p) p->Release(); }    // never
getting called!

Quote:
};

class _CopyVariantFromIThumbAdapter
{
public:
    static HRESULT copy(VARIANT* p1, CAdapt<SPThumb>* p2)
 {
  p2->m_T.p->AddRef();

  CComVariant v(p2->m_T);
  v.Detach(p1);

  return (p1 ? S_OK : E_NOINTERFACE);
    }

    static void init(VARIANT *p) {;}
    static void destroy(VARIANT *p) { if (p) p->pdispVal->Release(); }    //
never getting called!

Quote:
};



Tue, 18 May 2004 00:45:40 GMT  
 IXxxx* vs. CAdapt < CComPtr < IXxxx > > in STL Collections
The reason is a usual one concerning collections of pointers. CComPtr is a
smart pointer - it calls AddRef and Release in all the right places. With
collections of raw pointers, you are responsible for reference counting,
which makes the code human-error-prone.

CAdapt is necessary to deal with overloaded CComPtr::operator&() - STL
requires that T::operator&() return T*.

_Copy::destroy is used by CComEnumImpl when it is asked to take ownership of
a collection (AtlFlagTakeOwnership or AtlFlagCopy). Also, when _Copy::copy
fails for some element in Init or Next, _Copy::destroy is used by
CComEnumImpl and IEnumOnSTLImpl (the latter for Next only) to destroy
successfully copied elements.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken



Quote:
> Hello again,

> What, if any, is the real advantage of using CAdapt < CComPtr < IXxxx > >
> over simple IXxxx* element types in an STL-based collection
> (ICollectionOnSTL)?  It seems the code would be slightly cleaner without
the
> use of the CAdapt < CComPtr < > > templates and it personally causes me
some
> confusion regarding the references counting of the contained objects in
the
> custom _Copy policy classes:  (BTW, it seems that the _CopyXXXX::destroy()
> functions are never getting called so my contained objects aren't being
> released?!)

> typedef CComQIPtr<IThumb>      SPThumb;

> class _CopyIThumbFromIThumbAdapter
> {
> public:
>     static HRESULT copy(IThumb** p1, CAdapt<SPThumb>* p2)
>  {
>   *p1 = p2->m_T;
>   if (*p1)
>    (*p1)->AddRef();

>   return (p1 ? S_OK : E_NOINTERFACE);
>     }

>     static void init(SPThumb *p) {;}
>     static void destroy(SPThumb *p) { if (*p) p->Release(); }    // never
> getting called!
> };

> class _CopyVariantFromIThumbAdapter
> {
> public:
>     static HRESULT copy(VARIANT* p1, CAdapt<SPThumb>* p2)
>  {
>   p2->m_T.p->AddRef();

>   CComVariant v(p2->m_T);
>   v.Detach(p1);

>   return (p1 ? S_OK : E_NOINTERFACE);
>     }

>     static void init(VARIANT *p) {;}
>     static void destroy(VARIANT *p) { if (p) p->pdispVal->Release(); }
//
> never getting called!
> };



Sat, 22 May 2004 04:56:38 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<<>>>Need C code advice with functions and sorting.<<<>>>

4. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

5. >>>Windows Service<<<

6. basic_string<>::data() vs basic_string<>::c_str()

7. Copy policy for CAdapt<CComPtr<T>>

8. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

9. <<<< C Grammar for yacc needed >>>>

10. - - - = = =<><> W e b - U S A <><>= = = - - -

11. >>>> * Get keypress * <<<<

12. <Ctrl>+<Key>

 

 
Powered by phpBB® Forum Software