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