blob: 58ffe792128849c2843ef2dcfe2464c72437ff68 [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
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
20unit Thrift.Utils;
21
22interface
23
24uses
25 Classes, Windows, SysUtils, SyncObjs;
26
27type
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
53function IfValue(B: Boolean; const TrueValue, FalseValue: WideString): string;
54
55implementation
56
57
58function IfValue(B: Boolean; const TrueValue, FalseValue: WideString): string;
59begin
60 if B then
61 Result := TrueValue
62 else
63 Result := FalseValue;
64end;
65
66
67{ TOverlappedHelperImpl }
68
69constructor TOverlappedHelperImpl.Create;
70begin
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;
75end;
76
77
78
79destructor TOverlappedHelperImpl.Destroy;
80begin
81 try
82 FOverlapped.hEvent := 0;
83 FreeAndNil( FEvent);
84
85 finally
86 inherited Destroy;
87 end;
88
89end;
90
91
92function TOverlappedHelperImpl.Overlapped : TOverlapped;
93begin
94 result := FOverlapped;
95end;
96
97
98function TOverlappedHelperImpl.OverlappedPtr : POverlapped;
99begin
100 result := @FOverlapped;
101end;
102
103
104function TOverlappedHelperImpl.WaitHandle : THandle;
105begin
106 result := FOverlapped.hEvent;
107end;
108
109
110function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD;
111begin
112 result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout);
113end;
114
115
116
117
118end.