www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct - set to null?

reply Brad Anderson <brad sankaty.dot.com> writes:
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
next sibling parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"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).
May 31 2004
parent Brad Anderson <brad dsource.dot.org> writes:
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).
May 31 2004
prev sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
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
Jun 01 2004
parent Brad Anderson <brad sankaty.dot.com> writes:
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
Jun 01 2004