objpas.pp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1997,98 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { this unit makes Free Pascal as much as possible Delphi compatible }
  13. unit objpas;
  14. interface
  15. type
  16. { first, in object pascal, the types must be redefined }
  17. smallint = system.integer;
  18. integer = system.longint;
  19. { define some more types }
  20. shortstring = string;
  21. { some pointer definitions }
  22. pshortstring = ^shortstring;
  23. // pansistring = ^ansistring;
  24. // pwidestring = ^widestring;
  25. // pstring = pansistring;
  26. pextended = ^extended;
  27. { now the let's declare the base classes for the class object }
  28. { model }
  29. tobject = class;
  30. tclass = class of tobject;
  31. tobject = class
  32. { please don't change the order of virtual methods, because }
  33. { their vmt offsets are used by some assembler code which uses }
  34. { hard coded addresses (FK) }
  35. constructor create;
  36. destructor destroy;virtual;
  37. class function newinstance : tobject;virtual;
  38. procedure freeinstance;virtual;
  39. procedure free;
  40. class function initinstance(instance : pointer) : tobject;
  41. procedure cleanupinstance;
  42. function classtype : tclass;
  43. class function classinfo : pointer;
  44. class function classname : shortstring;
  45. class function classnameis(const name : string) : boolean;
  46. class function classparent : tclass;
  47. class function instancesize : longint;
  48. class function inheritsfrom(aclass : tclass) : boolean;
  49. { message handling routines }
  50. procedure dispatch(var message);
  51. procedure defaulthandler(var message);virtual;
  52. class function methodaddress(const name : shortstring) : pointer;
  53. class function methodname(address : pointer) : shortstring;
  54. function fieldaddress(const name : shortstring) : pointer;
  55. { interface functions, I don't know if we need this }
  56. {
  57. function getinterface(const iid : tguid;out obj) : boolean;
  58. class function getinterfaceentry(const iid : tguid) : pinterfaceentry;
  59. class function getinterfacetable : pinterfacetable;
  60. }
  61. function safecallexception(exceptobject : tobject;
  62. exceptaddr : pointer) : integer;virtual;
  63. end;
  64. TExceptProc = Procedure (Obj : TObject; Addr: Pointer);
  65. var
  66. abstracterrorproc : pointer;
  67. Const
  68. ExceptProc : Pointer {TExceptProc} = Nil;
  69. implementation
  70. { the reverse order of the parameters make code generation easier }
  71. function _is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'DO_IS'];
  72. begin
  73. _is:=aobject.inheritsfrom(aclass);
  74. end;
  75. { the reverse order of the parameters make code generation easier }
  76. procedure _as(aclass : tclass;aobject : tobject);[public,alias: 'DO_AS'];
  77. begin
  78. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  79. { throw an exception }
  80. end;
  81. procedure abstracterror;[public,alias: 'ABSTRACTERROR'];
  82. type
  83. proc = procedure;
  84. begin
  85. if assigned(abstracterrorproc) then
  86. proc(abstracterrorproc)()
  87. else
  88. runerror(210);
  89. end;
  90. {************************************************************************}
  91. { TOBJECT }
  92. {************************************************************************}
  93. constructor tobject.create;
  94. begin
  95. end;
  96. destructor tobject.destroy;
  97. begin
  98. end;
  99. procedure tobject.free;
  100. begin
  101. // the call via self avoids a warning
  102. if self<>nil then
  103. self.destroy;
  104. end;
  105. class function tobject.instancesize : longint;
  106. type
  107. plongint = ^longint;
  108. begin
  109. { type of self is class of tobject => it points to the vmt }
  110. { the size is saved at offset 0 }
  111. instancesize:=plongint(self)^;
  112. end;
  113. class function tobject.initinstance(instance : pointer) : tobject;
  114. type
  115. ppointer = ^pointer;
  116. begin
  117. fillchar(instance^,self.instancesize,0);
  118. { insert VMT pointer into the new created memory area }
  119. { (in class methods self contains the VMT!) }
  120. ppointer(instance)^:=pointer(self);
  121. initinstance:=tobject(instance);
  122. end;
  123. class function tobject.classparent : tclass;
  124. type
  125. ptclass = ^tclass;
  126. begin
  127. { type of self is class of tobject => it points to the vmt }
  128. { the parent vmt is saved at offset 8 }
  129. classparent:=(ptclass(self)+8)^;
  130. end;
  131. class function tobject.newinstance : tobject;
  132. var
  133. p : pointer;
  134. begin
  135. getmem(p,instancesize);
  136. initinstance(p);
  137. newinstance:=tobject(p);
  138. end;
  139. procedure tobject.freeinstance;
  140. var
  141. p : pointer;
  142. begin
  143. { !!! we should finalize some data }
  144. { self is a register, so we can't pass it call by reference }
  145. p:=pointer(self);
  146. freemem(p,instancesize);
  147. end;
  148. function tobject.classtype : tclass;
  149. begin
  150. classtype:=tclass(pointer(self)^)
  151. end;
  152. class function tobject.methodaddress(const name : shortstring) : pointer;
  153. begin
  154. methodaddress:=nil;
  155. end;
  156. class function tobject.methodname(address : pointer) : shortstring;
  157. begin
  158. methodname:='';
  159. end;
  160. function tobject.fieldaddress(const name : shortstring) : pointer;
  161. begin
  162. fieldaddress:=nil;
  163. end;
  164. function tobject.safecallexception(exceptobject : tobject;
  165. exceptaddr : pointer) : integer;
  166. begin
  167. safecallexception:=0;
  168. end;
  169. class function tobject.classinfo : pointer;
  170. begin
  171. classinfo:=nil;
  172. end;
  173. class function tobject.classname : shortstring;
  174. begin
  175. classname:='';
  176. end;
  177. class function tobject.classnameis(const name : string) : boolean;
  178. begin
  179. classnameis:=classname=name;
  180. end;
  181. class function tobject.inheritsfrom(aclass : tclass) : boolean;
  182. var
  183. c : tclass;
  184. begin
  185. c:=self;
  186. while assigned(c) do
  187. begin
  188. if c=aclass then
  189. begin
  190. inheritsfrom:=true;
  191. exit;
  192. end;
  193. c:=c.classparent;
  194. end;
  195. inheritsfrom:=false;
  196. end;
  197. procedure tobject.dispatch(var message);
  198. begin
  199. end;
  200. procedure tobject.defaulthandler(var message);
  201. begin
  202. end;
  203. procedure tobject.cleanupinstance;
  204. begin
  205. end;
  206. {$i except.inc}
  207. begin
  208. InitExceptions
  209. end.
  210. {
  211. $Log$
  212. Revision 1.5 1998-07-30 16:10:11 michael
  213. + Added support for ExceptProc+
  214. Revision 1.4 1998/07/29 15:44:33 michael
  215. included sysutils and math.pp as target. They compile now.
  216. Revision 1.3 1998/07/29 10:09:28 michael
  217. + put in exception support
  218. Revision 1.2 1998/03/25 23:40:24 florian
  219. + stuff from old objpash.inc and objpas.inc merged in
  220. }