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