⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objc.texi

📁 理解和实践操作系统的一本好书
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,@c 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.@c This is part of the GCC manual.@c For copying conditions, see the file gcc.texi.@node Objective-C@comment  node-name,  next,  previous,  up@chapter GNU Objective-C runtime featuresThis document is meant to describe some of the GNU Objective-C runtimefeatures.  It is not intended to teach you Objective-C, there are severalresources on the Internet that present the language.  Questions andcomments about this document to Ovidiu Predescu@email{ovidiu@@cup.hp.com}.@menu* Executing code before main::* Type encoding::* Garbage Collection::* Constant string objects::* compatibility_alias::@end menu@node Executing code before main, Type encoding, Objective-C, Objective-C@section @code{+load}: Executing code before mainThe GNU Objective-C runtime provides a way that allows you to executecode before the execution of the program enters the @code{main}function.  The code is executed on a per-class and a per-category basis,through a special class method @code{+load}.This facility is very useful if you want to initialize global variableswhich can be accessed by the program directly, without sending a messageto the class first.  The usual way to initialize global variables, in the@code{+initialize} method, might not be useful because@code{+initialize} is only called when the first message is sent to aclass object, which in some cases could be too late.Suppose for example you have a @code{FileStream} class that declares@code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, likebelow:@smallexampleFileStream *Stdin = nil;FileStream *Stdout = nil;FileStream *Stderr = nil;@@implementation FileStream+ (void)initialize@{    Stdin = [[FileStream new] initWithFd:0];    Stdout = [[FileStream new] initWithFd:1];    Stderr = [[FileStream new] initWithFd:2];@}/* @r{Other methods here} */@@end@end smallexampleIn this example, the initialization of @code{Stdin}, @code{Stdout} and@code{Stderr} in @code{+initialize} occurs too late.  The programmer cansend a message to one of these objects before the variables are actuallyinitialized, thus sending messages to the @code{nil} object.  The@code{+initialize} method which actually initializes the globalvariables is not invoked until the first message is sent to the classobject.  The solution would require these variables to be initializedjust before entering @code{main}.The correct solution of the above problem is to use the @code{+load}method instead of @code{+initialize}:@smallexample@@implementation FileStream+ (void)load@{    Stdin = [[FileStream new] initWithFd:0];    Stdout = [[FileStream new] initWithFd:1];    Stderr = [[FileStream new] initWithFd:2];@}/* @r{Other methods here} */@@end@end smallexampleThe @code{+load} is a method that is not overridden by categories.  If aclass and a category of it both implement @code{+load}, both methods areinvoked.  This allows some additional initializations to be performed ina category.This mechanism is not intended to be a replacement for @code{+initialize}.You should be aware of its limitations when you decide to use itinstead of @code{+initialize}.@menu* What you can and what you cannot do in +load::@end menu@node What you can and what you cannot do in +load,  , Executing code before main, Executing code before main@subsection What you can and what you cannot do in @code{+load}The @code{+load} implementation in the GNU runtime guarantees you the followingthings:@itemize @bullet@itemyou can write whatever C code you like;@itemyou can send messages to Objective-C constant strings (@code{@@"this is aconstant string"});@itemyou can allocate and send messages to objects whose class is implementedin the same file;@itemthe @code{+load} implementation of all super classes of a class are executed before the @code{+load} of that class is executed;@itemthe @code{+load} implementation of a class is executed before the@code{+load} implementation of any category.@end itemizeIn particular, the following things, even if they can work in aparticular case, are not guaranteed:@itemize @bullet@itemallocation of or sending messages to arbitrary objects;@itemallocation of or sending messages to objects whose classes have acategory implemented in the same file;@end itemizeYou should make no assumptions about receiving @code{+load} in siblingclasses when you write @code{+load} of a class.  The order in whichsibling classes receive @code{+load} is not guaranteed.The order in which @code{+load} and @code{+initialize} are called couldbe problematic if this matters.  If you don't allocate objects inside@code{+load}, it is guaranteed that @code{+load} is called before@code{+initialize}.  If you create an object inside @code{+load} the@code{+initialize} method of object's class is invoked even if@code{+load} was not invoked.  Note if you explicitly call @code{+load}on a class, @code{+initialize} will be called first.  To avoid possibleproblems try to implement only one of these methods.The @code{+load} method is also invoked when a bundle is dynamicallyloaded into your running program.  This happens automatically without anyintervening operation from you.  When you write bundles and you need towrite @code{+load} you can safely create and send messages to objects whoseclasses already exist in the running program.  The same restrictions asabove apply to classes defined in bundle.@node Type encoding, Garbage Collection, Executing code before main, Objective-C@section Type encodingThe Objective-C compiler generates type encodings for all thetypes.  These type encodings are used at runtime to find out informationabout selectors and methods and about objects and classes.The types are encoded in the following way:@c @sp 1@multitable @columnfractions .25 .75@item @code{_Bool}@tab @code{B}@item @code{char}@tab @code{c}@item @code{unsigned char}@tab @code{C}@item @code{short}@tab @code{s}@item @code{unsigned short}@tab @code{S}@item @code{int}@tab @code{i}@item @code{unsigned int}@tab @code{I}@item @code{long}@tab @code{l}@item @code{unsigned long}@tab @code{L}@item @code{long long}@tab @code{q}@item @code{unsigned long long}@tab @code{Q}@item @code{float}@tab @code{f}@item @code{double}@tab @code{d}@item @code{void}@tab @code{v}@item @code{id}@tab @code{@@}@item @code{Class}@tab @code{#}@item @code{SEL}@tab @code{:}@item @code{char*}@tab @code{*}@item unknown type@tab @code{?}@item Complex types@tab @code{j} followed by the inner type.  For example @code{_Complex double} is encoded as "jd".@item bit-fields@tab @code{b} followed by the starting position of the bit-field, the type of the bit-field and the size of the bit-field (the bit-fields encoding was changed from the NeXT's compiler encoding, see below)@end multitable@c @sp 1The encoding of bit-fields has changed to allow bit-fields to be properlyhandled by the runtime functions that compute sizes and alignments oftypes that contain bit-fields.  The previous encoding contained only thesize of the bit-field.  Using only this information it is not possible toreliably compute the size occupied by the bit-field.  This is veryimportant in the presence of the Boehm's garbage collector because theobjects are allocated using the typed memory facility available in thiscollector.  The typed memory allocation requires information about wherethe pointers are located inside the object.The position in the bit-field is the position, counting in bits, of thebit closest to the beginning of the structure.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -