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 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, SyncObjs; |
| 26 | |
| 27 | type |
| 28 | IOverlappedHelper = interface |
| 29 | ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}'] |
| 30 | function Overlapped : TOverlapped; |
| 31 | function OverlappedPtr : POverlapped; |
| 32 | function WaitHandle : THandle; |
| 33 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 34 | end; |
| 35 | |
| 36 | TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper) |
| 37 | strict protected |
| 38 | FOverlapped : TOverlapped; |
| 39 | FEvent : TEvent; |
| 40 | |
| 41 | // IOverlappedHelper |
| 42 | function Overlapped : TOverlapped; |
| 43 | function OverlappedPtr : POverlapped; |
| 44 | function WaitHandle : THandle; |
| 45 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 46 | public |
| 47 | constructor Create; |
| 48 | destructor Destroy; override; |
| 49 | end; |
| 50 | |
| 51 | |
| 52 | |
| 53 | function IfValue(B: Boolean; const TrueValue, FalseValue: WideString): string; |
| 54 | |
| 55 | implementation |
| 56 | |
| 57 | |
| 58 | function IfValue(B: Boolean; const TrueValue, FalseValue: WideString): string; |
| 59 | begin |
| 60 | if B then |
| 61 | Result := TrueValue |
| 62 | else |
| 63 | Result := FalseValue; |
| 64 | end; |
| 65 | |
| 66 | |
| 67 | { TOverlappedHelperImpl } |
| 68 | |
| 69 | constructor TOverlappedHelperImpl.Create; |
| 70 | begin |
| 71 | inherited Create; |
| 72 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 73 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 74 | FOverlapped.hEvent := FEvent.Handle; |
| 75 | end; |
| 76 | |
| 77 | |
| 78 | |
| 79 | destructor TOverlappedHelperImpl.Destroy; |
| 80 | begin |
| 81 | try |
| 82 | FOverlapped.hEvent := 0; |
| 83 | FreeAndNil( FEvent); |
| 84 | |
| 85 | finally |
| 86 | inherited Destroy; |
| 87 | end; |
| 88 | |
| 89 | end; |
| 90 | |
| 91 | |
| 92 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 93 | begin |
| 94 | result := FOverlapped; |
| 95 | end; |
| 96 | |
| 97 | |
| 98 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 99 | begin |
| 100 | result := @FOverlapped; |
| 101 | end; |
| 102 | |
| 103 | |
| 104 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 105 | begin |
| 106 | result := FOverlapped.hEvent; |
| 107 | end; |
| 108 | |
| 109 | |
| 110 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 111 | begin |
| 112 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 113 | end; |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | end. |