syshelpo.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Class Function TORDINALHELPER.Parse(const AString: string): TORDINALTYPE; inline; static;
  2. var
  3. Error: Integer;
  4. begin
  5. Val(AString,Result,Error);
  6. if Error<>0 then
  7. raise EConvertError.CreateFmt(SInvalidInteger,[AString]);
  8. end;
  9. Class Function TORDINALHELPER.Size: Integer; inline; static;
  10. begin
  11. Result:=SizeOf(TORDINALTYPE);
  12. end;
  13. Class Function TORDINALHELPER.ToString(const AValue: TORDINALTYPE): string; overload; inline; static;
  14. begin
  15. Str(AValue,Result);
  16. end;
  17. Class Function TORDINALHELPER.TryParse(const AString: string; out AValue: TORDINALTYPE): Boolean; inline; static;
  18. Var
  19. C : Integer;
  20. begin
  21. Val(AString,AValue,C);
  22. Result:=(C=0);
  23. end;
  24. Function TORDINALHELPER.ToBoolean: Boolean; inline;
  25. begin
  26. Result:=(Self<>0);
  27. end;
  28. Function TORDINALHELPER.ToDouble: Double; inline;
  29. begin
  30. Result:=Self;
  31. end;
  32. Function TORDINALHELPER.ToExtended: Extended; inline;
  33. begin
  34. Result:=Self;
  35. end;
  36. Function TORDINALHELPER.ToBinString: string; inline;
  37. begin
  38. Result:=BinStr(Self,SizeOf(TORDINALTYPE)*8);
  39. end;
  40. Function TORDINALHELPER.ToHexString(const AMinDigits: Integer): string;
  41. overload; inline;
  42. begin
  43. Result:=IntToHex(Self,AMinDigits);
  44. end;
  45. Function TORDINALHELPER.ToHexString: string; overload; inline;
  46. begin
  47. Result:=IntToHex(Self);
  48. end;
  49. Function TORDINALHELPER.ToSingle: Single; inline;
  50. begin
  51. Result:=Self;
  52. end;
  53. Function TORDINALHELPER.ToString: string; overload; inline;
  54. begin
  55. Str(Self,Result);
  56. end;
  57. Function TORDINALHELPER.SetBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  58. begin
  59. Self := Self or (TORDINALTYPE(1) shl index);
  60. Result:=Self;
  61. end;
  62. Function TORDINALHELPER.ClearBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  63. begin
  64. Self:=Self and not TORDINALTYPE((TORDINALTYPE(1) shl index));
  65. Result:=Self;
  66. end;
  67. Function TORDINALHELPER.ToggleBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  68. begin
  69. Self := Self xor TORDINALTYPE((TORDINALTYPE(1) shl index));
  70. Result:=Self;
  71. end;
  72. Function TORDINALHELPER.TestBit(const Index: TORDINALBITINDEX):Boolean; inline;
  73. begin
  74. Result := (Self and TORDINALTYPE((TORDINALTYPE(1) shl index)))<>0;
  75. end;
  76. procedure TORDINALHELPER.Clear;
  77. begin
  78. Self := 0;
  79. end;
  80. function TORDINALHELPER.HighestSetBitPos: int8;
  81. begin
  82. {$ifdef TORDINALTYPESIZE1}
  83. Result := int8(BsrByte(byte(Self)));
  84. {$else}
  85. {$ifdef TORDINALTYPESIZE2}
  86. Result := int8(BsrWord(word(Self)));
  87. {$else}
  88. {$ifdef TORDINALTYPESIZE4}
  89. Result := int8(BsrDWord(dword(Self)));
  90. {$else} // TORDINALTYPESIZE8
  91. Result := int8(BsrQWord(qword(Self)));
  92. {$endif}
  93. {$endif}
  94. {$endif}
  95. end;
  96. function TORDINALHELPER.LowestSetBitPos: int8;
  97. begin
  98. {$ifdef TORDINALTYPESIZE1}
  99. Result := int8(BsfByte(byte(Self)));
  100. {$else}
  101. {$ifdef TORDINALTYPESIZE2}
  102. Result := int8(BsfWord(word(Self)));
  103. {$else}
  104. {$ifdef TORDINALTYPESIZE4}
  105. Result := int8(BsfDWord(dword(Self)));
  106. {$else} // TORDINALTYPESIZE8
  107. Result := int8(BsfQWord(qword(Self)));
  108. {$endif}
  109. {$endif}
  110. {$endif}
  111. end;
  112. function TORDINALHELPER.SetBitsCount: byte;
  113. begin
  114. {$ifdef TORDINALTYPESIZE1}
  115. {$ifdef VER3_2_2}
  116. Result := PopCnt(Word(byte(Self)));
  117. {$else VER3_2_2}
  118. Result := PopCnt(byte(Self));
  119. {$endif VER3_2_2}
  120. {$else}
  121. {$ifdef TORDINALTYPESIZE2}
  122. Result := PopCnt(word(Self));
  123. {$else}
  124. {$ifdef TORDINALTYPESIZE4}
  125. Result := PopCnt(dword(Self));
  126. {$else} // TORDINALTYPESIZE8
  127. Result := PopCnt(qword(Self));
  128. {$endif}
  129. {$endif}
  130. {$endif}
  131. end;
  132. function TORDINALHELPER.GetBit(const aIndex: TORDINALBITINDEX): boolean;
  133. begin
  134. Result := ((Self shr aIndex) and TORDINALTYPE(1)) = TORDINALTYPE(1);
  135. end;
  136. procedure TORDINALHELPER.PutBit(const aIndex: TORDINALBITINDEX; const aNewValue: boolean);
  137. begin
  138. Self := Self or (TORDINALTYPE(1) shl aIndex) xor (TORDINALTYPE(not aNewValue) shl aIndex);
  139. end;
  140. function TORDINALHELPER.GetNibble(const aIndex: TORDINALNIBBLEINDEX): nibble;
  141. begin
  142. Result := TORDINALOVERLAY(Self).AsNibble[aIndex];
  143. end;
  144. procedure TORDINALHELPER.PutNibble(const aIndex: TORDINALNIBBLEINDEX; const aNewValue: nibble);
  145. begin
  146. TORDINALOVERLAY(Self).AsNibble[aIndex] := aNewValue;
  147. end;
  148. {$ifndef TORDINALTYPESIZE1} // TWordHelper, TDWordHelper, TQWordHelper jump in here (and others with 2, 4 and 8 bytes)
  149. function TORDINALHELPER.GetByte(const aIndex: TORDINALBYTEINDEX): byte;
  150. begin
  151. Result := TORDINALOVERLAY(Self).AsByte[aIndex];
  152. end;
  153. procedure TORDINALHELPER.PutByte(const aIndex: TORDINALBYTEINDEX; const aNewValue: byte);
  154. begin
  155. TORDINALOVERLAY(Self).AsByte[aIndex] := aNewValue;
  156. end;
  157. {$ifndef TORDINALTYPESIZE2} // TDWordHelper, TQWordHelper jump in here (and others with 4 and 8 bytes)
  158. function TORDINALHELPER.GetWord(const aIndex: TORDINALWORDINDEX): word;
  159. begin
  160. Result := TORDINALOVERLAY(Self).AsWord[aIndex];
  161. end;
  162. procedure TORDINALHELPER.PutWord(const aIndex: TORDINALWORDINDEX; const aNewValue: word);
  163. begin
  164. TORDINALOVERLAY(Self).AsWord[aIndex] := aNewValue;
  165. end;
  166. {$ifndef TORDINALTYPESIZE4} // TQWordHelper jumps in here (and others with 8 bytes)
  167. function TORDINALHELPER.GetDword(const aIndex: TORDINALDWORDINDEX): dword;
  168. begin
  169. Result := TORDINALOVERLAY(Self).AsDword[aIndex];
  170. end;
  171. procedure TORDINALHELPER.PutDword(const aIndex: TORDINALDWORDINDEX; const aNewValue: dword);
  172. begin
  173. TORDINALOVERLAY(Self).AsDword[aIndex] := aNewValue;
  174. end;
  175. {$endif}
  176. {$endif}
  177. {$endif}