Quick.Value.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.Value
  4. Description : Autofree value record
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 07/01/2019
  8. Modified : 24/01/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Value;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils,
  26. Variants;
  27. type
  28. TValueDataType = (dtNull, dtString, dtAnsiString, dtWideString, dtInteger, dtInt64, dtDouble, dtExtended, dtDateTime, dtBoolean, dtObject, dtOwnedObject,
  29. dtPointer, dtClass, dtInterface, dtRecord, dtArray, dtVariant);
  30. TValueData = class(TInterfacedObject);
  31. IValueString = interface
  32. ['{CECEF8BB-5C77-4291-8927-FB090577F27D}']
  33. function GetValue : string;
  34. procedure SetValue(const Value : string);
  35. property Value : string read GetValue write SetValue;
  36. end;
  37. TValueString = class(TValueData,IValueString)
  38. strict private
  39. fData : string;
  40. private
  41. function GetValue : string;
  42. procedure SetValue(const Value : string);
  43. public
  44. constructor Create(const Value : string);
  45. property Value : string read GetValue write SetValue;
  46. end;
  47. {$IFDEF MSWINDOWS}
  48. IValueAnsiString = interface
  49. ['{75775F25-6F7A-49F0-A1E0-BDE1F55EC378}']
  50. function GetValue : AnsiString;
  51. procedure SetValue(const Value : AnsiString);
  52. property Value : AnsiString read GetValue write SetValue;
  53. end;
  54. TValueAnsiString = class(TValueData,IValueAnsiString)
  55. strict private
  56. fData : AnsiString;
  57. private
  58. function GetValue : AnsiString;
  59. procedure SetValue(const Value : AnsiString);
  60. public
  61. constructor Create(const Value : AnsiString);
  62. property Value : AnsiString read GetValue write SetValue;
  63. end;
  64. IValueWideString = interface
  65. ['{9094B9CF-46AE-4FE0-AE1D-6E6CDABDAF36}']
  66. function GetValue : WideString;
  67. procedure SetValue(const Value : WideString);
  68. property Value : WideString read GetValue write SetValue;
  69. end;
  70. TValueWideString = class(TValueData,IValueWideString)
  71. strict private
  72. fData : WideString;
  73. private
  74. function GetValue : WideString;
  75. procedure SetValue(const Value : WideString);
  76. public
  77. constructor Create(const Value : WideString);
  78. property Value : WideString read GetValue write SetValue;
  79. end;
  80. {$ENDIF}
  81. IValueInteger = interface
  82. ['{5AB05017-C6F3-49BA-A92C-ECCA252B3E1D}']
  83. function GetValue : Int64;
  84. procedure SetValue(const Value : Int64);
  85. property Value : Int64 read GetValue write SetValue;
  86. end;
  87. { TValueInteger }
  88. TValueInteger= class(TValueData,IValueInteger)
  89. strict private
  90. fData : Int64;
  91. private
  92. function GetValue : Int64;
  93. procedure SetValue(const Value : Int64);
  94. public
  95. constructor Create(const Value : Int64);
  96. property Value : Int64 read GetValue write SetValue;
  97. end;
  98. IValueExtended = interface
  99. ['{D341182F-D4E5-4C07-9E03-68DA118B90B1}']
  100. function GetValue : Extended;
  101. procedure SetValue(const Value : Extended);
  102. property Value : Extended read GetValue write SetValue;
  103. end;
  104. TValueExtended = class(TValueData,IValueExtended)
  105. strict private
  106. fData : Extended;
  107. private
  108. function GetValue : Extended;
  109. procedure SetValue(const Value : Extended);
  110. public
  111. constructor Create(const Value : Extended);
  112. property Value : Extended read GetValue write SetValue;
  113. end;
  114. IValueObject = interface
  115. ['{5828FABC-6C5D-4954-941E-B3580F918A8B}']
  116. function GetValue : TObject;
  117. procedure SetValue(const Value : TObject);
  118. property Value : TObject read GetValue write SetValue;
  119. end;
  120. TValueObject = class(TValueData,IValueObject)
  121. strict private
  122. fData : TObject;
  123. private
  124. function GetValue : TObject;
  125. procedure SetValue(const Value : TObject);
  126. public
  127. constructor Create(const Value : TObject);
  128. property Value : TObject read GetValue write SetValue;
  129. end;
  130. IValuePointer = interface
  131. ['{9FE4E499-C487-4D24-8190-14FF3F9FE86B}']
  132. function GetValue : Pointer;
  133. procedure SetValue(const Value : Pointer);
  134. property Value : Pointer read GetValue write SetValue;
  135. end;
  136. TValuePointer = class(TValueData,IValuePointer)
  137. strict private
  138. fData : Pointer;
  139. private
  140. function GetValue : Pointer;
  141. procedure SetValue(const Value : Pointer);
  142. public
  143. constructor Create(const Value : Pointer);
  144. property Value : Pointer read GetValue write SetValue;
  145. end;
  146. IValueVariant = interface
  147. ['{8B1F8469-B872-47AD-83BB-F51920012943}']
  148. function GetValue : Variant;
  149. procedure SetValue(const Value : Variant);
  150. property Value : Variant read GetValue write SetValue;
  151. end;
  152. TValueVariant = class(TValueData,IValueVariant)
  153. strict private
  154. fData : Variant;
  155. private
  156. function GetValue : Variant;
  157. procedure SetValue(const Value : Variant);
  158. public
  159. constructor Create(const Value : Variant);
  160. property Value : Variant read GetValue write SetValue;
  161. end;
  162. TFlexValue = record
  163. private
  164. {$IFNDEF FPC}
  165. fDataIntf : IInterface;
  166. {$ELSE}
  167. fDataIntf : TValueData;
  168. {$ENDIF}
  169. fDataType : TValueDataType;
  170. function CastToString : string;
  171. {$IFDEF MSWINDOWS}
  172. function CastToAnsiString : AnsiString;
  173. function CastToWideString : WideString;
  174. {$ENDIF}
  175. function CastToBoolean: Boolean;
  176. function CastToClass: TClass;
  177. function CastToExtended: Extended;
  178. function CastToInt64: Int64;
  179. function CastToInteger: Integer;
  180. function CastToDateTime : TDateTime;
  181. function CastToObject: TObject;
  182. function CastToPointer: Pointer;
  183. function CastToInterface: Pointer;
  184. function CastToVariant: Variant;
  185. function CastToCardinal : Cardinal;
  186. procedure SetAsString(const Value : string);
  187. {$IFDEF MSWINDOWS}
  188. procedure SetAsAnsiString(const Value : AnsiString);
  189. procedure SetAsWideString(const Value : WideString);
  190. {$ENDIF}
  191. procedure SetAsBoolean(const Value: Boolean);
  192. procedure SetAsClass(const Value: TClass);
  193. procedure SetAsExtended(const Value: Extended);
  194. procedure SetAsInt64(const Value: Int64);
  195. procedure SetAsInteger(const Value: Integer);
  196. procedure SetAsObject(const Value: TObject);
  197. procedure SetAsPointer(const Value: Pointer);
  198. procedure SetAsDateTime(const Value : TDateTime);
  199. procedure SetAsVariant(const Value: Variant);
  200. procedure SetAsCardinal(const Value : Cardinal);
  201. public
  202. constructor Create(const Value: TVarRec);
  203. property DataType : TValueDataType read fDataType;
  204. property AsString : string read CastToString write SetAsString;
  205. {$IFDEF MSWINDOWS}
  206. property AsAnsiString : AnsiString read CastToAnsiString write SetAsAnsiString;
  207. property AsWideString : WideString read CastToWideString write SetAsWideString;
  208. {$ENDIF}
  209. property AsInteger : Integer read CastToInteger write SetAsInteger;
  210. property AsInt64 : Int64 read CastToInt64 write SetAsInt64;
  211. property AsExtended : Extended read CastToExtended write SetAsExtended;
  212. property AsBoolean : Boolean read CastToBoolean write SetAsBoolean;
  213. property AsPointer : Pointer read CastToPointer write SetAsPointer;
  214. property AsClass : TClass read CastToClass write SetAsClass;
  215. property AsInterface : Pointer read CastToInterface write SetAsPointer;
  216. property AsObject : TObject read CastToObject write SetAsObject;
  217. property AsVariant : Variant read CastToVariant write SetAsVariant;
  218. property AsCardinal : Cardinal read CastToCardinal write SetAsCardinal;
  219. property AsDateTime : TDateTime read CastToDateTime write SetAsDateTime;
  220. //function AsType<T> : T;
  221. function IsNullOrEmpty : Boolean; inline;
  222. function IsString : Boolean; inline;
  223. function IsInteger : Boolean; inline;
  224. function IsFloating : Boolean; inline;
  225. function IsDateTime : Boolean; inline;
  226. function IsBoolean : Boolean; inline;
  227. function IsInterface : Boolean; inline;
  228. function IsObject : Boolean; inline;
  229. function IsPointer : Boolean; inline;
  230. function IsVariant : Boolean; inline;
  231. procedure Clear; inline;
  232. procedure _AddRef; inline;
  233. procedure _Release; inline;
  234. {$IFNDEF FPC}
  235. class operator Implicit(const Value : TFlexValue) : string;
  236. class operator Implicit(Value : TFlexValue) : Integer;
  237. class operator Implicit(Value : TFlexValue) : Int64;
  238. class operator Implicit(Value : TFlexValue) : Extended;
  239. class operator Implicit(Value : TFlexValue) : TDateTime;
  240. class operator Implicit(Value : TFlexValue) : Boolean;
  241. class operator Implicit(Value : TFlexValue) : TClass;
  242. class operator Implicit(Value : TFlexValue) : TObject;
  243. class operator Implicit(Value : TFlexValue) : Pointer;
  244. {$ENDIF}
  245. end;
  246. implementation
  247. function TFlexValue.CastToString: string;
  248. begin
  249. try
  250. case fDataType of
  251. dtNull : Result := '';
  252. dtString : Result := (fDataIntf as IValueString).Value;
  253. {$IFDEF MSWINDOWS}
  254. dtAnsiString : Result := string((fDataIntf as IValueAnsiString).Value);
  255. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  256. {$ENDIF}
  257. dtInteger,
  258. dtInt64 : Result := IntToStr(AsInt64);
  259. dtBoolean : Result := BoolToStr(AsBoolean,True);
  260. dtDouble,
  261. dtExtended : Result := FloatToStr(AsExtended);
  262. dtDateTime : Result := DateTimeToStr(AsExtended);
  263. dtVariant : Result := string(AsVariant);
  264. dtClass : Result := AsClass.ClassName;
  265. else raise Exception.Create('DataType not supported');
  266. end;
  267. except
  268. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to String error: %s',[e.message]);
  269. end;
  270. end;
  271. {$IFDEF MSWINDOWS}
  272. function TFlexValue.CastToAnsiString: AnsiString;
  273. begin
  274. try
  275. case fDataType of
  276. dtNull : Result := '';
  277. dtString : Result := AnsiString((fDataIntf as IValueString).Value);
  278. dtAnsiString : Result := (fDataIntf as IValueAnsiString).Value;
  279. dtWideString : Result := AnsiString((fDataIntf as IValueWideString).Value);
  280. dtInteger,
  281. dtInt64 : Result := AnsiString(IntToStr(AsInt64));
  282. dtBoolean : Result := AnsiString(BoolToStr(AsBoolean,True));
  283. dtDouble,
  284. dtExtended : Result := AnsiString(FloatToStr(AsExtended));
  285. dtDateTime : Result := AnsiString(DateTimeToStr(AsExtended));
  286. dtVariant : Result := AnsiString(AsVariant);
  287. else raise Exception.Create('DataType not supported');
  288. end;
  289. except
  290. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to AnsiString error: %s',[e.message]);
  291. end;
  292. end;
  293. function TFlexValue.CastToWideString: WideString;
  294. begin
  295. try
  296. case fDataType of
  297. dtNull : Result := '';
  298. dtString : Result := Widestring((fDataIntf as IValueString).Value);
  299. {$IFDEF MSWINDOWS}
  300. dtAnsiString : Result := Widestring((fDataIntf as IValueAnsiString).Value);
  301. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  302. {$ENDIF}
  303. dtInteger,
  304. dtInt64 : Result := Widestring(IntToStr(AsInt64));
  305. dtBoolean : Result := Widestring(BoolToStr(AsBoolean,True));
  306. dtDouble,
  307. dtExtended : Result := Widestring(FloatToStr(AsExtended));
  308. dtDateTime : Result := Widestring(DateTimeToStr(AsExtended));
  309. dtVariant : Result := Widestring(AsVariant);
  310. else raise Exception.Create('DataType not supported');
  311. end;
  312. except
  313. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to WideString error: %s',[e.message]);
  314. end;
  315. end;
  316. {$ENDIF}
  317. function TFlexValue.CastToBoolean: Boolean;
  318. begin
  319. try
  320. case fDataType of
  321. dtNull : Result := False;
  322. dtString : Result := StrToBool((fDataIntf as IValueString).Value);
  323. {$IFDEF MSWINDOWS}
  324. dtAnsiString : Result := StrToBool(string((fDataIntf as IValueAnsiString).Value));
  325. dtWideString : Result := StrToBool((fDataIntf as IValueWideString).Value);
  326. {$ENDIF}
  327. dtInteger,
  328. dtInt64 :
  329. begin
  330. if (fDataIntf as IValueInteger).Value = 1 then Result := True
  331. else if (fDataIntf as IValueInteger).Value = 0 then Result := False
  332. else raise Exception.Create('Integer value not in 0-1 range');
  333. end;
  334. dtBoolean : Result := Boolean((fDataIntf as IValueInteger).Value);
  335. dtVariant : Result := Boolean(AsVariant);
  336. else raise Exception.Create('DataType not supported');
  337. end;
  338. except
  339. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Boolean error: %s',[e.message]);
  340. end;
  341. end;
  342. function TFlexValue.CastToCardinal: Cardinal;
  343. begin
  344. Result := AsInt64;
  345. end;
  346. function TFlexValue.CastToClass: TClass;
  347. begin
  348. try
  349. case fDataType of
  350. dtNull : Result := nil;
  351. dtClass : Result := (fDataIntf as TValuePointer).Value;
  352. else raise Exception.Create('DataType not supported');
  353. end;
  354. except
  355. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TClass error: %s',[e.message]);
  356. end;
  357. end;
  358. function TFlexValue.CastToDateTime: TDateTime;
  359. begin
  360. try
  361. case fDataType of
  362. dtNull : Result := 0.0;
  363. dtString : Result := StrToDateTime((fDataIntf as IValueString).Value);
  364. {$IFDEF MSWINDOWS}
  365. dtAnsiString : Result := StrToDateTime(string((fDataIntf as IValueAnsiString).Value));
  366. dtWideString : Result := StrToDateTime((fDataIntf as IValueWideString).Value);
  367. {$ENDIF}
  368. dtInteger,
  369. dtInt64 : Result := FileDateToDateTime(AsInt64);
  370. dtDouble,
  371. dtExtended,
  372. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  373. dtVariant : Result := Extended(AsVariant);
  374. else raise Exception.Create('DataType not supported');
  375. end;
  376. except
  377. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  378. end;
  379. end;
  380. function TFlexValue.CastToExtended: Extended;
  381. begin
  382. try
  383. case fDataType of
  384. dtNull : Result := 0.0;
  385. dtString : Result := StrToFloat((fDataIntf as IValueString).Value);
  386. {$IFDEF MSWINDOWS}
  387. dtAnsiString : Result := StrToFloat(string((fDataIntf as IValueAnsiString).Value));
  388. dtWideString : Result := StrToFloat((fDataIntf as IValueWideString).Value);
  389. {$ENDIF}
  390. dtInteger,
  391. dtInt64 : Result := AsInt64;
  392. dtBoolean : Result := AsInt64;
  393. dtDouble,
  394. dtExtended,
  395. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  396. dtVariant : Result := Extended(AsVariant);
  397. else raise Exception.Create('DataType not supported');
  398. end;
  399. except
  400. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  401. end;
  402. end;
  403. function TFlexValue.CastToInt64: Int64;
  404. begin
  405. try
  406. case fDataType of
  407. dtNull : Result := 0;
  408. dtString : Result := StrToInt((fDataIntf as IValueString).Value);
  409. {$IFDEF MSWINDOWS}
  410. dtAnsiString : Result := StrToInt(string((fDataIntf as IValueAnsiString).Value));
  411. dtWideString : Result := StrToInt((fDataIntf as IValueWideString).Value);
  412. {$ENDIF}
  413. dtInteger,
  414. dtInt64 : Result := (fDataIntf as IValueInteger).Value;
  415. dtBoolean : Result := Integer(AsBoolean);
  416. dtDateTime : Result := DateTimeToFileDate((fDataIntf as IValueExtended).Value);
  417. dtVariant : Result := Integer(AsVariant);
  418. else raise Exception.Create('DataType not supported');
  419. end;
  420. except
  421. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Integer error: %s',[e.message]);
  422. end;
  423. end;
  424. function TFlexValue.CastToInteger: Integer;
  425. begin
  426. Result := AsInt64;
  427. end;
  428. function TFlexValue.CastToObject: TObject;
  429. begin
  430. try
  431. case fDataType of
  432. dtObject,
  433. dtOwnedObject : Result := (fDataIntf as IValueObject).Value;
  434. dtPointer : Result := TObject((fDataIntf as IValueObject).Value);
  435. dtNull : Result := nil;
  436. else raise Exception.Create('DataType not supported');
  437. end;
  438. except
  439. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Object error: %s',[e.message]);
  440. end;
  441. end;
  442. function TFlexValue.CastToPointer: Pointer;
  443. begin
  444. try
  445. case fDataType of
  446. dtObject,
  447. dtOwnedObject : Result := Pointer((fDataIntf as IValueObject).Value);
  448. dtPointer : Result := (fDataIntf as IValuePointer).Value;
  449. dtNull : Result := nil;
  450. else raise Exception.Create('DataType not supported');
  451. end;
  452. except
  453. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Pointer error: %s',[e.message]);
  454. end;
  455. end;
  456. function TFlexValue.CastToVariant: Variant;
  457. begin
  458. try
  459. case fDataType of
  460. dtNull : Result := Variants.Null;
  461. dtBoolean : Result := AsVariant;
  462. dtVariant : Result := (fDataIntf as IValueVariant).Value;
  463. else raise Exception.Create('DataType not supported');
  464. end;
  465. except
  466. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Variant error: %s',[e.message]);
  467. end;
  468. end;
  469. function TFlexValue.CastToInterface: Pointer;
  470. begin
  471. try
  472. case fDataType of
  473. dtNull : Result := nil;
  474. dtInterface : Result := IInterface(fDataIntf);
  475. else raise Exception.Create('DataType not supported');
  476. end;
  477. except
  478. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Interface error: %s',[e.message]);
  479. end;
  480. end;
  481. procedure TFlexValue.Clear;
  482. begin
  483. if Pointer(fDataIntf) <> nil then fDataIntf := nil;
  484. fDataType := dtNull;
  485. end;
  486. constructor TFlexValue.Create(const Value: TVarRec);
  487. begin
  488. case Value.VType of
  489. {$IFNDEF NEXTGEN}
  490. vtString : AsString := string(Value.VString^);
  491. vtChar : AsString := string(Value.VChar);
  492. {$ENDIF}
  493. {$IFDEF MSWINDOWS}
  494. vtAnsiString : AsAnsiString := AnsiString(Value.VAnsiString);
  495. vtWideString : AsWideString := WideString(Value.VWideString);
  496. {$ENDIF}
  497. {$IFDEF UNICODE}
  498. vtUnicodeString: AsString := string(Value.VUnicodeString);
  499. {$ENDIF UNICODE}
  500. vtInteger : AsInteger := Value.VInteger;
  501. vtInt64 : AsInt64 := Value.VInt64^;
  502. vtExtended : AsExtended := Value.VExtended^;
  503. vtBoolean : AsBoolean := Value.VBoolean;
  504. vtVariant : AsVariant := Value.VVariant^;
  505. vtInterface : AsInterface := IInterface(Value.VInterface);
  506. vtClass : AsClass := Value.VClass;
  507. vtObject : AsObject := Value.VObject;
  508. vtPointer : AsPointer := Value.VPointer;
  509. else raise Exception.Create('DataType not supported by TFlexValue');
  510. end;
  511. {$IFDEF FPC}
  512. fDataIntf._AddRef;
  513. {$ENDIF}
  514. end;
  515. {$IFNDEF FPC}
  516. class operator TFlexValue.Implicit(Value: TFlexValue): Boolean;
  517. begin
  518. Result := Value.AsBoolean;
  519. end;
  520. class operator TFlexValue.Implicit(const Value: TFlexValue): string;
  521. begin
  522. Result := Value.AsString;
  523. end;
  524. class operator TFlexValue.Implicit(Value: TFlexValue): TObject;
  525. begin
  526. Result := Value.AsObject;
  527. end;
  528. class operator TFlexValue.Implicit(Value: TFlexValue): Pointer;
  529. begin
  530. Result := Value.AsPointer;
  531. end;
  532. class operator TFlexValue.Implicit(Value: TFlexValue): TDateTime;
  533. begin
  534. Result := Value.AsDateTime;
  535. end;
  536. class operator TFlexValue.Implicit(Value: TFlexValue): TClass;
  537. begin
  538. Result := Value.AsClass;
  539. end;
  540. class operator TFlexValue.Implicit(Value: TFlexValue): Int64;
  541. begin
  542. Result := Value.AsInt64;
  543. end;
  544. class operator TFlexValue.Implicit(Value: TFlexValue): Integer;
  545. begin
  546. Result := Value.AsInteger;
  547. end;
  548. class operator TFlexValue.Implicit(Value: TFlexValue): Extended;
  549. begin
  550. Result := Value.AsExtended;
  551. end;
  552. {$ENDIF}
  553. function TFlexValue.IsBoolean: Boolean;
  554. begin
  555. Result := fDataType = dtBoolean;
  556. end;
  557. function TFlexValue.IsDateTime: Boolean;
  558. begin
  559. Result := fDataType = dtDateTime;
  560. end;
  561. function TFlexValue.IsFloating: Boolean;
  562. begin
  563. Result := fDataType in [dtDouble,dtExtended];
  564. end;
  565. function TFlexValue.IsInteger: Boolean;
  566. begin
  567. Result := fDataType in [dtInteger,dtInt64];
  568. end;
  569. function TFlexValue.IsInterface: Boolean;
  570. begin
  571. Result := fDataType = dtInterface;
  572. end;
  573. function TFlexValue.IsNullOrEmpty: Boolean;
  574. begin
  575. Result := fDataIntf = nil;
  576. end;
  577. function TFlexValue.IsObject: Boolean;
  578. begin
  579. Result := fDataType = dtObject;
  580. end;
  581. function TFlexValue.IsPointer: Boolean;
  582. begin
  583. Result := fDataType = dtPointer;
  584. end;
  585. function TFlexValue.IsString: Boolean;
  586. begin
  587. Result := fDataType in [dtString,dtAnsiString,dtWideString];
  588. end;
  589. function TFlexValue.IsVariant: Boolean;
  590. begin
  591. Result := fDataType = dtVariant;
  592. end;
  593. {$IFDEF MSWINDOWS}
  594. procedure TFlexValue.SetAsAnsiString(const Value: AnsiString);
  595. begin
  596. Clear;
  597. fDataIntf := TValueAnsiString.Create(Value);
  598. fDataType := TValueDataType.dtAnsiString;
  599. end;
  600. {$ENDIF}
  601. procedure TFlexValue.SetAsBoolean(const Value: Boolean);
  602. begin
  603. Clear;
  604. fDataIntf := TValueInteger.Create(Value.ToInteger);
  605. fDataType := TValueDataType.dtBoolean;
  606. end;
  607. procedure TFlexValue.SetAsCardinal(const Value: Cardinal);
  608. begin
  609. Clear;
  610. fDataIntf := TValueInteger.Create(Value);
  611. fDataType := TValueDataType.dtInt64;
  612. end;
  613. procedure TFlexValue.SetAsClass(const Value: TClass);
  614. begin
  615. Clear;
  616. fDataIntf := TValuePointer.Create(Value);
  617. fDataType := TValueDataType.dtClass;
  618. end;
  619. procedure TFlexValue.SetAsDateTime(const Value: TDateTime);
  620. begin
  621. Clear;
  622. fDataIntf := TValueExtended.Create(Value);
  623. fDataType := TValueDataType.dtDateTime;
  624. end;
  625. procedure TFlexValue.SetAsExtended(const Value: Extended);
  626. begin
  627. Clear;
  628. fDataIntf := TValueExtended.Create(Value);
  629. fDataType := TValueDataType.dtExtended;
  630. end;
  631. procedure TFlexValue.SetAsInt64(const Value: Int64);
  632. begin
  633. Clear;
  634. fDataIntf := TValueInteger.Create(Value);
  635. fDataType := TValueDataType.dtInt64;
  636. end;
  637. procedure TFlexValue.SetAsInteger(const Value: Integer);
  638. begin
  639. Clear;
  640. fDataIntf := TValueInteger.Create(Value);
  641. fDataType := TValueDataType.dtInteger;
  642. end;
  643. procedure TFlexValue.SetAsObject(const Value: TObject);
  644. begin
  645. Clear;
  646. fDataIntf := TValueObject.Create(Value);
  647. fDataType := TValueDataType.dtObject;
  648. end;
  649. procedure TFlexValue.SetAsPointer(const Value: Pointer);
  650. begin
  651. Clear;
  652. fDataIntf := TValuePointer.Create(Value);
  653. fDataType := TValueDataType.dtPointer;
  654. end;
  655. procedure TFlexValue.SetAsString(const Value: string);
  656. begin
  657. Clear;
  658. fDataIntf := TValueString.Create(Value);
  659. fDataType := TValueDataType.dtString;
  660. end;
  661. procedure TFlexValue.SetAsVariant(const Value: Variant);
  662. begin
  663. Clear;
  664. fDataIntf := TValueVariant.Create(Value);
  665. fDataType := TValueDataType.dtVariant;
  666. end;
  667. {$IFDEF MSWINDOWS}
  668. procedure TFlexValue.SetAsWideString(const Value: WideString);
  669. begin
  670. Clear;
  671. fDataIntf := TValueWideString.Create(Value);
  672. fDataType := TValueDataType.dtWideString;
  673. end;
  674. {$ENDIF}
  675. procedure TFlexValue._AddRef;
  676. begin
  677. if Assigned(fDataIntf) then fDataIntf._AddRef;
  678. end;
  679. procedure TFlexValue._Release;
  680. begin
  681. if Assigned(fDataIntf) then fDataIntf._Release;
  682. end;
  683. { TValueStringData }
  684. constructor TValueString.Create(const Value: string);
  685. begin
  686. fData := Value;
  687. end;
  688. function TValueString.GetValue: string;
  689. begin
  690. Result := fData;
  691. end;
  692. procedure TValueString.SetValue(const Value: string);
  693. begin
  694. fData := Value;
  695. end;
  696. { TValueVariantData }
  697. constructor TValueVariant.Create(const Value: Variant);
  698. begin
  699. fData := Value;
  700. end;
  701. function TValueVariant.GetValue: Variant;
  702. begin
  703. Result := fData;
  704. end;
  705. procedure TValueVariant.SetValue(const Value: Variant);
  706. begin
  707. fData := Value;
  708. end;
  709. { TValueAnsiStringData }
  710. {$IFDEF MSWINDOWS}
  711. constructor TValueAnsiString.Create(const Value: AnsiString);
  712. begin
  713. fData := Value;
  714. end;
  715. function TValueAnsiString.GetValue: AnsiString;
  716. begin
  717. Result := fData;
  718. end;
  719. procedure TValueAnsiString.SetValue(const Value: AnsiString);
  720. begin
  721. fData := Value;
  722. end;
  723. { TValueWideStringData }
  724. constructor TValueWideString.Create(const Value: WideString);
  725. begin
  726. fData := Value;
  727. end;
  728. function TValueWideString.GetValue: WideString;
  729. begin
  730. Result := fData;
  731. end;
  732. procedure TValueWideString.SetValue(const Value: WideString);
  733. begin
  734. fData := Value;
  735. end;
  736. {$ENDIF}
  737. { TValueInteger }
  738. constructor TValueInteger.Create(const Value: Int64);
  739. begin
  740. fData := Value;
  741. end;
  742. function TValueInteger.GetValue: Int64;
  743. begin
  744. Result := fData;
  745. end;
  746. procedure TValueInteger.SetValue(const Value: Int64);
  747. begin
  748. fData := Value;
  749. end;
  750. { TValuePointer }
  751. constructor TValuePointer.Create(const Value: Pointer);
  752. begin
  753. fData := Value;
  754. end;
  755. function TValuePointer.GetValue: Pointer;
  756. begin
  757. Result := fData;
  758. end;
  759. procedure TValuePointer.SetValue(const Value: Pointer);
  760. begin
  761. fData := Value;
  762. end;
  763. { TValueExtended }
  764. constructor TValueExtended.Create(const Value: Extended);
  765. begin
  766. fData := Value;
  767. end;
  768. function TValueExtended.GetValue: Extended;
  769. begin
  770. Result := fData;
  771. end;
  772. procedure TValueExtended.SetValue(const Value: Extended);
  773. begin
  774. fData := Value;
  775. end;
  776. { TValueObject }
  777. constructor TValueObject.Create(const Value: TObject);
  778. begin
  779. fData := Value;
  780. end;
  781. function TValueObject.GetValue: TObject;
  782. begin
  783. Result := fData;
  784. end;
  785. procedure TValueObject.SetValue(const Value: TObject);
  786. begin
  787. fData := Value;
  788. end;
  789. end.