Quick.Value.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 : 15/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. {$IFNDEF NEXTGEN}
  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. IValuePointer = interface
  115. ['{9FE4E499-C487-4D24-8190-14FF3F9FE86B}']
  116. function GetValue : Pointer;
  117. procedure SetValue(const Value : Pointer);
  118. property Value : Pointer read GetValue write SetValue;
  119. end;
  120. TValuePointer = class(TValueData,IValuePointer)
  121. strict private
  122. fData : Pointer;
  123. private
  124. function GetValue : Pointer;
  125. procedure SetValue(const Value : Pointer);
  126. public
  127. constructor Create(const Value : Pointer);
  128. property Value : Pointer read GetValue write SetValue;
  129. end;
  130. IValueVariant = interface
  131. ['{8B1F8469-B872-47AD-83BB-F51920012943}']
  132. function GetValue : Variant;
  133. procedure SetValue(const Value : Variant);
  134. property Value : Variant read GetValue write SetValue;
  135. end;
  136. TValueVariant = class(TValueData,IValueVariant)
  137. strict private
  138. fData : Variant;
  139. private
  140. function GetValue : Variant;
  141. procedure SetValue(const Value : Variant);
  142. public
  143. constructor Create(const Value : Variant);
  144. property Value : Variant read GetValue write SetValue;
  145. end;
  146. TFlexValue = record
  147. private
  148. fDataIntf : IInterface;
  149. fDataType : TValueDataType;
  150. function CastToString : string;
  151. {$IFNDEF NEXTGEN}
  152. function CastToAnsiString : AnsiString;
  153. function CastToWideString : WideString;
  154. {$ENDIF}
  155. function CastToBoolean: Boolean;
  156. function CastToClass: TClass;
  157. function CastToExtended: Extended;
  158. function CastToInt64: Int64;
  159. function CastToInteger: Integer;
  160. function CastToDateTime : TDateTime;
  161. function CastToObject: TObject;
  162. function CastToPointer: Pointer;
  163. function CastToInterface: Pointer;
  164. function CastToVariant: Variant;
  165. function CastToCardinal : Cardinal;
  166. procedure SetAsString(const Value : string);
  167. {$IFNDEF NEXTGEN}
  168. procedure SetAsAnsiString(const Value : AnsiString);
  169. procedure SetAsWideString(const Value : WideString);
  170. {$ENDIF}
  171. procedure SetAsBoolean(const Value: Boolean);
  172. procedure SetAsClass(const Value: TClass);
  173. procedure SetAsExtended(const Value: Extended);
  174. procedure SetAsInt64(const Value: Int64);
  175. procedure SetAsInteger(const Value: Integer);
  176. procedure SetAsObject(const Value: TObject);
  177. procedure SetAsPointer(const Value: Pointer);
  178. procedure SetAsDateTime(const Value : TDateTime);
  179. procedure SetAsVariant(const Value: Variant);
  180. procedure SetAsCardinal(const Value : Cardinal);
  181. public
  182. constructor Create(const Value: TVarRec);
  183. property DataType : TValueDataType read fDataType;
  184. property AsString : string read CastToString write SetAsString;
  185. {$IFNDEF NEXTGEN}
  186. property AsAnsiString : AnsiString read CastToAnsiString write SetAsAnsiString;
  187. property AsWideString : WideString read CastToWideString write SetAsWideString;
  188. {$ENDIF}
  189. property AsInteger : Integer read CastToInteger write SetAsInteger;
  190. property AsInt64 : Int64 read CastToInt64 write SetAsInt64;
  191. property AsExtended : Extended read CastToExtended write SetAsExtended;
  192. property AsBoolean : Boolean read CastToBoolean write SetAsBoolean;
  193. property AsPointer : Pointer read CastToPointer write SetAsPointer;
  194. property AsClass : TClass read CastToClass write SetAsClass;
  195. property AsInterface : Pointer read CastToInterface write SetAsPointer;
  196. property AsObject : TObject read CastToObject write SetAsObject;
  197. property AsVariant : Variant read CastToVariant write SetAsVariant;
  198. property AsCardinal : Cardinal read CastToCardinal write SetAsCardinal;
  199. property AsDateTime : TDateTime read CastToDateTime write SetAsDateTime;
  200. //function AsType<T> : T;
  201. function IsNullOrEmpty : Boolean; inline;
  202. function IsString : Boolean; inline;
  203. function IsInteger : Boolean; inline;
  204. function IsFloating : Boolean; inline;
  205. function IsDateTime : Boolean; inline;
  206. function IsBoolean : Boolean; inline;
  207. function IsInterface : Boolean; inline;
  208. function IsObject : Boolean; inline;
  209. function IsPointer : Boolean; inline;
  210. function IsVariant : Boolean; inline;
  211. procedure Clear; inline;
  212. procedure _AddRef; inline;
  213. procedure _Release; inline;
  214. {$IFNDEF FPC}
  215. class operator Implicit(const Value : TFlexValue) : string;
  216. class operator Implicit(Value : TFlexValue) : Integer;
  217. class operator Implicit(Value : TFlexValue) : Int64;
  218. class operator Implicit(Value : TFlexValue) : Extended;
  219. class operator Implicit(Value : TFlexValue) : TDateTime;
  220. class operator Implicit(Value : TFlexValue) : Boolean;
  221. class operator Implicit(Value : TFlexValue) : TClass;
  222. class operator Implicit(Value : TFlexValue) : TObject;
  223. class operator Implicit(Value : TFlexValue) : Pointer;
  224. {$ENDIF}
  225. end;
  226. implementation
  227. function TFlexValue.CastToString: string;
  228. begin
  229. try
  230. case fDataType of
  231. dtNull : Result := '';
  232. dtString : Result := (fDataIntf as IValueString).Value;
  233. {$IFNDEF NEXTGEN}
  234. dtAnsiString : Result := string((fDataIntf as IValueAnsiString).Value);
  235. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  236. {$ENDIF}
  237. dtInteger,
  238. dtInt64 : Result := IntToStr(AsInt64);
  239. dtBoolean : Result := BoolToStr(AsBoolean,True);
  240. dtDouble,
  241. dtExtended : Result := FloatToStr(AsExtended);
  242. dtDateTime : Result := DateTimeToStr(AsExtended);
  243. dtVariant : Result := string(AsVariant);
  244. dtClass : Result := AsClass.ClassName;
  245. else raise Exception.Create('DataType not supported');
  246. end;
  247. except
  248. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to String error: %s',[e.message]);
  249. end;
  250. end;
  251. {$IFNDEF NEXTGEN}
  252. function TFlexValue.CastToAnsiString: AnsiString;
  253. begin
  254. try
  255. case fDataType of
  256. dtNull : Result := '';
  257. dtString : Result := AnsiString((fDataIntf as IValueString).Value);
  258. dtAnsiString : Result := (fDataIntf as IValueAnsiString).Value;
  259. dtWideString : Result := AnsiString((fDataIntf as IValueWideString).Value);
  260. dtInteger,
  261. dtInt64 : Result := AnsiString(IntToStr(AsInt64));
  262. dtBoolean : Result := AnsiString(BoolToStr(AsBoolean,True));
  263. dtDouble,
  264. dtExtended : Result := AnsiString(FloatToStr(AsExtended));
  265. dtDateTime : Result := AnsiString(DateTimeToStr(AsExtended));
  266. dtVariant : Result := AnsiString(AsVariant);
  267. else raise Exception.Create('DataType not supported');
  268. end;
  269. except
  270. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to AnsiString error: %s',[e.message]);
  271. end;
  272. end;
  273. function TFlexValue.CastToWideString: WideString;
  274. begin
  275. try
  276. case fDataType of
  277. dtNull : Result := '';
  278. dtString : Result := Widestring((fDataIntf as IValueString).Value);
  279. {$IFNDEF NEXTGEN}
  280. dtAnsiString : Result := Widestring((fDataIntf as IValueAnsiString).Value);
  281. dtWideString : Result := (fDataIntf as IValueWideString).Value;
  282. {$ENDIF}
  283. dtInteger,
  284. dtInt64 : Result := Widestring(IntToStr(AsInt64));
  285. dtBoolean : Result := Widestring(BoolToStr(AsBoolean,True));
  286. dtDouble,
  287. dtExtended : Result := Widestring(FloatToStr(AsExtended));
  288. dtDateTime : Result := Widestring(DateTimeToStr(AsExtended));
  289. dtVariant : Result := Widestring(AsVariant);
  290. else raise Exception.Create('DataType not supported');
  291. end;
  292. except
  293. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to WideString error: %s',[e.message]);
  294. end;
  295. end;
  296. {$ENDIF}
  297. function TFlexValue.CastToBoolean: Boolean;
  298. begin
  299. try
  300. case fDataType of
  301. dtNull : Result := False;
  302. dtString : Result := StrToBool((fDataIntf as IValueString).Value);
  303. {$IFNDEF NEXTGEN}
  304. dtAnsiString : Result := StrToBool(string((fDataIntf as IValueAnsiString).Value));
  305. dtWideString : Result := StrToBool((fDataIntf as IValueWideString).Value);
  306. {$ENDIF}
  307. dtInteger,
  308. dtInt64 :
  309. begin
  310. if (fDataIntf as IValueInteger).Value = 1 then Result := True
  311. else if (fDataIntf as IValueInteger).Value = 0 then Result := False
  312. else raise Exception.Create('Integer value not in 0-1 range');
  313. end;
  314. dtBoolean : Result := Boolean((fDataIntf as IValueInteger).Value);
  315. dtVariant : Result := Boolean(AsVariant);
  316. else raise Exception.Create('DataType not supported');
  317. end;
  318. except
  319. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Boolean error: %s',[e.message]);
  320. end;
  321. end;
  322. function TFlexValue.CastToCardinal: Cardinal;
  323. begin
  324. Result := AsInt64;
  325. end;
  326. function TFlexValue.CastToClass: TClass;
  327. begin
  328. try
  329. case fDataType of
  330. dtNull : Result := nil;
  331. dtClass : Result := (fDataIntf as TValuePointer).Value;
  332. else raise Exception.Create('DataType not supported');
  333. end;
  334. except
  335. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TClass error: %s',[e.message]);
  336. end;
  337. end;
  338. function TFlexValue.CastToDateTime: TDateTime;
  339. begin
  340. try
  341. case fDataType of
  342. dtNull : Result := 0.0;
  343. dtString : Result := StrToDateTime((fDataIntf as IValueString).Value);
  344. {$IFNDEF NEXTGEN}
  345. dtAnsiString : Result := StrToDateTime(string((fDataIntf as IValueAnsiString).Value));
  346. dtWideString : Result := StrToDateTime((fDataIntf as IValueWideString).Value);
  347. {$ENDIF}
  348. dtInteger,
  349. dtInt64 : Result := FileDateToDateTime(AsInt64);
  350. dtDouble,
  351. dtExtended,
  352. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  353. dtVariant : Result := Extended(AsVariant);
  354. else raise Exception.Create('DataType not supported');
  355. end;
  356. except
  357. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  358. end;
  359. end;
  360. function TFlexValue.CastToExtended: Extended;
  361. begin
  362. try
  363. case fDataType of
  364. dtNull : Result := 0.0;
  365. dtString : Result := StrToFloat((fDataIntf as IValueString).Value);
  366. {$IFNDEF NEXTGEN}
  367. dtAnsiString : Result := StrToFloat(string((fDataIntf as IValueAnsiString).Value));
  368. dtWideString : Result := StrToFloat((fDataIntf as IValueWideString).Value);
  369. {$ENDIF}
  370. dtInteger,
  371. dtInt64 : Result := AsInt64;
  372. dtBoolean : Result := AsInt64;
  373. dtDouble,
  374. dtExtended,
  375. dtDateTime : Result := (fDataIntf as IValueExtended).Value;
  376. dtVariant : Result := Extended(AsVariant);
  377. else raise Exception.Create('DataType not supported');
  378. end;
  379. except
  380. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Extended error: %s',[e.message]);
  381. end;
  382. end;
  383. function TFlexValue.CastToInt64: Int64;
  384. begin
  385. try
  386. case fDataType of
  387. dtNull : Result := 0;
  388. dtString : Result := StrToInt((fDataIntf as IValueString).Value);
  389. {$IFNDEF NEXTGEN}
  390. dtAnsiString : Result := StrToInt(string((fDataIntf as IValueAnsiString).Value));
  391. dtWideString : Result := StrToInt((fDataIntf as IValueWideString).Value);
  392. {$ENDIF}
  393. dtInteger,
  394. dtInt64 : Result := (fDataIntf as IValueInteger).Value;
  395. dtBoolean : Result := Integer(AsBoolean);
  396. dtDateTime : Result := DateTimeToFileDate((fDataIntf as IValueExtended).Value);
  397. dtVariant : Result := Integer(AsVariant);
  398. else raise Exception.Create('DataType not supported');
  399. end;
  400. except
  401. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Integer error: %s',[e.message]);
  402. end;
  403. end;
  404. function TFlexValue.CastToInteger: Integer;
  405. begin
  406. Result := AsInt64;
  407. end;
  408. function TFlexValue.CastToObject: TObject;
  409. begin
  410. try
  411. case fDataType of
  412. dtObject,
  413. dtOwnedObject : Result := (fDataIntf as IValuePointer).Value;
  414. dtNull : Result := nil;
  415. else raise Exception.Create('DataType not supported');
  416. end;
  417. except
  418. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Object error: %s',[e.message]);
  419. end;
  420. end;
  421. function TFlexValue.CastToPointer: Pointer;
  422. begin
  423. try
  424. case fDataType of
  425. dtObject,
  426. dtOwnedObject : Result := (fDataIntf as IValuePointer).Value;
  427. dtNull : Result := nil;
  428. else raise Exception.Create('DataType not supported');
  429. end;
  430. except
  431. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Pointer error: %s',[e.message]);
  432. end;
  433. end;
  434. function TFlexValue.CastToVariant: Variant;
  435. begin
  436. try
  437. case fDataType of
  438. dtNull : Result := Variants.Null;
  439. dtBoolean : Result := AsVariant;
  440. dtVariant : Result := (fDataIntf as IValueVariant).Value;
  441. else raise Exception.Create('DataType not supported');
  442. end;
  443. except
  444. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Variant error: %s',[e.message]);
  445. end;
  446. end;
  447. function TFlexValue.CastToInterface: Pointer;
  448. begin
  449. try
  450. case fDataType of
  451. dtNull : Result := nil;
  452. dtInterface : Result := IInterface(fDataIntf);
  453. else raise Exception.Create('DataType not supported');
  454. end;
  455. except
  456. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to Interface error: %s',[e.message]);
  457. end;
  458. end;
  459. procedure TFlexValue.Clear;
  460. begin
  461. if Pointer(fDataIntf) <> nil then fDataIntf := nil;
  462. fDataType := dtNull;
  463. end;
  464. constructor TFlexValue.Create(const Value: TVarRec);
  465. begin
  466. case Value.VType of
  467. {$IFNDEF NEXTGEN}
  468. vtString : AsString := string(Value.VString^);
  469. {$ENDIF}
  470. vtChar : AsString := string(Value.VChar);
  471. {$IFNDEF NEXTGEN}
  472. vtAnsiString : AsAnsiString := AnsiString(Value.VAnsiString);
  473. vtWideString : AsWideString := WideString(Value.VWideString);
  474. {$ENDIF}
  475. vtUnicodeString: AsString := string(Value.VUnicodeString);
  476. vtInteger : AsInteger := Value.VInteger;
  477. vtInt64 : AsInt64 := Value.VInt64^;
  478. vtExtended : AsExtended := Value.VExtended^;
  479. vtBoolean : AsBoolean := Value.VBoolean;
  480. vtVariant : AsVariant := Value.VVariant^;
  481. vtInterface : AsInterface := IInterface(Value.VInterface);
  482. vtClass : AsClass := Value.VClass;
  483. vtObject : AsObject := Value.VObject;
  484. vtPointer : AsPointer := Value.VPointer;
  485. else raise Exception.Create('DataType not supported by TFlexValue');
  486. end;
  487. //fDataIntf._AddRef;
  488. end;
  489. {$IFNDEF FPC}
  490. class operator TFlexValue.Implicit(Value: TFlexValue): Boolean;
  491. begin
  492. Result := Value.AsBoolean;
  493. end;
  494. class operator TFlexValue.Implicit(const Value: TFlexValue): string;
  495. begin
  496. Result := Value.AsString;
  497. end;
  498. class operator TFlexValue.Implicit(Value: TFlexValue): TObject;
  499. begin
  500. Result := Value.AsObject;
  501. end;
  502. class operator TFlexValue.Implicit(Value: TFlexValue): Pointer;
  503. begin
  504. Result := Value.AsPointer;
  505. end;
  506. class operator TFlexValue.Implicit(Value: TFlexValue): TDateTime;
  507. begin
  508. Result := Value.AsDateTime;
  509. end;
  510. class operator TFlexValue.Implicit(Value: TFlexValue): TClass;
  511. begin
  512. Result := Value.AsClass;
  513. end;
  514. class operator TFlexValue.Implicit(Value: TFlexValue): Int64;
  515. begin
  516. Result := Value.AsInt64;
  517. end;
  518. class operator TFlexValue.Implicit(Value: TFlexValue): Integer;
  519. begin
  520. Result := Value.AsInteger;
  521. end;
  522. class operator TFlexValue.Implicit(Value: TFlexValue): Extended;
  523. begin
  524. Result := Value.AsExtended;
  525. end;
  526. {$ENDIF}
  527. function TFlexValue.IsBoolean: Boolean;
  528. begin
  529. Result := fDataType = dtBoolean;
  530. end;
  531. function TFlexValue.IsDateTime: Boolean;
  532. begin
  533. Result := fDataType = dtDateTime;
  534. end;
  535. function TFlexValue.IsFloating: Boolean;
  536. begin
  537. Result := fDataType in [dtDouble,dtExtended];
  538. end;
  539. function TFlexValue.IsInteger: Boolean;
  540. begin
  541. Result := fDataType in [dtInteger,dtInt64];
  542. end;
  543. function TFlexValue.IsInterface: Boolean;
  544. begin
  545. Result := fDataType = dtInterface;
  546. end;
  547. function TFlexValue.IsNullOrEmpty: Boolean;
  548. begin
  549. Result := fDataIntf = nil;
  550. end;
  551. function TFlexValue.IsObject: Boolean;
  552. begin
  553. Result := fDataType = dtObject;
  554. end;
  555. function TFlexValue.IsPointer: Boolean;
  556. begin
  557. Result := fDataType = dtPointer;
  558. end;
  559. function TFlexValue.IsString: Boolean;
  560. begin
  561. Result := fDataType in [dtString,dtAnsiString,dtWideString];
  562. end;
  563. function TFlexValue.IsVariant: Boolean;
  564. begin
  565. Result := fDataType = dtVariant;
  566. end;
  567. {$IFNDEF NEXTGEN}
  568. procedure TFlexValue.SetAsAnsiString(const Value: AnsiString);
  569. begin
  570. Clear;
  571. fDataIntf := TValueAnsiString.Create(Value);
  572. fDataType := TValueDataType.dtAnsiString;
  573. end;
  574. {$ENDIF}
  575. procedure TFlexValue.SetAsBoolean(const Value: Boolean);
  576. begin
  577. Clear;
  578. fDataIntf := TValueInteger.Create(Value.ToInteger);
  579. fDataType := TValueDataType.dtBoolean;
  580. end;
  581. procedure TFlexValue.SetAsCardinal(const Value: Cardinal);
  582. begin
  583. Clear;
  584. fDataIntf := TValueInteger.Create(Value);
  585. fDataType := TValueDataType.dtInt64;
  586. end;
  587. procedure TFlexValue.SetAsClass(const Value: TClass);
  588. begin
  589. Clear;
  590. fDataIntf := TValuePointer.Create(Value);
  591. fDataType := TValueDataType.dtClass;
  592. end;
  593. procedure TFlexValue.SetAsDateTime(const Value: TDateTime);
  594. begin
  595. Clear;
  596. fDataIntf := TValueExtended.Create(Value);
  597. fDataType := TValueDataType.dtDateTime;
  598. end;
  599. procedure TFlexValue.SetAsExtended(const Value: Extended);
  600. begin
  601. Clear;
  602. fDataIntf := TValueExtended.Create(Value);
  603. fDataType := TValueDataType.dtExtended;
  604. end;
  605. procedure TFlexValue.SetAsInt64(const Value: Int64);
  606. begin
  607. Clear;
  608. fDataIntf := TValueInteger.Create(Value);
  609. fDataType := TValueDataType.dtInt64;
  610. end;
  611. procedure TFlexValue.SetAsInteger(const Value: Integer);
  612. begin
  613. Clear;
  614. fDataIntf := TValueInteger.Create(Value);
  615. fDataType := TValueDataType.dtInteger;
  616. end;
  617. procedure TFlexValue.SetAsObject(const Value: TObject);
  618. begin
  619. Clear;
  620. fDataIntf := TValuePointer.Create(Value);
  621. fDataType := TValueDataType.dtObject;
  622. end;
  623. procedure TFlexValue.SetAsPointer(const Value: Pointer);
  624. begin
  625. Clear;
  626. fDataIntf := TValuePointer.Create(Value);
  627. fDataType := TValueDataType.dtPointer;
  628. end;
  629. procedure TFlexValue.SetAsString(const Value: string);
  630. begin
  631. Clear;
  632. fDataIntf := TValueString.Create(Value);
  633. fDataType := TValueDataType.dtString;
  634. end;
  635. procedure TFlexValue.SetAsVariant(const Value: Variant);
  636. begin
  637. Clear;
  638. fDataIntf := TValueVariant.Create(Value);
  639. fDataType := TValueDataType.dtVariant;
  640. end;
  641. {$IFNDEF NEXTGEN}
  642. procedure TFlexValue.SetAsWideString(const Value: WideString);
  643. begin
  644. Clear;
  645. fDataIntf := TValueWideString.Create(Value);
  646. fDataType := TValueDataType.dtWideString;
  647. end;
  648. {$ENDIF}
  649. procedure TFlexValue._AddRef;
  650. begin
  651. if Assigned(fDataIntf) then fDataIntf._AddRef;
  652. end;
  653. procedure TFlexValue._Release;
  654. begin
  655. if Assigned(fDataIntf) then fDataIntf._Release;
  656. end;
  657. { TValueStringData }
  658. constructor TValueString.Create(const Value: string);
  659. begin
  660. fData := Value;
  661. end;
  662. function TValueString.GetValue: string;
  663. begin
  664. Result := fData;
  665. end;
  666. procedure TValueString.SetValue(const Value: string);
  667. begin
  668. fData := Value;
  669. end;
  670. { TValueVariantData }
  671. constructor TValueVariant.Create(const Value: Variant);
  672. begin
  673. fData := Value;
  674. end;
  675. function TValueVariant.GetValue: Variant;
  676. begin
  677. Result := fData;
  678. end;
  679. procedure TValueVariant.SetValue(const Value: Variant);
  680. begin
  681. fData := Value;
  682. end;
  683. { TValueAnsiStringData }
  684. {$IFNDEF NEXTGEN}
  685. constructor TValueAnsiString.Create(const Value: AnsiString);
  686. begin
  687. fData := Value;
  688. end;
  689. function TValueAnsiString.GetValue: AnsiString;
  690. begin
  691. Result := fData;
  692. end;
  693. procedure TValueAnsiString.SetValue(const Value: AnsiString);
  694. begin
  695. fData := Value;
  696. end;
  697. { TValueWideStringData }
  698. constructor TValueWideString.Create(const Value: WideString);
  699. begin
  700. fData := Value;
  701. end;
  702. function TValueWideString.GetValue: WideString;
  703. begin
  704. Result := fData;
  705. end;
  706. procedure TValueWideString.SetValue(const Value: WideString);
  707. begin
  708. fData := Value;
  709. end;
  710. {$ENDIF}
  711. { TValueInteger }
  712. constructor TValueInteger.Create(const Value: Int64);
  713. begin
  714. fData := Value;
  715. end;
  716. function TValueInteger.GetValue: Int64;
  717. begin
  718. Result := fData;
  719. end;
  720. procedure TValueInteger.SetValue(const Value: Int64);
  721. begin
  722. fData := Value;
  723. end;
  724. { TValuePointer }
  725. constructor TValuePointer.Create(const Value: Pointer);
  726. begin
  727. fData := Value;
  728. end;
  729. function TValuePointer.GetValue: Pointer;
  730. begin
  731. Result := fData;
  732. end;
  733. procedure TValuePointer.SetValue(const Value: Pointer);
  734. begin
  735. fData := Value;
  736. end;
  737. { TValueExtended }
  738. constructor TValueExtended.Create(const Value: Extended);
  739. begin
  740. fData := Value;
  741. end;
  742. function TValueExtended.GetValue: Extended;
  743. begin
  744. Result := fData;
  745. end;
  746. procedure TValueExtended.SetValue(const Value: Extended);
  747. begin
  748. fData := Value;
  749. end;
  750. end.