Jump to page: 1 2
Thread overview
[COMSTL; Adi's report] interface_cast + interface pointers
Dec 03, 2006
Matthew
Dec 05, 2006
Adi Shavit
Mar 21, 2007
Adi Shavit
Mar 21, 2007
Matthew Wilson
Mar 22, 2007
Adi Shavit
Mar 23, 2007
Matthew Wilson
Mar 25, 2007
Adi Shavit
Apr 08, 2007
Adi Shavit
Apr 08, 2007
Matthew Wilson
Apr 08, 2007
Adi Shavit
Apr 15, 2007
Adi Shavit
Apr 28, 2007
Adi Shavit
Apr 28, 2007
Matthew Wilson
December 03, 2006
> 6. Allow interface_cast_addref<>() to accept interface_ptr arguments
> instead of just bare pointers.
> This would allow more readable code.

This is something that I actually achieved a couple of years ago, and never got round to releasing. (I'd planned to discuss this as part of a series on smart pointers for the Smart Pointers column, but that dragged its feet, and ...)

As it stands, it supports code such as the following:

 {
  LPSTREAM  pstm;
  HRESULT   hr = ::CreateStreamOnHGlobal(NULL, true, &pstm);

  IStream_ptr  p(pstm, false);

  IUnknown_ptr punk = comstl2::interface_cast<IUnknown>(p);

  IMalloc_ptr  pstm2 = comstl2::interface_cast<IMalloc>(punk);
 }

I think you'll agree that this is rather good stuff. I will make it a priority to have this in 1.9.1.

Cheers

Matthew


December 05, 2006
Don't know how parse text/html message
March 21, 2007
Hi Matt,

  I'm reviving an old thread...

Matthew wrote:
>> 6. Allow interface_cast_addref<>() to accept interface_ptr arguments
>> instead of just bare pointers.
>> This would allow more readable code.
>> 
>
> This is something that I actually achieved a couple of years ago, and never got round to releasing. (I'd planned to discuss this as part of a series on smart pointers for the Smart Pointers column, but that dragged its feet, and ...)
>
> As it stands, it supports code such as the following:
>
>  {
>   LPSTREAM  pstm;
>   HRESULT   hr = ::CreateStreamOnHGlobal(NULL, true, &pstm);
>
>   IStream_ptr  p(pstm, false);
>
>   IUnknown_ptr punk = comstl2::interface_cast<IUnknown>(p);
>
>   IMalloc_ptr  pstm2 = comstl2::interface_cast<IMalloc>(punk);
>  }
>
> I think you'll agree that this is rather good stuff. I will make it a
> priority to have this in 1.9.1.
> 
Has this actually been done?

I'm having problems using this.
Here's my /working /code:


       //...
       stlsoft::ref_ptr<IGraphBuilder> pGraph;
       stlsoft::ref_ptr<IMediaControl> pMediaControl;
       //...

       if(SUCCEEDED(co_create_instance(CLSID_FilterGraph, *pGraph*)))
       {

    pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph.get()*),
    false);
         //...

When I remove the *.get()* the whole thing blows up:


    pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph*),
    false);

