PDA

View Full Version : Question about record getters.



chronozphere
22-10-2010, 12:49 PM
Hi guys,

I have this getter function:



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 :)

AthenaOfDelphi
22-10-2010, 01:50 PM
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.

chronozphere
22-10-2010, 06:40 PM
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:



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:



function TManager.HasTexture(aName: String): Boolean;
begin
Result := Assigned( FindTexture(aName) );
end;


Which is IMHO alot neater than:


function TManager.HasTexture(aName: String): Boolean;
begin
Result := True;
try
FindTexture(aName);
finally
on E: ETextureException do
Result := false;
end;
end;

User137
23-10-2010, 10:46 AM
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

chronozphere
23-10-2010, 11:10 AM
Ok, let me be more clear. I'm not talking about font's and characters here. Just about record setters in common. ;)

Brainer
23-10-2010, 11:49 AM
How about ZeroMemory?


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.

User137
23-10-2010, 11:57 AM
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.