Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 22, 2007 C++ cast to D cast | ||||
---|---|---|---|---|
| ||||
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 Re: C++ cast to D cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | 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 Re: C++ cast to D cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | 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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | 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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | 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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | "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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | "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 Re: C++ cast to D cast Part 2 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 |
Copyright © 1999-2021 by the D Language Foundation