objcrtlutils.pas 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {
  2. objcrtlutils.pas
  3. Copyright (C) 2009 Dmitry Boyarintsev
  4. This unit is implementation for dynamic Objective-C Run-time Library based on run-time version 2.0
  5. headers included with XCode 3.1.2
  6. The original copyright note of is kept on each include file
  7. }
  8. unit objcrtlutils;
  9. {$mode objfpc}{$H+}
  10. interface
  11. uses
  12. objcrtl;
  13. function alloc(classname: PChar): id; inline;
  14. function allocex(classname: PChar; extraBytes: Integer): id; inline;
  15. function objcclass(classname: PChar): _class; inline;
  16. function selector(cmdname: PChar): SEL; inline;
  17. procedure release(objc: id); inline;
  18. function AllocAndInit(classname: PChar): id; inline;
  19. function AllocAndInitEx(classname: PChar; extraBytes: Integer): id; inline;
  20. function super(obj: id): objc_super;
  21. implementation
  22. var
  23. SEL_alloc : SEL = nil;
  24. SEL_init : SEL = nil;
  25. SEL_release : SEL = nil;
  26. function super(obj: id): objc_super;
  27. begin
  28. Result.reciever := obj;
  29. Result.class_ := class_getSuperclass(object_getClass(obj));
  30. end;
  31. function allocex(classname: PChar; extraBytes: Integer): id; inline;
  32. begin
  33. Result := class_createInstance( objcclass(classname), extraBytes);
  34. end;
  35. function alloc(classname: PChar): id; inline;
  36. begin
  37. Result := allocex(classname, 0);
  38. // Result := objc_msgSend( objc_getClass(classname), selector('alloc'), []);
  39. end;
  40. function objcclass(classname: PChar): _class; inline;
  41. begin
  42. Result := _class(objc_getClass(classname));
  43. end;
  44. function selector(cmdname: PChar): SEL; inline;
  45. begin
  46. Result := sel_registerName(cmdname);
  47. end;
  48. procedure release(objc: id); inline;
  49. begin
  50. if SEL_release=nil then SEL_release := selector('release');
  51. objc_msgSend(objc, SEL_release, []);
  52. end;
  53. function AllocAndInit(classname: PChar): id; inline;
  54. begin
  55. if SEL_init=nil then SEL_init := selector('init');
  56. Result:= objc_msgSend( alloc( classname ), SEL_init, []);
  57. end;
  58. function AllocAndInitEx(classname: PChar; extraBytes: Integer): id; inline;
  59. begin
  60. if SEL_init=nil then SEL_init := selector('init');
  61. Result := objc_msgSend( allocEx( classname, extraBytes ), SEL_init, []);
  62. end;
  63. end.