Quick.Value.pas 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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.5
  7. Created : 07/01/2019
  8. Modified : 03/04/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: IInterface;
  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. procedure SetAsInterface(const Value: IInterface);
  202. public
  203. constructor Create(const Value: TVarRec);
  204. property DataType : TValueDataType read fDataType;
  205. property AsString : string read CastToString write SetAsString;
  206. {$IFDEF MSWINDOWS}
  207. property AsAnsiString : AnsiString read CastToAnsiString write SetAsAnsiString;
  208. property AsWideString : WideString read CastToWideString write SetAsWideString;
  209. {$ENDIF}
  210. property AsInteger : Integer read CastToInteger write SetAsInteger;
  211. property AsInt64 : Int64 read CastToInt64 write SetAsInt64;
  212. property AsExtended : Extended read CastToExtended write SetAsExtended;
  213. property AsBoolean : Boolean read CastToBoolean write SetAsBoolean;
  214. property AsPointer : Pointer read CastToPointer write SetAsPointer;
  215. property AsClass : TClass read CastToClass write SetAsClass;
  216. property AsInterface : IInterface read CastToInterface write SetAsInterface;
  217. property AsObject : TObject read CastToObject write SetAsObject;
  218. property AsVariant : Variant read CastToVariant write SetAsVariant;
  219. property AsCardinal : Cardinal read CastToCardinal write SetAsCardinal;
  220. property AsDateTime : TDateTime read CastToDateTime write SetAsDateTime;
  221. //function AsType<T> : T;
  222. function IsNullOrEmpty : Boolean; inline;
  223. function IsString : Boolean; inline;
  224. function IsInteger : Boolean; inline;
  225. function IsFloating : Boolean; inline;
  226. function IsDateTime : Boolean; inline;
  227. function IsBoolean : Boolean; inline;
  228. function IsInterface : Boolean; inline;
  229. function IsObject : Boolean; inline;
  230. function IsPointer : Boolean; inline;
  231. function IsVariant : Boolean; inline;
  232. procedure Clear; inline;
  233. procedure _AddRef; inline;
  234. procedure _Release; inline;
  235. {$IFNDEF FPCS}
  236. class operator Implicit(const Value : TFlexValue) : string;
  237. class operator Implicit(Value : TFlexValue) : Integer;
  238. class operator Implicit(Value : TFlexValue) : Int64;
  239. class operator Implicit(Value : TFlexValue) : Extended;
  240. class operator Implicit(Value : TFlexValue) : TDateTime;
  241. class operator Implicit(Value : TFlexValue) : Boolean;
  242. class operator Implicit(Value : TFlexValue) : TClass;
  243. class operator Implicit(Value : TFlexValue) : TObject;
  244. class operator Implicit(Value : TFlexValue) : Pointer;
  245. class operator Implicit(Value : TFlexValue) : Variant;
  246. class operator Implicit(const Value : string) : TFlexValue;
  247. class operator Implicit(Value : Integer) : TFlexValue;
  248. class operator Implicit(Value : Int64) : TFlexValue;
  249. class operator Implicit(Value : Extended) : TFlexValue;
  250. class operator Implicit(Value : TDateTime) : TFlexValue;
  251. class operator Implicit(Value : Boolean) : TFlexValue;
  252. class operator Implicit(Value : TClass) : TFlexValue;
  253. class operator Implicit(Value : TObject) : TFlexValue;
  254. class operator Implicit(Value : Pointer) : TFlexValue;
  255. class operator Implicit(Value : Variant) : TFlexValue;
  256. {$ENDIF}
  257. end;
  258. TFlexPair = record
  259. Name : string;
  260. Value : TFlexValue;
  261. constructor Create(const aName : string; aValue : TFlexValue);
  262. end;
  263. implementation
  264. function TFlexValue.CastToString: string;
  265. begin
  266. try
  267. case fDataType of
  268. dtNull : Result := '';
  269. dtString : Result := (fDataIntf as IValueString).Value;
  270. {$IFDEF MSWINDOWS}
  271. dtAnsiString : Result := string((fDataIntf as IValueAnsiString).Value);
  272. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  273. {$ENDIF}
  274. dtInteger,
  275. dtInt64 : Result := IntToStr(AsInt64);
  276. dtBoolean : Result := BoolToStr(AsBoolean,True);
  277. dtDouble,
  278. dtExtended : Result := FloatToStr(AsExtended);
  279. dtDateTime : Result := DateTimeToStr(AsExtended);
  280. dtVariant : Result := string(AsVariant);
  281. dtClass : Result := AsClass.ClassName;
  282. else raise Exception.Create('DataType not supported');
  283. end;
  284. except
  285. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to String error: %s',[e.message]);
  286. end;
  287. end;
  288. {$IFDEF MSWINDOWS}
  289. function TFlexValue.CastToAnsiString: AnsiString;
  290. begin
  291. try
  292. case fDataType of
  293. dtNull : Result := '';
  294. dtString : Result := AnsiString((fDataIntf as IValueString).Value);
  295. dtAnsiString : Result := (fDataIntf as IValueAnsiString).Value;
  296. dtWideString : Result := AnsiString((fDataIntf as IValueWideString).Value);
  297. dtInteger,
  298. dtInt64 : Result := AnsiString(IntToStr(AsInt64));
  299. dtBoolean : Result := AnsiString(BoolToStr(AsBoolean,True));
  300. dtDouble,
  301. dtExtended : Result := AnsiString(FloatToStr(AsExtended));
  302. dtDateTime : Result := AnsiString(DateTimeToStr(AsExtended));
  303. dtVariant : Result := AnsiString(AsVariant);
  304. else raise Exception.Create('DataType not supported');
  305. end;
  306. except
  307. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to AnsiString error: %s',[e.message]);
  308. end;
  309. end;
  310. function TFlexValue.CastToWideString: WideString;
  311. begin
  312. try
  313. case fDataType of
  314. dtNull : Result := '';
  315. dtString : Result := Widestring((fDataIntf as IValueString).Value);
  316. {$IFDEF MSWINDOWS}
  317. dtAnsiString : Result := Widestring((fDataIntf as IValueAnsiString).Value);
  318. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  319. {$ENDIF}
  320. dtInteger,
  321. dtInt64 : Result := Widestring(IntToStr(AsInt64));
  322. dtBoolean : Result := Widestring(BoolToStr(AsBoolean,True));
  323. dtDouble,
  324. dtExtended : Result := Widestring(FloatToStr(AsExtended));
  325. dtDateTime : Result := Widestring(DateTimeToStr(AsExtended));
  326. dtVariant : Result := Widestring(AsVariant);
  327. else raise Exception.Create('DataType not supported');
  328. end;
  329. except
  330. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to WideString error: %s',[e.message]);
  331. end;
  332. end;
  333. {$ENDIF}
  334. function TFlexValue.CastToBoolean: Boolean;
  335. begin
  336. try
  337. case fDataType of
  338. dtNull : Result := False;
  339. dtString : Result := StrToBool((fDataIntf as IValueString).Value);
  340. {$IFDEF MSWINDOWS}
  341. dtAnsiString : Result := StrToBool(string((fDataIntf as IValueAnsiString).Value));
  342. dtWideString : Result := StrToBool((fDataIntf as IValueWideString).Value);
  343. {$ENDIF}
  344. dtInteger,
  345. dtInt64 :
  346. begin
  347. if (fDataIntf as IValueInteger).Value = 1 then Result := True
  348. else if (fDataIntf as IValueInteger).Value = 0 then Result := False
  349. else raise Exception.Create('Integer value not in 0-1 range');
  350. end;
  351. dtBoolean : Result := Boolean((fDataIntf as IValueInteger).Value);
  352. dtVariant : Result := Boolean(AsVariant);
  353. else raise Exception.Create('DataType not supported');
  354. end;
  355. except
  356. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Boolean error: %s',[e.message]);
  357. end;
  358. end;
  359. function TFlexValue.CastToCardinal: Cardinal;
  360. begin
  361. Result := AsInt64;
  362. end;
  363. function TFlexValue.CastToClass: TClass;
  364. begin
  365. try
  366. case fDataType of
  367. dtNull : Result := nil;
  368. dtClass : Result := (fDataIntf as TValuePointer).Value;
  369. else raise Exception.Create('DataType not supported');
  370. end;
  371. except
  372. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TClass error: %s',[e.message]);
  373. end;
  374. end;
  375. function TFlexValue.CastToDateTime: TDateTime;
  376. begin
  377. try
  378. case fDataType of
  379. dtNull : Result := 0.0;
  380. dtString : Result := StrToDateTime((fDataIntf as IValueString).Value);
  381. {$IFDEF MSWINDOWS}
  382. dtAnsiString : Result := StrToDateTime(string((fDataIntf as IValueAnsiString).Value));
  383. dtWideString : Result := StrToDateTime((fDataIntf as IValueWideString).Value);
  384. {$ENDIF}
  385. dtInteger,
  386. dtInt64 : Result := FileDateToDateTime(AsInt64);
  387. dtDouble,
  388. dtExtended,
  389. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  390. dtVariant : Result := Extended(AsVariant);
  391. else raise Exception.Create('DataType not supported');
  392. end;
  393. except
  394. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  395. end;
  396. end;
  397. function TFlexValue.CastToExtended: Extended;
  398. begin
  399. try
  400. case fDataType of
  401. dtNull : Result := 0.0;
  402. dtString : Result := StrToFloat((fDataIntf as IValueString).Value);
  403. {$IFDEF MSWINDOWS}
  404. dtAnsiString : Result := StrToFloat(string((fDataIntf as IValueAnsiString).Value));
  405. dtWideString : Result := StrToFloat((fDataIntf as IValueWideString).Value);
  406. {$ENDIF}
  407. dtInteger,
  408. dtInt64 : Result := AsInt64;
  409. dtBoolean : Result := AsInt64;
  410. dtDouble,
  411. dtExtended,
  412. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  413. dtVariant : Result := Extended(AsVariant);
  414. else raise Exception.Create('DataType not supported');
  415. end;
  416. except
  417. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  418. end;
  419. end;
  420. function TFlexValue.CastToInt64: Int64;
  421. begin
  422. try
  423. case fDataType of
  424. dtNull : Result := 0;
  425. dtString : Result := StrToInt((fDataIntf as IValueString).Value);
  426. {$IFDEF MSWINDOWS}
  427. dtAnsiString : Result := StrToInt(string((fDataIntf as IValueAnsiString).Value));
  428. dtWideString : Result := StrToInt((fDataIntf as IValueWideString).Value);
  429. {$ENDIF}
  430. dtInteger,
  431. dtInt64 : Result := (fDataIntf as IValueInteger).Value;
  432. dtBoolean : Result := Integer(AsBoolean);
  433. dtDateTime : Result := DateTimeToFileDate((fDataIntf as IValueExtended).Value);
  434. dtVariant : Result := Integer(AsVariant);
  435. else raise Exception.Create('DataType not supported');
  436. end;
  437. except
  438. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Integer error: %s',[e.message]);
  439. end;
  440. end;
  441. function TFlexValue.CastToInteger: Integer;
  442. begin
  443. Result := AsInt64;
  444. end;
  445. function TFlexValue.CastToObject: TObject;
  446. begin
  447. try
  448. case fDataType of
  449. dtObject,
  450. dtOwnedObject : Result := (fDataIntf as IValueObject).Value;
  451. {$IFNDEF FPC}
  452. dtPointer : Result := TObject((fDataIntf as IValueObject).Value);
  453. {$ELSE}
  454. dtPointer : Result := TObject((fDataIntf as IValuePointer).Value);
  455. {$ENDIF}
  456. dtNull : Result := nil;
  457. else raise Exception.Create('DataType not supported');
  458. end;
  459. except
  460. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Object error: %s',[e.message]);
  461. end;
  462. end;
  463. function TFlexValue.CastToPointer: Pointer;
  464. begin
  465. try
  466. case fDataType of
  467. dtObject,
  468. dtOwnedObject : Result := Pointer((fDataIntf as IValueObject).Value);
  469. dtPointer : Result := (fDataIntf as IValuePointer).Value;
  470. dtNull : Result := nil;
  471. else raise Exception.Create('DataType not supported');
  472. end;
  473. except
  474. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Pointer error: %s',[e.message]);
  475. end;
  476. end;
  477. function TFlexValue.CastToVariant: Variant;
  478. begin
  479. try
  480. case fDataType of
  481. dtNull : Result := Variants.Null;
  482. dtBoolean : Result := AsVariant;
  483. dtVariant : Result := (fDataIntf as IValueVariant).Value;
  484. else raise Exception.Create('DataType not supported');
  485. end;
  486. except
  487. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Variant error: %s',[e.message]);
  488. end;
  489. end;
  490. function TFlexValue.CastToInterface: IInterface;
  491. begin
  492. try
  493. case fDataType of
  494. dtNull : Result := nil;
  495. dtInterface : Result := fDataIntf;
  496. dtPointer : Result := IInterface(fDataIntf);
  497. else raise Exception.Create('DataType not supported');
  498. end;
  499. except
  500. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Interface error: %s',[e.message]);
  501. end;
  502. end;
  503. procedure TFlexValue.Clear;
  504. begin
  505. if Pointer(fDataIntf) <> nil then fDataIntf := nil;
  506. fDataType := dtNull;
  507. end;
  508. constructor TFlexValue.Create(const Value: TVarRec);
  509. begin
  510. case Value.VType of
  511. {$IFNDEF NEXTGEN}
  512. vtString : AsString := string(Value.VString^);
  513. vtChar : AsString := string(Value.VChar);
  514. {$ENDIF}
  515. {$IFDEF MSWINDOWS}
  516. vtAnsiString : AsAnsiString := AnsiString(Value.VAnsiString);
  517. vtWideString : AsWideString := WideString(Value.VWideString);
  518. {$ENDIF}
  519. {$IFDEF UNICODE}
  520. vtUnicodeString: AsString := string(Value.VUnicodeString);
  521. {$ENDIF UNICODE}
  522. vtInteger : AsInteger := Value.VInteger;
  523. vtInt64 : AsInt64 := Value.VInt64^;
  524. vtExtended : AsExtended := Value.VExtended^;
  525. vtBoolean : AsBoolean := Value.VBoolean;
  526. vtVariant : AsVariant := Value.VVariant^;
  527. vtInterface : AsInterface := IInterface(Value.VInterface);
  528. vtClass : AsClass := Value.VClass;
  529. vtObject : AsObject := Value.VObject;
  530. vtPointer : AsPointer := Value.VPointer;
  531. else raise Exception.Create('DataType not supported by TFlexValue');
  532. end;
  533. {$IFDEF FPC}
  534. fDataIntf._AddRef;
  535. {$ENDIF}
  536. end;
  537. {$IFNDEF FPCS}
  538. class operator TFlexValue.Implicit(Value: TFlexValue): Boolean;
  539. begin
  540. Result := Value.AsBoolean;
  541. end;
  542. class operator TFlexValue.Implicit(const Value: TFlexValue): string;
  543. begin
  544. Result := Value.AsString;
  545. end;
  546. class operator TFlexValue.Implicit(Value: TFlexValue): TObject;
  547. begin
  548. Result := Value.AsObject;
  549. end;
  550. class operator TFlexValue.Implicit(Value: TFlexValue): Pointer;
  551. begin
  552. Result := Value.AsPointer;
  553. end;
  554. class operator TFlexValue.Implicit(Value: TFlexValue): TDateTime;
  555. begin
  556. Result := Value.AsDateTime;
  557. end;
  558. class operator TFlexValue.Implicit(Value: TFlexValue): TClass;
  559. begin
  560. Result := Value.AsClass;
  561. end;
  562. class operator TFlexValue.Implicit(Value: TFlexValue): Int64;
  563. begin
  564. Result := Value.AsInt64;
  565. end;
  566. class operator TFlexValue.Implicit(Value: TFlexValue): Integer;
  567. begin
  568. Result := Value.AsInteger;
  569. end;
  570. class operator TFlexValue.Implicit(Value: TFlexValue): Extended;
  571. begin
  572. Result := Value.AsExtended;
  573. end;
  574. class operator TFlexValue.Implicit(Value: TFlexValue): Variant;
  575. begin
  576. Result := Value.AsVariant;
  577. end;
  578. class operator TFlexValue.Implicit(Value: Variant): TFlexValue;
  579. begin
  580. Result.AsVariant := Value;
  581. end;
  582. class operator TFlexValue.Implicit(const Value : string) : TFlexValue;
  583. begin
  584. Result.AsString := Value;
  585. end;
  586. class operator TFlexValue.Implicit(Value : Integer) : TFlexValue;
  587. begin
  588. Result.AsInteger := Value;
  589. end;
  590. class operator TFlexValue.Implicit(Value : Int64) : TFlexValue;
  591. begin
  592. Result.AsInt64 := Value;
  593. end;
  594. class operator TFlexValue.Implicit(Value : Extended) : TFlexValue;
  595. begin
  596. Result.AsExtended := Value;
  597. end;
  598. class operator TFlexValue.Implicit(Value : TDateTime) : TFlexValue;
  599. begin
  600. Result.AsDateTime := Value;
  601. end;
  602. class operator TFlexValue.Implicit(Value : Boolean) : TFlexValue;
  603. begin
  604. Result.AsBoolean := Value;
  605. end;
  606. class operator TFlexValue.Implicit(Value : TClass) : TFlexValue;
  607. begin
  608. Result.AsClass := Value;
  609. end;
  610. class operator TFlexValue.Implicit(Value : TObject) : TFlexValue;
  611. begin
  612. Result.AsObject := Value;
  613. end;
  614. class operator TFlexValue.Implicit(Value : Pointer) : TFlexValue;
  615. begin
  616. Result.AsPointer := Value;
  617. end;
  618. {$ENDIF}
  619. function TFlexValue.IsBoolean: Boolean;
  620. begin
  621. Result := fDataType = dtBoolean;
  622. end;
  623. function TFlexValue.IsDateTime: Boolean;
  624. begin
  625. Result := fDataType = dtDateTime;
  626. end;
  627. function TFlexValue.IsFloating: Boolean;
  628. begin
  629. Result := fDataType in [dtDouble,dtExtended];
  630. end;
  631. function TFlexValue.IsInteger: Boolean;
  632. begin
  633. Result := fDataType in [dtInteger,dtInt64];
  634. end;
  635. function TFlexValue.IsInterface: Boolean;
  636. begin
  637. Result := fDataType = dtInterface;
  638. end;
  639. function TFlexValue.IsNullOrEmpty: Boolean;
  640. begin
  641. Result := fDataIntf = nil;
  642. end;
  643. function TFlexValue.IsObject: Boolean;
  644. begin
  645. Result := fDataType = dtObject;
  646. end;
  647. function TFlexValue.IsPointer: Boolean;
  648. begin
  649. Result := fDataType = dtPointer;
  650. end;
  651. function TFlexValue.IsString: Boolean;
  652. begin
  653. Result := fDataType in [dtString,dtAnsiString,dtWideString];
  654. end;
  655. function TFlexValue.IsVariant: Boolean;
  656. begin
  657. Result := fDataType = dtVariant;
  658. end;
  659. {$IFDEF MSWINDOWS}
  660. procedure TFlexValue.SetAsAnsiString(const Value: AnsiString);
  661. begin
  662. Clear;
  663. fDataIntf := TValueAnsiString.Create(Value);
  664. fDataType := TValueDataType.dtAnsiString;
  665. end;
  666. {$ENDIF}
  667. procedure TFlexValue.SetAsBoolean(const Value: Boolean);
  668. begin
  669. Clear;
  670. fDataIntf := TValueInteger.Create(Value.ToInteger);
  671. fDataType := TValueDataType.dtBoolean;
  672. end;
  673. procedure TFlexValue.SetAsCardinal(const Value: Cardinal);
  674. begin
  675. Clear;
  676. fDataIntf := TValueInteger.Create(Value);
  677. fDataType := TValueDataType.dtInt64;
  678. end;
  679. procedure TFlexValue.SetAsClass(const Value: TClass);
  680. begin
  681. Clear;
  682. fDataIntf := TValuePointer.Create(Value);
  683. fDataType := TValueDataType.dtClass;
  684. end;
  685. procedure TFlexValue.SetAsDateTime(const Value: TDateTime);
  686. begin
  687. Clear;
  688. fDataIntf := TValueExtended.Create(Value);
  689. fDataType := TValueDataType.dtDateTime;
  690. end;
  691. procedure TFlexValue.SetAsExtended(const Value: Extended);
  692. begin
  693. Clear;
  694. fDataIntf := TValueExtended.Create(Value);
  695. fDataType := TValueDataType.dtExtended;
  696. end;
  697. procedure TFlexValue.SetAsInt64(const Value: Int64);
  698. begin
  699. Clear;
  700. fDataIntf := TValueInteger.Create(Value);
  701. fDataType := TValueDataType.dtInt64;
  702. end;
  703. procedure TFlexValue.SetAsInteger(const Value: Integer);
  704. begin
  705. Clear;
  706. fDataIntf := TValueInteger.Create(Value);
  707. fDataType := TValueDataType.dtInteger;
  708. end;
  709. procedure TFlexValue.SetAsInterface(const Value: IInterface);
  710. begin
  711. {$IFNDEF FPC}
  712. fDataIntf := Value;
  713. {$ELSE}
  714. fDataIntf := Pointer(Value);
  715. {$ENDIF}
  716. fDataType := TValueDataType.dtInterface;
  717. end;
  718. procedure TFlexValue.SetAsObject(const Value: TObject);
  719. begin
  720. Clear;
  721. fDataIntf := TValueObject.Create(Value);
  722. fDataType := TValueDataType.dtObject;
  723. end;
  724. procedure TFlexValue.SetAsPointer(const Value: Pointer);
  725. begin
  726. Clear;
  727. fDataIntf := TValuePointer.Create(Value);
  728. fDataType := TValueDataType.dtPointer;
  729. end;
  730. procedure TFlexValue.SetAsString(const Value: string);
  731. begin
  732. Clear;
  733. fDataIntf := TValueString.Create(Value);
  734. fDataType := TValueDataType.dtString;
  735. end;
  736. function TryVarAsType(aValue : Variant; aVarType : Word) : Boolean;
  737. begin
  738. try
  739. VarAsType(aValue,aVarType);
  740. Result := True;
  741. except
  742. Result := False;
  743. end;
  744. end;
  745. procedure TFlexValue.SetAsVariant(const Value: Variant);
  746. begin
  747. Clear;
  748. case VarType(Value) and varTypeMask of
  749. varEmpty,
  750. varNull : Clear;
  751. varSmallInt,
  752. varInteger,
  753. varByte,
  754. varWord,
  755. varLongWord,
  756. varInt64 : SetAsInt64(Value);
  757. varSingle,
  758. varDouble,
  759. varCurrency : SetAsExtended(Value);
  760. varDate : SetAsDateTime(Value);
  761. varOleStr : SetAsString(Value);
  762. varDispatch : begin
  763. if TryVarAsType(Value,varInt64) then SetAsInt64(Value)
  764. else if TryVarAsType(Value,varDouble) then SetAsExtended(Value)
  765. else if TryVarAsType(Value,varBoolean) then SetAsBoolean(Value)
  766. else if TryVarAsType(Value,varString) then SetAsString(Value)
  767. else
  768. begin
  769. fDataIntf := TValueVariant.Create(Value);
  770. fDataType := TValueDataType.dtVariant;
  771. end;
  772. end;
  773. //varError : typeString := 'varError';
  774. varBoolean : SetAsBoolean(Value);
  775. //varStrArg : typeString := 'varStrArg';
  776. varString : SetAsString(Value);
  777. //varAny : typeString := 'varAny';
  778. //varTypeMask : typeString := 'varTypeMask';
  779. else
  780. begin
  781. fDataIntf := TValueVariant.Create(Value);
  782. fDataType := TValueDataType.dtVariant;
  783. end;
  784. end;
  785. end;
  786. {$IFDEF MSWINDOWS}
  787. procedure TFlexValue.SetAsWideString(const Value: WideString);
  788. begin
  789. Clear;
  790. fDataIntf := TValueWideString.Create(Value);
  791. fDataType := TValueDataType.dtWideString;
  792. end;
  793. {$ENDIF}
  794. procedure TFlexValue._AddRef;
  795. begin
  796. if Assigned(fDataIntf) then fDataIntf._AddRef;
  797. end;
  798. procedure TFlexValue._Release;
  799. begin
  800. if Assigned(fDataIntf) then fDataIntf._Release;
  801. end;
  802. { TValueStringData }
  803. constructor TValueString.Create(const Value: string);
  804. begin
  805. fData := Value;
  806. end;
  807. function TValueString.GetValue: string;
  808. begin
  809. Result := fData;
  810. end;
  811. procedure TValueString.SetValue(const Value: string);
  812. begin
  813. fData := Value;
  814. end;
  815. { TValueVariantData }
  816. constructor TValueVariant.Create(const Value: Variant);
  817. begin
  818. fData := Value;
  819. end;
  820. function TValueVariant.GetValue: Variant;
  821. begin
  822. Result := fData;
  823. end;
  824. procedure TValueVariant.SetValue(const Value: Variant);
  825. begin
  826. fData := Value;
  827. end;
  828. { TValueAnsiStringData }
  829. {$IFDEF MSWINDOWS}
  830. constructor TValueAnsiString.Create(const Value: AnsiString);
  831. begin
  832. fData := Value;
  833. end;
  834. function TValueAnsiString.GetValue: AnsiString;
  835. begin
  836. Result := fData;
  837. end;
  838. procedure TValueAnsiString.SetValue(const Value: AnsiString);
  839. begin
  840. fData := Value;
  841. end;
  842. { TValueWideStringData }
  843. constructor TValueWideString.Create(const Value: WideString);
  844. begin
  845. fData := Value;
  846. end;
  847. function TValueWideString.GetValue: WideString;
  848. begin
  849. Result := fData;
  850. end;
  851. procedure TValueWideString.SetValue(const Value: WideString);
  852. begin
  853. fData := Value;
  854. end;
  855. {$ENDIF}
  856. { TValueInteger }
  857. constructor TValueInteger.Create(const Value: Int64);
  858. begin
  859. fData := Value;
  860. end;
  861. function TValueInteger.GetValue: Int64;
  862. begin
  863. Result := fData;
  864. end;
  865. procedure TValueInteger.SetValue(const Value: Int64);
  866. begin
  867. fData := Value;
  868. end;
  869. { TValuePointer }
  870. constructor TValuePointer.Create(const Value: Pointer);
  871. begin
  872. fData := Value;
  873. end;
  874. function TValuePointer.GetValue: Pointer;
  875. begin
  876. Result := fData;
  877. end;
  878. procedure TValuePointer.SetValue(const Value: Pointer);
  879. begin
  880. fData := Value;
  881. end;
  882. { TValueExtended }
  883. constructor TValueExtended.Create(const Value: Extended);
  884. begin
  885. fData := Value;
  886. end;
  887. function TValueExtended.GetValue: Extended;
  888. begin
  889. Result := fData;
  890. end;
  891. procedure TValueExtended.SetValue(const Value: Extended);
  892. begin
  893. fData := Value;
  894. end;
  895. { TValueObject }
  896. constructor TValueObject.Create(const Value: TObject);
  897. begin
  898. fData := Value;
  899. end;
  900. function TValueObject.GetValue: TObject;
  901. begin
  902. Result := fData;
  903. end;
  904. procedure TValueObject.SetValue(const Value: TObject);
  905. begin
  906. fData := Value;
  907. end;
  908. { TFlexPair }
  909. constructor TFlexPair.Create(const aName: string; aValue: TFlexValue);
  910. begin
  911. Name := aName;
  912. Value := aValue;
  913. end;
  914. end.