zeoslib  UNKNOWN
 All Files
ZClasses.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Core classes and interfaces }
5 { }
6 { Originally written by Sergey Seroukhov }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
20 { }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
39 { }
40 { }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZClasses;
53 
54 interface
55 
56 {$I ZCore.inc}
57 
58 uses
59  SysUtils, Classes;
60 
61 const
62  ZEOS_MAJOR_VERSION = 7;
63  ZEOS_MINOR_VERSION = 1;
64  ZEOS_SUB_VERSION = 4;
65  ZEOS_STATUS = 'stable';
66  ZEOS_VERSION = '7.1.4-stable';
67 {$IFDEF ENABLE_POOLED}
68  {Pooled Protocol Prefix, including final dot}
69  PooledPrefix = 'pooled.';
70 {$ENDIF}
71 
72 
73 type
74  {$IFDEF OLDFPC}
75  PDateTime = ^TDateTime;
76 
77  TAggregatedObject = class(TObject)
78  private
79  FController: Pointer;
80  function GetController: IInterface;
81  protected
82  {$IFDEF FPC2_5UP}
83  function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; virtual;
84  function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
85  function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
86  {$ELSE}
87  function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
88  function _AddRef : longint;stdcall;
89  function _Release : longint;stdcall;
90  {$ENDIF}
91  public
92  constructor Create(const Controller: IInterface);
93  property Controller: IInterface read GetController;
94  end;
95 
96  TContainedObject = class(TAggregatedObject, IInterface)
97  protected
98  {$IFDEF FPC2_5UP}
99  function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; override;
100  {$ELSE}
101  function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
102  {$ENDIF}
103 
104  end;
105  {$ENDIF}
106 
107  {** Replacement for generic interface type. }
108  IZInterface = IUnknown;
109 
110  {** Represents an interface for all abstract object. }
111  IZObject = interface(IZInterface)
112  ['{EF46E5F7-00CF-4DDA-BED0-057D6686AEE0}']
113  function Equals(const Value: IZInterface): Boolean;
114  function GetHashCode: LongInt;
115  function Clone: IZInterface;
116  function ToString: string;
117  function InstanceOf(const IId: TGUID): Boolean;
118  end;
119 
120  {** Represents a fake interface for coparable objects. }
121  IZComparable = interface(IZObject)
122  ['{04112081-F07B-4BBF-A757-817816EB67C1}']
123  end;
124 
125  {** Represents an interface to clone objects. }
126  IZClonnable = interface(IZObject)
127  ['{ECB7F3A4-7B2E-4130-BA66-54A2D43C0149}']
128  end;
129 
130  {** Represents a generic collection iterator interface. }
131  IZIterator = interface(IZObject)
132  ['{D964DDD0-2308-4D9B-BD36-5810632512F7}']
133  function HasNext: Boolean;
134  function Next: IZInterface;
135  end;
136 
137  {** Represents a collection of object interfaces. }
138  IZCollection = interface(IZClonnable)
139  ['{51417C87-F992-4CAD-BC53-CF3925DD6E4C}']
140 
141  function Get(Index: Integer): IZInterface;
142  procedure Put(Index: Integer; const Item: IZInterface);
143  function IndexOf(const Item: IZInterface): Integer;
144  function GetCount: Integer;
145  function GetIterator: IZIterator;
146 
147  function First: IZInterface;
148  function Last: IZInterface;
149 
150  function Add(const Item: IZInterface): Integer;
151  procedure Insert(Index: Integer; const Item: IZInterface);
152  function Remove(const Item: IZInterface): Integer;
153 
154  procedure Exchange(Index1, Index2: Integer);
155  procedure Delete(Index: Integer);
156  procedure Clear;
157 
158  function Contains(const Item: IZInterface): Boolean;
159  function ContainsAll(const Col: IZCollection): Boolean;
160  function AddAll(const Col: IZCollection): Boolean;
161  function RemoveAll(const Col: IZCollection): Boolean;
162 
163  property Count: Integer read GetCount;
164  property Items[Index: Integer]: IZInterface read Get write Put; default;
165  end;
166 
167  {** Represents a hash map interface. }
168  IZHashMap = interface(IZClonnable)
169  ['{782C64F4-AD09-4F56-AF2B-E4193A05BBCE}']
170 
171  function Get(const Key: IZInterface): IZInterface;
172  procedure Put(const Key: IZInterface; const Value: IZInterface);
173  function GetKeys: IZCollection;
174  function GetValues: IZCollection;
175  function GetCount: Integer;
176 
177  function Remove(Key: IZInterface): Boolean;
178  procedure Clear;
179 
180  property Count: Integer read GetCount;
181  property Keys: IZCollection read GetKeys;
182  property Values: IZCollection read GetValues;
183  end;
184 
185  {** Represents a stack interface. }
186  IZStack = interface(IZClonnable)
187  ['{8FEA0B3F-0C02-4E70-BD8D-FB0F42D4497B}']
188 
189  function Peek: IZInterface;
190  function Pop: IZInterface;
191  procedure Push(Value: IZInterface);
192  function GetCount: Integer;
193 
194  property Count: Integer read GetCount;
195  end;
196 
197 {$IFDEF WITH_NEWTOBJECT} // to suppress the overload warning of the Equals overload, Marco. (overload a non overload-declared funtion)
198  {$WARNINGS OFF}
199 {$ENDIF}
200  {** Implements an abstract interfaced object. }
201  TZAbstractObject = class(TInterfacedObject, IZObject)
202  public
203  function Equals(const Value: IZInterface): Boolean; {$IFDEF WITH_NEWTOBJECT}overload;{$ENDIF} virtual;
204  function GetHashCode: LongInt;
205  function Clone: IZInterface; virtual;
206  function ToString: string;{$IFDEF WITH_NEWTOBJECT}override{$ELSE} virtual{$ENDIF} ;
207  function InstanceOf(const IId: TGUID): Boolean;
208  end;
209 {$IFDEF WITH_NEWTOBJECT}
210  {$WARNINGS ON}
211 {$ENDIF}
212 
213 implementation
214 
215 uses ZMessages, ZCompatibility;
216 
217 {$IFDEF oldFPC}
218 
219 { TAggregatedObject }
220 
221 constructor TAggregatedObject.Create(const Controller: IInterface);
222 begin
223  FController := Pointer(Controller);
224 end;
225 
226 function TAggregatedObject.GetController: IInterface;
227 begin
228  Result := IInterface(FController);
229 end;
230 
231 {$IFDEF FPC2_5UP}
232 function TAggregatedObject.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;
233 {$ELSE}
234 function TAggregatedObject.QueryInterface(const iid : tguid;out obj) : longint;stdcall;
235 {$ENDIF}
236 begin
237  Result := IInterface(FController).QueryInterface(IID, Obj);
238 end;
239 
240 function TAggregatedObject._AddRef: longint;
241 begin
242  Result := IInterface(FController)._AddRef;
243 end;
244 
245 function TAggregatedObject._Release : longint;
246 begin
247  Result := IInterface(FController)._Release;
248 end;
249 
250 { TContainedObject }
251 
252 {$IFDEF FPC2_5UP}
253 function TContainedObject.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
254 {$ELSE}
255 function TContainedObject.QueryInterface(const iid : tguid;out obj) : longint;stdcall;
256 {$ENDIF}
257 
258 begin
259  if GetInterface(IID, Obj) then
260  Result := S_OK
261  else
262  Result := E_NOINTERFACE;
263 end;
264 
265 {$ENDIF}
266 
267 { TZAbstractObject }
268 
269 {**
270  Checks is the specified value equals to this object.
271  @param Value an interface to some object.
272  @return <code>True</code> if the objects are identical.
273 }
274 function TZAbstractObject.Equals(const Value: IZInterface): Boolean;
275 begin
276  if Value <> nil then
277  begin
278  Result := (IZInterface(Self) = Value)
279  or ((Self as IZInterface) = (Value as IZInterface));
280  end else
281  Result := False;
282 end;
283 
284 {**
285  Gets a unique hash for this object.
286  @return a unique hash for this object.
287 }
288 function TZAbstractObject.GetHashCode: LongInt;
289 begin
290  Result := LongInt(Self);
291 end;
292 
293 {**
294  Clones an object instance.
295  @return a clonned object instance.
296 }
297 function TZAbstractObject.Clone: IZInterface;
298 begin
299  raise Exception.Create(SClonningIsNotSupported);
300  result := nil;
301 end;
302 
303 {**
304  Checks is this object implements a specified interface.
305  @param IId an interface id.
306  @return <code>True</code> if this object support the interface.
307 }
308 function TZAbstractObject.InstanceOf(const IId: TGUID): Boolean;
309 begin
310  Result := GetInterfaceEntry(IId) <> nil;
311 end;
312 
313 {**
314  Converts this object into the string representation.
315  @return a string representation for this object.
316 }
317 function TZAbstractObject.ToString: string;
318 begin
319  Result := Format('%s <%p>', [ClassName, Pointer(Self)])
320 end;
321 
322 end.
323