Rewrite %initialize-object in Scheme
[bpt/guile.git] / libguile / goops.h
1 /* classes: h_files */
2
3 #ifndef SCM_GOOPS_H
4 #define SCM_GOOPS_H
5
6 /* Copyright (C) 1998,1999,2000,2001,2002,2003, 2006, 2008, 2009, 2011, 2015 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 /* This software is a derivative work of other copyrighted softwares; the
27 * copyright notices of these softwares are placed in the file COPYRIGHTS
28 *
29 * This file is based upon stklos.h from the STk distribution by
30 * Erick Gallesio <eg@unice.fr>.
31 */
32
33 #include "libguile/__scm.h"
34
35 #include "libguile/validate.h"
36
37 /* {Class flags}
38 *
39 * These are used for efficient identification of instances of a
40 * certain class or its subclasses when traversal of the inheritance
41 * graph would be too costly.
42 */
43 #define SCM_VTABLE_FLAG_GOOPS_CLASS SCM_VTABLE_FLAG_GOOPS_0
44 #define SCM_VTABLE_FLAG_GOOPS_VALID SCM_VTABLE_FLAG_GOOPS_1
45
46 #define SCM_CLASS_OF(x) SCM_STRUCT_VTABLE (x)
47 #define SCM_CLASS_FLAGS(class) (SCM_VTABLE_FLAGS (class))
48 #define SCM_OBJ_CLASS_FLAGS(obj) (SCM_STRUCT_VTABLE_FLAGS (obj))
49 #define SCM_SET_CLASS_FLAGS(c, f) (SCM_SET_VTABLE_FLAGS (c, f))
50 #define SCM_CLEAR_CLASS_FLAGS(c, f) (SCM_CLEAR_VTABLE_FLAGS (c, f))
51
52 #define SCM_CLASSF_METACLASS (SCM_VTABLE_FLAG_GOOPS_CLASS|SCM_VTABLE_FLAG_VTABLE)
53 #define SCM_CLASSF_GOOPS_VALID SCM_VTABLE_FLAG_GOOPS_VALID
54 #define SCM_CLASSF_GOOPS SCM_VTABLE_FLAG_GOOPS_CLASS
55 #define SCM_CLASSF_GOOPS_OR_VALID (SCM_CLASSF_GOOPS | SCM_CLASSF_GOOPS_VALID)
56
57 /*
58 * scm_class_class
59 */
60
61 /* see also, SCM_VTABLE_BASE_LAYOUT, and build_class_class_slots */
62 #define SCM_CLASS_CLASS_LAYOUT \
63 "pw" /* redefined */ \
64 "pw" /* direct supers */ \
65 "pw" /* direct slots */ \
66 "pw" /* direct subclasses */ \
67 "pw" /* direct methods */ \
68 "pw" /* cpl */ \
69 "pw" /* slots */ \
70 "pw" /* getters-n-setters */ \
71 "pw" /* nfields */
72
73 #define scm_si_redefined (scm_vtable_offset_user + 0)
74 #define scm_si_direct_supers (scm_vtable_offset_user + 1) /* (class ...) */
75 #define scm_si_direct_slots (scm_vtable_offset_user + 2) /* ((name . options) ...) */
76 #define scm_si_direct_subclasses (scm_vtable_offset_user + 3) /* (class ...) */
77 #define scm_si_direct_methods (scm_vtable_offset_user + 4) /* (methods ...) */
78 #define scm_si_cpl (scm_vtable_offset_user + 5) /* (class ...) */
79 #define scm_si_slots (scm_vtable_offset_user + 6) /* ((name . options) ...) */
80 #define scm_si_getters_n_setters (scm_vtable_offset_user + 7)
81 #define scm_si_nfields (scm_vtable_offset_user + 8) /* an integer */
82 #define SCM_N_CLASS_SLOTS (scm_vtable_offset_user + 9)
83
84 #define SCM_OBJ_CLASS_REDEF(x) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (x) [scm_si_redefined]))
85 #define SCM_INST(x) SCM_STRUCT_DATA (x)
86
87 #define SCM_CLASS_OF(x) SCM_STRUCT_VTABLE (x)
88 #define SCM_ACCESSORS_OF(x) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (x)[scm_si_getters_n_setters]))
89
90 #define SCM_CLASSP(x) \
91 (SCM_STRUCTP (x) && SCM_STRUCT_VTABLE_FLAGS (x) & SCM_CLASSF_METACLASS)
92 #define SCM_VALIDATE_CLASS(pos, x) SCM_MAKE_VALIDATE_MSG (pos, x, CLASSP, "class")
93
94 #define SCM_INSTANCEP(x) \
95 (SCM_STRUCTP (x) && (SCM_STRUCT_VTABLE_FLAGS (x) & SCM_CLASSF_GOOPS))
96 #define SCM_VALIDATE_INSTANCE(pos, x) SCM_MAKE_VALIDATE_MSG (pos, x, INSTANCEP, "instance")
97
98 #define SCM_SLOT(x, i) (SCM_STRUCT_SLOT_REF (x, i))
99 #define SCM_SET_SLOT(x, i, v) (SCM_STRUCT_SLOT_SET (x, i, v))
100
101 #define SCM_SUBCLASSP(c1, c2) (scm_is_true (scm_c_memq (c2, SCM_SLOT (c1, scm_si_cpl))))
102 #define SCM_IS_A_P(x, c) \
103 (SCM_INSTANCEP (x) && SCM_SUBCLASSP (SCM_CLASS_OF (x), c))
104
105 #define SCM_GENERICP(x) (scm_is_generic (x))
106 #define SCM_VALIDATE_GENERIC(pos, x) SCM_MAKE_VALIDATE_MSG (pos, x, GENERICP, "generic function")
107
108 #define SCM_METHODP(x) (scm_is_method (x))
109 #define SCM_VALIDATE_METHOD(pos, x) SCM_MAKE_VALIDATE_MSG (pos, x, METHODP, "method")
110
111 #define SCM_SET_CLASS_DESTRUCTOR(c, d) SCM_SET_VTABLE_DESTRUCTOR (c, d)
112
113 SCM_INTERNAL SCM scm_i_port_class[];
114 SCM_INTERNAL SCM scm_i_smob_class[];
115
116 SCM_API SCM scm_module_goops;
117
118 SCM_API SCM scm_goops_version (void);
119 SCM_API void scm_load_goops (void);
120 SCM_API SCM scm_make_extended_class (char const *type_name, int applicablep);
121 SCM_API void scm_make_port_classes (long ptobnum, char *type_name);
122 SCM_API SCM scm_ensure_accessor (SCM name);
123 SCM_API SCM scm_class_of (SCM obj);
124
125 /* Low level functions exported */
126 SCM_API SCM scm_make_next_method (SCM methods, SCM args, SCM gf);
127 SCM_INTERNAL SCM scm_make_standard_class (SCM meta, SCM name, SCM dsupers,
128 SCM dslots);
129
130 /* Primitives exported */
131 SCM_API SCM scm_sys_allocate_instance (SCM c, SCM initargs);
132 SCM_API SCM scm_slot_ref (SCM obj, SCM slot_name);
133 SCM_API SCM scm_slot_set_x (SCM obj, SCM slot_name, SCM value);
134
135 SCM_INTERNAL void scm_i_inherit_applicable (SCM c);
136 SCM_INTERNAL SCM scm_i_get_keyword (SCM key, SCM l, long len,
137 SCM default_value, const char *subr);
138 SCM_API SCM scm_get_keyword (SCM key, SCM l, SCM default_value);
139 SCM_API SCM scm_sys_inherit_magic_x (SCM c, SCM dsupers);
140 SCM_API SCM scm_instance_p (SCM obj);
141 SCM_API int scm_is_generic (SCM x);
142 SCM_API int scm_is_method (SCM x);
143 SCM_API SCM scm_class_name (SCM obj);
144 SCM_API SCM scm_class_direct_supers (SCM obj);
145 SCM_API SCM scm_class_direct_slots (SCM obj);
146 SCM_API SCM scm_class_direct_subclasses (SCM obj);
147 SCM_API SCM scm_class_direct_methods (SCM obj);
148 SCM_API SCM scm_class_precedence_list (SCM obj);
149 SCM_API SCM scm_class_slots (SCM obj);
150 SCM_API SCM scm_generic_function_name (SCM obj);
151 SCM_API SCM scm_generic_function_methods (SCM obj);
152 SCM_API SCM scm_method_generic_function (SCM obj);
153 SCM_API SCM scm_method_specializers (SCM obj);
154 SCM_API SCM scm_method_procedure (SCM obj);
155 SCM_API SCM scm_slot_ref_using_class (SCM cls, SCM obj, SCM slot_name);
156 SCM_API SCM scm_slot_set_using_class_x (SCM cls, SCM obj, SCM slot_name, SCM value);
157 SCM_API SCM scm_slot_bound_using_class_p (SCM cls, SCM obj, SCM slot_name);
158 SCM_API SCM scm_slot_exists_using_class_p (SCM cls, SCM obj, SCM slot_name);
159 SCM_API SCM scm_slot_bound_p (SCM obj, SCM slot_name);
160 SCM_API SCM scm_slot_exists_p (SCM obj, SCM slot_name);
161 SCM_API SCM scm_sys_modify_instance (SCM old, SCM newinst);
162 SCM_API SCM scm_sys_modify_class (SCM old, SCM newcls);
163 SCM_API SCM scm_sys_invalidate_class (SCM cls);
164 SCM_API SCM scm_sys_invalidate_method_cache_x (SCM gf);
165 SCM_API SCM scm_generic_capability_p (SCM proc);
166 SCM_API SCM scm_enable_primitive_generic_x (SCM subrs);
167 SCM_INTERNAL SCM scm_set_primitive_generic_x (SCM subr, SCM generic);
168 SCM_API SCM scm_primitive_generic_generic (SCM subr);
169 SCM_API SCM stklos_version (void);
170 SCM_API SCM scm_make (SCM args);
171 SCM_API void scm_change_object_class (SCM, SCM, SCM);
172
173 /* These procedures are for dispatching to a generic when a primitive
174 fails to apply. They raise a wrong-type-arg error if the primitive's
175 generic has not been initialized yet. */
176 SCM_API SCM scm_wta_dispatch_0 (SCM gf, const char *subr);
177 SCM_API SCM scm_wta_dispatch_1 (SCM gf, SCM a1, int pos, const char *subr);
178 SCM_API SCM scm_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, const char *subr);
179 SCM_API SCM scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr);
180
181 SCM_INTERNAL SCM scm_i_define_class_for_vtable (SCM vtable);
182
183
184 SCM_INTERNAL void scm_init_goops (void);
185
186 #endif /* SCM_GOOPS_H */
187
188 /*
189 Local Variables:
190 c-file-style: "gnu"
191 End:
192 */