2020-03-11 01:35:31 +01:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// THIS FILE IS GENERATED BY "inv generate-nullables" DO NOT CHANGE MANUALLY!
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2020-03-11 01:35:31 +01:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
unit MVCFramework.Nullables;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2023-01-29 17:29:24 +01:00
|
|
|
System.SysUtils, System.Classes, System.TypInfo, System.RTTI;
|
2020-03-11 01:35:31 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
EMVCNullable = class(Exception)
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
///INTERFACE.BEGIN
|
|
|
|
Nullable$TYPE$ = record
|
|
|
|
private
|
|
|
|
fValue: $TYPE$;
|
|
|
|
fHasValue: String;
|
|
|
|
function GetHasValue: Boolean;
|
2022-08-01 19:11:42 +02:00
|
|
|
function GetIsNull: Boolean;
|
2020-03-11 01:35:31 +01:00
|
|
|
public
|
|
|
|
procedure CheckHasValue;
|
|
|
|
function GetValue: $TYPE$;
|
|
|
|
procedure SetValue(const Value: $TYPE$);
|
|
|
|
class operator Implicit(const Value: $TYPE$): Nullable$TYPE$;
|
|
|
|
class operator Implicit(const Value: Nullable$TYPE$): $TYPE$;
|
2022-08-01 19:11:42 +02:00
|
|
|
class operator Implicit(const Value: Pointer): Nullable$TYPE$;
|
2023-01-29 17:29:24 +01:00
|
|
|
class operator Equal(LeftValue: Nullable$TYPE$; RightValue: Nullable$TYPE$) : Boolean;
|
2022-08-01 19:11:42 +02:00
|
|
|
///<summary>
|
|
|
|
///Returns `True` if the Nullable$TYPE$ contains a value
|
|
|
|
///</summary>
|
2020-03-11 01:35:31 +01:00
|
|
|
property HasValue: Boolean read GetHasValue;
|
2022-08-01 19:11:42 +02:00
|
|
|
///<summary>
|
|
|
|
///Returns `True` if the Nullable$TYPE$ contains a null
|
|
|
|
///</summary>
|
|
|
|
property IsNull: Boolean read GetIsNull;
|
|
|
|
///<summary>
|
|
|
|
///Alias of `SetNull`
|
|
|
|
///</summary>
|
2020-03-11 01:35:31 +01:00
|
|
|
procedure Clear;
|
2022-08-01 19:11:42 +02:00
|
|
|
///<summary>
|
|
|
|
///Set the value to `null`
|
|
|
|
///</summary>
|
2020-03-11 01:35:31 +01:00
|
|
|
procedure SetNull;
|
2022-08-01 19:11:42 +02:00
|
|
|
///<summary>
|
2024-11-05 12:29:01 +01:00
|
|
|
///Returns the value stored or the default value for the type if the value is not set
|
2022-08-01 19:11:42 +02:00
|
|
|
///</summary>
|
2020-03-11 01:35:31 +01:00
|
|
|
function ValueOrDefault: $TYPE$;
|
2024-11-05 12:29:01 +01:00
|
|
|
///<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$;
|
2022-08-01 19:11:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns true is both item have the same value and that value is not null.
|
|
|
|
/// </summary>
|
|
|
|
function Equals(const Value: Nullable$TYPE$): Boolean;
|
|
|
|
///<summary>
|
2023-01-29 17:29:24 +01:00
|
|
|
///Returns true if the nullable contains a value and returns the contained value in the out Value parameter.
|
|
|
|
///</summary>
|
|
|
|
function TryHasValue(out Value: $TYPE$): Boolean; overload;
|
|
|
|
///<summary>
|
|
|
|
///Returns true if the nullable contains a value and returns the contained value in the out Value parameter.
|
|
|
|
///</summary>
|
|
|
|
function TryHasValue(out Value: TValue): Boolean; overload;
|
|
|
|
///<summary>
|
2022-08-01 19:11:42 +02:00
|
|
|
///Returns the value stored or raises exception if no value is stored
|
|
|
|
///</summary>
|
2020-03-11 01:35:31 +01:00
|
|
|
property Value: $TYPE$ read GetValue write SetValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
///INTERFACE.END
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2023-01-28 23:31:00 +01:00
|
|
|
uses
|
2024-08-15 21:58:20 +02:00
|
|
|
System.Math, System.DateUtils, System.Types;
|
|
|
|
|
|
|
|
function DateAreEquals(const DateA, DateB: TDate): Boolean;
|
|
|
|
begin
|
|
|
|
Result := CompareDate(DateA, DateB) = 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TimeAreEquals(const TimeA, TimeB: TTime): Boolean;
|
|
|
|
begin
|
|
|
|
Result := CompareValue(TimeA,TimeB, 0.000001) = 0;
|
|
|
|
end;
|
2023-01-28 23:31:00 +01:00
|
|
|
|
2020-03-11 01:35:31 +01:00
|
|
|
///IMPLEMENTATION.BEGIN
|
|
|
|
|
|
|
|
{ Nullable$TYPE$ }
|
|
|
|
|
|
|
|
procedure Nullable$TYPE$.CheckHasValue;
|
|
|
|
begin
|
|
|
|
if not GetHasValue then
|
|
|
|
begin
|
2023-01-28 23:31:00 +01:00
|
|
|
raise EMVCNullable.Create('Nullable$TYPE$ value is null');
|
2020-03-11 01:35:31 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-01-29 17:29:24 +01:00
|
|
|
function Nullable$TYPE$.TryHasValue(out Value: $TYPE$): Boolean;
|
|
|
|
begin
|
|
|
|
Result := HasValue;
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
Value := fValue;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function Nullable$TYPE$.TryHasValue(out Value: TValue): Boolean;
|
|
|
|
begin
|
|
|
|
Result := HasValue;
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
Value := TValue.From<$TYPE$>(fValue);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-03-11 01:35:31 +01:00
|
|
|
procedure Nullable$TYPE$.Clear;
|
|
|
|
begin
|
|
|
|
SetNull;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function Nullable$TYPE$.Equals(const Value: Nullable$TYPE$): Boolean;
|
|
|
|
begin
|
2023-01-28 23:31:00 +01:00
|
|
|
Result := Self = Value;
|
2020-03-11 01:35:31 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function Nullable$TYPE$.GetHasValue: Boolean;
|
|
|
|
begin
|
|
|
|
Result := fHasValue = '_';
|
|
|
|
end;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
function Nullable$TYPE$.GetIsNull: Boolean;
|
|
|
|
begin
|
|
|
|
Result := not HasValue;
|
|
|
|
end;
|
|
|
|
|
2020-03-11 01:35:31 +01:00
|
|
|
function Nullable$TYPE$.GetValue: $TYPE$;
|
|
|
|
begin
|
|
|
|
CheckHasValue;
|
|
|
|
Result := fValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class operator Nullable$TYPE$.Implicit(const Value: Nullable$TYPE$): $TYPE$;
|
|
|
|
begin
|
|
|
|
Result := Value.Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class operator Nullable$TYPE$.Implicit(const Value: $TYPE$): Nullable$TYPE$;
|
|
|
|
begin
|
|
|
|
Result.Value := Value;
|
|
|
|
end;
|
|
|
|
|
2022-08-01 19:11:42 +02:00
|
|
|
class operator Nullable$TYPE$.Implicit(const Value: Pointer): Nullable$TYPE$;
|
|
|
|
begin
|
|
|
|
if Value = nil then
|
|
|
|
begin
|
|
|
|
Result.SetNull;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
raise EInvalidPointer.Create('Pointer value can only be "nil"');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-01-28 23:31:00 +01:00
|
|
|
class operator Nullable$TYPE$.Equal(LeftValue: Nullable$TYPE$; RightValue: Nullable$TYPE$) : Boolean;
|
|
|
|
begin
|
|
|
|
Result := $COMPARE$;
|
|
|
|
end;
|
|
|
|
|
2020-03-11 01:35:31 +01:00
|
|
|
procedure Nullable$TYPE$.SetNull;
|
|
|
|
begin
|
|
|
|
fValue := Default ($TYPE$);
|
|
|
|
fHasValue := '';
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure Nullable$TYPE$.SetValue(const Value: $TYPE$);
|
|
|
|
begin
|
|
|
|
fValue := Value;
|
|
|
|
fHasValue := '_';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function Nullable$TYPE$.ValueOrDefault: $TYPE$;
|
|
|
|
begin
|
|
|
|
if HasValue then
|
|
|
|
begin
|
|
|
|
Result := GetValue
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := Default ($TYPE$);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2024-11-05 12:29:01 +01:00
|
|
|
function Nullable$TYPE$.ValueOrElse(const ElseValue: $TYPE$): $TYPE$;
|
|
|
|
begin
|
|
|
|
if HasValue then
|
|
|
|
begin
|
|
|
|
Result := GetValue
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := ElseValue;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2020-03-11 01:35:31 +01:00
|
|
|
///IMPLEMENTATION.END
|
|
|
|
|
|
|
|
end.
|