Refactor XS metaclass object structure
[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 /* for portability */
12 #ifndef newSVpvs_share
13 #define newSVpvs_share(s) Perl_newSVpvn_share(aTHX_ s, sizeof(s)-1, 0U)
14 #endif
15
16 #ifndef get_cvs
17 #define get_cvs(name, flags) get_cv(name, flags)
18 #endif
19
20 #ifndef GvNAME_get
21 #define GvNAME_get GvNAME
22 #endif
23 #ifndef GvNAMELEN_get
24 #define GvNAMELEN_get GvNAMELEN
25 #endif
26
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)
30 AV* mouse_mro_get_linear_isa(pTHX_ HV* const stash);
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
41 #define MOUSE_CALL_BOOT(name) STMT_START {      \
42         EXTERN_C XS(CAT2(boot_, name));         \
43         PUSHMARK(SP);                           \
44         CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
45     } STMT_END
46
47 extern SV* mouse_package;
48 extern SV* mouse_namespace;
49 extern SV* mouse_methods;
50 extern SV* mouse_name;
51
52 void
53 mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...)
54 #ifdef __attribute__format__
55     __attribute__format__(__printf__, 3, 4);
56 #else
57     ;
58 #endif
59
60 #define is_class_loaded(sv) mouse_is_class_loaded(aTHX_ sv)
61 bool mouse_is_class_loaded(pTHX_ SV*);
62
63 #define is_an_instance_of(klass, sv) mouse_is_an_instance_of(aTHX_ gv_stashpvs(klass, GV_ADD), (sv))
64
65 #define IsObject(sv)   (SvROK(sv) && SvOBJECT(SvRV(sv)))
66 #define IsArrayRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV)
67 #define IsHashRef(sv)  (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV)
68 #define IsCodeRef(sv)  (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVCV)
69
70 #define mcall0(invocant, m)        mouse_call0(aTHX_ (invocant), (m))
71 #define mcall1(invocant, m, arg1)  mouse_call1(aTHX_ (invocant), (m), (arg1))
72 #define mcall0s(invocant, m)       mcall0((invocant), sv_2mortal(newSVpvs_share(m)))
73 #define mcall1s(invocant, m, arg1) mcall1((invocant), sv_2mortal(newSVpvs_share(m)), (arg1))
74
75 #define get_metaclass_by_name(name) mouse_get_metaclass_by_name(aTHX_ name)
76
77 SV* mouse_call0(pTHX_ SV *const self, SV *const method);
78 SV* mouse_call1(pTHX_ SV *const self, SV *const method, SV* const arg1);
79
80 SV* mouse_get_metaclass_by_name(pTHX_ SV* const metaclass_name);
81
82 GV* mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create);
83 #define stash_fetch(s, n, l, c) mouse_stash_fetch(aTHX_ (s), (n), (l), (c))
84 #define stash_fetchs(s, n, c)   mouse_stash_fetch(aTHX_ (s), STR_WITH_LEN(n), (c))
85
86
87 #define MOUSEf_DIE_ON_FAIL 0x01
88 MAGIC* mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags);
89
90 /* MOUSE_av_at(av, ix) is the safer version of AvARRAY(av)[ix] if perl is compiled with -DDEBUGGING */
91 #ifdef DEBUGGING
92 #define MOUSE_av_at(av, ix)  *mouse_av_at_safe(aTHX_ (av) , (ix))
93 SV** mouse_av_at_safe(pTHX_ AV* const mi, I32 const ix);
94 #else
95 #define MOUSE_av_at(av, ix)  AvARRAY(av)[ix]
96 #endif
97
98 #define dMOUSE_self  SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
99 SV* mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv);
100
101 #define MOUSE_mg_obj(mg)     ((mg)->mg_obj)
102 #define MOUSE_mg_ptr(mg)     ((mg)->mg_ptr)
103 #define MOUSE_mg_flags(mg)   ((mg)->mg_private)
104 #define MOUSE_mg_virtual(mg) ((mg)->mg_virtual)
105
106 #define MOUSE_mg_slot(mg)   MOUSE_mg_obj(mg)
107 #define MOUSE_mg_xa(mg)    ((AV*)MOUSE_mg_ptr(mg))
108
109
110 /* mouse_instance.xs stuff */
111 SV*  mouse_instance_create     (pTHX_ HV* const stash);
112 SV*  mouse_instance_clone      (pTHX_ SV* const instance);
113 bool mouse_instance_has_slot   (pTHX_ SV* const instance, SV* const slot);
114 SV*  mouse_instance_get_slot   (pTHX_ SV* const instance, SV* const slot);
115 SV*  mouse_instance_set_slot   (pTHX_ SV* const instance, SV* const slot, SV* const value);
116 SV*  mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot);
117 void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
118
119 #define has_slot(self, key)         mouse_instance_has_slot(aTHX_ self, key)
120 #define get_slot(self, key)         mouse_instance_get_slot(aTHX_ self, key)
121 #define set_slot(self, key, value)  mouse_instance_set_slot(aTHX_ self, key, value)
122 #define delete_slot(self, key)      mouse_instance_delete_slot(aTHX_ self, key)
123 #define weaken_slot(self, key)      mouse_instance_weaken_slot(aTHX_ self, key)
124
125 #define has_slots(self, key)        has_slot(self, sv_2mortal(newSVpvs_share(key)))
126 #define get_slots(self, key)        get_slot(self, sv_2mortal(newSVpvs_share(key)))
127 #define set_slots(self, key, value) set_slot(self, sv_2mortal(newSVpvs_share(key)), value)
128
129 /* mouse_simle_accessor.xs */
130 #define INSTALL_SIMPLE_READER(klass, name)                  INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
131 #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)
132
133 #define INSTALL_SIMPLE_WRITER(klass, name)                  INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
134 #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)
135
136 #define INSTALL_SIMPLE_PREDICATE(klass, name)                INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, name)
137 #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)
138
139 CV* mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl);
140
141 XS(XS_Mouse_simple_reader);
142 XS(XS_Mouse_simple_writer);
143 XS(XS_Mouse_simple_clearer);
144 XS(XS_Mouse_simple_predicate);
145
146 CV* mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl);
147
148 XS(XS_Mouse_accessor);
149 XS(XS_Mouse_reader);
150 XS(XS_Mouse_writer);
151
152 /* type constraints */
153
154 int mouse_tc_check(pTHX_ SV* const tc, SV* const sv);
155
156 int mouse_tc_Any       (pTHX_ SV*, SV* const sv);
157 int mouse_tc_Bool      (pTHX_ SV*, SV* const sv);
158 int mouse_tc_Undef     (pTHX_ SV*, SV* const sv);
159 int mouse_tc_Defined   (pTHX_ SV*, SV* const sv);
160 int mouse_tc_Value     (pTHX_ SV*, SV* const sv);
161 int mouse_tc_Num       (pTHX_ SV*, SV* const sv);
162 int mouse_tc_Int       (pTHX_ SV*, SV* const sv);
163 int mouse_tc_Str       (pTHX_ SV*, SV* const sv);
164 int mouse_tc_ClassName (pTHX_ SV*, SV* const sv);
165 int mouse_tc_RoleName  (pTHX_ SV*, SV* const sv);
166 int mouse_tc_Ref       (pTHX_ SV*, SV* const sv);
167 int mouse_tc_ScalarRef (pTHX_ SV*, SV* const sv);
168 int mouse_tc_ArrayRef  (pTHX_ SV*, SV* const sv);
169 int mouse_tc_HashRef   (pTHX_ SV*, SV* const sv);
170 int mouse_tc_CodeRef   (pTHX_ SV*, SV* const sv);
171 int mouse_tc_RegexpRef (pTHX_ SV*, SV* const sv);
172 int mouse_tc_GlobRef   (pTHX_ SV*, SV* const sv);
173 int mouse_tc_FileHandle(pTHX_ SV*, SV* const sv);
174 int mouse_tc_Object    (pTHX_ SV*, SV* const sv);
175
176 CV* mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name);
177
178 int mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance);
179
180 XS(XS_Mouse_constraint_check);
181
182 #endif /* !MOUSE_H */
183