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

📄 objc.texi

📁 理解和实践操作系统的一本好书
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
The non-atomic types are encoded as follows:@c @sp 1@multitable @columnfractions .2 .8@item pointers@tab @samp{^} followed by the pointed type.@item arrays@tab @samp{[} followed by the number of elements in the array followed by the type of the elements followed by @samp{]}@item structures@tab @samp{@{} followed by the name of the structure (or @samp{?} if the structure is unnamed), the @samp{=} sign, the type of the members and by @samp{@}}@item unions@tab @samp{(} followed by the name of the structure (or @samp{?} if the union is unnamed), the @samp{=} sign, the type of the members followed by @samp{)}@end multitableHere are some types and their encodings, as they are generated by thecompiler on an i386 machine:@sp 1@multitable @columnfractions .25 .75@item Objective-C type@tab Compiler encoding@item@smallexampleint a[10];@end smallexample@tab @code{[10i]}@item@smallexamplestruct @{  int i;  float f[3];  int a:3;  int b:2;  char c;@}@end smallexample@tab @code{@{?=i[3f]b128i3b131i2c@}}@end multitable@sp 1In addition to the types the compiler also encodes the typespecifiers.  The table below describes the encoding of the currentObjective-C type specifiers:@sp 1@multitable @columnfractions .25 .75@item Specifier@tab Encoding@item @code{const}@tab @code{r}@item @code{in}@tab @code{n}@item @code{inout}@tab @code{N}@item @code{out}@tab @code{o}@item @code{bycopy}@tab @code{O}@item @code{oneway}@tab @code{V}@end multitable@sp 1The type specifiers are encoded just before the type.  Unlike typeshowever, the type specifiers are only encoded when they appear in methodargument types.@node Garbage Collection, Constant string objects, Type encoding, Objective-C@section Garbage CollectionSupport for a new memory management policy has been added by using apowerful conservative garbage collector, known as theBoehm-Demers-Weiser conservative garbage collector.  It is available from@w{@uref{http://www.hpl.hp.com/personal/Hans_Boehm/gc/}}.To enable the support for it you have to configure the compiler using anadditional argument, @w{@option{--enable-objc-gc}}.  You need to havegarbage collector installed before building the compiler.  This willbuild an additional runtime library which has several enhancements tosupport the garbage collector.  The new library has a new name,@file{libobjc_gc.a} to not conflict with the non-garbage-collectedlibrary.When the garbage collector is used, the objects are allocated using theso-called typed memory allocation mechanism available in theBoehm-Demers-Weiser collector.  This mode requires precise information onwhere pointers are located inside objects.  This information is computedonce per class, immediately after the class has been initialized.There is a new runtime function @code{class_ivar_set_gcinvisible()}which can be used to declare a so-called @dfn{weak pointer}reference.  Such a pointer is basically hidden for the garbage collector;this can be useful in certain situations, especially when you want tokeep track of the allocated objects, yet allow them to becollected.  This kind of pointers can only be members of objects, youcannot declare a global pointer as a weak reference.  Every type which isa pointer type can be declared a weak pointer, including @code{id},@code{Class} and @code{SEL}.Here is an example of how to use this feature.  Suppose you want toimplement a class whose instances hold a weak pointer reference; thefollowing class does this:@smallexample@@interface WeakPointer : Object@{    const void* weakPointer;@}- initWithPointer:(const void*)p;- (const void*)weakPointer;@@end@@implementation WeakPointer+ (void)initialize@{  class_ivar_set_gcinvisible (self, "weakPointer", YES);@}- initWithPointer:(const void*)p@{  weakPointer = p;  return self;@}- (const void*)weakPointer@{  return weakPointer;@}@@end@end smallexampleWeak pointers are supported through a new type character specifierrepresented by the @samp{!} character.  The@code{class_ivar_set_gcinvisible()} function adds or removes thisspecifier to the string type description of the instance variable namedas argument.@c =========================================================================@node Constant string objects@section Constant string objectsGNU Objective-C provides constant string objects that are generateddirectly by the compiler.  You declare a constant string object byprefixing a C constant string with the character @samp{@@}:@smallexample  id myString = @@"this is a constant string object";@end smallexampleThe constant string objects are by default instances of the@code{NXConstantString} class which is provided by the GNU Objective-Cruntime.  To get the definition of this class you must include the@file{objc/NXConstStr.h} header file.User defined libraries may want to implement their own constant stringclass.  To be able to support them, the GNU Objective-C compiler providesa new command line options @option{-fconstant-string-class=@var{class-name}}.The provided class should adhere to a strict structure, the sameas @code{NXConstantString}'s structure:@smallexample@@interface MyConstantStringClass@{  Class isa;  char *c_string;  unsigned int len;@}@@end@end smallexample@code{NXConstantString} inherits from @code{Object}; user classlibraries may choose to inherit the customized constant string classfrom a different class than @code{Object}.  There is no requirement inthe methods the constant string class has to implement, but the finalivar layout of the class must be the compatible with the givenstructure.When the compiler creates the statically allocated constant stringobject, the @code{c_string} field will be filled by the compiler withthe string; the @code{length} field will be filled by the compiler withthe string length; the @code{isa} pointer will be filled with@code{NULL} by the compiler, and it will later be fixed up automaticallyat runtime by the GNU Objective-C runtime library to point to the classwhich was set by the @option{-fconstant-string-class} option when theobject file is loaded (if you wonder how it works behind the scenes, thename of the class to use, and the list of static objects to fixup, arestored by the compiler in the object file in a place where the GNUruntime library will find them at runtime).As a result, when a file is compiled with the@option{-fconstant-string-class} option, all the constant string objectswill be instances of the class specified as argument to this option.  Itis possible to have multiple compilation units referring to differentconstant string classes, neither the compiler nor the linker impose anyrestrictions in doing this.@c =========================================================================@node compatibility_alias@section compatibility_aliasThis is a feature of the Objective-C compiler rather than of theruntime, anyway since it is documented nowhere and its existence wasforgotten, we are documenting it here.The keyword @code{@@compatibility_alias} allows you to define a class nameas equivalent to another class name.  For example:@smallexample@@compatibility_alias WOApplication GSWApplication;@end smallexampletells the compiler that each time it encounters @code{WOApplication} asa class name, it should replace it with @code{GSWApplication} (that is,@code{WOApplication} is just an alias for @code{GSWApplication}).There are some constraints on how this can be used---@itemize @bullet@item @code{WOApplication} (the alias) must not be an existing class;@item @code{GSWApplication} (the real class) must be an existing class.@end itemize

⌨️ 快捷键说明

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