objc-features.info
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:13k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. This is Info file objc-features.info, produced by Makeinfo version 1.68
  2. from the input file objc-features.texi.
  3. 
  4. File: objc-features.info,  Node: Top,  Next: Executing code before main,  Up: (dir)
  5.    (dir)
  6. GNU Objective-C runtime features
  7. ********************************
  8.    This document is meant to describe some of the GNU Objective-C
  9. runtime features. It is not intended to teach you Objective-C, there
  10. are several resources on the Internet that present the language.
  11. Questions and comments about this document to Ovidiu Predescu
  12. `<ovidiu@cup.hp.com>'.
  13. * Menu:
  14. * Executing code before main::
  15. * Type encoding::
  16. * Garbage Collection::
  17. 
  18. File: objc-features.info,  Node: Executing code before main,  Next: What you can and what you cannot do in +load,  Prev: Top,  Up: Top
  19. `+load': Executing code before main
  20. ===================================
  21.    The GNU Objective-C runtime provides a way that allows you to execute
  22. code before the execution of the program enters the `main' function.
  23. The code is executed on a per-class and a per-category basis, through a
  24. special class method `+load'.
  25.    This facility is very useful if you want to initialize global
  26. variables which can be accessed by the program directly, without
  27. sending a message to the class first. The usual way to initialize
  28. global variables, in the `+initialize' method, might not be useful
  29. because `+initialize' is only called when the first message is sent to a
  30. class object, which in some cases could be too late.
  31.    Suppose for example you have a `FileStream' class that declares
  32. `Stdin', `Stdout' and `Stderr' as global variables, like below:
  33.      FileStream *Stdin = nil;
  34.      FileStream *Stdout = nil;
  35.      FileStream *Stderr = nil;
  36.      
  37.      @implementation FileStream
  38.      
  39.      + (void)initialize
  40.      {
  41.          Stdin = [[FileStream new] initWithFd:0];
  42.          Stdout = [[FileStream new] initWithFd:1];
  43.          Stderr = [[FileStream new] initWithFd:2];
  44.      }
  45.      
  46.      /* Other methods here */
  47.      @end
  48.    In this example, the initialization of `Stdin', `Stdout' and
  49. `Stderr' in `+initialize' occurs too late. The programmer can send a
  50. message to one of these objects before the variables are actually
  51. initialized, thus sending messages to the `nil' object. The
  52. `+initialize' method which actually initializes the global variables is
  53. not invoked until the first message is sent to the class object. The
  54. solution would require these variables to be initialized just before
  55. entering `main'.
  56.    The correct solution of the above problem is to use the `+load'
  57. method instead of `+initialize':
  58.      @implementation FileStream
  59.      
  60.      + (void)load
  61.      {
  62.          Stdin = [[FileStream new] initWithFd:0];
  63.          Stdout = [[FileStream new] initWithFd:1];
  64.          Stderr = [[FileStream new] initWithFd:2];
  65.      }
  66.      
  67.      /* Other methods here */
  68.      @end
  69.    The `+load' is a method that is not overridden by categories. If a
  70. class and a category of it both implement `+load', both methods are
  71. invoked.  This allows some additional initializations to be performed in
  72. a category.
  73.    This mechanism is not intended to be a replacement for `+initialize'.
  74. You should be aware of its limitations when you decide to use it
  75. instead of `+initialize'.
  76. * Menu:
  77. * What you can and what you cannot do in +load::
  78. 
  79. File: objc-features.info,  Node: What you can and what you cannot do in +load,  Next: Type encoding,  Prev: Executing code before main,  Up: Executing code before main
  80. What you can and what you cannot do in `+load'
  81. ----------------------------------------------
  82.    The +load implementation in the GNU runtime guarantees you the
  83. following things:
  84.    * you can write whatever C code you like;
  85.    * you can send messages to Objective-C constant strings (@"this is a
  86.      constant string");
  87.    * you can allocate and send messages to objects whose class is
  88.      implemented in the same file;
  89.    * the `+load' implementation of all super classes of a class are
  90.      executed before the `+load' of that class is executed;
  91.    * the `+load' implementation of a class is executed before the
  92.      `+load' implementation of any category.
  93.    In particular, the following things, even if they can work in a
  94. particular case, are not guaranteed:
  95.    * allocation of or sending messages to arbitrary objects;
  96.    * allocation of or sending messages to objects whose classes have a
  97.      category implemented in the same file;
  98.    You should make no assumptions about receiving `+load' in sibling
  99. classes when you write `+load' of a class. The order in which sibling
  100. classes receive `+load' is not guaranteed.
  101.    The order in which `+load' and `+initialize' are called could be
  102. problematic if this matters. If you don't allocate objects inside
  103. `+load', it is guaranteed that `+load' is called before `+initialize'.
  104. If you create an object inside `+load' the `+initialize' method of
  105. object's class is invoked even if `+load' was not invoked. Note if you
  106. explicitly call `+load' on a class, `+initialize' will be called first.
  107. To avoid possible problems try to implement only one of these methods.
  108.    The `+load' method is also invoked when a bundle is dynamically
  109. loaded into your running program. This happens automatically without any
  110. intervening operation from you. When you write bundles and you need to
  111. write `+load' you can safely create and send messages to objects whose
  112. classes already exist in the running program. The same restrictions as
  113. above apply to classes defined in bundle.
  114. 
  115. File: objc-features.info,  Node: Type encoding,  Next: Garbage Collection,  Prev: What you can and what you cannot do in +load,  Up: Top
  116. Type encoding
  117. =============
  118.    The Objective-C compiler generates type encodings for all the types.
  119. These type encodings are used at runtime to find out information about
  120. selectors and methods and about objects and classes.
  121.    The types are encoded in the following way:
  122. `char'             `c'                                                    
  123. `unsigned char'    `C'                                                    
  124. `short'            `s'                                                    
  125. `unsigned short'   `S'                                                    
  126. `int'              `i'                                                    
  127. `unsigned int'     `I'                                                    
  128. `long'             `l'                                                    
  129. `unsigned long'    `L'                                                    
  130. `long long'        `q'                                                    
  131. `unsigned long     `Q'                                                    
  132. long'                                                                     
  133. `float'            `f'                                                    
  134. `double'           `d'                                                    
  135. `void'             `v'                                                    
  136. `id'               `@'                                                    
  137. `Class'            `#'                                                    
  138. `SEL'              `:'                                                    
  139. `char*'            `*'                                                    
  140. unknown type       `?'                                                    
  141. bitfields          `b' followed by the starting position of the           
  142.                    bitfield, the type of the bitfield and the size of     
  143.                    the bitfield (the bitfields encoding was changed from  
  144.                    the NeXT's compiler encoding, see below)               
  145.    The encoding of bitfields has changed to allow bitfields to be
  146. properly handled by the runtime functions that compute sizes and
  147. alignments of types that contain bitfields. The previous encoding
  148. contained only the size of the bitfield. Using only this information it
  149. is not possible to reliably compute the size occupied by the bitfield.
  150. This is very important in the presence of the Boehm's garbage collector
  151. because the objects are allocated using the typed memory facility
  152. available in this collector. The typed memory allocation requires
  153. information about where the pointers are located inside the object.
  154.    The position in the bitfield is the position, counting in bits, of
  155. the bit closest to the beginning of the structure.
  156.    The non-atomic types are encoded as follows:
  157. pointers       `'^'' followed by the pointed type.                        
  158. arrays         `'['' followed by the number of elements in the array      
  159.                followed by the type of the elements followed by `']''     
  160. structures     `'{'' followed by the name of the structure (or '?' if     
  161.                the structure is unnamed), the '=' sign, the type of the   
  162.                members and by `'}''                                       
  163. unions         `'('' followed by the name of the structure (or '?' if     
  164.                the union is unnamed), the '=' sign, the type of the       
  165.                members followed by `')''                                  
  166.    Here are some types and their encodings, as they are generated by the
  167. compiler on a i386 machine:
  168. Objective-C type   Compiler encoding                                      
  169.      int a[10];    `[10i]'                                                
  170.      struct {      `{?=i[3f]b128i3b131i2c}'                               
  171.        int i;                                                             
  172.        float f[3];                                                        
  173.        int a:3;                                                           
  174.        int b:2;                                                           
  175.        char c;                                                            
  176.      }                                                                    
  177.    In addition to the types the compiler also encodes the type
  178. specifiers. The table below describes the encoding of the current
  179. Objective-C type specifiers:
  180. Specifier          Encoding                                               
  181. `const'            `r'                                                    
  182. `in'               `n'                                                    
  183. `inout'            `N'                                                    
  184. `out'              `o'                                                    
  185. `bycopy'           `O'                                                    
  186. `oneway'           `V'                                                    
  187.    The type specifiers are encoded just before the type. Unlike types
  188. however, the type specifiers are only encoded when they appear in method
  189. argument types.
  190. 
  191. File: objc-features.info,  Node: Garbage Collection,  Prev: Type encoding,  Up: Top
  192. Garbage Collection
  193. ==================
  194.    Support for a new memory management policy has been added by using a
  195. powerful conservative garbage collector, known as the
  196. Boehm-Demers-Weiser conservative garbage collector. It is available from
  197. `http://reality.sgi.com/employees/boehm_mti/gc.html'.
  198.    To enable the support for it you have to configure the compiler
  199. using an additional argument, `--enable-objc-gc'. You need to have
  200. garbage collector installed before building the compiler. This will
  201. build an additional runtime library which has several enhancements to
  202. support the garbage collector. The new library has a new name,
  203. `libobjc_gc.a' to not conflict with the non-garbage-collected library.
  204.    When the garbage collector is used, the objects are allocated using
  205. the so-called typed memory allocation mechanism available in the
  206. Boehm-Demers-Weiser collector. This mode requires precise information on
  207. where pointers are located inside objects. This information is computed
  208. once per class, immediately after the class has been initialized.
  209.    There is a new runtime function `class_ivar_set_gcinvisible()' which
  210. can be used to declare a so-called *weak pointer* reference. Such a
  211. pointer is basically hidden for the garbage collector; this can be
  212. useful in certain situations, especially when you want to keep track of
  213. the allocated objects, yet allow them to be collected. This kind of
  214. pointers can only be members of objects, you cannot declare a global
  215. pointer as a weak reference. Every type which is a pointer type can be
  216. declared a weak pointer, including `id', `Class' and `SEL'.
  217.    Here is an example of how to use this feature. Suppose you want to
  218. implement a class whose instances hold a weak pointer reference; the
  219. following class does this:
  220.      @interface WeakPointer : Object
  221.      {
  222.          const void* weakPointer;
  223.      }
  224.      
  225.      - initWithPointer:(const void*)p;
  226.      - (const void*)weakPointer;
  227.      @end
  228.      
  229.      
  230.      @implementation WeakPointer
  231.      
  232.      + (void)initialize
  233.      {
  234.        class_ivar_set_gcinvisible (self, "weakPointer", YES);
  235.      }
  236.      
  237.      - initWithPointer:(const void*)p
  238.      {
  239.        weakPointer = p;
  240.        return self;
  241.      }
  242.      
  243.      - (const void*)weakPointer
  244.      {
  245.        return weakPointer;
  246.      }
  247.      
  248.      @end
  249.    Weak pointers are supported through a new type character specifier
  250. represented by the `'!'' character. The `class_ivar_set_gcinvisible()'
  251. function adds or removes this specifier to the string type description
  252. of the instance variable named as argument.
  253. 
  254. Tag Table:
  255. Node: Top113
  256. Node: Executing code before main645
  257. Node: What you can and what you cannot do in +load3303
  258. Node: Type encoding5489
  259. Node: Garbage Collection10712
  260. 
  261. End Tag Table