delphimvcframework/sources/MVCFramework.Nullables.pas.template
2020-03-11 01:35:31 +01:00

151 lines
3.5 KiB
Plaintext

// ***************************************************************************
//
// THIS FILE IS GENERATED BY "inv generate-nullables" DO NOT CHANGE MANUALLY!
//
// ***************************************************************************
//
// *************************************************************************** }
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
//
// 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
System.SysUtils, System.Classes;
type
EMVCNullable = class(Exception)
end;
///INTERFACE.BEGIN
Nullable$TYPE$ = record
private
fValue: $TYPE$;
fHasValue: String;
function GetHasValue: Boolean;
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$;
property HasValue: Boolean read GetHasValue;
///<summary>
///Alias of `SetNull`
///</summary>
procedure Clear;
///<summary>
///Set the value to `null`
///</summary>
procedure SetNull;
///<summary>
///Returns the value stored or the default value for the type is the value is not set
///</summary>
function ValueOrDefault: $TYPE$;
/// <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>
///Returns the value stored or raises exception if no value is stored
///</summary>
property Value: $TYPE$ read GetValue write SetValue;
end;
///INTERFACE.END
implementation
///IMPLEMENTATION.BEGIN
{ Nullable$TYPE$ }
procedure Nullable$TYPE$.CheckHasValue;
begin
if not GetHasValue then
begin
raise EMVCNullable.Create('Value is null');
end;
end;
procedure Nullable$TYPE$.Clear;
begin
SetNull;
end;
function Nullable$TYPE$.Equals(const Value: Nullable$TYPE$): Boolean;
begin
Result := (Self.HasValue and Value.HasValue) and (Self.Value = Value.Value);
end;
function Nullable$TYPE$.GetHasValue: Boolean;
begin
Result := fHasValue = '_';
end;
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;
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;
///IMPLEMENTATION.END
end.