Added ValueOrElse method to nullables. Works like ValueOrDefault but instead of default value for type, allows to return an "else" value in case the instance is null

This commit is contained in:
Daniele Teti 2024-11-05 12:29:01 +01:00
parent 1bb4d7f7ca
commit 8b256f787f
2 changed files with 306 additions and 17 deletions

View File

@ -74,9 +74,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: String; function ValueOrDefault: String;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: String): String;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -130,9 +134,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Currency; function ValueOrDefault: Currency;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Currency): Currency;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -186,9 +194,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Boolean; function ValueOrDefault: Boolean;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Boolean): Boolean;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -242,9 +254,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: TDate; function ValueOrDefault: TDate;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: TDate): TDate;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -298,9 +314,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: TTime; function ValueOrDefault: TTime;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: TTime): TTime;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -354,9 +374,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: TDateTime; function ValueOrDefault: TDateTime;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: TDateTime): TDateTime;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -410,9 +434,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Single; function ValueOrDefault: Single;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Single): Single;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -466,9 +494,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Double; function ValueOrDefault: Double;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Double): Double;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -522,9 +554,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Extended; function ValueOrDefault: Extended;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Extended): Extended;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -578,9 +614,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Int16; function ValueOrDefault: Int16;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Int16): Int16;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -634,9 +674,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: UInt16; function ValueOrDefault: UInt16;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: UInt16): UInt16;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -690,9 +734,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Int32; function ValueOrDefault: Int32;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Int32): Int32;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -746,9 +794,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: UInt32; function ValueOrDefault: UInt32;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: UInt32): UInt32;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -802,9 +854,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: Int64; function ValueOrDefault: Int64;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: Int64): Int64;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -858,9 +914,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: UInt64; function ValueOrDefault: UInt64;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: UInt64): UInt64;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -914,9 +974,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: TGUID; function ValueOrDefault: TGUID;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: TGUID): TGUID;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -1080,6 +1144,19 @@ begin
end; end;
end; end;
function NullableString.ValueOrElse(const ElseValue: String): String;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableCurrency } { NullableCurrency }
@ -1189,6 +1266,19 @@ begin
end; end;
end; end;
function NullableCurrency.ValueOrElse(const ElseValue: Currency): Currency;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableBoolean } { NullableBoolean }
@ -1298,6 +1388,19 @@ begin
end; end;
end; end;
function NullableBoolean.ValueOrElse(const ElseValue: Boolean): Boolean;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableTDate } { NullableTDate }
@ -1407,6 +1510,19 @@ begin
end; end;
end; end;
function NullableTDate.ValueOrElse(const ElseValue: TDate): TDate;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableTTime } { NullableTTime }
@ -1516,6 +1632,19 @@ begin
end; end;
end; end;
function NullableTTime.ValueOrElse(const ElseValue: TTime): TTime;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableTDateTime } { NullableTDateTime }
@ -1626,6 +1755,19 @@ begin
end; end;
end; end;
function NullableTDateTime.ValueOrElse(const ElseValue: TDateTime): TDateTime;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableSingle } { NullableSingle }
@ -1735,6 +1877,19 @@ begin
end; end;
end; end;
function NullableSingle.ValueOrElse(const ElseValue: Single): Single;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableDouble } { NullableDouble }
@ -1844,6 +1999,19 @@ begin
end; end;
end; end;
function NullableDouble.ValueOrElse(const ElseValue: Double): Double;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableExtended } { NullableExtended }
@ -1953,6 +2121,19 @@ begin
end; end;
end; end;
function NullableExtended.ValueOrElse(const ElseValue: Extended): Extended;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableInt16 } { NullableInt16 }
@ -2061,6 +2242,19 @@ begin
end; end;
end; end;
function NullableInt16.ValueOrElse(const ElseValue: Int16): Int16;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableUInt16 } { NullableUInt16 }
@ -2169,6 +2363,19 @@ begin
end; end;
end; end;
function NullableUInt16.ValueOrElse(const ElseValue: UInt16): UInt16;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableInt32 } { NullableInt32 }
@ -2277,6 +2484,19 @@ begin
end; end;
end; end;
function NullableInt32.ValueOrElse(const ElseValue: Int32): Int32;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableUInt32 } { NullableUInt32 }
@ -2385,6 +2605,19 @@ begin
end; end;
end; end;
function NullableUInt32.ValueOrElse(const ElseValue: UInt32): UInt32;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableInt64 } { NullableInt64 }
@ -2493,6 +2726,19 @@ begin
end; end;
end; end;
function NullableInt64.ValueOrElse(const ElseValue: Int64): Int64;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableUInt64 } { NullableUInt64 }
@ -2601,6 +2847,19 @@ begin
end; end;
end; end;
function NullableUInt64.ValueOrElse(const ElseValue: UInt64): UInt64;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
{ NullableTGUID } { NullableTGUID }
@ -2709,6 +2968,19 @@ begin
end; end;
end; end;
function NullableTGUID.ValueOrElse(const ElseValue: TGUID): TGUID;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
function GetNullableType(const aTypeInfo: PTypeInfo): TNullableType; function GetNullableType(const aTypeInfo: PTypeInfo): TNullableType;
begin begin

View File

@ -71,9 +71,13 @@ type
///</summary> ///</summary>
procedure SetNull; procedure SetNull;
///<summary> ///<summary>
///Returns the value stored or the default value for the type is the value is not set ///Returns the value stored or the default value for the type if the value is not set
///</summary> ///</summary>
function ValueOrDefault: $TYPE$; function ValueOrDefault: $TYPE$;
///<summary>
///Returns the value stored or else the value passed as parameter if the value is not set
///</summary>
function ValueOrElse(const ElseValue: $TYPE$): $TYPE$;
/// <summary> /// <summary>
/// Returns true is both item have the same value and that value is not null. /// Returns true is both item have the same value and that value is not null.
/// </summary> /// </summary>
@ -217,6 +221,19 @@ begin
end; end;
end; end;
function Nullable$TYPE$.ValueOrElse(const ElseValue: $TYPE$): $TYPE$;
begin
if HasValue then
begin
Result := GetValue
end
else
begin
Result := ElseValue;
end;
end;
///IMPLEMENTATION.END ///IMPLEMENTATION.END
end. end.