Re: [fpc-pascal] Primitive Record Wrappers

2016-02-27 Thread Maciej Izak
2016-02-27 4:38 GMT+01:00 Mazola Winstrol :

> In the code bellow, the generic type TNullableTyple is implemented (and
> incomplete for now).
>
>
It can be done more optimal with incoming operators Initialize and
Finalize. I have working implementation but I need to adjust the code to
the FPC compiler code standard :P


-- 
Best regards,
Maciej Izak
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Codepage + class helper raises "Error identifier idents no member ..."

2016-02-27 Thread Sven Barth
Am 26.02.2016 20:31 schrieb "silvioprog" :
>
> Hello,
>
> Consider the following code:
>
> === code ===
>
> program project1;
>
> {$mode objfpc}{$H+}
> //{$codepage utf8}
>
> uses Classes;
>
> type
>   TFoo = class helper for TStream
>   public
> procedure Bar;
>   end;
>
>   procedure TFoo.Bar;
>   begin
>   end;
>
> var
>   s: string = '';
>   m: TStream;
> begin
>   m := TMemoryStream.Create;
>   try
> m.Bar;
>   finally
> m.Free;
>   end;
> end.
>
> === /code ===
>
> It compiles fine, but when you uncomment the line "//{$codepage utf8}",
it raises:
>
> 'Error: identifier idents no member "Bar"'
>
> If you confirm it as bug I can open a issue on bugtracker.

That definitely shouldn't be the case. Please report.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Primitive Record Wrappers

2016-02-27 Thread Sven Barth
Am 27.02.2016 09:36 schrieb "Maciej Izak" :
>
> 2016-02-27 4:38 GMT+01:00 Mazola Winstrol :
>>
>> In the code bellow, the generic type TNullableTyple is implemented (and
incomplete for now).
>>
>
> It can be done more optimal with incoming operators Initialize and
Finalize. I have working implementation but I need to adjust the code to
the FPC compiler code standard :P

Sidenote for this (don't know whether you have already ensured that): these
operators must not be useable as global operators as after all they must he
known in the RTTI and that can't work if they're declared in another unit
that might not even be included... (That's in contrast to how operators
normally behave in FPC, but there's not really a way around it :/ )

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] Forcibly terminating a thread

2016-02-27 Thread Mark Morgan Lloyd
I've got a program here which, under some conditions of testing, ends up 
with a thread stuck inside a system call (detail: a mainframe emulator, 
which is still trying to read a damaged punched card when the operator 
hits the power-off button).


Is there a way of sending an unambiguous system-level kill signal to a 
thread? KillThread(ThreadID) doesn't appear to be brutal enough, and I'm 
unsure of the correct way to access pthread_kill().


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Forcibly terminating a thread

2016-02-27 Thread Jonas Maebe

On 27/02/16 12:40, Mark Morgan Lloyd wrote:

Is there a way of sending an unambiguous system-level kill signal to a
thread? KillThread(ThreadID) doesn't appear to be brutal enough, and I'm
unsure of the correct way to access pthread_kill().


You cannot asynchronously kill threads on Unix platforms with the 
cthreads unit, and even on platforms where you can, you should never do 
it. All resources held by that thread will leak, and locks it held will 
never be unlocked.


KillThread() on Unix calls pthread_cancel(), which indicates to the 
system libraries (or any other libraries that include support for this) 
that that as soon as they know it is safe to kill the current thread, 
they should abort it and call its registered cleanup functions.


Since you are in an emulator, the way to solve this is probably to have 
your own threads regularly check the "terminated" property of the 
tthread and if it's set (which you can do by calling thread.terminate 
"on it" from another thread), to clean up everything and abort.



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


Re: [fpc-pascal] Codepage + class helper raises "Error identifier idents no member ..."

2016-02-27 Thread Bart
On 2/27/16, Sven Barth  wrote:

> That definitely shouldn't be the case. Please report.

It seems to do it for any codepage you insert.

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


Re: [fpc-pascal] Codepage + class helper raises "Error identifier idents no member ..."

2016-02-27 Thread Bart
On 2/27/16, Bart  wrote:

> It seems to do it for any codepage you insert.

And it disappears if you comment out the {H+} directive.

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


