Jump to page: 1 2
Thread overview
C++ cast to D cast
Sep 22, 2007
BLS
Sep 22, 2007
Bill Baxter
Sep 22, 2007
BLS
Re: C++ cast to D cast Part 2
Sep 22, 2007
BLS
Sep 22, 2007
Bill Baxter
Sep 22, 2007
BLS
Sep 22, 2007
Bill Baxter
Sep 23, 2007
BLS
Sep 23, 2007
BLS
September 22, 2007
Hi, I need some advice regarding "how to translate C++ casts into D" casts.
As you can see the there are a lot within the return Statement.

class CGDI // C++
{
public:
  HDC m_hDC;
public:

  HFONT SelectFont(HFONT hFont)
  {
  return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont)));
  }
  HFONT SelectObject(CFont cf)
  {
   if (IsValidHandle())
    return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont());
    return NULL;
  }
}
I guess :
  return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont)));
But it makes no sense to me. Can you offer me some advice ?

Many thanks in advance? and sorry about my ignorance ...
Bjoern
September 22, 2007
BLS wrote:
> Hi, I need some advice regarding "how to translate C++ casts into D" casts.
> As you can see the there are a lot within the return Statement.
> 
> class CGDI // C++
> {
> public:
>   HDC m_hDC;
> public:
> 
>   HFONT SelectFont(HFONT hFont)
>   {
>   return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont)));
>   }
>   HFONT SelectObject(CFont cf)
>   {
>    if (IsValidHandle())
>     return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont());
>     return NULL;
>   }
> }
> I guess :
>   return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont)));
> But it makes no sense to me. Can you offer me some advice ?

I would guess
   return (cast(HFONT)SelectObject(m_hDC, cast(HGDIOBJ)cast(HFONT)hFont));

It's just a function call -- SelectObject(m_hDC,hFont)

> 
> Many thanks in advance? and sorry about my ignorance ...

Maybe you just need some sleep?  :-)

--bb
September 22, 2007
Bill Baxter schrieb:
> BLS wrote:
>> Hi, I need some advice regarding "how to translate C++ casts into D" casts.
>> As you can see the there are a lot within the return Statement.
>>
>> class CGDI // C++
>> {
>> public:
>>   HDC m_hDC;
>> public:
>>
>>   HFONT SelectFont(HFONT hFont)
>>   {
>>   return ((HFONT)::SelectObject((m_hDC), (HGDIOBJ)(HFONT)(hFont)));
>>   }
>>   HFONT SelectObject(CFont cf)
>>   {
>>    if (IsValidHandle())
>>     return (HFONT) ::SelectObject(m_hDC,(HFONT)cf.GetFont());
>>     return NULL;
>>   }
>> }
>> I guess :
>>   return (cast(HFONT)SelectObject(cast(m_hDC), cast(HGDIOBJ)cast(HFONT)cast(hFont)));
>> But it makes no sense to me. Can you offer me some advice ?
> 
> I would guess
>    return (cast(HFONT)SelectObject(m_hDC, cast(HGDIOBJ)cast(HFONT)hFont));
> 
> It's just a function call -- SelectObject(m_hDC,hFont)
> 
>>
>> Many thanks in advance? and sorry about my ignorance ...
> 
> Maybe you just need some sleep?  :-)
> 
> --bb

Thanks Bill. Yep, need more coffee
Bjoern
September 22, 2007
Hi Bill.
Just something Regan and I allready have discussed ... but we are not sure about .

C++
CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA)); 


D
CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);

where CWin is a class.
The problem here is that pActive in D is a reference.
Bjoern

September 22, 2007
BLS wrote:
> Hi Bill.
> Just something Regan and I allready have discussed ... but we are not sure about .
> 
> C++
> CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA));
> 
> D
> CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
> 
> where CWin is a class.
> The problem here is that pActive in D is a reference.
> Bjoern
> 

The problem I see is that cast for classes in D is like dynamic_cast<> in C++.  It will return null if the typeinfo system doesn't think HWND is a base class of CWin.

Maybe there's some way to disable that, like by casting to void* first?   Or maybe it's smarter than I'm thinking.

--bb
September 22, 2007
Bill Baxter schrieb:
> BLS wrote:
>> Hi Bill.
>> Just something Regan and I allready have discussed ... but we are not sure about .
>>
>> C++
>> CWin* pActive= reinterpret_cast<CWin*>((HWND)::GetWindowLong(msg.hwnd,GWL_USERDATA));
>>
>> D
>> CWin pActive = cast(CWin) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
>>
>> where CWin is a class.
>> The problem here is that pActive in D is a reference.
>> Bjoern
>>
> 
> The problem I see is that cast for classes in D is like dynamic_cast<> in C++.  It will return null if the typeinfo system doesn't think HWND is a base class of CWin.
> 
> Maybe there's some way to disable that, like by casting to void* first?   Or maybe it's smarter than I'm thinking.
> 
> --bb
You mean

CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
???
Thanks
Bjoern
September 22, 2007
"BLS" <nanali@nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1@digitalmars.com...

> CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);

Yessir.


September 22, 2007
Jarrett Billingsley wrote:
> "BLS" <nanali@nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1@digitalmars.com...
> 
>> CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
> 
> Yessir. 

Yes that's what I meant, but I have no idea if it works.  It was just a guess.  Jarrett you can confirm that that avoids the runtime check in the cast?

--bb
September 23, 2007
"Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fd3tfr$8i6$2@digitalmars.com...
> Jarrett Billingsley wrote:
>> "BLS" <nanali@nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1@digitalmars.com...
>>
>>> CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
>>
>> Yessir.
>
> Yes that's what I meant, but I have no idea if it works.  It was just a guess.  Jarrett you can confirm that that avoids the runtime check in the cast?
>
> --bb

class A
{

}

class B
{

}

void main()
{
    A a = new A();
    B b = cast(B)cast(void*)a;
    B b2 = cast(B)a;

    Stdout.formatln("{:X8} {:X8}", cast(void*)b, cast(void*)b2);
}

This prints out (something like)

0012AB60 00000000

So yes, the cast(B)cast(void*) avoids a runtime check.


September 23, 2007
Jarrett Billingsley schrieb:
> "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fd3tfr$8i6$2@digitalmars.com...
>> Jarrett Billingsley wrote:
>>> "BLS" <nanali@nospam-wanadoo.fr> wrote in message news:fd2v57$1ueb$1@digitalmars.com...
>>>
>>>> CWin pActive = cast(CWin)cast(void*) cast(HWND)GetWindowLong(msg.hwnd,GWL_USERDATA);
>>> Yessir.
>> Yes that's what I meant, but I have no idea if it works.  It was just a guess.  Jarrett you can confirm that that avoids the runtime check in the cast?
>>
>> --bb
> 
> class A
> {
> 
> }
> 
> class B
> {
> 
> }
> 
> void main()
> {
>     A a = new A();
>     B b = cast(B)cast(void*)a;
>     B b2 = cast(B)a;
> 
>     Stdout.formatln("{:X8} {:X8}", cast(void*)b, cast(void*)b2);
> }
> 
> This prints out (something like)
> 
> 0012AB60 00000000
> 
> So yes, the cast(B)cast(void*) avoids a runtime check. 
> 
>
Thanks !
Jarret, do you see a chance to place your solution, as well as comments at
http://www.prowiki.org/wiki4d/wiki.cgi?PortingOverview
Porting Code from C++ to D
(Hope I am not asking for too much, but the more you are going into details the more ... You know :)
Bjoern
« First   ‹ Prev
1 2