Skip to content

Commit

Permalink
API/Batch: handle booleans better
Browse files Browse the repository at this point in the history
Before, `true` was being handled as `-1` only (all bits set), and the `Enabled` property was not being casted to the same value, so it couldn't be used in the Batch API consistently with other boolean properties.
  • Loading branch information
PMeira committed Jul 24, 2023
1 parent 0caaffc commit e1af74c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/CAPI/CAPI_Obj.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ procedure Batch_CreateByInt32Property(DSS: TDSSContext; var ResultPtr: TDSSObjec
propOffset: PtrUint;
i: Integer;
propFlags: TPropertyFlags;
ptype: TPropertyType;
begin
if DSS = NIL then DSS := DSSPrime;
cls := DSS.DSSClassList.At(clsIdx);
Expand All @@ -1079,23 +1080,29 @@ procedure Batch_CreateByInt32Property(DSS: TDSSContext; var ResultPtr: TDSSObjec
Exit;
end;

if not (cls.PropertyType[propidx] in [
ptype := cls.PropertyType[propidx];
if not (ptype in [
TPropertyType.IntegerProperty,
TPropertyType.MappedIntEnumProperty,
TPropertyType.MappedStringEnumProperty,
TPropertyType.BooleanProperty,
TPropertyType.IntegerOnStructArrayProperty
TPropertyType.IntegerOnStructArrayProperty,
TPropertyType.EnabledProperty
]) then
begin
Exit;
end;
if (ptype = TPropertyType.BooleanProperty) or (ptype = TPropertyType.EnabledProperty) then
begin
value := Integer(LongBool(value <> 0));
end;

propFlags := cls.PropertyFlags[propIdx];
propOffset := cls.PropertyOffset[propIdx];
objlist := TDSSObjectPtr(cls.ElementList.InternalPointer);
ensureBatchSize(cls.ElementList.Count, ResultPtr, ResultCount);
outptr := ResultPtr;
if (cls.PropertyType[propidx] in [
if (ptype in [
TPropertyType.IntegerProperty,
TPropertyType.MappedIntEnumProperty,
TPropertyType.MappedStringEnumProperty,
Expand Down Expand Up @@ -1465,6 +1472,7 @@ procedure Batch_Int32(batch: TDSSObjectPtr; batchSize: Integer; Index: Integer;
// prev: Integer;
// intptr: PInteger;
// propFlags: TPropertyFlags;
ptype: TPropertyType;
begin
if (batch = NIL) or (batch^ = NIL) then
Exit;
Expand All @@ -1473,7 +1481,8 @@ procedure Batch_Int32(batch: TDSSObjectPtr; batchSize: Integer; Index: Integer;
// propFlags := cls.PropertyFlags[Index];
// propOffset := cls.PropertyOffset[Index];

if not (cls.PropertyType[Index] in [
ptype := cls.PropertyType[Index];
if not (ptype in [
TPropertyType.IntegerProperty,
TPropertyType.MappedIntEnumProperty,
TPropertyType.MappedStringEnumProperty,
Expand All @@ -1483,6 +1492,11 @@ procedure Batch_Int32(batch: TDSSObjectPtr; batchSize: Integer; Index: Integer;
]) then
Exit;

if (ptype = TPropertyType.BooleanProperty) and not (Operation in [Batch_Increment]) then
begin
Value := Integer(LongBool(value <> 0));
end;

// if (cls.PropertyType[Index] in [
// TPropertyType.IntegerProperty,
// TPropertyType.MappedIntEnumProperty,
Expand Down
7 changes: 6 additions & 1 deletion src/General/DSSObjectHelper.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,11 @@ procedure TDSSClassHelper.SetObjInteger(ptr: Pointer; Index: Integer; Value: Int
if (TPropertyFlag.ConditionalReadOnly in flags) and (PLongBool(PByte(obj) + PropertyOffset3[Index])^) then
Exit;

if ptype = TPropertyType.BooleanProperty then
begin
Value := Integer(LongBool(value <> 0));
end;

if flags = [] then
begin
// Most properties don't have any flags set, just skip the checks
Expand Down Expand Up @@ -3306,7 +3311,7 @@ function TDSSClassHelper.GetObjInteger(Obj: Pointer; Index: Integer): Integer;
end;

TPropertyType.EnabledProperty:
Result := Integer(TDSSCktElement(obj).Enabled);
Result := Integer(LongBool(TDSSCktElement(obj).Enabled));

TPropertyType.BooleanProperty:
Result := Integer(PLongBool(PByte(obj) + PropertyOffset[Index])^);
Expand Down

0 comments on commit e1af74c

Please sign in to comment.