The compilation errors are really weird:

    --------------------Configuration: ConsoleApp - Win32
    Debug--------------------
    Compiling...
    SequenceProcessor.cpp
    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(351)
    : error C2784: 'I *__cdecl
    stlsoft::comstl_project::simple_interface_cast(I *)' : could not
    deduce template argument for ' *' from 'class
    stlsoft::ref_ptr<struct IGraphBuilder,st
    ruct IGraphBuilder,struct IGraphBuilder>'

    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(566)
    : see reference to function template instantiation '__thiscall
    stlsoft::comstl_project::interface_cast_base<struct IMediaControl
    *,struct stlsoft::comstl_project::addref_
    release<struct IMediaControl *>,struct
    stlsoft::comstl_project::ignore_interface_cast_exception>::stlsoft::comstl_project::interface_cast_base<struct
    IMediaControl *,struct
    stlsoft::comstl_project::addref_release<struct IMediaControl
    *>,struct stlso
    ft::comstl_project::ignore_interface_cast_exception>(class
    stlsoft::ref_ptr<struct IGraphBuilder,struct IGraphBuilder,struct
    IGraphBuilder> &,enum
    stlsoft::comstl_project::interface_cast_base<struct IMediaControl
    *,struct stlsoft::comstl_project::ad
    dref_release<struct IMediaControl *>,struct
    stlsoft::comstl_project::ignore_interface_cast_exception>::NullThrowPermission)'
    being compiled
    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(353)
    : fatal error C1903: unable to recover from previous error(s);
    stopping compilation

    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(566)
    : see reference to function template instantiation '__thiscall
    stlsoft::comstl_project::interface_cast_base<struct IMediaControl
    *,struct stlsoft::comstl_project::addref_
    release<struct IMediaControl *>,struct
    stlsoft::comstl_project::ignore_interface_cast_exception>::stlsoft::comstl_project::interface_cast_base<struct
    IMediaControl *,struct
    stlsoft::comstl_project::addref_release<struct IMediaControl
    *>,struct stlso
    ft::comstl_project::ignore_interface_cast_exception>(class
    stlsoft::ref_ptr<struct IGraphBuilder,struct IGraphBuilder,struct
    IGraphBuilder> &,enum
    stlsoft::comstl_project::interface_cast_base<struct IMediaControl
    *,struct stlsoft::comstl_project::ad
    dref_release<struct IMediaControl *>,struct
    stlsoft::comstl_project::ignore_interface_cast_exception>::NullThrowPermission)'
    being compiled
    Error executing cl.exe.

I suspect it has something to do with MSVC6.
Any thought?

Thanks,
Adi



March 21, 2007
As far as I can remember, the adjustment to interface_cast_addref<> to take ref_ptr<X> arguments has _not_ been done, so the code as you've written it will not work.

However, the following (cleaner, nicer, more transparent) version should:

   //...
   stlsoft::ref_ptr<IGraphBuilder> pGraph;
   stlsoft::ref_ptr<IMediaControl> pMediaControl;
   //...

   if(SUCCEEDED(co_create_instance(CLSID_FilterGraph, pGraph)))
   {
      pMediaControl = interface_cast<IMediaControl>(pGraph);
     //...


:-)

Matt

  "Adi Shavit" <adish@gentech.co.il> wrote in message news:ets930$28it$1@digitalmars.com...
  Hi Matt,

    I'm reviving an old thread...

  Matthew wrote:
6. Allow interface_cast_addref<>() to accept interface_ptr arguments
instead of just bare pointers.
This would allow more readable code.

This is something that I actually achieved a couple of years ago, and never
got round to releasing. (I'd planned to discuss this as part of a series on
smart pointers for the Smart Pointers column, but that dragged its feet, and
...)

As it stands, it supports code such as the following:

 {
  LPSTREAM  pstm;
  HRESULT   hr = ::CreateStreamOnHGlobal(NULL, true, &pstm);

  IStream_ptr  p(pstm, false);

  IUnknown_ptr punk = comstl2::interface_cast<IUnknown>(p);

  IMalloc_ptr  pstm2 = comstl2::interface_cast<IMalloc>(punk);
 }

