objects.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:3k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* GNU Objective C Runtime class related functions
  2.    Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
  3.    Contributed by Kresten Krab Thorup
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 2, or (at your option) any later version.
  8. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  11. details.
  12. You should have received a copy of the GNU General Public License along with
  13. GNU CC; see the file COPYING.  If not, write to the Free Software
  14. Foundation, 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.  */
  16. /* As a special exception, if you link this library with files compiled with
  17.    GCC to produce an executable, this does not cause the resulting executable
  18.    to be covered by the GNU General Public License. This exception does not
  19.    however invalidate any other reasons why the executable file might be
  20.    covered by the GNU General Public License.  */
  21. #include "objc.h"
  22. #include "runtime.h" /* the kitchen sink */
  23. #include <string.h> // memset
  24. #if OBJC_WITH_GC
  25. # include <gc.h>
  26. #endif
  27. id __objc_object_alloc (Class);
  28. id __objc_object_dispose (id);
  29. id __objc_object_copy (id);
  30. externobjcvardef id (*_objc_object_alloc) (Class) = __objc_object_alloc;   /* !T:SINGLE */ 
  31. externobjcvardef id (*_objc_object_dispose) (id) = __objc_object_dispose; /* !T:SINGLE */
  32. externobjcvardef id (*_objc_object_copy) (id) = __objc_object_copy;    /* !T:SINGLE */
  33. id
  34. class_create_instance(Class class)
  35. {
  36.   id new = nil;
  37. #if OBJC_WITH_GC
  38.   if (CLS_ISCLASS(class))
  39.     new = (id)GC_malloc_explicitly_typed (class->instance_size,
  40.   class->gc_object_type);
  41. #else
  42.   if (CLS_ISCLASS(class))
  43.     new = (*_objc_object_alloc)(class);
  44. #endif
  45.   if (new!=nil)
  46.     {
  47.       memset (new, 0, class->instance_size);
  48.       new->class_pointer = class;
  49.     }
  50.   return new;
  51. }
  52. id
  53. object_copy(id object)
  54. {
  55.   if ((object!=nil)&&CLS_ISCLASS(object->class_pointer))
  56.     return (*_objc_object_copy)(object);
  57.   else
  58.     return nil;
  59. }
  60. id
  61. object_dispose(id object)
  62. {
  63.   if ((object!=nil)&&CLS_ISCLASS(object->class_pointer))
  64.     {
  65.       if (_objc_object_dispose)
  66.         (*_objc_object_dispose)(object);
  67.       else
  68.         objc_free(object);
  69.     }
  70.   return nil;
  71. }
  72. id __objc_object_alloc(Class class)
  73. {
  74.   return (id)objc_malloc(class->instance_size);
  75. }
  76. id __objc_object_dispose(id object) 
  77. {
  78.   objc_free(object);
  79.   return 0;
  80. }
  81. id __objc_object_copy(id object)
  82. {
  83.   id copy = class_create_instance(object->class_pointer);
  84.   memcpy(copy, object, object->class_pointer->instance_size);
  85.   return copy;
  86. }