Thread overview
struct - set to null?
May 31, 2004
Brad Anderson
May 31, 2004
Vathix
Jun 01, 2004
Brad Anderson
Jun 01, 2004
Ben Hinkle
Jun 01, 2004
Brad Anderson
May 31, 2004
I am porting some Java code that used classes as structs (because Java lacks structs).

> LOGFONT oldFont;

and later:

> void releaseWidget() {
>     oldFont = null;
> }

The compiler (dmd 0.89, WinXP) complains that you cannot implicitly convert void* to LOGFONT.

So, what options do I have to set this struct to null or point to nothing?

> oldFont = '\0';  (now it's char to LOGFONT)

Also, how do I do the following?

> if(oldFont == null) return;

I poked around the DM website and the Wiki, and found constructors for structs with opCall() but nothing about destructors or setting/comparing structs to null.

BA
May 31, 2004
"Brad Anderson" <brad@sankaty.dot.com> wrote in message news:c9eloi$2d83$1@digitaldaemon.com...
> I am porting some Java code that used classes as structs (because Java
lacks
> structs).
>
>  > LOGFONT oldFont;
>
> and later:
>
>  > void releaseWidget() {
>  >     oldFont = null;
>  > }
>
> The compiler (dmd 0.89, WinXP) complains that you cannot implicitly
convert
> void* to LOGFONT.
>
> So, what options do I have to set this struct to null or point to nothing?
>
>  > oldFont = '\0';  (now it's char to LOGFONT)
>
> Also, how do I do the following?
>
>  > if(oldFont == null) return;
>
> I poked around the DM website and the Wiki, and found constructors for
structs
> with opCall() but nothing about destructors or setting/comparing structs
to null.
>
> BA

Maybe you should use a pointer and set it to null when you're done with it.
You can't set a struct to null just like you can't set an int to null.
Sometimes you do want to initialize all data to zero, but that's different,
I do this:
   (cast(byte*)(&struct))[0 .. struct.size] = 0;
I admit it's ugly, memset() is actually cleaner. To reinitialize the struct
you could do this:
   struct = struct.init;
But either would be very inefficient to do often and worse to check for
those values (especially for that huge LOGFONT).


June 01, 2004
Vathix,

What about another struct member/property (whatever they're called).

struct LOGFONT {
    ...
    bool isCurrent
}

I could set it to true or false and not bother with re-initializing everything.

Any thoughts as to how it would affect any .sizeof calcs in its use? I.E. any negative effects because the WinAPI is expecting a certain set of data and it would receive extra?

Thanks for the response.

BA

Vathix wrote:
> "Brad Anderson" <brad@sankaty.dot.com> wrote in message
> news:c9eloi$2d83$1@digitaldaemon.com...
> 
>>I am porting some Java code that used classes as structs (because Java
> 
> lacks
> 
>>structs).
>>
>> > LOGFONT oldFont;
>>
>>and later:
>>
>> > void releaseWidget() {
>> >     oldFont = null;
>> > }
>>
>>The compiler (dmd 0.89, WinXP) complains that you cannot implicitly
> 
> convert
> 
>>void* to LOGFONT.
>>
>>So, what options do I have to set this struct to null or point to nothing?
>>
>> > oldFont = '\0';  (now it's char to LOGFONT)
>>
>>Also, how do I do the following?
>>
>> > if(oldFont == null) return;
>>
>>I poked around the DM website and the Wiki, and found constructors for
> 
> structs
> 
>>with opCall() but nothing about destructors or setting/comparing structs
> 
> to null.
> 
>>BA
> 
> 
> Maybe you should use a pointer and set it to null when you're done with it.
> You can't set a struct to null just like you can't set an int to null.
> Sometimes you do want to initialize all data to zero, but that's different,
> I do this:
>    (cast(byte*)(&struct))[0 .. struct.size] = 0;
> I admit it's ugly, memset() is actually cleaner. To reinitialize the struct
> you could do this:
>    struct = struct.init;
> But either would be very inefficient to do often and worse to check for
> those values (especially for that huge LOGFONT).
> 
> 
June 01, 2004
Brad Anderson wrote:

> I am porting some Java code that used classes as structs (because Java
> lacks structs).
> 
>  > LOGFONT oldFont;
> 
> and later:
> 
>  > void releaseWidget() {
>  >     oldFont = null;
>  > }
> 
> The compiler (dmd 0.89, WinXP) complains that you cannot implicitly
> convert void* to LOGFONT.
> 
> So, what options do I have to set this struct to null or point to nothing?
> 
>  > oldFont = '\0';  (now it's char to LOGFONT)
> 
> Also, how do I do the following?
> 
>  > if(oldFont == null) return;
> 
> I poked around the DM website and the Wiki, and found constructors for structs with opCall() but nothing about destructors or setting/comparing structs to null.
> 
> BA

I bet the Java code is explicitly setting oldFont to null to help the GC
collect the LOGFONT as garbage. I would skip the oldFont=null in D.
A related question has to do with how the port passes LOGFONT to functions:
are you passing by value or pointer or inout?

-Ben
June 01, 2004
Ben Hinkle wrote:
> 
> I bet the Java code is explicitly setting oldFont to null to help the GC
> collect the LOGFONT as garbage. I would skip the oldFont=null in D.

And I would too, except that later, there's a test to see if LOGFONT is null.

if(LOGFONT == null)

> A related question has to do with how the port passes LOGFONT to functions:
> are you passing by value or pointer or inout?

I believe we are switching to pass them by pointer.  Which is better?

BTW, LOGFONT will have extra properties if version(Unicode) is true. The old way in Java is to have a base class and the unicode and non-unicode classes inherit from it and add their wchar/char properties.

BA