////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2001-2023 Alexey Kuryakin daqgroup@mail.ru under MIT license //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// This file is part of the CRW-DAQ project by DaqGroup - component CRWLIB.   //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Purpose:                                                                   //
// DIM Queue library. See http://dim.web.cern.ch on DIM.                      //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// History:                                                                   //
// 20201123 - Created by A.K.                                                 //
// 20230501 - Modified for FPC (A.K.)                                         //
////////////////////////////////////////////////////////////////////////////////

unit _crw_dimq; // DIM Queue

{$I _crw_sysdef}

{$IFDEF FPC}{$mode Delphi}{$ENDIF}{$H+}

interface

uses
 _crw_sharemem, // NB: THIS UNIT MUST BE FIRST !!!
 {$IFDEF UNIX} cthreads, dl, {$ENDIF}
 {$IFDEF WINDOWS} windows, {$ENDIF}
 sysutils, classes, contnrs, math,
 _crw_alloc, _crw_dim;

 /////////////////
 // TDimDataHolder
 /////////////////
type
 TDimDataHolder=class(TMasterObject)
 private
  myLinkedTag  : PtrInt;
  myDataString : LongString;
  function  GetLinkedTag:PtrInt;
  function  GetDataLength:SizeInt;
  function  GetDataBuffer:Pointer;
  function  GetDataString:LongString;
 public
  constructor Create;
  destructor  Destroy; override;
 public
  property  LinkedTag  : PtrInt     read GetLinkedTag;
  property  DataLength : SizeInt    read GetDataLength;
  property  DataBuffer : Pointer    read GetDataBuffer;
  property  DataString : LongString read GetDataString;
 public
  procedure ClearData;
  function  PutData(aTag:PtrInt; aData:Pointer; aLeng:SizeInt):SizeInt;
 end;

 ////////////////
 // TDimDataQueue
 ////////////////
type
 TDimDataQueue = class(TLatch)
 private
  myList  : TFPObjectList;
  myQueue : TObjectQueue;
  myStack : TObjectStack;
 public
  function  Push(Tag:TDimLong; Buffer:Pointer; Size:Integer):SizeInt;
  function  Pop(out Holder:TDimDataHolder):SizeInt;
  function  Count:Integer;
 public
  constructor Create;
  destructor  Destroy; override;
 end;

procedure Kill(var TheObject:TDimDataQueue); overload;

 /////////////////////////////////////////////////////
 // DimDataQueue is the main instance of TDimDataQueue
 /////////////////////////////////////////////////////
function DimDataQueue:TDimDataQueue;

procedure TestDimQueue;

implementation

 /////////////////
 // TDimDataHolder
 /////////////////

constructor TDimDataHolder.Create;
begin
 inherited Create;
 ClearData;
end;

destructor TDimDataHolder.Destroy;
begin
 ClearData;
 inherited Destroy;
end;

function  TDimDataHolder.GetLinkedTag:PtrInt;
begin
 if Assigned(Self) then Result:=myLinkedTag else Result:=0;
end;

function  TDimDataHolder.GetDataLength:SizeInt;
begin
 if Assigned(Self) then Result:=Length(myDataString) else Result:=0;
end;

function  TDimDataHolder.GetDataBuffer:Pointer;
begin
 if Assigned(Self) then Result:=Pointer(myDataString) else Result:=nil;
end;

function  TDimDataHolder.GetDataString:LongString;
begin
 if Assigned(Self) then Result:=myDataString;
end;

function  TDimDataHolder.PutData(aTag:PtrInt; aData:Pointer; aLeng:SizeInt):SizeInt;
begin
 Result:=0;
 if Assigned(Self) then
 try
  myLinkedTag:=aTag;
  if (aLeng>0) and Assigned(aData)
  then SetString(myDataString,PChar(aData),aLeng)
  else myDataString:='';
  Result:=Length(myDataString);
 except
  on E:Exception do BugReport(E,Self,'PutData');
 end;
end;