I think you'll agree that this is rather good stuff. I will make it a
priority to have this in 1.9.1.
  Has this actually been done?

  I'm having problems using this.
  Here's my working code:


       //...
       stlsoft::ref_ptr<IGraphBuilder> pGraph;
       stlsoft::ref_ptr<IMediaControl> pMediaControl;
       //...

       if(SUCCEEDED(co_create_instance(CLSID_FilterGraph, pGraph)))
       {
          pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph.get()), false);
         //...

  When I remove the .get() the whole thing blows up:

          pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph), false);

  The compilation errors are really weird:

    --------------------Configuration: ConsoleApp - Win32 Debug--------------------
    Compiling...
    SequenceProcessor.cpp
    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(351) : error C2784: 'I *__cdecl stlsoft::comstl_project::simple_interface_cast(I *)' : could not deduce template argument for ' *' from 'class stlsoft::ref_ptr<struct IGraphBuilder,st
    ruct IGraphBuilder,struct IGraphBuilder>'
            h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(566) : see reference to function template instantiation '__thiscall stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::addref_
    release<struct IMediaControl *>,struct stlsoft::comstl_project::ignore_interface_cast_exception>::stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::addref_release<struct IMediaControl *>,struct stlso
    ft::comstl_project::ignore_interface_cast_exception>(class stlsoft::ref_ptr<struct IGraphBuilder,struct IGraphBuilder,struct IGraphBuilder> &,enum stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::ad
    dref_release<struct IMediaControl *>,struct stlsoft::comstl_project::ignore_interface_cast_exception>::NullThrowPermission)' being compiled
    h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(353) : fatal error C1903: unable to recover from previous error(s); stopping compilation
            h:\adish\dev\stlsoft\include\comstl\conversion\interface_cast.hpp(566) : see reference to function template instantiation '__thiscall stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::addref_
    release<struct IMediaControl *>,struct stlsoft::comstl_project::ignore_interface_cast_exception>::stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::addref_release<struct IMediaControl *>,struct stlso
    ft::comstl_project::ignore_interface_cast_exception>(class stlsoft::ref_ptr<struct IGraphBuilder,struct IGraphBuilder,struct IGraphBuilder> &,enum stlsoft::comstl_project::interface_cast_base<struct IMediaControl *,struct stlsoft::comstl_project::ad
    dref_release<struct IMediaControl *>,struct stlsoft::comstl_project::ignore_interface_cast_exception>::NullThrowPermission)' being compiled
    Error executing cl.exe.

  I suspect it has something to do with MSVC6.
  Any thought?

  Thanks,
  Adi



March 22, 2007
Indeed, it does!

Can you explain what the difference is between each of the versions, and why this (confusing) redundancy exists?

Here they are for reference:

    //...
    stlsoft::ref_ptr<IGraphBuilder> pGraph;
    stlsoft::ref_ptr<IMediaControl> pMediaControl;
    //...

    1.
    pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph.get()*),
    false); // this is OK
    2.
    pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph*),
    false);       // this blows up.
    3. pMediaControl = interface_cast<IMediaControl>(*pGraph*);
                    // this is OK

Thanks,
Adi


Matthew Wilson wrote:
> As far as I can remember, the adjustment to interface_cast_addref<> to
> take ref_ptr<X> arguments has _not_ been done, so the code as you've
> written it will not work.
> 
> However, the following (cleaner, nicer, more transparent) version should:
> 
>    //...
>    stlsoft::ref_ptr<IGraphBuilder> pGraph;
>    stlsoft::ref_ptr<IMediaControl> pMediaControl;
>    //...
>
>    if(SUCCEEDED(co_create_instance(CLSID_FilterGraph, *pGraph*)))
>    {
>       pMediaControl = interface_cast<IMediaControl>(*pGraph*);
>      //...
> 
> :-)
> 
> Matt
>


March 23, 2007
1. is a cast from a pointer to a pointre
3. is a case from a smartptr to a smartptr
2. is an error (I presume when you say blows up you mean it fails to compile?)

I don't know what you mean by "confusing redundancy". I don't see any redundancy at all, since it is no more/less reasonable to want to be able to cast raw pointers as smartptrs. Is that what you mean?

  "Adi Shavit" <adish@gentech.co.il> wrote in message news:eturnn$6cq$1@digitalmars.com...
  Indeed, it does!

  Can you explain what the difference is between each of the versions, and why this (confusing) redundancy exists?

  Here they are for reference:

    //...
    stlsoft::ref_ptr<IGraphBuilder> pGraph;
    stlsoft::ref_ptr<IMediaControl> pMediaControl;
    //...

    1. pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph.get()), false); // this is OK
    2. pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph), false);       // this blows up.
    3. pMediaControl = interface_cast<IMediaControl>(pGraph);                         // this is OK

  Thanks,
  Adi


  Matthew Wilson wrote:
    As far as I can remember, the adjustment to interface_cast_addref<> to take ref_ptr<X> arguments has _not_ been done, so the code as you've written it will not work.

    However, the following (cleaner, nicer, more transparent) version should:

       //...
       stlsoft::ref_ptr<IGraphBuilder> pGraph;
       stlsoft::ref_ptr<IMediaControl> pMediaControl;
       //...

       if(SUCCEEDED(co_create_instance(CLSID_FilterGraph, pGraph)))
       {
          pMediaControl = interface_cast<IMediaControl>(pGraph);
         //...


    :-)

    Matt



