Equivalent of InterlockedExchangeAdd for Linux using Delphi 10.2) - linux

Delphi 10.2 (having support for Linux) has a cross Platform function AtomicExchange which is equivalent to Windows InterlocekdEchange. So far so good...
I have to port Win32 code making use of InterlockedExchangeAdd which has no AtomicExchangeAdd equivalent.
My question is: what can I use to replace InterlockedExchangeAdd when compiling for Linux ?

There is a hidden implementation of this function in System.SysUtils.pas:
function AtomicExchangeAdd(var Addend: Integer; Value: Integer): Integer;
begin
Result := AtomicIncrement(Addend, Value) - Value;
end;
It makes use of the fact that AtomicIncrement returns the new value of Addend, while InterlockedExchangeAdd returns the old value. Subtracting Value gives the expected result and obviously is thread-safe.

InterlockedExchangeAdd() "performs an atomic addition of Value to the value pointed to by Addend. The result is stored in the address specified by Addend."
The System.SyncObjs unit has a TInterlocked class, which has overloaded Add() methods to do the same thing:
Increments an integer value with another.
There are two overloaded Add methods. Both Add methods increment a Target by Increment.
class function Add(var Target: Integer; Increment: Integer): Integer; overload; static; inline;
class function Add(var Target: Int64; Increment: Int64): Int64; overload; static; inline;
The difference is that InterlockedExchangeAdd() "returns the initial value of the variable pointed to by Addend", whereas TInterlocked.Add() "returns the value of the incremented parameter" instead. So, if you use the return value, you will have to account for that difference, eg:
function InterlockedExchangeAdd(var Addend: Integer; Value: Integer): Integer;
begin
Result := TInterlocked.Add(Addend, Value) - Value;
end;

Related

Optional Parameters in Delphi [duplicate]

Is there any way to skip the default params, say suppose my method declaration is like this:
procedure MyProc1(var isAttr1: Boolean = FALSE;
var isAttr2: Boolean = FALSE; var isAttr3: Boolean = FALSE);
I can't call the function like this:
Self.MyProc1( , , Attr3);
because I don't want unnecessary var declarations, at the same time I want the last param return value (it is a var type)
Thank for help in advance.
Sorry, you can't do this. What's more you can't have a var parameter with a default as you have with isAttr1.
If your parameters had sufficiently different parameter types, then you could use overloaded methods as an alternative to default parameters, which is a technique that I personally prefer. But your parameters are all Boolean and so you can't do that here.
Other posters have suggested something like this:
procedure Myfunc1(var isAttr1, isAttr2, isAttr3: Boolean); overload;
procedure Myfunc1(var isAttr3: Boolean); overload;
This will compile and work but would be counter to the principle of least surprise. For overloaded procedures like this, you would expect, for the procedure with only a single parameter, for that single parameter to be the first parameter of the procedure with multiple parameters. In other words you would expect:
procedure Myfunc1(var isAttr1, isAttr2, isAttr3: Boolean); overload;
procedure Myfunc1(var isAttr1: Boolean); overload;
In this case you should use the overload directive:
The Overload directive allows you to
have different versions of the same
named function or procedure with
different arguments.
Yes, make an overloaded version of MyFunc1(isAttr3 : boolean = FALSE); overload;
Have it make up the dummy params and pass them for you.
As the other poster points out, you can do this with VAR.
Also, it's wrong to call it MyFunc if it's not a Function. Call it MyProc!
My solution:
function ov(p1 : boolean; p2 : boolean; p3 : boolean) : boolean; overload;
begin
result := p1 or p2 or p3;
end;
function ov(p3 : boolean) : boolean; overload;
begin
result := ov(false, false, p3);
end;
Now you can have your choice of:
ov(TestBool3)
or
ov(TestBool1, TestBool2, TestBool3)

How do I copy a source file from a directory specified by the user? [duplicate]

