1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051 |
- { ***************************************************************************
- Copyright (c) 2016-2019 Kike Pérez
- Unit : Quick.Value
- Description : Autofree value record
- Author : Kike Pérez
- Version : 1.5
- Created : 07/01/2019
- Modified : 03/04/2019
- This file is part of QuickLib: https://github.com/exilon/QuickLib
- ***************************************************************************
- 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 Quick.Value;
- {$i QuickLib.inc}
- interface
- uses
- SysUtils,
- Variants;
- type
- TValueDataType = (dtNull, dtString, dtAnsiString, dtWideString, dtInteger, dtInt64, dtDouble, dtExtended, dtDateTime, dtBoolean, dtObject, dtOwnedObject,
- dtPointer, dtClass, dtInterface, dtRecord, dtArray, dtVariant);
- TValueData = class(TInterfacedObject);
- IValueString = interface
- ['{CECEF8BB-5C77-4291-8927-FB090577F27D}']
- function GetValue : string;
- procedure SetValue(const Value : string);
- property Value : string read GetValue write SetValue;
- end;
- TValueString = class(TValueData,IValueString)
- strict private
- fData : string;
- private
- function GetValue : string;
- procedure SetValue(const Value : string);
- public
- constructor Create(const Value : string);
- property Value : string read GetValue write SetValue;
- end;
- {$IFDEF MSWINDOWS}
- IValueAnsiString = interface
- ['{75775F25-6F7A-49F0-A1E0-BDE1F55EC378}']
- function GetValue : AnsiString;
- procedure SetValue(const Value : AnsiString);
- property Value : AnsiString read GetValue write SetValue;
- end;
- TValueAnsiString = class(TValueData,IValueAnsiString)
- strict private
- fData : AnsiString;
- private
- function GetValue : AnsiString;
- procedure SetValue(const Value : AnsiString);
- public
- constructor Create(const Value : AnsiString);
- property Value : AnsiString read GetValue write SetValue;
- end;
- IValueWideString = interface
- ['{9094B9CF-46AE-4FE0-AE1D-6E6CDABDAF36}']
- function GetValue : WideString;
- procedure SetValue(const Value : WideString);
- property Value : WideString read GetValue write SetValue;
- end;
- TValueWideString = class(TValueData,IValueWideString)
- strict private
- fData : WideString;
- private
- function GetValue : WideString;
- procedure SetValue(const Value : WideString);
- public
- constructor Create(const Value : WideString);
- property Value : WideString read GetValue write SetValue;
- end;
- {$ENDIF}
- IValueInteger = interface
- ['{5AB05017-C6F3-49BA-A92C-ECCA252B3E1D}']
- function GetValue : Int64;
- procedure SetValue(const Value : Int64);
- property Value : Int64 read GetValue write SetValue;
- end;
- { TValueInteger }
- TValueInteger= class(TValueData,IValueInteger)
- strict private
- fData : Int64;
- private
- function GetValue : Int64;
- procedure SetValue(const Value : Int64);
- public
- constructor Create(const Value : Int64);
- property Value : Int64 read GetValue write SetValue;
- end;
- IValueExtended = interface
- ['{D341182F-D4E5-4C07-9E03-68DA118B90B1}']
- function GetValue : Extended;
- procedure SetValue(const Value : Extended);
- property Value : Extended read GetValue write SetValue;
- end;
- TValueExtended = class(TValueData,IValueExtended)
- strict private
- fData : Extended;
- private
- function GetValue : Extended;
- procedure SetValue(const Value : Extended);
- public
- constructor Create(const Value : Extended);
- property Value : Extended read GetValue write SetValue;
- end;
- IValueObject = interface
- ['{5828FABC-6C5D-4954-941E-B3580F918A8B}']
- function GetValue : TObject;
- procedure SetValue(const Value : TObject);
- property Value : TObject read GetValue write SetValue;
- end;
- TValueObject = class(TValueData,IValueObject)
- strict private
- fData : TObject;
- private
- function GetValue : TObject;
- procedure SetValue(const Value : TObject);
- public
- constructor Create(const Value : TObject);
- property Value : TObject read GetValue write SetValue;
- end;
- IValuePointer = interface
- ['{9FE4E499-C487-4D24-8190-14FF3F9FE86B}']
- function GetValue : Pointer;
- procedure SetValue(const Value : Pointer);
- property Value : Pointer read GetValue write SetValue;
- end;
- TValuePointer = class(TValueData,IValuePointer)
- strict private
- fData : Pointer;
- private
- function GetValue : Pointer;
- procedure SetValue(const Value : Pointer);
- public
- constructor Create(const Value : Pointer);
- property Value : Pointer read GetValue write SetValue;
- end;
- IValueVariant = interface
- ['{8B1F8469-B872-47AD-83BB-F51920012943}']
- function GetValue : Variant;
- procedure SetValue(const Value : Variant);
- property Value : Variant read GetValue write SetValue;
- end;
- TValueVariant = class(TValueData,IValueVariant)
- strict private
- fData : Variant;
- private
- function GetValue : Variant;
- procedure SetValue(const Value : Variant);
- public
- constructor Create(const Value : Variant);
- property Value : Variant read GetValue write SetValue;
- end;
- TFlexValue = record
- private
- {$IFNDEF FPC}
- fDataIntf : IInterface;
- {$ELSE}
- fDataIntf : TValueData;
- {$ENDIF}
- fDataType : TValueDataType;
- function CastToString : string;
- {$IFDEF MSWINDOWS}
- function CastToAnsiString : AnsiString;
- function CastToWideString : WideString;
- {$ENDIF}
- function CastToBoolean: Boolean;
- function CastToClass: TClass;
- function CastToExtended: Extended;
- function CastToInt64: Int64;
- function CastToInteger: Integer;
- function CastToDateTime : TDateTime;
- function CastToObject: TObject;
- function CastToPointer: Pointer;
- function CastToInterface: IInterface;
- function CastToVariant: Variant;
- function CastToCardinal : Cardinal;
- procedure SetAsString(const Value : string);
- {$IFDEF MSWINDOWS}
- procedure SetAsAnsiString(const Value : AnsiString);
- procedure SetAsWideString(const Value : WideString);
- {$ENDIF}
- procedure SetAsBoolean(const Value: Boolean);
- procedure SetAsClass(const Value: TClass);
- procedure SetAsExtended(const Value: Extended);
- procedure SetAsInt64(const Value: Int64);
- procedure SetAsInteger(const Value: Integer);
- procedure SetAsObject(const Value: TObject);
- procedure SetAsPointer(const Value: Pointer);
- procedure SetAsDateTime(const Value : TDateTime);
- procedure SetAsVariant(const Value: Variant);
- procedure SetAsCardinal(const Value : Cardinal);
- procedure SetAsInterface(const Value: IInterface);
- public
- constructor Create(const Value: TVarRec);
- property DataType : TValueDataType read fDataType;
- property AsString : string read CastToString write SetAsString;
- {$IFDEF MSWINDOWS}
- property AsAnsiString : AnsiString read CastToAnsiString write SetAsAnsiString;
- property AsWideString : WideString read CastToWideString write SetAsWideString;
- {$ENDIF}
- property AsInteger : Integer read CastToInteger write SetAsInteger;
- property AsInt64 : Int64 read CastToInt64 write SetAsInt64;
- property AsExtended : Extended read CastToExtended write SetAsExtended;
- property AsBoolean : Boolean read CastToBoolean write SetAsBoolean;
- property AsPointer : Pointer read CastToPointer write SetAsPointer;
- property AsClass : TClass read CastToClass write SetAsClass;
- property AsInterface : IInterface read CastToInterface write SetAsInterface;
- property AsObject : TObject read CastToObject write SetAsObject;
- property AsVariant : Variant read CastToVariant write SetAsVariant;
- property AsCardinal : Cardinal read CastToCardinal write SetAsCardinal;
- property AsDateTime : TDateTime read CastToDateTime write SetAsDateTime;
- //function AsType<T> : T;
- function IsNullOrEmpty : Boolean; inline;
- function IsString : Boolean; inline;
- function IsInteger : Boolean; inline;
- function IsFloating : Boolean; inline;
- function IsDateTime : Boolean; inline;
- function IsBoolean : Boolean; inline;
- function IsInterface : Boolean; inline;
- function IsObject : Boolean; inline;
- function IsPointer : Boolean; inline;
- function IsVariant : Boolean; inline;
- procedure Clear; inline;
- procedure _AddRef; inline;
- procedure _Release; inline;
- {$IFNDEF FPCS}
- class operator Implicit(const Value : TFlexValue) : string;
- class operator Implicit(Value : TFlexValue) : Integer;
- class operator Implicit(Value : TFlexValue) : Int64;
- class operator Implicit(Value : TFlexValue) : Extended;
- class operator Implicit(Value : TFlexValue) : TDateTime;
- class operator Implicit(Value : TFlexValue) : Boolean;
- class operator Implicit(Value : TFlexValue) : TClass;
- class operator Implicit(Value : TFlexValue) : TObject;
- class operator Implicit(Value : TFlexValue) : Pointer;
- class operator Implicit(Value : TFlexValue) : Variant;
- class operator Implicit(const Value : string) : TFlexValue;
- class operator Implicit(Value : Integer) : TFlexValue;
- class operator Implicit(Value : Int64) : TFlexValue;
- class operator Implicit(Value : Extended) : TFlexValue;
- class operator Implicit(Value : TDateTime) : TFlexValue;
- class operator Implicit(Value : Boolean) : TFlexValue;
- class operator Implicit(Value : TClass) : TFlexValue;
- class operator Implicit(Value : TObject) : TFlexValue;
- class operator Implicit(Value : Pointer) : TFlexValue;
- class operator Implicit(Value : Variant) : TFlexValue;
- {$ENDIF}
- end;
- TFlexPair = record
- Name : string;
- Value : TFlexValue;
- constructor Create(const aName : string; aValue : TFlexValue);
- end;
- implementation
- function TFlexValue.CastToString: string;
- begin
- try
- case fDataType of
- dtNull : Result := '';
- dtString : Result := (fDataIntf as IValueString).Value;
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := string((fDataIntf as IValueAnsiString).Value);
- dtWideString : Result := (fDataIntf as IValueWideString).Value;
- {$ENDIF}
- dtInteger,
- dtInt64 : Result := IntToStr(AsInt64);
- dtBoolean : Result := BoolToStr(AsBoolean,True);
- dtDouble,
- dtExtended : Result := FloatToStr(AsExtended);
- dtDateTime : Result := DateTimeToStr(AsExtended);
- dtVariant : Result := string(AsVariant);
- dtClass : Result := AsClass.ClassName;
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to String error: %s',[e.message]);
- end;
- end;
- {$IFDEF MSWINDOWS}
- function TFlexValue.CastToAnsiString: AnsiString;
- begin
- try
- case fDataType of
- dtNull : Result := '';
- dtString : Result := AnsiString((fDataIntf as IValueString).Value);
- dtAnsiString : Result := (fDataIntf as IValueAnsiString).Value;
- dtWideString : Result := AnsiString((fDataIntf as IValueWideString).Value);
- dtInteger,
- dtInt64 : Result := AnsiString(IntToStr(AsInt64));
- dtBoolean : Result := AnsiString(BoolToStr(AsBoolean,True));
- dtDouble,
- dtExtended : Result := AnsiString(FloatToStr(AsExtended));
- dtDateTime : Result := AnsiString(DateTimeToStr(AsExtended));
- dtVariant : Result := AnsiString(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to AnsiString error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToWideString: WideString;
- begin
- try
- case fDataType of
- dtNull : Result := '';
- dtString : Result := Widestring((fDataIntf as IValueString).Value);
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := Widestring((fDataIntf as IValueAnsiString).Value);
- dtWideString : Result := (fDataIntf as IValueWideString).Value;
- {$ENDIF}
- dtInteger,
- dtInt64 : Result := Widestring(IntToStr(AsInt64));
- dtBoolean : Result := Widestring(BoolToStr(AsBoolean,True));
- dtDouble,
- dtExtended : Result := Widestring(FloatToStr(AsExtended));
- dtDateTime : Result := Widestring(DateTimeToStr(AsExtended));
- dtVariant : Result := Widestring(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to WideString error: %s',[e.message]);
- end;
- end;
- {$ENDIF}
- function TFlexValue.CastToBoolean: Boolean;
- begin
- try
- case fDataType of
- dtNull : Result := False;
- dtString : Result := StrToBool((fDataIntf as IValueString).Value);
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := StrToBool(string((fDataIntf as IValueAnsiString).Value));
- dtWideString : Result := StrToBool((fDataIntf as IValueWideString).Value);
- {$ENDIF}
- dtInteger,
- dtInt64 :
- begin
- if (fDataIntf as IValueInteger).Value = 1 then Result := True
- else if (fDataIntf as IValueInteger).Value = 0 then Result := False
- else raise Exception.Create('Integer value not in 0-1 range');
- end;
- dtBoolean : Result := Boolean((fDataIntf as IValueInteger).Value);
- dtVariant : Result := Boolean(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Boolean error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToCardinal: Cardinal;
- begin
- Result := AsInt64;
- end;
- function TFlexValue.CastToClass: TClass;
- begin
- try
- case fDataType of
- dtNull : Result := nil;
- dtClass : Result := (fDataIntf as TValuePointer).Value;
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TClass error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToDateTime: TDateTime;
- begin
- try
- case fDataType of
- dtNull : Result := 0.0;
- dtString : Result := StrToDateTime((fDataIntf as IValueString).Value);
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := StrToDateTime(string((fDataIntf as IValueAnsiString).Value));
- dtWideString : Result := StrToDateTime((fDataIntf as IValueWideString).Value);
- {$ENDIF}
- dtInteger,
- dtInt64 : Result := FileDateToDateTime(AsInt64);
- dtDouble,
- dtExtended,
- dtDateTime : Result := (fDataIntf as IValueExtended).Value;
- dtVariant : Result := Extended(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToExtended: Extended;
- begin
- try
- case fDataType of
- dtNull : Result := 0.0;
- dtString : Result := StrToFloat((fDataIntf as IValueString).Value);
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := StrToFloat(string((fDataIntf as IValueAnsiString).Value));
- dtWideString : Result := StrToFloat((fDataIntf as IValueWideString).Value);
- {$ENDIF}
- dtInteger,
- dtInt64 : Result := AsInt64;
- dtBoolean : Result := AsInt64;
- dtDouble,
- dtExtended,
- dtDateTime : Result := (fDataIntf as IValueExtended).Value;
- dtVariant : Result := Extended(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToInt64: Int64;
- begin
- try
- case fDataType of
- dtNull : Result := 0;
- dtString : Result := StrToInt((fDataIntf as IValueString).Value);
- {$IFDEF MSWINDOWS}
- dtAnsiString : Result := StrToInt(string((fDataIntf as IValueAnsiString).Value));
- dtWideString : Result := StrToInt((fDataIntf as IValueWideString).Value);
- {$ENDIF}
- dtInteger,
- dtInt64 : Result := (fDataIntf as IValueInteger).Value;
- dtBoolean : Result := Integer(AsBoolean);
- dtDateTime : Result := DateTimeToFileDate((fDataIntf as IValueExtended).Value);
- dtVariant : Result := Integer(AsVariant);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Integer error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToInteger: Integer;
- begin
- Result := AsInt64;
- end;
- function TFlexValue.CastToObject: TObject;
- begin
- try
- case fDataType of
- dtObject,
- dtOwnedObject : Result := (fDataIntf as IValueObject).Value;
- {$IFNDEF FPC}
- dtPointer : Result := TObject((fDataIntf as IValueObject).Value);
- {$ELSE}
- dtPointer : Result := TObject((fDataIntf as IValuePointer).Value);
- {$ENDIF}
- dtNull : Result := nil;
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Object error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToPointer: Pointer;
- begin
- try
- case fDataType of
- dtObject,
- dtOwnedObject : Result := Pointer((fDataIntf as IValueObject).Value);
- dtPointer : Result := (fDataIntf as IValuePointer).Value;
- dtNull : Result := nil;
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Pointer error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToVariant: Variant;
- begin
- try
- case fDataType of
- dtNull : Result := Variants.Null;
- dtBoolean : Result := AsVariant;
- dtVariant : Result := (fDataIntf as IValueVariant).Value;
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Variant error: %s',[e.message]);
- end;
- end;
- function TFlexValue.CastToInterface: IInterface;
- begin
- try
- case fDataType of
- dtNull : Result := nil;
- dtInterface : Result := fDataIntf;
- dtPointer : Result := IInterface(fDataIntf);
- else raise Exception.Create('DataType not supported');
- end;
- except
- on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Interface error: %s',[e.message]);
- end;
- end;
- procedure TFlexValue.Clear;
- begin
- if Pointer(fDataIntf) <> nil then fDataIntf := nil;
- fDataType := dtNull;
- end;
- constructor TFlexValue.Create(const Value: TVarRec);
- begin
- case Value.VType of
- {$IFNDEF NEXTGEN}
- vtString : AsString := string(Value.VString^);
- vtChar : AsString := string(Value.VChar);
- {$ENDIF}
- {$IFDEF MSWINDOWS}
- vtAnsiString : AsAnsiString := AnsiString(Value.VAnsiString);
- vtWideString : AsWideString := WideString(Value.VWideString);
- {$ENDIF}
- {$IFDEF UNICODE}
- vtUnicodeString: AsString := string(Value.VUnicodeString);
- {$ENDIF UNICODE}
- vtInteger : AsInteger := Value.VInteger;
- vtInt64 : AsInt64 := Value.VInt64^;
- vtExtended : AsExtended := Value.VExtended^;
- vtBoolean : AsBoolean := Value.VBoolean;
- vtVariant : AsVariant := Value.VVariant^;
- vtInterface : AsInterface := IInterface(Value.VInterface);
- vtClass : AsClass := Value.VClass;
- vtObject : AsObject := Value.VObject;
- vtPointer : AsPointer := Value.VPointer;
- else raise Exception.Create('DataType not supported by TFlexValue');
- end;
- {$IFDEF FPC}
- fDataIntf._AddRef;
- {$ENDIF}
- end;
- {$IFNDEF FPCS}
- class operator TFlexValue.Implicit(Value: TFlexValue): Boolean;
- begin
- Result := Value.AsBoolean;
- end;
- class operator TFlexValue.Implicit(const Value: TFlexValue): string;
- begin
- Result := Value.AsString;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): TObject;
- begin
- Result := Value.AsObject;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): Pointer;
- begin
- Result := Value.AsPointer;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): TDateTime;
- begin
- Result := Value.AsDateTime;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): TClass;
- begin
- Result := Value.AsClass;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): Int64;
- begin
- Result := Value.AsInt64;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): Integer;
- begin
- Result := Value.AsInteger;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): Extended;
- begin
- Result := Value.AsExtended;
- end;
- class operator TFlexValue.Implicit(Value: TFlexValue): Variant;
- begin
- Result := Value.AsVariant;
- end;
- class operator TFlexValue.Implicit(Value: Variant): TFlexValue;
- begin
- Result.AsVariant := Value;
- end;
- class operator TFlexValue.Implicit(const Value : string) : TFlexValue;
- begin
- Result.AsString := Value;
- end;
- class operator TFlexValue.Implicit(Value : Integer) : TFlexValue;
- begin
- Result.AsInteger := Value;
- end;
- class operator TFlexValue.Implicit(Value : Int64) : TFlexValue;
- begin
- Result.AsInt64 := Value;
- end;
- class operator TFlexValue.Implicit(Value : Extended) : TFlexValue;
- begin
- Result.AsExtended := Value;
- end;
- class operator TFlexValue.Implicit(Value : TDateTime) : TFlexValue;
- begin
- Result.AsDateTime := Value;
- end;
- class operator TFlexValue.Implicit(Value : Boolean) : TFlexValue;
- begin
- Result.AsBoolean := Value;
- end;
- class operator TFlexValue.Implicit(Value : TClass) : TFlexValue;
- begin
- Result.AsClass := Value;
- end;
- class operator TFlexValue.Implicit(Value : TObject) : TFlexValue;
- begin
- Result.AsObject := Value;
- end;
- class operator TFlexValue.Implicit(Value : Pointer) : TFlexValue;
- begin
- Result.AsPointer := Value;
- end;
- {$ENDIF}
- function TFlexValue.IsBoolean: Boolean;
- begin
- Result := fDataType = dtBoolean;
- end;
- function TFlexValue.IsDateTime: Boolean;
- begin
- Result := fDataType = dtDateTime;
- end;
- function TFlexValue.IsFloating: Boolean;
- begin
- Result := fDataType in [dtDouble,dtExtended];
- end;
- function TFlexValue.IsInteger: Boolean;
- begin
- Result := fDataType in [dtInteger,dtInt64];
- end;
- function TFlexValue.IsInterface: Boolean;
- begin
- Result := fDataType = dtInterface;
- end;
- function TFlexValue.IsNullOrEmpty: Boolean;
- begin
- Result := fDataIntf = nil;
- end;
- function TFlexValue.IsObject: Boolean;
- begin
- Result := fDataType = dtObject;
- end;
- function TFlexValue.IsPointer: Boolean;
- begin
- Result := fDataType = dtPointer;
- end;
- function TFlexValue.IsString: Boolean;
- begin
- Result := fDataType in [dtString,dtAnsiString,dtWideString];
- end;
- function TFlexValue.IsVariant: Boolean;
- begin
- Result := fDataType = dtVariant;
- end;
- {$IFDEF MSWINDOWS}
- procedure TFlexValue.SetAsAnsiString(const Value: AnsiString);
- begin
- Clear;
- fDataIntf := TValueAnsiString.Create(Value);
- fDataType := TValueDataType.dtAnsiString;
- end;
- {$ENDIF}
- procedure TFlexValue.SetAsBoolean(const Value: Boolean);
- begin
- Clear;
- fDataIntf := TValueInteger.Create(Value.ToInteger);
- fDataType := TValueDataType.dtBoolean;
- end;
- procedure TFlexValue.SetAsCardinal(const Value: Cardinal);
- begin
- Clear;
- fDataIntf := TValueInteger.Create(Value);
- fDataType := TValueDataType.dtInt64;
- end;
- procedure TFlexValue.SetAsClass(const Value: TClass);
- begin
- Clear;
- fDataIntf := TValuePointer.Create(Value);
- fDataType := TValueDataType.dtClass;
- end;
- procedure TFlexValue.SetAsDateTime(const Value: TDateTime);
- begin
- Clear;
- fDataIntf := TValueExtended.Create(Value);
- fDataType := TValueDataType.dtDateTime;
- end;
- procedure TFlexValue.SetAsExtended(const Value: Extended);
- begin
- Clear;
- fDataIntf := TValueExtended.Create(Value);
- fDataType := TValueDataType.dtExtended;
- end;
- procedure TFlexValue.SetAsInt64(const Value: Int64);
- begin
- Clear;
- fDataIntf := TValueInteger.Create(Value);
- fDataType := TValueDataType.dtInt64;
- end;
- procedure TFlexValue.SetAsInteger(const Value: Integer);
- begin
- Clear;
- fDataIntf := TValueInteger.Create(Value);
- fDataType := TValueDataType.dtInteger;
- end;
- procedure TFlexValue.SetAsInterface(const Value: IInterface);
- begin
- {$IFNDEF FPC}
- fDataIntf := Value;
- {$ELSE}
- fDataIntf := Pointer(Value);
- {$ENDIF}
- fDataType := TValueDataType.dtInterface;
- end;
- procedure TFlexValue.SetAsObject(const Value: TObject);
- begin
- Clear;
- fDataIntf := TValueObject.Create(Value);
- fDataType := TValueDataType.dtObject;
- end;
- procedure TFlexValue.SetAsPointer(const Value: Pointer);
- begin
- Clear;
- fDataIntf := TValuePointer.Create(Value);
- fDataType := TValueDataType.dtPointer;
- end;
- procedure TFlexValue.SetAsString(const Value: string);
- begin
- Clear;
- fDataIntf := TValueString.Create(Value);
- fDataType := TValueDataType.dtString;
- end;
- function TryVarAsType(aValue : Variant; aVarType : Word) : Boolean;
- begin
- try
- VarAsType(aValue,aVarType);
- Result := True;
- except
- Result := False;
- end;
- end;
- procedure TFlexValue.SetAsVariant(const Value: Variant);
- begin
- Clear;
- case VarType(Value) and varTypeMask of
- varEmpty,
- varNull : Clear;
- varSmallInt,
- varInteger,
- varByte,
- varWord,
- varLongWord,
- varInt64 : SetAsInt64(Value);
- varSingle,
- varDouble,
- varCurrency : SetAsExtended(Value);
- varDate : SetAsDateTime(Value);
- varOleStr : SetAsString(Value);
- varDispatch : begin
- if TryVarAsType(Value,varInt64) then SetAsInt64(Value)
- else if TryVarAsType(Value,varDouble) then SetAsExtended(Value)
- else if TryVarAsType(Value,varBoolean) then SetAsBoolean(Value)
- else if TryVarAsType(Value,varString) then SetAsString(Value)
- else
- begin
- fDataIntf := TValueVariant.Create(Value);
- fDataType := TValueDataType.dtVariant;
- end;
- end;
- //varError : typeString := 'varError';
- varBoolean : SetAsBoolean(Value);
- //varStrArg : typeString := 'varStrArg';
- varString : SetAsString(Value);
- //varAny : typeString := 'varAny';
- //varTypeMask : typeString := 'varTypeMask';
- else
- begin
- fDataIntf := TValueVariant.Create(Value);
- fDataType := TValueDataType.dtVariant;
- end;
- end;
- end;
- {$IFDEF MSWINDOWS}
- procedure TFlexValue.SetAsWideString(const Value: WideString);
- begin
- Clear;
- fDataIntf := TValueWideString.Create(Value);
- fDataType := TValueDataType.dtWideString;
- end;
- {$ENDIF}
- procedure TFlexValue._AddRef;
- begin
- if Assigned(fDataIntf) then fDataIntf._AddRef;
- end;
- procedure TFlexValue._Release;
- begin
- if Assigned(fDataIntf) then fDataIntf._Release;
- end;
- { TValueStringData }
- constructor TValueString.Create(const Value: string);
- begin
- fData := Value;
- end;
- function TValueString.GetValue: string;
- begin
- Result := fData;
- end;
- procedure TValueString.SetValue(const Value: string);
- begin
- fData := Value;
- end;
- { TValueVariantData }
- constructor TValueVariant.Create(const Value: Variant);
- begin
- fData := Value;
- end;
- function TValueVariant.GetValue: Variant;
- begin
- Result := fData;
- end;
- procedure TValueVariant.SetValue(const Value: Variant);
- begin
- fData := Value;
- end;
- { TValueAnsiStringData }
- {$IFDEF MSWINDOWS}
- constructor TValueAnsiString.Create(const Value: AnsiString);
- begin
- fData := Value;
- end;
- function TValueAnsiString.GetValue: AnsiString;
- begin
- Result := fData;
- end;
- procedure TValueAnsiString.SetValue(const Value: AnsiString);
- begin
- fData := Value;
- end;
- { TValueWideStringData }
- constructor TValueWideString.Create(const Value: WideString);
- begin
- fData := Value;
- end;
- function TValueWideString.GetValue: WideString;
- begin
- Result := fData;
- end;
- procedure TValueWideString.SetValue(const Value: WideString);
- begin
- fData := Value;
- end;
- {$ENDIF}
- { TValueInteger }
- constructor TValueInteger.Create(const Value: Int64);
- begin
- fData := Value;
- end;
- function TValueInteger.GetValue: Int64;
- begin
- Result := fData;
- end;
- procedure TValueInteger.SetValue(const Value: Int64);
- begin
- fData := Value;
- end;
- { TValuePointer }
- constructor TValuePointer.Create(const Value: Pointer);
- begin
- fData := Value;
- end;
- function TValuePointer.GetValue: Pointer;
- begin
- Result := fData;
- end;
- procedure TValuePointer.SetValue(const Value: Pointer);
- begin
- fData := Value;
- end;
- { TValueExtended }
- constructor TValueExtended.Create(const Value: Extended);
- begin
- fData := Value;
- end;
- function TValueExtended.GetValue: Extended;
- begin
- Result := fData;
- end;
- procedure TValueExtended.SetValue(const Value: Extended);
- begin
- fData := Value;
- end;
- { TValueObject }
- constructor TValueObject.Create(const Value: TObject);
- begin
- fData := Value;
- end;
- function TValueObject.GetValue: TObject;
- begin
- Result := fData;
- end;
- procedure TValueObject.SetValue(const Value: TObject);
- begin
- fData := Value;
- end;
- { TFlexPair }
- constructor TFlexPair.Create(const aName: string; aValue: TFlexValue);
- begin
- Name := aName;
- Value := aValue;
- end;
- end.
|