Change class holders to simple readers
[gitmo/Mouse.git] / mouse.h
1 #ifndef MOUSE_H
2 #define MOUSE_H
3
4 #define PERL_NO_GET_CONTEXT
5 #include <EXTERN.h>
6 #include <perl.h>
7 #include <XSUB.h>
8
9 #include "ppport.h"
10
11 /* Portability stuff */
12
13 #ifndef newSVpvs_share
14 #define newSVpvs_share(s) Perl_newSVpvn_share(aTHX_ s, sizeof(s)-1, 0U)
15 #endif
16
17 #ifndef get_cvs
18 #define get_cvs(name, flags) get_cv(name, flags)
19 #endif
20
21 #ifndef GvNAME_get
22 #define GvNAME_get GvNAME
23 #endif
24 #ifndef GvNAMELEN_get
25 #define GvNAMELEN_get GvNAMELEN
26 #endif
27
28 #ifndef mro_get_linear_isa
29 #define no_mro_get_linear_isa
30 #define mro_get_linear_isa(stash) mouse_mro_get_linear_isa(aTHX_ stash)
31 AV* mouse_mro_get_linear_isa(pTHX_ HV* const stash);
32 #endif /* !mro_get_linear_isa */
33
34 #ifndef mro_get_pkg_gen
35 #ifdef no_mro_get_linear_isa
36 #define mro_get_pkg_gen(stash) ((void)stash, PL_sub_generation)
37 #else
38 #define mro_get_pkg_gen(stash) (HvAUX(stash)->xhv_mro_meta ? HvAUX(stash)->xhv_mro_meta->pkg_gen : (U32)0)
39 #endif /* !no_mro_get_linear_isa */
40 #endif /* mro_get_package_gen */
41
42 #if !defined(__GNUC__)
43 #  if (!defined(__cplusplus__) || !defined(__STDC_VERSION__) ||  (__STDC_VERSION__ < 199901L)) && !defined(inline)
44 #    define inline /* nothing */
45 #  endif
46 #endif
47
48 /* Mouse stuff */
49
50 #define newAV_mortal() (AV*)sv_2mortal((SV*)newAV())
51 #define newHV_mortal() (HV*)sv_2mortal((SV*)newHV())
52
53 #define MOUSE_CALL_BOOT(name) STMT_START {      \
54         EXTERN_C XS(CAT2(boot_, name));         \
55         PUSHMARK(SP);                           \
56         CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
57     } STMT_END
58
59 extern SV* mouse_package;
60 extern SV* mouse_namespace;
61 extern SV* mouse_methods;
62 extern SV* mouse_name;
63
64 void
65 mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...)
66 #ifdef __attribute__format__
67     __attribute__format__(__printf__, 3, 4);
68 #else
69     ;
70 #endif
71
72 #define is_class_loaded(sv) mouse_is_class_loaded(aTHX_ sv)
73 bool mouse_is_class_loaded(pTHX_ SV*);
74
75 #define is_an_instance_of(klass, sv) mouse_is_an_instance_of(aTHX_ gv_stashpvs(klass, GV_ADD), (sv))
76
77 #define IsObject(sv)   (SvROK(sv) && SvOBJECT(SvRV(sv)))
78 #define IsArrayRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV)
79 #define IsHashRef(sv)  (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV)
80 #define IsCodeRef(sv)  (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVCV)
81
82 #define mcall0(invocant, m)          mouse_call0(aTHX_ (invocant), (m))
83 #define mcall1(invocant, m, arg1)    mouse_call1(aTHX_ (invocant), (m), (arg1))
84 #define predicate_call(invocant, m)  mouse_predicate_call(aTHX_ (invocant), (m))
85
86 #define mcall0s(invocant, m)          mcall0((invocant), sv_2mortal(newSVpvs_share(m)))
87 #define mcall1s(invocant, m, arg1)    mcall1((invocant), sv_2mortal(newSVpvs_share(m)), (arg1))
88 #define predicate_calls(invocant, m)  predicate_call((invocant), sv_2mortal(newSVpvs_share(m)))
89
90
91 #define get_metaclass(name) mouse_get_metaclass(aTHX_ name)
92
93 SV* mouse_call0(pTHX_ SV *const self, SV *const method);
94 SV* mouse_call1(pTHX_ SV *const self, SV *const method, SV* const arg1);
95 int mouse_predicate_call(pTHX_ SV* const self, SV* const method);
96
97 SV* mouse_get_metaclass(pTHX_ SV* metaclass_name);
98
99 GV* mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create);
100 #define stash_fetch(s, n, l, c) mouse_stash_fetch(aTHX_ (s), (n), (l), (c))
101 #define stash_fetchs(s, n, c)   mouse_stash_fetch(aTHX_ (s), STR_WITH_LEN(n), (c))
102
103
104 #define MOUSEf_DIE_ON_FAIL 0x01
105 MAGIC* mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags);
106
107 /* MOUSE_av_at(av, ix) is the safer version of AvARRAY(av)[ix] if perl is compiled with -DDEBUGGING */
108 #ifdef DEBUGGING
109 #define MOUSE_av_at(av, ix)  *mouse_av_at_safe(aTHX_ (av) , (ix))
110 SV** mouse_av_at_safe(pTHX_ AV* const mi, I32 const ix);
111 #else
112 #define MOUSE_av_at(av, ix)  AvARRAY(av)[ix]
113 #endif
114
115 #define MOUSE_mg_obj(mg)     ((mg)->mg_obj)
116 #define MOUSE_mg_ptr(mg)     ((mg)->mg_ptr)
117 #define MOUSE_mg_len(mg)     ((mg)->mg_len)
118 #define MOUSE_mg_flags(mg)   ((mg)->mg_private)
119 #define MOUSE_mg_virtual(mg) ((mg)->mg_virtual)
120
121 #define MOUSE_mg_slot(mg)   MOUSE_mg_obj(mg)
122 #define MOUSE_mg_xa(mg)    ((AV*)MOUSE_mg_ptr(mg))
123
124
125 /* mouse_instance.xs stuff */
126 SV*  mouse_instance_create     (pTHX_ HV* const stash);
127 SV*  mouse_instance_clone      (pTHX_ SV* const instance);
128 bool mouse_instance_has_slot   (pTHX_ SV* const instance, SV* const slot);
129 SV*  mouse_instance_get_slot   (pTHX_ SV* const instance, SV* const slot);
130 SV*  mouse_instance_set_slot   (pTHX_ SV* const instance, SV* const slot, SV* const value);
131 SV*  mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot);
132 void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
133
134 #define has_slot(self, key)         mouse_instance_has_slot(aTHX_ self, key)
135 #define get_slot(self, key)         mouse_instance_get_slot(aTHX_ self, key)
136 #define set_slot(self, key, value)  mouse_instance_set_slot(aTHX_ self, key, value)
137 #define delete_slot(self, key)      mouse_instance_delete_slot(aTHX_ self, key)
138 #define weaken_slot(self, key)      mouse_instance_weaken_slot(aTHX_ self, key)
139
140 #define has_slots(self, key)        has_slot(self, sv_2mortal(newSVpvs_share(key)))
141 #define get_slots(self, key)        get_slot(self, sv_2mortal(newSVpvs_share(key)))
142 #define set_slots(self, key, value) set_slot(self, sv_2mortal(newSVpvs_share(key)), value)
143
144 /* mouse_simle_accessor.xs */
145 #define INSTALL_SIMPLE_READER(klass, name) \
146     INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
147 #define INSTALL_SIMPLE_READER_WITH_KEY(klass, name, key) \
148     (void)mouse_simple_accessor_generate(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, XS_Mouse_simple_reader, NULL, 0)
149
150 #define INSTALL_SIMPLE_READER_WITH_DEFAULTSV(klass, name, dsv) \
151     (void)mouse_simple_accessor_generate(aTHX_ "Mouse::Meta::" #klass "::" #name, #name, sizeof(#name)-1, XS_Mouse_simple_reader, (dsv), HEf_SVKEY)
152 #define INSTALL_SIMPLE_READER_WITH_DEFAULTS(klass, name, ds) \
153     INSTALL_SIMPLE_READER_WITH_DEFAULTSV(klass, name, newSVpvs(ds))
154
155 #define INSTALL_SIMPLE_WRITER(klass, name) \
156     NSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
157 #define INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, key) \
158     (void)mouse_simple_accessor_generate(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, XS_Mouse_simple_writer, NULL, 0)
159
160 #define INSTALL_SIMPLE_PREDICATE(klass, name) \
161     INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, name)
162 #define INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, key) \
163     (void)mouse_simple_accessor_generate(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, XS_Mouse_simple_predicate, NULL, 0)
164
165 CV* mouse_simple_accessor_generate(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen);
166
167 XS(XS_Mouse_simple_accessor);
168 XS(XS_Mouse_simple_reader);
169 XS(XS_Mouse_simple_writer);
170 XS(XS_Mouse_simple_clearer);
171 XS(XS_Mouse_simple_predicate);
172
173 CV* mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl);
174
175 XS(XS_Mouse_accessor);
176 XS(XS_Mouse_reader);
177 XS(XS_Mouse_writer);
178
179 /* type constraints */
180
181 int mouse_tc_check(pTHX_ SV* const tc, SV* const sv);
182
183 int mouse_tc_Any       (pTHX_ SV*, SV* const sv);
184 int mouse_tc_Bool      (pTHX_ SV*, SV* const sv);
185 int mouse_tc_Undef     (pTHX_ SV*, SV* const sv);
186 int mouse_tc_Defined   (pTHX_ SV*, SV* const sv);
187 int mouse_tc_Value     (pTHX_ SV*, SV* const sv);
188 int mouse_tc_Num       (pTHX_ SV*, SV* const sv);
189 int mouse_tc_Int       (pTHX_ SV*, SV* const sv);
190 int mouse_tc_Str       (pTHX_ SV*, SV* const sv);
191 int mouse_tc_ClassName (pTHX_ SV*, SV* const sv);
192 int mouse_tc_RoleName  (pTHX_ SV*, SV* const sv);
193 int mouse_tc_Ref       (pTHX_ SV*, SV* const sv);
194 int mouse_tc_ScalarRef (pTHX_ SV*, SV* const sv);
195 int mouse_tc_ArrayRef  (pTHX_ SV*, SV* const sv);
196 int mouse_tc_HashRef   (pTHX_ SV*, SV* const sv);
197 int mouse_tc_CodeRef   (pTHX_ SV*, SV* const sv);
198 int mouse_tc_RegexpRef (pTHX_ SV*, SV* const sv);
199 int mouse_tc_GlobRef   (pTHX_ SV*, SV* const sv);
200 int mouse_tc_FileHandle(pTHX_ SV*, SV* const sv);
201 int mouse_tc_Object    (pTHX_ SV*, SV* const sv);
202
203 CV* mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name);
204
205 int mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance);
206
207 XS(XS_Mouse_constraint_check);
208
209 /* Mouse XS Attribute object */
210
211 AV* mouse_get_xa(pTHX_ SV* const attr);
212 SV* mouse_xa_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags);
213 SV* mouse_xa_set_default(pTHX_ AV* const xa, SV* const object);
214
215 enum mouse_xa_ix_t{
216     MOUSE_XA_SLOT,      /* for constructors, sync to mg_obj */
217     MOUSE_XA_FLAGS,     /* for constructors, sync to mg_private */
218     MOUSE_XA_ATTRIBUTE,
219     MOUSE_XA_INIT_ARG,
220     MOUSE_XA_TC,
221     MOUSE_XA_TC_CODE,
222
223     MOUSE_XA_last
224 };
225
226 #define MOUSE_xa_slot(m)      MOUSE_av_at(m, MOUSE_XA_SLOT)
227 #define MOUSE_xa_flags(m)     SvUVX( MOUSE_av_at(m, MOUSE_XA_FLAGS) )
228 #define MOUSE_xa_attribute(m) MOUSE_av_at(m, MOUSE_XA_ATTRIBUTE)
229 #define MOUSE_xa_init_arg(m)  MOUSE_av_at(m, MOUSE_XA_INIT_ARG)
230 #define MOUSE_xa_tc(m)        MOUSE_av_at(m, MOUSE_XA_TC)
231 #define MOUSE_xa_tc_code(m)   MOUSE_av_at(m, MOUSE_XA_TC_CODE)
232
233 enum mouse_xa_flags_t{
234     MOUSEf_ATTR_HAS_TC          = 0x0001,
235     MOUSEf_ATTR_HAS_DEFAULT     = 0x0002,
236     MOUSEf_ATTR_HAS_BUILDER     = 0x0004,
237     MOUSEf_ATTR_HAS_INITIALIZER = 0x0008, /* not used */
238     MOUSEf_ATTR_HAS_TRIGGER     = 0x0010,
239
240     MOUSEf_ATTR_IS_LAZY         = 0x0020,
241     MOUSEf_ATTR_IS_WEAK_REF     = 0x0040,
242     MOUSEf_ATTR_IS_REQUIRED     = 0x0080,
243
244     MOUSEf_ATTR_SHOULD_COERCE   = 0x0100,
245
246     MOUSEf_ATTR_SHOULD_AUTO_DEREF
247                                 = 0x0200,
248     MOUSEf_TC_IS_ARRAYREF       = 0x0400,
249     MOUSEf_TC_IS_HASHREF        = 0x0800,
250
251     MOUSEf_OTHER1               = 0x1000,
252     MOUSEf_OTHER2               = 0x2000,
253     MOUSEf_OTHER3               = 0x4000,
254     MOUSEf_OTHER4               = 0x8000,
255
256     MOUSEf_MOUSE_MASK           = 0xFFFF /* not used */
257 };
258
259
260 #endif /* !MOUSE_H */
261