So given this function, I get the error "Identifier Expected" on the GetRoot := ROOTPage.Values[0]; line. I expect it is telling me that the ROOTPage is not defined?
const
DefaultRoot = 'C:\IAmGRoot';
Var
ROOTPage : TInputQueryWizardPage;
procedure SetupRoot;
begin
ROOTPage := CreateInputQueryPage(wpUserInfo,
ExpandConstant('{cm:RootTitle}'),
ExpandConstant('{cm:RootInstructions}'),
ExpandConstant('{cm:RootDescription}') + ' "' + DefaultRoot + '"'
);
ROOTPage.Add(ExpandConstant('{cm:SSRoot}') + ':', False);
ROOTPage.Values[0] := ExpandConstant('{DefaultRoot}');
// add SSROOT to path
end;
function GetRoot : string;
begin
GetRoot := ROOTPage.Values[0];
end;
How should I interpret this error. What is an identifier in Pascal?
This page tells me that identifiers are variable names. Perhaps I need to expand the ROOTPage.Values[0] in some way since I am referencing an array from an Inno Setup object?
Or maybe I need to return the value differently. I saw one page on Pascal that said that you needed to avoid assigning to the function value on parameter less functions to avoid a recursive loop. Does this mean I should pass in a dummy value? or is there a different syntax? That page did not explain.
I secretly think my real issue is that I am not defining my function correctly... but well. That much compiles at least. This question could become: How do you handle a parameter-less function in Pascal?
I do not think Inno Setup is part of the issue, but I am working with Inno Setup in case that is important.
Update:
It doesn't seem to be the array because this gets the same error:
const
DefaultRoot = 'C:\IAmGRoot';
function GetRoot : string;
begin
GetRoot := DefaultRoot;
end;
Update:
This link has said that the function name can be replaced/ should be replaced with the key word Result such as the following code. I actually knew this, but the Inno Setup compiler does not recognize this as valid syntax. It then tells me my function is an invalid prototype.
function GetRoot : string;
begin
Result := DefaultRoot;
end;
Update:
If I do this I get "Invalid Prototype for GetRoot"
function GetRoot : boolean;
begin
Result := False;
end;
Update for #Martin Prikryl:
Well I use it a few places but the typical use would be like this:
[Files]
Source: "C:\ValidPath\Release\*"; DestDir: "{app}\bin"; Components: DefinedComponent
Source: "C:\ValidPath\Deployment\*"; DestDir: "{code:GetRoot}\"; Flags: ignoreversion recursesubdirs; Components: DefinedComponent
Identifier expected
Your code would be correct in a Pascal, but it does not compile in Pascal Script.
In Pascal, when you want to assign a return value of a function, you either assign the value to a "variable" with a name of the function or to Result variable.
So this is correct:
function GetRoot: string;
begin
GetRoot := ROOTPage.Values[0];
end;
And this too (both are equivalent):
function GetRoot: string;
begin
Result := ROOTPage.Values[0];
end;
In the Pascal Script, only the Result works. When you use the name of the function, you get the "Identifier expected."
Invalid prototype
You get this when the function is called from outside of the Code section and a specific parameter list/return value is required. But you didn't tell us, what you use the GetRoot function for.
There are two places, where you can use a custom function in Inno Setup:
Check parameter: For this the function must return a Boolean and take either no parameter or one parameter (the parameter type is determined by a value you provide in the Check parameter).
function MyProgCheck(): Boolean;
function MyDirCheck(DirName: String): Boolean;
Scripted Constants: The function must return a string and take one string parameter, even if no parameter is provided in the scripted constant. I assume this is your use case. If you do not need any parameter, just declare it, but do not use it:
function GetRoot(Param: String): string;
begin
Result := ROOTPage.Values[0];
end;

Compile Error when trying to return PChar or OleVariant for UDF

The title doesn't quite capture the essence of the issue.
I have a UDF function that returns a PChar.
function AccountDescription(sAccountId: PChar) : PChar; stdcall;
This was working fine but I realized I wanted to return #N/A if the accountId was not found.
I discovered CVErr(xlErrNA) and changed the Signature to return OleVariant.
But now I am receiving [Error] Incompatible types: 'OleVariant' and 'PAnsiChar'.
I could not find any information on how to resolve this so I figure my understanding of the problem must not be correct.
I tried just passing a string which compiled but produced a runtime error of "Invalid variant type".
The full code is:
function AccountDescription(sAccountId: PChar): OleVariant; stdcall;
var
strResult: string;
strPChar : PChar;
begin
try
strResult:= repo.GetAccount(sAccountId).Description;
strPChar := strAlloc(length(strResult)+1) ;
StrPCopy(strPChar, strResult) ;
Result := strPChar;
except
Result := CVErr(xlErrNA);
end;
end;
Note: Is excel responsible for destroying the string or is that my cleanup? Should I be creating a copy or should I just be returning a pointer to an existing string. After typing it I feel like I should be returning a pointer.
Update:
Removed some irrelevant code in the example.
Now using:
function AccountDescription(sAccountId: PChar): OleVariant; stdcall;
var
strResult: string;
begin
try
Result := PChar(repo.GetAccount(sAccountId).Description);
except
Result := CVErr(xlErrNA);
end;
end;
You do not need the PChar cast, you can assign a String directly to an OleVariant (it will be converted by the RTL into a BSTR that the receiver will then free when done using it):
Result := repo.GetAccount(sAccountId).Description;
As for reporting an error, do you have a viable CVErr() function in your Delphi code? In VB, CVErr() returns a Variant of type Error (varError in Delphi) containing an error code (xlErrNA is 2042). Delphi has a VarAsError() function for that same purpose:
Result := VarAsError(2042);

