Quick.Value.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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 : 14/03/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 FPC}
  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. {$ENDIF}
  246. end;
  247. implementation
  248. function TFlexValue.CastToString: string;
  249. begin
  250. try
  251. case fDataType of
  252. dtNull : Result := '';
  253. dtString : Result := (fDataIntf as IValueString).Value;
  254. {$IFDEF MSWINDOWS}
  255. dtAnsiString : Result := string((fDataIntf as IValueAnsiString).Value);
  256. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  257. {$ENDIF}
  258. dtInteger,
  259. dtInt64 : Result := IntToStr(AsInt64);
  260. dtBoolean : Result := BoolToStr(AsBoolean,True);
  261. dtDouble,
  262. dtExtended : Result := FloatToStr(AsExtended);
  263. dtDateTime : Result := DateTimeToStr(AsExtended);
  264. dtVariant : Result := string(AsVariant);
  265. dtClass : Result := AsClass.ClassName;
  266. else raise Exception.Create('DataType not supported');
  267. end;
  268. except
  269. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to String error: %s',[e.message]);
  270. end;
  271. end;
  272. {$IFDEF MSWINDOWS}
  273. function TFlexValue.CastToAnsiString: AnsiString;
  274. begin
  275. try
  276. case fDataType of
  277. dtNull : Result := '';
  278. dtString : Result := AnsiString((fDataIntf as IValueString).Value);
  279. dtAnsiString : Result := (fDataIntf as IValueAnsiString).Value;
  280. dtWideString : Result := AnsiString((fDataIntf as IValueWideString).Value);
  281. dtInteger,
  282. dtInt64 : Result := AnsiString(IntToStr(AsInt64));
  283. dtBoolean : Result := AnsiString(BoolToStr(AsBoolean,True));
  284. dtDouble,
  285. dtExtended : Result := AnsiString(FloatToStr(AsExtended));
  286. dtDateTime : Result := AnsiString(DateTimeToStr(AsExtended));
  287. dtVariant : Result := AnsiString(AsVariant);
  288. else raise Exception.Create('DataType not supported');
  289. end;
  290. except
  291. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to AnsiString error: %s',[e.message]);
  292. end;
  293. end;
  294. function TFlexValue.CastToWideString: WideString;
  295. begin
  296. try
  297. case fDataType of
  298. dtNull : Result := '';
  299. dtString : Result := Widestring((fDataIntf as IValueString).Value);
  300. {$IFDEF MSWINDOWS}
  301. dtAnsiString : Result := Widestring((fDataIntf as IValueAnsiString).Value);
  302. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  303. {$ENDIF}
  304. dtInteger,
  305. dtInt64 : Result := Widestring(IntToStr(AsInt64));
  306. dtBoolean : Result := Widestring(BoolToStr(AsBoolean,True));
  307. dtDouble,
  308. dtExtended : Result := Widestring(FloatToStr(AsExtended));
  309. dtDateTime : Result := Widestring(DateTimeToStr(AsExtended));
  310. dtVariant : Result := Widestring(AsVariant);
  311. else raise Exception.Create('DataType not supported');
  312. end;
  313. except
  314. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to WideString error: %s',[e.message]);
  315. end;
  316. end;
  317. {$ENDIF}
  318. function TFlexValue.CastToBoolean: Boolean;
  319. begin
  320. try
  321. case fDataType of
  322. dtNull : Result := False;
  323. dtString : Result := StrToBool((fDataIntf as IValueString).Value);
  324. {$IFDEF MSWINDOWS}
  325. dtAnsiString : Result := StrToBool(string((fDataIntf as IValueAnsiString).Value));
  326. dtWideString : Result := StrToBool((fDataIntf as IValueWideString).Value);
  327. {$ENDIF}
  328. dtInteger,
  329. dtInt64 :
  330. begin
  331. if (fDataIntf as IValueInteger).Value = 1 then Result := True
  332. else if (fDataIntf as IValueInteger).Value = 0 then Result := False
  333. else raise Exception.Create('Integer value not in 0-1 range');
  334. end;
  335. dtBoolean : Result := Boolean((fDataIntf as IValueInteger).Value);
  336. dtVariant : Result := Boolean(AsVariant);
  337. else raise Exception.Create('DataType not supported');
  338. end;
  339. except
  340. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Boolean error: %s',[e.message]);
  341. end;
  342. end;
  343. function TFlexValue.CastToCardinal: Cardinal;
  344. begin
  345. Result := AsInt64;
  346. end;
  347. function TFlexValue.CastToClass: TClass;
  348. begin
  349. try
  350. case fDataType of
  351. dtNull : Result := nil;
  352. dtClass : Result := (fDataIntf as TValuePointer).Value;
  353. else raise Exception.Create('DataType not supported');
  354. end;
  355. except
  356. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TClass error: %s',[e.message]);
  357. end;
  358. end;
  359. function TFlexValue.CastToDateTime: TDateTime;
  360. begin
  361. try
  362. case fDataType of
  363. dtNull : Result := 0.0;
  364. dtString : Result := StrToDateTime((fDataIntf as IValueString).Value);
  365. {$IFDEF MSWINDOWS}
  366. dtAnsiString : Result := StrToDateTime(string((fDataIntf as IValueAnsiString).Value));
  367. dtWideString : Result := StrToDateTime((fDataIntf as IValueWideString).Value);
  368. {$ENDIF}
  369. dtInteger,
  370. dtInt64 : Result := FileDateToDateTime(AsInt64);
  371. dtDouble,
  372. dtExtended,
  373. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  374. dtVariant : Result := Extended(AsVariant);
  375. else raise Exception.Create('DataType not supported');
  376. end;
  377. except
  378. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  379. end;
  380. end;
  381. function TFlexValue.CastToExtended: Extended;
  382. begin
  383. try
  384. case fDataType of
  385. dtNull : Result := 0.0;
  386. dtString : Result := StrToFloat((fDataIntf as IValueString).Value);
  387. {$IFDEF MSWINDOWS}
  388. dtAnsiString : Result := StrToFloat(string((fDataIntf as IValueAnsiString).Value));
  389. dtWideString : Result := StrToFloat((fDataIntf as IValueWideString).Value);
  390. {$ENDIF}
  391. dtInteger,
  392. dtInt64 : Result := AsInt64;
  393. dtBoolean : Result := AsInt64;
  394. dtDouble,
  395. dtExtended,
  396. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  397. dtVariant : Result := Extended(AsVariant);
  398. else raise Exception.Create('DataType not supported');
  399. end;
  400. except
  401. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  402. end;
  403. end;
  404. function TFlexValue.CastToInt64: Int64;
  405. begin
  406. try
  407. case fDataType of
  408. dtNull : Result := 0;
  409. dtString : Result := StrToInt((fDataIntf as IValueString).Value);
  410. {$IFDEF MSWINDOWS}
  411. dtAnsiString : Result := StrToInt(string((fDataIntf as IValueAnsiString).Value));
  412. dtWideString : Result := StrToInt((fDataIntf as IValueWideString).Value);
  413. {$ENDIF}
  414. dtInteger,
  415. dtInt64 : Result := (fDataIntf as IValueInteger).Value;
  416. dtBoolean : Result := Integer(AsBoolean);
  417. dtDateTime : Result := DateTimeToFileDate((fDataIntf as IValueExtended).Value);
  418. dtVariant : Result := Integer(AsVariant);
  419. else raise Exception.Create('DataType not supported');
  420. end;
  421. except
  422. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Integer error: %s',[e.message]);
  423. end;
  424. end;
  425. function TFlexValue.CastToInteger: Integer;
  426. begin
  427. Result := AsInt64;
  428. end;
  429. function TFlexValue.CastToObject: TObject;
  430. begin
  431. try
  432. case fDataType of
  433. dtObject,
  434. dtOwnedObject : Result := (fDataIntf as IValueObject).Value;
  435. dtPointer : Result := TObject((fDataIntf as IValueObject).Value);
  436. dtNull : Result := nil;
  437. else raise Exception.Create('DataType not supported');
  438. end;
  439. except
  440. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Object error: %s',[e.message]);
  441. end;
  442. end;
  443. function TFlexValue.CastToPointer: Pointer;
  444. begin
  445. try
  446. case fDataType of
  447. dtObject,
  448. dtOwnedObject : Result := Pointer((fDataIntf as IValueObject).Value);
  449. dtPointer : Result := (fDataIntf as IValuePointer).Value;
  450. dtNull : Result := nil;
  451. else raise Exception.Create('DataType not supported');
  452. end;
  453. except
  454. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Pointer error: %s',[e.message]);
  455. end;
  456. end;
  457. function TFlexValue.CastToVariant: Variant;
  458. begin
  459. try
  460. case fDataType of
  461. dtNull : Result := Variants.Null;
  462. dtBoolean : Result := AsVariant;
  463. dtVariant : Result := (fDataIntf as IValueVariant).Value;
  464. else raise Exception.Create('DataType not supported');
  465. end;
  466. except
  467. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Variant error: %s',[e.message]);
  468. end;
  469. end;
  470. function TFlexValue.CastToInterface: IInterface;
  471. begin
  472. try
  473. case fDataType of
  474. dtNull : Result := nil;
  475. dtInterface : Result := fDataIntf;
  476. dtPointer : Result := IInterface(fDataIntf);
  477. else raise Exception.Create('DataType not supported');
  478. end;
  479. except
  480. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Interface error: %s',[e.message]);
  481. end;
  482. end;
  483. procedure TFlexValue.Clear;
  484. begin
  485. if Pointer(fDataIntf) <> nil then fDataIntf := nil;
  486. fDataType := dtNull;
  487. end;
  488. constructor TFlexValue.Create(const Value: TVarRec);
  489. begin
  490. case Value.VType of
  491. {$IFNDEF NEXTGEN}
  492. vtString : AsString := string(Value.VString^);
  493. vtChar : AsString := string(Value.VChar);
  494. {$ENDIF}
  495. {$IFDEF MSWINDOWS}
  496. vtAnsiString : AsAnsiString := AnsiString(Value.VAnsiString);
  497. vtWideString : AsWideString := WideString(Value.VWideString);
  498. {$ENDIF}
  499. {$IFDEF UNICODE}
  500. vtUnicodeString: AsString := string(Value.VUnicodeString);
  501. {$ENDIF UNICODE}
  502. vtInteger : AsInteger := Value.VInteger;
  503. vtInt64 : AsInt64 := Value.VInt64^;
  504. vtExtended : AsExtended := Value.VExtended^;
  505. vtBoolean : AsBoolean := Value.VBoolean;
  506. vtVariant : AsVariant := Value.VVariant^;
  507. vtInterface : AsInterface := IInterface(Value.VInterface);
  508. vtClass : AsClass := Value.VClass;
  509. vtObject : AsObject := Value.VObject;
  510. vtPointer : AsPointer := Value.VPointer;
  511. else raise Exception.Create('DataType not supported by TFlexValue');
  512. end;
  513. {$IFDEF FPC}
  514. fDataIntf._AddRef;
  515. {$ENDIF}
  516. end;
  517. {$IFNDEF FPC}
  518. class operator TFlexValue.Implicit(Value: TFlexValue): Boolean;
  519. begin
  520. Result := Value.AsBoolean;
  521. end;
  522. class operator TFlexValue.Implicit(const Value: TFlexValue): string;
  523. begin
  524. Result := Value.AsString;
  525. end;
  526. class operator TFlexValue.Implicit(Value: TFlexValue): TObject;
  527. begin
  528. Result := Value.AsObject;
  529. end;
  530. class operator TFlexValue.Implicit(Value: TFlexValue): Pointer;
  531. begin
  532. Result := Value.AsPointer;
  533. end;
  534. class operator TFlexValue.Implicit(Value: TFlexValue): TDateTime;
  535. begin
  536. Result := Value.AsDateTime;
  537. end;
  538. class operator TFlexValue.Implicit(Value: TFlexValue): TClass;
  539. begin
  540. Result := Value.AsClass;
  541. end;
  542. class operator TFlexValue.Implicit(Value: TFlexValue): Int64;
  543. begin
  544. Result := Value.AsInt64;
  545. end;
  546. class operator TFlexValue.Implicit(Value: TFlexValue): Integer;
  547. begin
  548. Result := Value.AsInteger;
  549. end;
  550. class operator TFlexValue.Implicit(Value: TFlexValue): Extended;
  551. begin
  552. Result := Value.AsExtended;
  553. end;
  554. {$ENDIF}
  555. function TFlexValue.IsBoolean: Boolean;
  556. begin
  557. Result := fDataType = dtBoolean;
  558. end;
  559. function TFlexValue.IsDateTime: Boolean;
  560. begin
  561. Result := fDataType = dtDateTime;
  562. end;
  563. function TFlexValue.IsFloating: Boolean;
  564. begin
  565. Result := fDataType in [dtDouble,dtExtended];
  566. end;
  567. function TFlexValue.IsInteger: Boolean;
  568. begin
  569. Result := fDataType in [dtInteger,dtInt64];
  570. end;
  571. function TFlexValue.IsInterface: Boolean;
  572. begin
  573. Result := fDataType = dtInterface;
  574. end;
  575. function TFlexValue.IsNullOrEmpty: Boolean;
  576. begin
  577. Result := fDataIntf = nil;
  578. end;
  579. function TFlexValue.IsObject: Boolean;
  580. begin
  581. Result := fDataType = dtObject;
  582. end;
  583. function TFlexValue.IsPointer: Boolean;
  584. begin
  585. Result := fDataType = dtPointer;
  586. end;
  587. function TFlexValue.IsString: Boolean;
  588. begin
  589. Result := fDataType in [dtString,dtAnsiString,dtWideString];
  590. end;
  591. function TFlexValue.IsVariant: Boolean;
  592. begin
  593. Result := fDataType = dtVariant;
  594. end;
  595. {$IFDEF MSWINDOWS}
  596. procedure TFlexValue.SetAsAnsiString(const Value: AnsiString);
  597. begin
  598. Clear;
  599. fDataIntf := TValueAnsiString.Create(Value);
  600. fDataType := TValueDataType.dtAnsiString;
  601. end;
  602. {$ENDIF}
  603. procedure TFlexValue.SetAsBoolean(const Value: Boolean);
  604. begin
  605. Clear;
  606. fDataIntf := TValueInteger.Create(Value.ToInteger);
  607. fDataType := TValueDataType.dtBoolean;
  608. end;
  609. procedure TFlexValue.SetAsCardinal(const Value: Cardinal);
  610. begin
  611. Clear;
  612. fDataIntf := TValueInteger.Create(Value);
  613. fDataType := TValueDataType.dtInt64;
  614. end;
  615. procedure TFlexValue.SetAsClass(const Value: TClass);
  616. begin
  617. Clear;
  618. fDataIntf := TValuePointer.Create(Value);
  619. fDataType := TValueDataType.dtClass;
  620. end;
  621. procedure TFlexValue.SetAsDateTime(const Value: TDateTime);
  622. begin
  623. Clear;
  624. fDataIntf := TValueExtended.Create(Value);
  625. fDataType := TValueDataType.dtDateTime;
  626. end;
  627. procedure TFlexValue.SetAsExtended(const Value: Extended);
  628. begin
  629. Clear;
  630. fDataIntf := TValueExtended.Create(Value);
  631. fDataType := TValueDataType.dtExtended;
  632. end;
  633. procedure TFlexValue.SetAsInt64(const Value: Int64);
  634. begin
  635. Clear;
  636. fDataIntf := TValueInteger.Create(Value);
  637. fDataType := TValueDataType.dtInt64;
  638. end;
  639. procedure TFlexValue.SetAsInteger(const Value: Integer);
  640. begin
  641. Clear;
  642. fDataIntf := TValueInteger.Create(Value);
  643. fDataType := TValueDataType.dtInteger;
  644. end;
  645. procedure TFlexValue.SetAsInterface(const Value: IInterface);
  646. begin
  647. {$IFNDEF FPC}
  648. fDataIntf := Value;
  649. {$ELSE}
  650. fDataIntf := Pointer(Value);
  651. {$ENDIF}
  652. fDataType := TValueDataType.dtInterface;
  653. end;
  654. procedure TFlexValue.SetAsObject(const Value: TObject);
  655. begin
  656. Clear;
  657. fDataIntf := TValueObject.Create(Value);
  658. fDataType := TValueDataType.dtObject;
  659. end;
  660. procedure TFlexValue.SetAsPointer(const Value: Pointer);
  661. begin
  662. Clear;
  663. fDataIntf := TValuePointer.Create(Value);
  664. fDataType := TValueDataType.dtPointer;
  665. end;
  666. procedure TFlexValue.SetAsString(const Value: string);
  667. begin
  668. Clear;
  669. fDataIntf := TValueString.Create(Value);
  670. fDataType := TValueDataType.dtString;
  671. end;
  672. procedure TFlexValue.SetAsVariant(const Value: Variant);
  673. begin
  674. Clear;
  675. fDataIntf := TValueVariant.Create(Value);
  676. fDataType := TValueDataType.dtVariant;
  677. end;
  678. {$IFDEF MSWINDOWS}
  679. procedure TFlexValue.SetAsWideString(const Value: WideString);
  680. begin
  681. Clear;
  682. fDataIntf := TValueWideString.Create(Value);
  683. fDataType := TValueDataType.dtWideString;
  684. end;
  685. {$ENDIF}
  686. procedure TFlexValue._AddRef;
  687. begin
  688. if Assigned(fDataIntf) then fDataIntf._AddRef;
  689. end;
  690. procedure TFlexValue._Release;
  691. begin
  692. if Assigned(fDataIntf) then fDataIntf._Release;
  693. end;
  694. { TValueStringData }
  695. constructor TValueString.Create(const Value: string);
  696. begin
  697. fData := Value;
  698. end;
  699. function TValueString.GetValue: string;
  700. begin
  701. Result := fData;
  702. end;
  703. procedure TValueString.SetValue(const Value: string);
  704. begin
  705. fData := Value;
  706. end;
  707. { TValueVariantData }
  708. constructor TValueVariant.Create(const Value: Variant);
  709. begin
  710. fData := Value;
  711. end;
  712. function TValueVariant.GetValue: Variant;
  713. begin
  714. Result := fData;
  715. end;
  716. procedure TValueVariant.SetValue(const Value: Variant);
  717. begin
  718. fData := Value;
  719. end;
  720. { TValueAnsiStringData }
  721. {$IFDEF MSWINDOWS}
  722. constructor TValueAnsiString.Create(const Value: AnsiString);
  723. begin
  724. fData := Value;
  725. end;
  726. function TValueAnsiString.GetValue: AnsiString;
  727. begin
  728. Result := fData;
  729. end;
  730. procedure TValueAnsiString.SetValue(const Value: AnsiString);
  731. begin
  732. fData := Value;
  733. end;
  734. { TValueWideStringData }
  735. constructor TValueWideString.Create(const Value: WideString);
  736. begin
  737. fData := Value;
  738. end;
  739. function TValueWideString.GetValue: WideString;
  740. begin
  741. Result := fData;
  742. end;
  743. procedure TValueWideString.SetValue(const Value: WideString);
  744. begin
  745. fData := Value;
  746. end;
  747. {$ENDIF}
  748. { TValueInteger }
  749. constructor TValueInteger.Create(const Value: Int64);
  750. begin
  751. fData := Value;
  752. end;
  753. function TValueInteger.GetValue: Int64;
  754. begin
  755. Result := fData;
  756. end;
  757. procedure TValueInteger.SetValue(const Value: Int64);
  758. begin
  759. fData := Value;
  760. end;
  761. { TValuePointer }
  762. constructor TValuePointer.Create(const Value: Pointer);
  763. begin
  764. fData := Value;
  765. end;
  766. function TValuePointer.GetValue: Pointer;
  767. begin
  768. Result := fData;
  769. end;
  770. procedure TValuePointer.SetValue(const Value: Pointer);
  771. begin
  772. fData := Value;
  773. end;
  774. { TValueExtended }
  775. constructor TValueExtended.Create(const Value: Extended);
  776. begin
  777. fData := Value;
  778. end;
  779. function TValueExtended.GetValue: Extended;
  780. begin
  781. Result := fData;
  782. end;
  783. procedure TValueExtended.SetValue(const Value: Extended);
  784. begin
  785. fData := Value;
  786. end;
  787. { TValueObject }
  788. constructor TValueObject.Create(const Value: TObject);
  789. begin
  790. fData := Value;
  791. end;
  792. function TValueObject.GetValue: TObject;
  793. begin
  794. Result := fData;
  795. end;
  796. procedure TValueObject.SetValue(const Value: TObject);
  797. begin
  798. fData := Value;
  799. end;
  800. end.