objcrtlutils.pas 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. {$IFNDEF FPC_DOTTEDUNITS}
  9. unit objcrtlutils;
  10. {$ENDIF FPC_DOTTEDUNITS}
  11. {$mode objfpc}{$H+}
  12. interface
  13. {$IFDEF FPC_DOTTEDUNITS}
  14. uses
  15. Api.ObjC.Rtl;
  16. {$ELSE FPC_DOTTEDUNITS}
  17. uses
  18. objcrtl;
  19. {$ENDIF FPC_DOTTEDUNITS}
  20. function alloc(classname: PAnsiChar): id; inline;
  21. function allocex(classname: PAnsiChar; extraBytes: Integer): id; inline;
  22. function objcclass(classname: PAnsiChar): _class; inline;
  23. function selector(cmdname: PAnsiChar): SEL; inline;
  24. procedure release(objc: id); inline;
  25. function AllocAndInit(classname: PAnsiChar): id; inline;
  26. function AllocAndInitEx(classname: PAnsiChar; extraBytes: Integer): id; inline;
  27. function super(obj: id): objc_super;
  28. implementation
  29. var
  30. SEL_alloc : SEL = nil;
  31. SEL_init : SEL = nil;
  32. SEL_release : SEL = nil;
  33. function super(obj: id): objc_super;
  34. begin
  35. Result.reciever := obj;
  36. Result.class_ := class_getSuperclass(object_getClass(obj));
  37. end;
  38. function allocex(classname: PAnsiChar; extraBytes: Integer): id; inline;
  39. begin
  40. Result := class_createInstance( objcclass(classname), extraBytes);
  41. end;
  42. function alloc(classname: PAnsiChar): id; inline;
  43. begin
  44. Result := allocex(classname, 0);
  45. // Result := objc_msgSend( objc_getClass(classname), selector('alloc'), []);
  46. end;
  47. function objcclass(classname: PAnsiChar): _class; inline;
  48. begin
  49. Result := _class(objc_getClass(classname));
  50. end;
  51. function selector(cmdname: PAnsiChar): SEL; inline;
  52. begin
  53. Result := sel_registerName(cmdname);
  54. end;
  55. procedure release(objc: id); inline;
  56. begin
  57. if SEL_release=nil then SEL_release := selector('release');
  58. objc_msgSend(objc, SEL_release, []);
  59. end;
  60. function AllocAndInit(classname: PAnsiChar): id; inline;
  61. begin
  62. if SEL_init=nil then SEL_init := selector('init');
  63. Result:= objc_msgSend( alloc( classname ), SEL_init, []);
  64. end;
  65. function AllocAndInitEx(classname: PAnsiChar; extraBytes: Integer): id; inline;
  66. begin
  67. if SEL_init=nil then SEL_init := selector('init');
  68. Result := objc_msgSend( allocEx( classname, extraBytes ), SEL_init, []);
  69. end;
  70. end.