Re: [fpc-pascal] Codepage + class helper raises "Error identifier idents no member ..."

2016-02-27 Thread Bart
On 2/27/16, Bart  wrote:

>> It seems to do it for any codepage you insert.
>
> And it disappears if you comment out the {H+} directive.

It's the combination of
- codepage
- {$H+}
- var s: string = '';

Removing the initiaiation of the local string var (replace it with:
"var s: string")  makes compilation possible.

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


Re: [fpc-pascal] Codepage + class helper raises "Error identifier idents no member ..."

2016-02-27 Thread Bart
On 2/27/16, Sven Barth  wrote:

> That definitely shouldn't be the case. Please report.

Done.
http://bugs.freepascal.org/view.php?id=29745

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


Re: [fpc-pascal] Forcibly terminating a thread

2016-02-27 Thread Mark Morgan Lloyd

Jonas Maebe wrote:

On 27/02/16 12:40, Mark Morgan Lloyd wrote:

Is there a way of sending an unambiguous system-level kill signal to a
thread? KillThread(ThreadID) doesn't appear to be brutal enough, and I'm
unsure of the correct way to access pthread_kill().


You cannot asynchronously kill threads on Unix platforms with the 
cthreads unit, and even on platforms where you can, you should never do 
it. All resources held by that thread will leak, and locks it held will 
never be unlocked.


Ouch. But as I said, this is specifically at termination, I'd like to 
shut down in good order so that I can output the number of times each 
thread's spun and none of them will be reused.


KillThread() on Unix calls pthread_cancel(), which indicates to the 
system libraries (or any other libraries that include support for this) 
that that as soon as they know it is safe to kill the current thread, 
they should abort it and call its registered cleanup functions.


Since you are in an emulator, the way to solve this is probably to have 
your own threads regularly check the "terminated" property of the 
tthread and if it's set (which you can do by calling thread.terminate 
"on it" from another thread), to clean up everything and abort.


It's not so much /in/ an emulator as the code /is/ an emulator.

The threads check for various termination conditions every time they 
spin, but in this test case one of them's gone into a syscall that can't 
be completed.


Interestingly, this is /exactly/ why a dot was added after the final END 
in a number of ALGOL dialects: the emulator is for a B5500 and the 
hardware doesn't have a "no more cards in hopper" signal.


"The first thing that Dijkstra wanted to see was BEGIN END.
compiled and executed.

"We apologized for the “.” being required since that was not a part of 
Algol. The “.” did not bother him at all. He thought it was a good idea."


And that's obviously why we've got it in Pascal as well.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Primitive Record Wrappers

2016-02-27 Thread Mazola Winstrol
I've implemented a new version. For this version i created a mock class to
use with FHasValue (the previous implementation uses a hack to the
interface internal layout).


unit NullableTypes;

{$mode delphi}{$H+}

interface

type

  { TMockInterfacedObject }

  TMockInterfacedObject = class(TObject, IUnknown)
  strict private
class var FInstance: TMockInterfacedObject;
  public
