'idiomatic' porting of c and or c++ code that does NULL checking

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 24 19:17:57 PDT 2014


On 8/23/2014 7:19 PM, nikki wrote:

>
> How would you write it?
```
// Put this somewhere you can import it into any module calling SDL
class SDLError : Error {
     public this( string msg, string file = __FILE__, size_t line = 
__LINE__ ) {
         import std.string;
         import std.conv;
         import derelict.sdl2.sdl;

         auto fmsg = format( "%s: %s", msg, to!string( SDL_GetError() ));
         super( fmsg, file, line, null );
     }
}

void init()
{
     //Initialize SDL
     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
     {
         throw new SDL_Error( "Failed to init SDL" );
     }


     //Create window
     gWindow = SDL_CreateWindow( "SDL Tutorial", 
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, 
SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
     if( !gWindow )
     {
         throw new SDLError( "Failed to create window" );
     }

     //Get window surface
     gScreenSurface = SDL_GetWindowSurface( gWindow );
}

```

Regarding 'is' vs == for equality, I prefer to use the latter with 
pointers returned from C so that I can keep in mind that it actually is 
something given to me from C and is not managed by the GC. I can tell at 
a glance.

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com



More information about the Digitalmars-d-learn mailing list