March 25, 2007
Don't know how parse text/html message
April 08, 2007
Any comments on this?
Adi

Adi Shavit wrote:
> Yes, of course, you're right.
> I got a bit disoriented due to the similar names and the template
> types, thus the "confusing" bit.
> I re-read your article /*"enhanced comstl::interface_cast"*/ and that
> sorted me out.
>
> Anyway, I have a bigger challenge. One that I don't think has a simple
> solution.
> ref_ptr + interface_cast give a very robust and readable interface to
> COM programming. That's great.
> However, there are still some places where I wish a cleaner syntax was
> possible.
>
> Here's a quote from my original question list:
>
>     5. Functions like co_create_instance and other initialization functions
>     > expect an address of a pointer as the second argument. This means that
>     > when used with interface_ptr, a temporary bare pointer must be used to
>     > be given like so:
>     >
>     >        // Create a source filter specified by filename
>     >        interface_ptr<IBaseFilter> pSource; // The interface_ptr<> object
>     >        {
>     >           IBaseFilter* ppSource= NULL; // temporary bare pointer
>     >
>     >           if(FAILED(pGraph->AddSourceFilter(a2w(c_str_ptr(filename)),0,&ppSource)))
>     >           {
>     >              //...
>     >              return 0;
>     >           }
>     >           pSource.set(ppSource, false); // setting the smart pointer.
>     >        }
> 
>
>     [your wrote:] Agree that this is not pretty.
>
> Indeed, it is not pretty. This type of code appears in several places
> in my program.
> Since *pSource *is initially empty, it does not even have a raw ptr to
> pass to *AddSourceFilter*().
>
> I was wondering if there might be a way to make this prettier. It would be nice to have something like this:
>
>             // Create a source filter specified by filename
>             interface_ptr<IBaseFilter> pSource; // The interface_ptr<> object
>             if(FAILED(pGraph->AddSourceFilter(a2w(c_str_ptr(filename)),0, *pSource.getAssignableInterface(false)*)))
>             {
>                //...
>                return 0;
>             }
>
> Where, presumably, *getAssignableInterface()* returns an *IBaseFilter*** instance or something convertable to it (the false regards the ref-count).
>
> I don't really have a concrete idea about how this might be implemented, though maybe if a temporary *IBaseFilter** *convertable object is returned, then checks/updates to the actual *pSource *could be done in the temp's dtor. Also, this convertable class could be made temp only, or something along these lines...
>
> Any thoughts?
>
> Adi
>
>
>
> Matthew Wilson wrote:
>> 1. is a cast from a pointer to a pointre
>> 3. is a case from a smartptr to a smartptr
>> 2. is an error (I presume when you say blows up you mean it fails to
>> compile?)
>> 
>> I don't know what you mean by "confusing redundancy". I don't see any
>> redundancy at all, since it is no more/less reasonable to want to be
>> able to cast raw pointers as smartptrs. Is that what you mean?
>> 
>>
>>     "Adi Shavit" <adish@gentech.co.il <mailto:adish@gentech.co.il>>
>>     wrote in message news:eturnn$6cq$1@digitalmars.com...
>>     Indeed, it does!
>>
>>     Can you explain what the difference is between each of the
>>     versions, and why this (confusing) redundancy exists?
>>
>>     Here they are for reference:
>>
>>         //...
>>         stlsoft::ref_ptr<IGraphBuilder> pGraph;
>>         stlsoft::ref_ptr<IMediaControl> pMediaControl;
>>         //...
>>
>>         1.
>>         pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph.get()*),
>>         false); // this is OK
>>         2.
>>         pMediaControl.set(interface_cast_addref<IMediaControl*>(*pGraph*),
>>         false);       // this blows up.
>>         3. pMediaControl = interface_cast<IMediaControl>(*pGraph*);
>>                               // this is OK
>>
>>     Thanks,
>>     Adi
>>
>>