Custom property only trigger read statement

So, I'm trying to make a component that will do the job on setting the settings of a excel, libreoffice, etc... cells. At first I just wanted to set the value, but now, I need to change cell background color, change font name, style, set a formula, etc... So for that, I decided to do a type that will hold all the things I want to change and so, I did this:
type
TMyCell = class
private
FBgColor: TColor;
FValue: String;
FFormula: String;
FFormat: String;
FFont: TFont;
public
constructor Create;
destructor Destroy;
property Value: String read FValue write FValue;
property Formula: String read FFormula write FFormula;
property Format: String read FFormat write FFormat;
property BgColor: TColor read FBgColor write FBgColor;
property Font: TFont read FFont write FFont;
end;
{ TMyCell }
constructor TMyCell.Create;
begin
FFont := TFont.Create;
end;
destructor TMyCell.Destroy;
begin
FFont.Free;
end;
And my component look like this:
type
TMyPlan = class(TComponent)
private
FExcel: Variant;
procedure SetMyCell(Row, Column: Integer; Value: TMyCell);
function GetMyCell(Row, Column: Integer): TMyCell;
public
constructor Create(AOwner: TComponent);
destructor Destroy;
property Cell[Row, Column: Integer]: TMyCell read GetMyCell write SetMyCell;
end;
{ TMyPlan }
constructor TMyPlan.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExcel := CreateOleObject('Excel.Application');
FExcel.Workbooks.Add(1);
end;
destructor TMyPlan.Destroy;
begin
FExcel := Unassigned;
inherited;
end;
function TMyPlan.GetMyCell(Row, Column: Integer): TMyCell;
begin
Result := TMyCell.Create;
Result.Value := FExcel.Cells[Row, Column];;
end;
procedure TMyPlan.SetMyCell(Row, Column: Integer; Value: TMyCell);
begin
FExcel.Cells[Row, Column] := Value.Value;
end;
Just to let you know, I already did some components, and I'm still learning how to do them properly, so this may have a not decent structure, anyway, this is the first time that I'm trying to do something like this, a property that has input parameters with subproperties, and it doesn't seem to work as I though it would.
Back to the topic, it doesn't matter how I call my property
Set: MyPlan.Cell[1, 1].Value := '1';
Get: ShowMessage(MyPlan.Cell[1, 1].Value);
Either way only the GetMyCell function is triggered. Why's that?
See my answer to this question: "Left side cannot be assigned to" for record type properties in Delphi
While what you're doing isn't quite the same thing, it is similar. However, in your case, you're allocating a new instance of TMyCell for every access to GetMyCell. This "temporary" instance is isn't being freed and will leak (Unless you're doing this on one of the ARC platforms).
The reason your SetMyCell isn't being called is because you're not actually setting the cell itself, rather you're setting a value on the cell instance (that I explained above is leaking).

TStringList CustomSort method in FreePascal

What am I doign wrong with the following code
function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;
and I call it as :
var
SL :TstringList;
SL.CustomSort(CompareFloat);
//SL.CustomSort(#CompareFloat); // Tried this one also
The first function call 'SL.CustomSort(CompareFloat)' retrieves that error from compiler "Error: Wrong number of parameters specified for call to "CompareFloat"
Second function call 'SL.CustomSort(#CompareFloat)' retrieves that error from compiler Error: Only class methods can be referred with class references
SL.CustomSort(CompareFloat); works if you add {$mode delphi} directive to somewhere to the beginning of a unit.
However SL.CustomSort(#CompareFloat); should work fine. Make sure the error message is not caused by something else.
Example:
program Project1;
//{$mode delphi}
uses
Classes,
SysUtils;
function CompareFloat(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := StrToInt(List[Index1]) - StrToInt(List[Index2]);
end;
var
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.Add('3');
SL.Add('2');
SL.Add('1');
SL.CustomSort(#CompareFloat);
//SL.CustomSort(CompareFloat);
Writeln(SL[0], SL[1], SL[2]);
Readln;
finally
SL.Free;
end;
end.

Resources