class constructor Create;
class destructor Destroy;
class property Instance: TMockInterfacedObject read FInstance;
function QueryInterface({$IFDEF
FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) :
longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _AddRef: longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _Release: longint; {$IFNDEF
WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  end;

  { TNullable }

  TNullable = record
  strict private
FValue: T;
FHasValue: IInterface;
function GetValue: T;
function GetHasValue: Boolean;
procedure SetValue(const AValue: T);
procedure SetFlatInterface(var Intf: IInterface);
  public
constructor Create(const AValue: T);
procedure Clear;
function GetValueOrDefault: T; overload;
function GetValueOrDefault(Default: T): T; overload;
property HasValue: Boolean read GetHasValue;
property Value: T read GetValue;

class operator Implicit(const AValue: Pointer): TNullable;
class operator Implicit(AValue: TNullable): T;
class operator Implicit(const AValue: T): TNullable;
class operator Explicit(AValue: TNullable): T;
  end;


  TInteger = TNullable;

implementation

uses
  SysUtils;

{ TNullable }

procedure TNullable.SetFlatInterface(var Intf: IInterface);
begin
  Intf := TMockInterfacedObject.Instance;
end;

class operator TNullable.Explicit(AValue: TNullable): T;
begin
  Result := AValue.Value;
end;

function TNullable.GetHasValue: Boolean;
begin
  Result := FHasValue <> nil;
end;

function TNullable.GetValue: T;
begin
  if not HasValue then
raise Exception.Create('Invalid operation, Nullable type has no value');
  Result := FValue;
end;

function TNullable.GetValueOrDefault: T;
begin
  if HasValue then
Result := FValue
  else
   Result := Default(T);
end;

function TNullable.GetValueOrDefault(Default: T): T;
begin
  if not HasValue then
Result := Default
  else
Result := FValue;
end;

class operator TNullable.Implicit(const AValue: Pointer): TNullable;
begin
  if AValue = nil then
Result.Clear
  else
raise Exception.Create('Invalid operation, incompatible values.');
end;

class operator TNullable.Implicit(AValue: TNullable): T;
begin
  Result := AValue.Value;
end;

class operator TNullable.Implicit(const AValue: T): TNullable;
begin
  Result := TNullable.Create(AValue);
end;

procedure TNullable.SetValue(const AValue: T);
begin
  FValue := AValue;
  SetFlatInterface(FHasValue);
end;

constructor TNullable.Create(const AValue: T);
begin
  FValue := AValue;
  SetFlatInterface(FHasValue);
end;

procedure TNullable.Clear;
begin
  FHasValue := nil;
end;

{ TMockInterfacedObject }

class constructor TMockInterfacedObject.Create;
begin
  FInstance := TMockInterfacedObject.Create;
end;

class destructor TMockInterfacedObject.Destroy;
begin
  FInstance.Free;
end;

function TMockInterfacedObject.QueryInterface({$IFDEF
FPC_HAS_CONSTREF}constref
  {$ELSE}const{$ENDIF} iid : tguid;out obj): longint;
begin
  Result := E_NOINTERFACE;
end;

function TMockInterfacedObject._AddRef: longint;
begin
  Result := -1;
end;

function TMockInterfacedObject._Release: longint;
begin
  Result := -1;
end;

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

Re: [fpc-pascal] Primitive Record Wrappers

2016-02-27 Thread Mazola Winstrol
2016-02-27 0:49 GMT-03:00 Dmitry Boyarintsev :

> On Fri, Feb 26, 2016 at 10:38 PM, Mazola Winstrol 
> wrote:
>
>> In the code bellow, the generic type TNullableTyple is implemented (and
>> incomplete for now).
>>
> How to reset TNullableType to Null value? HasValue seems to be read-only.
>

In the last version, there is two ways:

var
   NullInt: TNullable;
begin
   NullInt.Clear; // Method 1
   NullInt := nil; // Method 2
end;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Primitive Record Wrappers

2016-02-27 Thread silvioprog
On Sat, Feb 27, 2016 at 2:49 PM, Mazola Winstrol 
wrote:

> I've implemented a new version. For this version i created a mock class to
> use with FHasValue (the previous implementation uses a hack to the
> interface internal layout).
>

Thanks for share that. It seems a very useful unit.

unit NullableTypes;
>
> {$mode delphi}{$H+}
>

$MODE DELPHI implies {$H ON}
, so so you should
remove the {$H+}.

-- 
Silvio Clécio
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Possible bug in Numeric test

2016-02-27 Thread steveg

Drat, that was a bad assumption of mine - sorry

I meant it fails for any of E1 or E2 entries
so any entry beginning with E and then a following number

eg: IsNum('E1');

Sorry for the poor example

On 25/02/16 03:54, Vojtěch Čihák wrote:


I tried your function in FPC 3.0.0 in mode ObjFPC and it returns False 
for string 'E1/E2/etc'.


V.

__
> Od: steveg 
> Komu: "FPC-Pascal users discussions" 
> Datum: 24.02.2016 00:57
> Předmět: [fpc-pascal] Possible bug in Numeric test
>

Not sure if this is considered a bug or not :)

I have found this function returns TRUE if passed 'E1/E2/etc'
I am guessing it is seeing the 'E' as exponent


function IsNum( const sSrc :string ) :boolean;
var
  Code :integer;
  Num :real = 0;
begin
  Num := Num;
  Val(sSrc, Num, Code);
  Exit( Code = 0 );
end;

Thanks - SteveG
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



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


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