www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ cast to D cast

reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Sep 22 2007
parent BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
prev sibling parent reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Sep 22 2007
parent reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 22 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Sep 22 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Sep 22 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Sep 23 2007
parent reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 23 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BLS" <nanali nospam-wanadoo.fr> wrote in message 
news:fd6g6h$pev$1 digitalmars.com...
 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
Done.
Sep 23 2007
parent BLS <nanali nospam-wanadoo.fr> writes:
Jarrett Billingsley schrieb:
 "BLS" <nanali nospam-wanadoo.fr> wrote in message 
 news:fd6g6h$pev$1 digitalmars.com...
 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
Done.
Have to sleep, guess I'll spend myself a bottle of beer now, what remains : beeristasty = Muchas.Gracias.Hombre() Bjoern (I Do not speak spanish)
Sep 23 2007