objpas.pp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. var
  65. abstracterrorproc : pointer;
  66. implementation
  67. { the reverse order of the parameters make code generation easier }
  68. function _is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'DO_IS'];
  69. begin
  70. _is:=aobject.inheritsfrom(aclass);
  71. end;
  72. { the reverse order of the parameters make code generation easier }
  73. procedure _as(aclass : tclass;aobject : tobject);[public,alias: 'DO_AS'];
  74. begin
  75. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  76. { throw an exception }
  77. end;
  78. procedure abstracterror;[public,alias: 'ABSTRACTERROR'];
  79. type
  80. proc = procedure;
  81. begin
  82. if assigned(abstracterrorproc) then
  83. proc(abstracterrorproc)()
  84. else
  85. runerror(210);
  86. end;
  87. {************************************************************************}
  88. { TOBJECT }
  89. {************************************************************************}
  90. constructor tobject.create;
  91. begin
  92. end;
  93. destructor tobject.destroy;
  94. begin
  95. end;
  96. procedure tobject.free;
  97. begin
  98. // the call via self avoids a warning
  99. if self<>nil then
  100. self.destroy;
  101. end;
  102. class function tobject.instancesize : longint;
  103. type
  104. plongint = ^longint;
  105. begin
  106. { type of self is class of tobject => it points to the vmt }
  107. { the size is saved at offset 0 }
  108. instancesize:=plongint(self)^;
  109. end;
  110. class function tobject.initinstance(instance : pointer) : tobject;
  111. type
  112. ppointer = ^pointer;
  113. begin
  114. fillchar(instance^,self.instancesize,0);
  115. { insert VMT pointer into the new created memory area }
  116. { (in class methods self contains the VMT!) }
  117. ppointer(instance)^:=pointer(self);
  118. initinstance:=tobject(instance);
  119. end;
  120. class function tobject.classparent : tclass;
  121. type
  122. ptclass = ^tclass;
  123. begin
  124. { type of self is class of tobject => it points to the vmt }
  125. { the parent vmt is saved at offset 8 }
  126. classparent:=(ptclass(self)+8)^;
  127. end;
  128. class function tobject.newinstance : tobject;
  129. var
  130. p : pointer;
  131. begin
  132. getmem(p,instancesize);
  133. initinstance(p);
  134. newinstance:=tobject(p);
  135. end;
  136. procedure tobject.freeinstance;
  137. var
  138. p : pointer;
  139. begin
  140. { !!! we should finalize some data }
  141. { self is a register, so we can't pass it call by reference }
  142. p:=pointer(self);
  143. freemem(p,instancesize);
  144. end;
  145. function tobject.classtype : tclass;
  146. begin
  147. classtype:=tclass(pointer(self)^)
  148. end;
  149. class function tobject.methodaddress(const name : shortstring) : pointer;
  150. begin
  151. methodaddress:=nil;
  152. end;
  153. class function tobject.methodname(address : pointer) : shortstring;
  154. begin
  155. methodname:='';
  156. end;
  157. function tobject.fieldaddress(const name : shortstring) : pointer;
  158. begin
  159. fieldaddress:=nil;
  160. end;
  161. function tobject.safecallexception(exceptobject : tobject;
  162. exceptaddr : pointer) : integer;
  163. begin
  164. safecallexception:=0;
  165. end;
  166. class function tobject.classinfo : pointer;
  167. begin
  168. classinfo:=nil;
  169. end;
  170. class function tobject.classname : shortstring;
  171. begin
  172. classname:='';
  173. end;
  174. class function tobject.classnameis(const name : string) : boolean;
  175. begin
  176. classnameis:=classname=name;
  177. end;
  178. class function tobject.inheritsfrom(aclass : tclass) : boolean;
  179. var
  180. c : tclass;
  181. begin
  182. c:=self;
  183. while assigned(c) do
  184. begin
  185. if c=aclass then
  186. begin
  187. inheritsfrom:=true;
  188. exit;
  189. end;
  190. c:=c.classparent;
  191. end;
  192. inheritsfrom:=false;
  193. end;
  194. procedure tobject.dispatch(var message);
  195. begin
  196. end;
  197. procedure tobject.defaulthandler(var message);
  198. begin
  199. end;
  200. procedure tobject.cleanupinstance;
  201. begin
  202. end;
  203. end.
  204. {
  205. $Log$
  206. Revision 1.2 1998-03-25 23:40:24 florian
  207. + stuff from old objpash.inc and objpas.inc merged in
  208. }