Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | (* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | *) |
| 19 | |
| 20 | unit Thrift.Utils; |
| 21 | |
| 22 | interface |
| 23 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 24 | {$I Thrift.Defines.inc} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 25 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 26 | uses |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 27 | {$IFDEF OLD_UNIT_NAMES} |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 28 | Classes, Windows, SysUtils, Character, SyncObjs, TypInfo, Rtti; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 29 | {$ELSE} |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 30 | System.Classes, Winapi.Windows, System.SysUtils, System.Character, |
| 31 | System.SyncObjs, System.TypInfo, System.Rtti; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 32 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | |
| 34 | type |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 35 | ISupportsToString = interface |
| 36 | ['{AF71C350-E0CD-4E94-B77C-0310DC8227FF}'] |
| 37 | function ToString : string; |
| 38 | end; |
| 39 | |
| 40 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 41 | IOverlappedHelper = interface |
| 42 | ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}'] |
| 43 | function Overlapped : TOverlapped; |
| 44 | function OverlappedPtr : POverlapped; |
| 45 | function WaitHandle : THandle; |
| 46 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 47 | end; |
| 48 | |
| 49 | TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper) |
| 50 | strict protected |
| 51 | FOverlapped : TOverlapped; |
| 52 | FEvent : TEvent; |
| 53 | |
| 54 | // IOverlappedHelper |
| 55 | function Overlapped : TOverlapped; |
| 56 | function OverlappedPtr : POverlapped; |
| 57 | function WaitHandle : THandle; |
| 58 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 59 | public |
| 60 | constructor Create; |
| 61 | destructor Destroy; override; |
| 62 | end; |
| 63 | |
| 64 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 65 | TThriftStringBuilder = class( TStringBuilder) |
| 66 | public |
| 67 | function Append(const Value: TBytes): TStringBuilder; overload; |
| 68 | function Append(const Value: ISupportsToString): TStringBuilder; overload; |
| 69 | end; |
| 70 | |
| 71 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 72 | Base64Utils = class sealed |
| 73 | public |
| 74 | class function Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 75 | class function Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 76 | end; |
| 77 | |
| 78 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 79 | CharUtils = class sealed |
| 80 | public |
| 81 | class function IsHighSurrogate( const c : Char) : Boolean; static; inline; |
| 82 | class function IsLowSurrogate( const c : Char) : Boolean; static; inline; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 83 | |
| 84 | class function IsHtmlDoctype( const fourBytes : Integer) : Boolean; static; |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 85 | end; |
| 86 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 87 | |
| 88 | IntegerUtils = class sealed |
| 89 | strict private |
| 90 | class procedure SwapBytes( var one, two : Byte); static; inline; |
| 91 | class procedure Swap2( const pValue : Pointer); static; |
| 92 | class procedure Swap4( const pValue : Pointer); static; |
| 93 | class procedure Swap8( const pValue : Pointer); static; |
| 94 | public |
| 95 | class procedure SwapByteOrder( const pValue : Pointer; const size : Integer); overload; static; |
| 96 | end; |
| 97 | |
| 98 | |
| 99 | TGuidHelper = record helper for System.TGuid |
| 100 | public |
| 101 | function SwapByteOrder : TGuid; |
| 102 | |
| 103 | {$IFDEF Debug} |
| 104 | class procedure SelfTest; static; |
| 105 | {$ENDIF} |
| 106 | end; |
| 107 | |
| 108 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 109 | EnumUtils<T> = class sealed |
| 110 | public |
| 111 | class function ToString(const value : Integer) : string; reintroduce; static; inline; |
| 112 | end; |
| 113 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 114 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 115 | StringUtils<T> = class sealed |
| 116 | public |
| 117 | class function ToString(const value : T) : string; reintroduce; static; inline; |
| 118 | end; |
| 119 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 120 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 121 | const |
| 122 | THRIFT_MIMETYPE = 'application/x-thrift'; |
| 123 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 124 | {$IFDEF Win64} |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 125 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 126 | {$ENDIF} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 127 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 128 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 129 | implementation |
| 130 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 131 | { TOverlappedHelperImpl } |
| 132 | |
| 133 | constructor TOverlappedHelperImpl.Create; |
| 134 | begin |
| 135 | inherited Create; |
| 136 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 137 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 138 | FOverlapped.hEvent := FEvent.Handle; |
| 139 | end; |
| 140 | |
| 141 | |
| 142 | |
| 143 | destructor TOverlappedHelperImpl.Destroy; |
| 144 | begin |
| 145 | try |
| 146 | FOverlapped.hEvent := 0; |
| 147 | FreeAndNil( FEvent); |
| 148 | |
| 149 | finally |
| 150 | inherited Destroy; |
| 151 | end; |
| 152 | |
| 153 | end; |
| 154 | |
| 155 | |
| 156 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 157 | begin |
| 158 | result := FOverlapped; |
| 159 | end; |
| 160 | |
| 161 | |
| 162 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 163 | begin |
| 164 | result := @FOverlapped; |
| 165 | end; |
| 166 | |
| 167 | |
| 168 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 169 | begin |
| 170 | result := FOverlapped.hEvent; |
| 171 | end; |
| 172 | |
| 173 | |
| 174 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 175 | begin |
| 176 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 177 | end; |
| 178 | |
| 179 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 180 | { Base64Utils } |
| 181 | |
| 182 | class function Base64Utils.Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 183 | const ENCODE_TABLE : PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 184 | begin |
| 185 | ASSERT( len in [1..3]); |
| 186 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shr 2) and $3F]); |
| 187 | case len of |
| 188 | 3 : begin |
| 189 | Inc(dstOff); |
| 190 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 191 | Inc(dstOff); |
| 192 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff + 1] shl 2) and $3C) or ((src[srcOff + 2] shr 6) and $03)]); |
| 193 | Inc(dstOff); |
| 194 | dst[dstOff] := Byte( ENCODE_TABLE[ src[srcOff + 2] and $3F]); |
| 195 | result := 4; |
| 196 | end; |
| 197 | |
| 198 | 2 : begin |
| 199 | Inc(dstOff); |
| 200 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 201 | Inc(dstOff); |
| 202 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff + 1] shl 2) and $3C]); |
| 203 | result := 3; |
| 204 | end; |
| 205 | |
| 206 | 1 : begin |
| 207 | Inc(dstOff); |
| 208 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shl 4) and $30]); |
| 209 | result := 2; |
| 210 | end; |
| 211 | |
| 212 | else |
| 213 | ASSERT( FALSE); |
Jens Geyer | 9f9535c | 2014-12-14 04:16:05 +0100 | [diff] [blame] | 214 | result := 0; // because invalid call |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 215 | end; |
| 216 | end; |
| 217 | |
| 218 | |
| 219 | class function Base64Utils.Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 220 | const DECODE_TABLE : array[0..$FF] of Integer |
| 221 | = ( -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 222 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 223 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, |
| 224 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, |
| 225 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, |
| 226 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, |
| 227 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, |
| 228 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, |
| 229 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 230 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 231 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 232 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 233 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 234 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 235 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 236 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ); |
| 237 | begin |
| 238 | ASSERT( len in [1..4]); |
| 239 | result := 1; |
| 240 | dst[dstOff] := ((DECODE_TABLE[src[srcOff] and $0FF] shl 2) |
| 241 | or (DECODE_TABLE[src[srcOff + 1] and $0FF] shr 4)); |
| 242 | |
| 243 | if (len > 2) then begin |
| 244 | Inc( result); |
| 245 | Inc( dstOff); |
| 246 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 1] and $0FF] shl 4) and $F0) |
| 247 | or (DECODE_TABLE[src[srcOff + 2] and $0FF] shr 2)); |
| 248 | |
| 249 | if (len > 3) then begin |
| 250 | Inc( result); |
| 251 | Inc( dstOff); |
| 252 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 2] and $0FF] shl 6) and $C0) |
| 253 | or DECODE_TABLE[src[srcOff + 3] and $0FF]); |
| 254 | end; |
| 255 | end; |
| 256 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 257 | |
| 258 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 259 | class function CharUtils.IsHighSurrogate( const c : Char) : Boolean; |
| 260 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 261 | {$IF CompilerVersion < 25.0} |
| 262 | {$IFDEF OLD_UNIT_NAMES} |
| 263 | result := Character.IsHighSurrogate(c); |
| 264 | {$ELSE} |
| 265 | result := System.Character.IsHighSurrogate(c); |
| 266 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 267 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 268 | result := c.IsHighSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 269 | {$IFEND} |
| 270 | end; |
| 271 | |
| 272 | |
| 273 | class function CharUtils.IsLowSurrogate( const c : Char) : Boolean; |
| 274 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 275 | {$IF CompilerVersion < 25.0} |
| 276 | {$IFDEF OLD_UNIT_NAMES} |
| 277 | result := Character.IsLowSurrogate(c); |
| 278 | {$ELSE} |
| 279 | result := System.Character.IsLowSurrogate(c); |
| 280 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 281 | {$ELSE} |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 282 | result := c.IsLowSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 283 | {$IFEND} |
| 284 | end; |
| 285 | |
| 286 | |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 287 | class function CharUtils.IsHtmlDoctype( const fourBytes : Integer) : Boolean; |
| 288 | var pc : PAnsiChar; |
| 289 | const HTML_BEGIN : PAnsiChar = 'OD!<'; // first 4 bytes of '<!DOCTYPE ' in LE byte order |
| 290 | begin |
| 291 | pc := @fourBytes; |
| 292 | |
| 293 | if UpCase(pc^) <> HTML_BEGIN[0] |
| 294 | then Exit(FALSE); |
| 295 | |
| 296 | Inc( pc); |
| 297 | if UpCase(pc^) <> HTML_BEGIN[1] |
| 298 | then Exit(FALSE); |
| 299 | |
| 300 | |
| 301 | Inc( pc); |
| 302 | if UpCase(pc^) <> HTML_BEGIN[2] |
| 303 | then Exit(FALSE); |
| 304 | |
| 305 | Inc( pc); |
| 306 | result := (UpCase(pc^) = HTML_BEGIN[3]); |
| 307 | end; |
| 308 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 309 | { IntegerUtils } |
| 310 | |
| 311 | |
| 312 | class procedure IntegerUtils.SwapBytes( var one, two : Byte); |
| 313 | var tmp : Byte; |
| 314 | begin |
| 315 | tmp := one; |
| 316 | one := two; |
| 317 | two := tmp; |
| 318 | end; |
| 319 | |
| 320 | |
| 321 | class procedure IntegerUtils.Swap2( const pValue : Pointer); |
| 322 | var pData : PByteArray absolute pValue; |
| 323 | begin |
| 324 | SwapBytes( pData^[0], pData^[1]); |
| 325 | end; |
| 326 | |
| 327 | |
| 328 | class procedure IntegerUtils.Swap4( const pValue : Pointer); |
| 329 | var pData : PByteArray absolute pValue; |
| 330 | begin |
| 331 | SwapBytes( pData^[0], pData^[3]); |
| 332 | SwapBytes( pData^[1], pData^[2]); |
| 333 | end; |
| 334 | |
| 335 | |
| 336 | class procedure IntegerUtils.Swap8( const pValue : Pointer); |
| 337 | var pData : PByteArray absolute pValue; |
| 338 | begin |
| 339 | SwapBytes( pData^[0], pData^[7]); |
| 340 | SwapBytes( pData^[1], pData^[6]); |
| 341 | SwapBytes( pData^[2], pData^[5]); |
| 342 | SwapBytes( pData^[3], pData^[4]); |
| 343 | end; |
| 344 | |
| 345 | |
| 346 | class procedure IntegerUtils.SwapByteOrder( const pValue : Pointer; const size : Integer); |
| 347 | begin |
| 348 | case size of |
| 349 | 2 : Swap2( pValue); |
| 350 | 4 : Swap4( pValue); |
| 351 | 8 : Swap8( pValue); |
| 352 | else |
| 353 | raise EArgumentException.Create('Unexpected size'); |
| 354 | end; |
| 355 | end; |
| 356 | |
| 357 | |
| 358 | { TGuidHelper } |
| 359 | |
| 360 | |
| 361 | function TGuidHelper.SwapByteOrder : TGuid; |
| 362 | // convert to/from network byte order |
| 363 | // - https://www.ietf.org/rfc/rfc4122.txt |
| 364 | // - https://stackoverflow.com/questions/10850075/guid-uuid-compatibility-issue-between-net-and-linux |
| 365 | // - https://lists.gnu.org/archive/html/bug-parted/2002-01/msg00099.html |
| 366 | begin |
| 367 | result := Self; |
| 368 | |
| 369 | IntegerUtils.SwapByteOrder( @result.D1, SizeOf(result.D1)); |
| 370 | IntegerUtils.SwapByteOrder( @result.D2, SizeOf(result.D2)); |
| 371 | IntegerUtils.SwapByteOrder( @result.D3, SizeOf(result.D3)); |
| 372 | //result.D4 = array of byte -> implicitly correct |
| 373 | end; |
| 374 | |
| 375 | |
| 376 | {$IFDEF Debug} |
| 377 | class procedure TGuidHelper.SelfTest; |
| 378 | var guid : TGuid; |
| 379 | pBytes : PByteArray; |
| 380 | i, expected : Integer; |
| 381 | const TEST_GUID : TGuid = '{00112233-4455-6677-8899-aabbccddeeff}'; |
| 382 | begin |
| 383 | // host to network |
| 384 | guid := TEST_GUID; |
| 385 | guid := guid.SwapByteOrder; |
| 386 | |
| 387 | // validate network order |
| 388 | pBytes := @guid; |
| 389 | for i := 0 to $F do begin |
| 390 | expected := i * $11; |
| 391 | ASSERT( pBytes^[i] = expected); |
| 392 | end; |
| 393 | |
| 394 | // network to host and final validation |
| 395 | guid := guid.SwapByteOrder; |
| 396 | ASSERT( IsEqualGuid( guid, TEST_GUID)); |
| 397 | end; |
| 398 | {$ENDIF} |
| 399 | |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 400 | |
| 401 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 402 | {$IFDEF Win64} |
| 403 | |
| 404 | function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; inline; |
| 405 | begin |
| 406 | {$IFDEF OLD_UNIT_NAMES} |
| 407 | result := Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 408 | {$ELSE} |
| 409 | result := WinApi.Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 410 | {$ENDIF} |
| 411 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 412 | |
| 413 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 414 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 415 | var old : Int64; |
| 416 | begin |
| 417 | repeat |
| 418 | Old := Addend; |
| 419 | until (InterlockedCompareExchange64( Addend, Old + Value, Old) = Old); |
| 420 | result := Old; |
| 421 | end; |
| 422 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 423 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 424 | |
| 425 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 426 | { EnumUtils<T> } |
| 427 | |
| 428 | class function EnumUtils<T>.ToString(const value : Integer) : string; |
| 429 | var pType : PTypeInfo; |
| 430 | begin |
| 431 | pType := PTypeInfo(TypeInfo(T)); |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 432 | if Assigned(pType) |
| 433 | and (pType^.Kind = tkEnumeration) |
| 434 | {$IF CompilerVersion >= 23.0} // TODO: Range correct? What we know is that XE does not offer it, but Rio has it |
| 435 | and (pType^.TypeData^.MaxValue >= value) |
| 436 | and (pType^.TypeData^.MinValue <= value) |
| 437 | {$ELSE} |
| 438 | and FALSE // THRIFT-5048: pType^.TypeData^ member not supported -> prevent GetEnumName() from reading outside the legal range |
| 439 | {$IFEND} |
| 440 | then result := GetEnumName( PTypeInfo(pType), value) |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 441 | else result := IntToStr(Ord(value)); |
| 442 | end; |
| 443 | |
| 444 | |
| 445 | { StringUtils<T> } |
| 446 | |
| 447 | class function StringUtils<T>.ToString(const value : T) : string; |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 448 | type PInterface = ^IInterface; |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 449 | var pType : PTypeInfo; |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 450 | stos : ISupportsToString; |
| 451 | pIntf : PInterface; // Workaround: Rio does not allow the direct typecast |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 452 | begin |
| 453 | pType := PTypeInfo(TypeInfo(T)); |
| 454 | if Assigned(pType) then begin |
| 455 | case pType^.Kind of |
Jens Geyer | ec57271 | 2020-07-13 21:15:31 +0200 | [diff] [blame] | 456 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 457 | tkInterface : begin |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 458 | pIntf := PInterface(@value); |
| 459 | if Supports( pIntf^, ISupportsToString, stos) then begin |
| 460 | result := stos.toString; |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 461 | Exit; |
| 462 | end; |
| 463 | end; |
Jens Geyer | ec57271 | 2020-07-13 21:15:31 +0200 | [diff] [blame] | 464 | |
| 465 | tkEnumeration : begin |
| 466 | case SizeOf(value) of |
| 467 | 1 : begin result := EnumUtils<T>.ToString( PShortInt(@value)^); Exit; end; |
| 468 | 2 : begin result := EnumUtils<T>.ToString( PSmallInt(@value)^); Exit; end; |
| 469 | 4 : begin result := EnumUtils<T>.ToString( PLongInt(@value)^); Exit; end; |
| 470 | else |
| 471 | ASSERT(FALSE); // in theory, this should not happen |
| 472 | end; |
| 473 | end; |
| 474 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 475 | end; |
| 476 | end; |
| 477 | |
| 478 | result := TValue.From<T>(value).ToString; |
| 479 | end; |
| 480 | |
| 481 | |
| 482 | { TThriftStringBuilder } |
| 483 | |
| 484 | function TThriftStringBuilder.Append(const Value: TBytes): TStringBuilder; |
| 485 | begin |
| 486 | Result := Append( string( RawByteString(Value)) ); |
| 487 | end; |
| 488 | |
| 489 | function TThriftStringBuilder.Append( const Value: ISupportsToString): TStringBuilder; |
| 490 | begin |
| 491 | Result := Append( Value.ToString ); |
| 492 | end; |
| 493 | |
| 494 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 495 | begin |
| 496 | {$IFDEF Debug} |
| 497 | TGuid.SelfTest; |
| 498 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 499 | end. |