Re: [fpc-pascal] methods of an object to create others objects

2010-12-02 Thread David Emerson
> Wouldn't it be nice if we had a try..except..finally statement
> supported in FPC. All-in-one.

We do: it is called "try try"

try try
  // ...
except
  // ...
finally
  // ...

Okay, so maybe that's slightly nonstandard, but I'd rather use this awkwardness 
than add an even more awkward extra indentation level

~D.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Cannot get TFPTimer to work

2010-12-02 Thread Darius Blaszyk
Whatever I tried, I cannot get TFPTimer to work. Can someone help? For
some reason the OnTimer even is never fired. Tried on Windows and Linux.
Here's a snippet I used as test.

Regards, Darius



program fptimertest;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils, fpTimer;

type

  { TBaseObj }

  TBaseObj = class(TObject)
 tmr: TFPTimer;
  public
 constructor Create;
 constructor Destroy;
 procedure OnTimerExec(Sender: TObject);
  end;

{ TBaseObj }

constructor TBaseObj.Create;
begin
  tmr := TFPTimer.Create(nil);
  tmr.Interval := 1;
  tmr.OnTimer:=...@ontimerexec;
  tmr.StartTimer;
end;

constructor TBaseObj.Destroy;
begin
  tmr.StopTimer;
  tmr.Free;
end;

procedure TBaseObj.OnTimerExec(Sender: TObject);
begin
  writeln('Timer executed');
end;

var
  test: TBaseObj;
  i: integer;

begin
  test := TBaseObj.Create;
  for i := 1 to 1000 do
sleep(10);

  writeln('done');
  readln;
end.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] methods of an object to create others objects

2010-12-02 Thread Martin

On 02/12/2010 18:26, David Emerson wrote:

Wouldn't it be nice if we had a try..except..finally statement
supported in FPC. All-in-one.

We do: it is called "try try"

try try
   // ...
except
   // ...
finally
   // ...


I wonder where the *big* advantage of this "try..except..finally" is?

Looking at your code above there are 2 (simplified) cases.
1) you handle the exception in the except case, and it is ot re-risen
2) you re-raise it again
Variations of the 2 above:
3) a combination: sometimes handle,sometimes raise
4) the need to cover a new exception that may arise while handling the 
first exception


Lets look at them:
1)
  try
DoDangerousStuff;
  except
FixTheDamege
  end;
  DoYourFinalizationStuff;

Since any exception is handled, and no new exception is risen in the 
except block, the code after the except block is *always* executed. It 
does not need a "finally" statement.



2) re-raise the exception
  try
DoDangerousStuff;
  except
DoYourFinalizationStuff;
LogTheExceptionAnd_Raise_ItAgain
  end;
  DoYourFinalizationStuff;

In case of an exception, the 2nd "DoYourFinalizationStuff;" is not 
called. To avoid code duplication, you would have to put the code into a 
local subroutine.


But yes, in this ase, an extra finally may be slighly more readable


3 and 4 are handled in the same way as 2.

Additionally, if your finalization code is only "FreeAndNil(SomeVar)", 
then you can add it in and after the exception block, and even allow it 
to run twice. The 2nd run does no damage.


my 2 cents
Martin

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cannot get TFPTimer to work

2010-12-02 Thread Burkhard Carstens
Am Donnerstag, 2. Dezember 2010 19:39 schrieb Darius Blaszyk:
> Whatever I tried, I cannot get TFPTimer to work. Can someone help?
> For some reason the OnTimer even is never fired. Tried on Windows and
> Linux. Here's a snippet I used as test.
>
> Regards, Darius
>

[..]

> begin
>   test := TBaseObj.Create;
>   for i := 1 to 1000 do

begin

> sleep(10);

checkSynchronize;
end;

>
>   writeln('done');
>   readln;
> end.

Isn't OnTimerExec called using Synchronize?



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cannot get TFPTimer to work

2010-12-02 Thread Michael Van Canneyt



On Thu, 2 Dec 2010, Burkhard Carstens wrote:


Am Donnerstag, 2. Dezember 2010 19:39 schrieb Darius Blaszyk:

Whatever I tried, I cannot get TFPTimer to work. Can someone help?
For some reason the OnTimer even is never fired. Tried on Windows and
Linux. Here's a snippet I used as test.

Regards, Darius



[..]


begin
  test := TBaseObj.Create;
  for i := 1 to 1000 do


begin


sleep(10);


checkSynchronize;
end;



  writeln('done');
  readln;
end.


Isn't OnTimerExec called using Synchronize?


Yes.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cannot get TFPTimer to work

2010-12-02 Thread Darius Blaszyk
> checkSynchronize;

Thanks Burkhard. It works now.

Regards, Darius

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cannot get TFPTimer to work

2010-12-02 Thread Brian Winfrey
See .../fpcsrc/packages/fcl-base/examples/testtimer.pp for example usage.


On Thu, Dec 2, 2010 at 10:39 AM, Darius Blaszyk
 wrote:
