Quick.Value.pas 24 KB

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