April 08, 2007
Still thinking.

You know me - sometimes months or years pass before I get an answer on an ng question. :-)

I had an idea yesterday actually, but haven't had time to even write it down yet.

Matthew

P.S. Any response to my be.speech idea? ;-)


  "Adi Shavit" <adish@gentech.co.il> wrote in message news:evbhs3$2r39$1@digitalmars.com...
  Any comments on this?
  Adi

  Adi Shavit wrote:
    Yes, of course, you're right.
    I got a bit disoriented due to the similar names and the template types, thus the "confusing" bit.
    I re-read your article "enhanced comstl::interface_cast" and that sorted me out.

    Anyway, I have a bigger challenge. One that I don't think has a simple solution.
    ref_ptr + interface_cast give a very robust and readable interface to COM programming. That's great.
    However, there are still some places where I wish a cleaner syntax was possible.

    Here's a quote from my original question list:


5. Functions like co_create_instance and other initialization functions
> expect an address of a pointer as the second argument. This means that when used with interface_ptr, a temporary bare pointer must be used to be given like so:
>
>        // Create a source filter specified by filename
>        interface_ptr<IBaseFilter> pSource; // The interface_ptr<> object
>        {
>           IBaseFilter* ppSource= NULL; // temporary bare pointer
>
>           if(FAILED(pGraph->AddSourceFilter(a2w(c_str_ptr(filename)),0,&ppSource)))
>           {
>              //...
>              return 0;
>           }
>           pSource.set(ppSource, false); // setting the smart pointer.
>        }
  [your wrote:] Agree that this is not pretty.Indeed, it is not pretty. This type of code appears in several places in my program.
    Since pSource is initially empty, it does not even have a raw ptr to pass to AddSourceFilter().

    I was wondering if there might be a way to make this prettier.
    It would be nice to have something like this:


        // Create a source filter specified by filename
        interface_ptr<IBaseFilter> pSource; // The interface_ptr<> object
        if(FAILED(pGraph->AddSourceFilter(a2w(c_str_ptr(filename)),0, pSource.getAssignableInterface(false))))
        {
           //...
           return 0;
        }
Where, presumably, getAssignableInterface() returns an IBaseFilter** instance or something convertable to it (the false regards the ref-count).

    I don't really have a concrete idea about how this might be implemented, though maybe if a temporary IBaseFilter** convertable object is returned, then checks/updates to the actual pSource could be done in the temp's dtor. Also, this convertable class could be made temp only, or something along these lines...

    Any thoughts?

    Adi



    Matthew Wilson wrote:
      1. is a cast from a pointer to a pointre
      3. is a case from a smartptr to a smartptr
      2. is an error (I presume when you say blows up you mean it fails to compile?)

      I don't know what you mean by "confusing redundancy". I don't see any redundancy at all, since it is no more/less reasonable to want to be able to cast raw pointers as smartptrs. Is that what you mean?

        "Adi Shavit" <adish@gentech.co.il> wrote in message news:eturnn$6cq$1@digitalmars.com...
        Indeed, it does!

        Can you explain what the difference is between each of the versions, and why this (confusing) redundancy exists?

        Here they are for reference:

          //...
          stlsoft::ref_ptr<IGraphBuilder> pGraph;
          stlsoft::ref_ptr<IMediaControl> pMediaControl;
          //...

          1. pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph.get()), false); // this is OK
          2. pMediaControl.set(interface_cast_addref<IMediaControl*>(pGraph), false);       // this blows up.
          3. pMediaControl = interface_cast<IMediaControl>(pGraph);                         // this is OK

        Thanks,
        Adi




April 08, 2007
> Still thinking.
> 
> You know me - sometimes months or years pass before I get an answer on
> an ng question. :-)
:-)
I think what I had in mind is similar to your invoke_get<> idea,

> 
> P.S. Any response to my be.speech idea? ;-)
I sent you an email yesterday...
I'll resend in case it didn't come thorough.
Adi



« First   ‹ Prev
1 2