> Whatever I tried, I cannot get TFPTimer to work. Can someone help? For
> some reason the OnTimer even is never fired. Tried on Windows and Linux.
> Here's a snippet I used as test.
>
> Regards, Darius
>
>
>
> program fptimertest;
>
> {$mode objfpc}{$H+}
>
> uses
>  {$IFDEF UNIX}
>  cthreads,
>  {$ENDIF}
>  SysUtils, fpTimer;
>
> type
>
>  { TBaseObj }
>
>  TBaseObj = class(TObject)
>     tmr: TFPTimer;
>  public
>     constructor Create;
>     constructor Destroy;
>     procedure OnTimerExec(Sender: TObject);
>  end;
>
> { TBaseObj }
>
> constructor TBaseObj.Create;
> begin
>  tmr := TFPTimer.Create(nil);
>  tmr.Interval := 1;
>  tmr.OnTimer:=...@ontimerexec;
>  tmr.StartTimer;
> end;
>
> constructor TBaseObj.Destroy;
> begin
>  tmr.StopTimer;
>  tmr.Free;
> end;
>
> procedure TBaseObj.OnTimerExec(Sender: TObject);
> begin
>  writeln('Timer executed');
> end;
>
> var
>  test: TBaseObj;
>  i: integer;
>
> begin
>  test := TBaseObj.Create;
>  for i := 1 to 1000 do
>    sleep(10);
>
>  writeln('done');
>  readln;
> end.
>
> ___
> fpc-pascal maillist  -  fpc-pas...@lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] OFFTOPIC: Money Model, for FPC

2010-12-02 Thread Osvaldo Filho
Is Firebird really free?

If you’re expecting some sensation, I’ll not make you happy. Shortly, yes,
Firebird is free, completely. You can use it wherever you want and you don’t
have to pay anything nor release sources of your application nor …

On the other hand, the whole truth, considering all the edge cases and
consequences, is different. To keep high quality of final product, keep
adding new features, provide bug fixes – simply moving forward – the creator
needs some resources. If these resources will not be available, it will be
effectively dead. The resources I’m here talking about, in case of Firebird,
are people doing full time or regular development. These people have
families, houses, hobbies, … And for all of these items you need money (in
our society [image: :)] ). They’re not doing it for fun (only), but also for
living as well.

So Firebird actually needs some money to keep moving. It doesn’t have
licenses to buy or something like that. We’re simply relying on the fact,
that people using it, similar to people working on it, do love it. And are
educated enough to realize all this and provide, even small, support. Thus
next time you’ll be deploying your application with Firebird, think about
sending $10 or even $1. I bet it’s nothing for you (compared to price of the
application or money you’re paying for toilet paper in your office). And ten
thousand people (not much) donating $10 makes a huge difference. It’s not
only about few donating $1.

And by the way, Firebird is not “just” engine, but tools around too: .NET
driver [image: ;)] , Java driver, documentation, QA, …, you name it.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

[fpc-pascal] 2D Dynamic arrays and BlockRead

2010-12-02 Thread andrew.bennett
After using BlockRead to fill a 2D dynamic array, I get an access violation on 
the 
very first reference. A 2D array with only one dimension dynamic works OK.
What am I missing?

Windows, FPC 2.2.2, 2.2.4 or 2.4.2.

Program WriteA ; { Write a binary file using using static array and BlockWrite }
Const W = 2 ; H = 2 ; FName = 'C:\FPC\2.2.2\USR\GASH.BIN' ;
Type  A2T = Array[0..H-1,0..W-1] Of Single ;
Var D : A2T ; F : File ; J, C : Longint ;
Begin
  D[0,0] := 1 ; D[0,1] := 2 ; D[1,0] := 3 ; D[1,1] := 4 ;
  Assign(F, FName) ;  Rewrite(F, 1) ;
  For J := 0 To H-1 Do Blockwrite(F, D[J], W*Sizeof(Single), C) ;
  Close(F) ;
  Writeln(D[0,0]:2:0, #9, D[0,1]:2:0, #9, D[1,0]:2:0, #9, D[1,1]:2:0) ;
End.

Program DynRd ; { Read binary file written by "WriteA" }
Const W = 2 ; H = 2 ; FName = 'C:\FPC\2.2.2\USR\GASH.BIN' ;
Type
  STat  = Array[0..W-1] Of Single ; { Static array }
  DST = Array Of STat ; { One dimension dynamic, the other static }
  D2T = Array Of Array Of Single ; { Two dynamic dimensions }
Var DD : D2T ; DS : DST ; F : File ; J, C : Longint ;
Begin
  SetLength(DS, H) ; { Test mixed array }
  Assign(F, FName) ;
  Reset(F, 1) ;
  For J := 0 To H-1 Do Begin
BlockRead(F, DS[J], W*Sizeof(Single), C) ;
If (C <> W*Sizeof(Single)) Then Writeln('Oops at J = ', J) ;
  End ;
  Close(F) ;
  Writeln(DS[0,0]:2:0, #9, DS[0,1]:2:0, #9, DS[1,0]:2:0, #9, DS[1,1]:2:0,
#9'Should be 1, 2, 3, 4') ; { This works }
  SetLength(DS, 0) ;

  SetLength(DD, H, W) ; { Test 2D dynamic array }
  Assign(F, FName) ;  Reset(F, 1) ;
  For J := 0 To H-1 Do Begin
BlockRead(F, DD[J], W*Sizeof(Single), C) ;
If (C <> W*Sizeof(Single)) Then Writeln('Oops at J = ', J) ;
  End ;
  Close(F) ;
  Writeln(DD[0,0]:2:0, #9, DD[0,1]:2:0, #9, DD[1,0]:2:0, #9, DD[1,1]:2:0,
#9'Should also be 1, 2, 3, 4') ; { Quits, error 216, reading DD[0,0] }
  SetLength(DD, 0, 0) ;
End.

 
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal