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