procedure  TDimDataHolder.ClearData;
begin
 if Assigned(Self) then begin
  myDataString:='';
  myLinkedTag:=0;
 end;
end;

 ////////////////
 // TDimDataQueue
 ////////////////

constructor TDimDataQueue.Create;
begin
 inherited Create;
 myList:=TFPObjectList.Create;
 myQueue:=TObjectQueue.Create;
 myStack:=TObjectStack.Create;
end;

destructor TDimDataQueue.Destroy;
begin
 FreeAndNil(myStack);
 FreeAndNil(myQueue);
 FreeAndNil(myList);
 inherited Destroy;
end;

function TDimDataQueue.Push(Tag:TDimLong; Buffer:Pointer; Size:Integer):SizeInt;
var Holder:TDimDataHolder; Obj:TObject;
begin
 Result:=0;
 if (Tag<>0) then
 if (Size>0) then
 if (Buffer<>nil) then
 if Assigned(Self) then
 try
  Lock;
  try
   Obj:=myStack.Pop;
   if (Obj is TDimDataHolder) then begin
    Holder:=TDimDataHolder(Obj);
    myQueue.Push(Holder);
   end else begin
    Holder:=TDimDataHolder.Create;
    myQueue.Push(Holder);
    myList.Add(Holder);
   end;
   Result:=Holder.PutData(Tag,Buffer,Size);
  finally
   Unlock;
  end;
 except
  on E:Exception do BugReport(E,Self,'Push');
 end;
end;

function TDimDataQueue.Pop(out Holder:TDimDataHolder):SizeInt;
var Obj:TObject;
begin
 Result:=0;
 Holder:=nil;
 if Assigned(Self) then
 try
  Lock;
  try
   Obj:=myQueue.Pop;
   if (Obj is TDimDataHolder) then begin
    Holder:=TDimDataHolder(Obj);
    Result:=Holder.DataLength;
    myStack.Push(Obj);
   end;
  finally
   UnLock;
  end;
 except
  on E:Exception do BugReport(E,Self,'Pop');
 end;
end;

function TDimDataQueue.Count:Integer;
begin
 Result:=0;
 if Assigned(Self) then
 try
  Lock;
  try
   Result:=myQueue.Count;
  finally
   UnLock;
  end;
 except
  on E:Exception do BugReport(E,Self,'Count');
 end;
end;


procedure TestDimQueue;
var i,n:Integer; Tag:TDimLong; S:LongString; Holder:TDimDataHolder;
begin
 try
  // Push data to queue
  for i:=1 to 10 do begin
   Tag:=i; S:=Format('Data[%d]',[i]);
   n:=DimDataQueue.Push(Tag,PChar(S),Length(s));
   writeln('Push ',Tag,' ',S,' ',n);
  end;
  // Extract data from queue
  while DimDataQueue.Pop(Holder)>0 do begin
   Tag:=Holder.LinkedTag;
   S:=Holder.DataString;
   n:=Holder.DataLength;
   writeln('Pop ',Tag,' ',S,' ',n);
  end;
 except
  on E:Exception do BugReport(E,nil,'TestDimQueue');
 end;
end;

procedure Kill(var TheObject:TDimDataQueue); overload;
begin
 try
  FreeAndNil(TheObject);
 except
  on E:Exception do BugReport(E,nil,'Kill');
 end;
end;

var
 TheDimDataQueue : TDimDataQueue = nil;

function DimDataQueue:TDimDataQueue;
begin
 if not Assigned(TheDimDataQueue) then begin
  TheDimDataQueue:=TDimDataQueue.Create;
 end;
 Result:=TheDimDataQueue;
end;

///////////////////////////////////////
// Unit initialization and finalization
///////////////////////////////////////

procedure Init_crw_dimq;
begin
end;

procedure Free_crw_dimq;
begin
 Kill(TheDimDataQueue);
end;

initialization

 Init_crw_dimq;

finalization

 Free_crw_dimq;

end.

//////////////
// END OF FILE
//////////////

