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