2007. január 23., kedd

Replacement for the C ternary conditional operator "?"


Problem/Question/Abstract:

Replacement for the C ternary conditional operator "?"

Answer:

Solve 1:

Original functions by Project JEDI Code Library (JCL).

It's better(read faster) than use Variant Type.

Ps.: Portuguese comments.

//*******************************************************
// Declara??o
// Substitui??o do operador "?" em C
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart: Boolean): Boolean;
  overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart: Byte): Byte;
  overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart: Cardinal):
  Cardinal; overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart: Char): Char;
  overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart: Extended):
  Extended; overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart: Integer):
  Integer; overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Pointer): Pointer; overload;
function Iff(const Condition: Boolean; const TruePart, FalsePart:
  string): string; overload;
{$IFDEF SUPPORTS_INT64}
function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Int64): Int64; overload;
{$ENDIF SUPPORTS_INT64}

//*******************************************************
// Fun?�es
// Substitui??o do operador "?" em C
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Boolean): Boolean; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Byte): Byte; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Cardinal): Cardinal; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Char): Char; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Extended): Extended; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Integer): Integer; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Pointer): Pointer; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  string): string; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
//*******************************************************
{$IFDEF SUPPORTS_INT64}

function Iff(const Condition: Boolean; const TruePart, FalsePart:
  Int64): Int64; overload;
begin
  if Condition then
    Result := TruePart
  else
    Result := FalsePart;
end;
{$ENDIF SUPPORTS_INT64}


Solve 2:

Delphi 6+ has the following functions:

function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0):
  Integer; overload;

function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64;
  overload;

function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0):
  Double; overload;

function IfThen(AValue: Boolean; const ATrue: string; const AFalse: string =
  ''): string; overload;

Nincsenek megjegyzések:

Megjegyzés küldése