Results 1 to 7 of 7

Thread: Question about record getters.

  1. #1

    Question about record getters.

    Hi guys,

    I have this getter function:

    Code:
    function TNashaGLFont.GetCharacter(const I: Integer): TnaFontChar;
    begin
      if (I >= 0) and (I < Count) then
        result := FChars[I]
      else
        //what do do here?
    end;
    So what should I do there? I could:

    > Return a nullified record. But which function should I use to nullify it so that it works in both Delphi and FPC?
    > I could return a PNaFontChar instead but that would make the data editable, which is not always wanted.
    > I could simply do nothing and have an extra compiler warning.

    What do you guys normally do with this?

    Thanks
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2
    Personally, I'd raise an exception... under normal operating conditions, I'm guessing this error will only occur if you haven't provided all the characters required by the text being displayed by your application... if so, then one of two things has happened... you've added a message and forgotten to check the font (a development issue which should be corrected before delivery, and hence one that should be fixed ASAP by the developer) or the application is installed and the user has messed with it or it's files (an installation issue beyond your control that should be fixed).

    You could use assert, raise an exception during development using an assert and then compile them out with a release build, but I would go with raising an exception simply because from the looks of it the situation should never occur... if it does, you've messed up and you need to know that you've messed up so you can fix it.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3
    Hah. I do use exceptions in my code, but I somehow overlooked them. This is indeed the best thing to do.

    Sometimes, I don't want to use exceptions, because I want to have "silent failure". That would be something like:

    Code:
    Texture := Manager.FindTexture('mytexture');
    if not assigned(Texture) then
    begin
      //Respond accordingly.
    end;
    So FindTexture will return NIL when nothing is found. This can be handy when you want to write a function like Manager.IsTextureLoaded(). You will just do the following:

    Code:
    function TManager.HasTexture(aName: String): Boolean;
    begin
      Result := Assigned( FindTexture(aName) );
    end;
    Which is IMHO alot neater than:
    Code:
    function TManager.HasTexture(aName: String): Boolean;
    begin
      Result := True;
      try
        FindTexture(aName);
      finally
        on E: ETextureException do
          Result := false;
      end;
    end;
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    Personally, i wouldn't have any use for this function. Class internal functions must be coded so they don't go cross the array boundaries.

    I would propably return #0. None of the characters before 32 hold any value to font drawing so they are ignored. #10 or #13 may be used in line spaces #9 for tab. Either way, actual font texture needs characters from 32..255

  5. #5
    Ok, let me be more clear. I'm not talking about font's and characters here. Just about record setters in common.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #6
    How about ZeroMemory?
    Code:
    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      Windows, SysUtils;
    
    type
      TRecord = record
        X: Single;
        Z: String[30];
      end;
    
    var
      X: TRecord;
    begin
      X.X := 3.14;
      X.Z := 'jajeczko';
      Writeln(X.X:0:2, ' ', X.Z);
    
      ZeroMemory(@X, SizeOf(X));
      Writeln(X.X:0:2, ' ', X.Z);
    
      Readln;
    end.

  7. #7
    Quote Originally Posted by chronozphere View Post
    Ok, let me be more clear. I'm not talking about font's and characters here. Just about record setters in common.
    By record do you mean record or any datatype?
    If the datatype is bigger than 4 bytes i will pass it as pointer or var variable and return boolean true/false if it was success. It would in the long run be the slowest possible solution to return the whole record as function result.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •