Re: Detecting type of variable ?
- From: "Skybuck Flying" <spam@xxxxxxxxxxx>
- Date: Thu, 25 Jan 2007 07:06:23 +0100
Normally I don't post my units and test code... but this unit is kinda funny
and cool so here goes:
Not sure if it will work in practice though as noted in the comments...
gonna try it though :D
*** Begin of Debug program ***
program DebugProgramForGenericBooleanOperationsVersion001;
{$APPTYPE CONSOLE}
{
Debug program for Generic Boolean Operations.
version 0.01 created on 25 january 2007 by Skybuck Flying.
proper unit and testing code.
How these will perform for doubles in real world scenerio's remains to be
seen ;)
}
uses
SysUtils,
Unit_GenericBooleanOperations_version_001 in
'..\..\..\Units\Technical\GenericBooleanOperations\Unit_GenericBooleanOperations_version_001.pas';
procedure Test( ParaVariable : variant );
begin
if HasBooleanValue( ParaVariable ) then
begin
if GenericIsTrue(ParaVariable) then
begin
writeln('true');
end else
if GenericIsFalse(ParaVariable) then
begin
writeln('false');
end;
end else
begin
writeln('unknown');
end;
end;
type
TvariableType = boolean;
// TvariableType = double;
var
vVariable : TvariableType;
begin
// double test
vVariable := variant(0.0);
write('0.0: ');
Test( vVariable );
// double test
write('1.0: ');
vVariable := variant(1.0);
Test( vVariable );
// double test
write('-1.0: ');
vVariable := variant(-1.0);
Test( vVariable );
// double test
write('0.5: ');
vVariable := variant(0.5);
Test( vVariable );
// double test
write('-0.5: ');
vVariable := variant(-0.5);
Test( vVariable );
// double test
write('10.5: ');
vVariable := variant(10.5);
Test( vVariable );
// double test
write('-10.5: ');
vVariable := variant(-10.5);
Test( vVariable );
// boolean test
write('false: ');
vVariable := variant(false);
Test( vVariable );
// boolean test
write('true: ');
vVariable := variant(true);
Test( vVariable );
readln;
end.
*** End of Debug program ***
*** Begin of Unit ***
unit Unit_GenericBooleanOperations_version_001;
{
Unit_GenericBooleanOperations
version 0.01 created on 25 january 2007 by Skybuck Flying.
How these will perform for doubles in real world scenerio's remains to be
seen ;)
}
interface
function HasBooleanValue( ParaVariable : variant ) : boolean;
function GenericIsTrue( ParaVariable : variant ) : boolean;
function GenericIsFalse( ParaVariable : variant ) : boolean;
implementation
var
GeneralTrue : variant;
GeneralFalse : variant;
function HasBooleanValue( ParaVariable : variant ) : boolean;
begin
result := false;
if (ParaVariable = GeneralTrue) or (-variant(ParaVariable) = GeneralTrue)
then
begin
result := true;
end else
if (ParaVariable = GeneralFalse) then
begin
result := true;
end;
end;
function GenericIsTrue( ParaVariable : variant ) : boolean;
begin
result := false;
if (ParaVariable = GeneralTrue) or (-Variant(ParaVariable) = GeneralTrue)
then
begin
result := true;
end;
end;
function GenericIsFalse( ParaVariable : variant ) : boolean;
begin
result := false;
if (ParaVariable = GeneralFalse) then
begin
result := true;
end;
end;
initialization
GeneralTrue := variant(true);
GeneralFalse := variant(false);
end.
*** End of Unit ***
Bye,
Skybuck.
.
- Follow-Ups:
- Re: Detecting type of variable ?
- From: Skybuck Flying
- Re: Detecting type of variable ?
- References:
- Detecting type of variable ?
- From: Skybuck Flying
- Re: Detecting type of variable ?
- From: Skybuck Flying
- Re: Detecting type of variable ?
- From: Skybuck Flying
- Re: Detecting type of variable ?
- From: Skybuck Flying
- Re: Detecting type of variable ?
- From: Skybuck Flying
- Detecting type of variable ?
- Prev by Date: Re: Detecting type of variable ?
- Next by Date: Re: Detecting type of variable ?
- Previous by thread: Re: Detecting type of variable ?
- Next by thread: Re: Detecting type of variable ?
- Index(es):
Relevant Pages
|