From e1af74c5fcb4fc230230195411690c31732cf9ec Mon Sep 17 00:00:00 2001 From: Paulo Meira <10246101+PMeira@users.noreply.github.com> Date: Mon, 24 Jul 2023 10:04:01 -0300 Subject: [PATCH] API/Batch: handle booleans better 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. --- src/CAPI/CAPI_Obj.pas | 22 ++++++++++++++++++---- src/General/DSSObjectHelper.pas | 7 ++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/CAPI/CAPI_Obj.pas b/src/CAPI/CAPI_Obj.pas index bb3e156ff..dc8be1dca 100644 --- a/src/CAPI/CAPI_Obj.pas +++ b/src/CAPI/CAPI_Obj.pas @@ -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); @@ -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, @@ -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; @@ -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, @@ -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, diff --git a/src/General/DSSObjectHelper.pas b/src/General/DSSObjectHelper.pas index ec09c2ef4..eacfced99 100644 --- a/src/General/DSSObjectHelper.pas +++ b/src/General/DSSObjectHelper.pas @@ -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 @@ -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])^);