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