Change is-a predicate stuff
[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 #define  NEED_newSVpvn_flags
10 #include "ppport.h"
11
12 /* for portability */
13 #ifndef newSVpvs_share
14 #define newSVpvs_share(s) Perl_newSVpvn_share(aTHX_ s, sizeof(s)-1, 0U)
15 #endif
16
17 #ifndef GvNAME_get
18 #define GvNAME_get GvNAME
19 #endif
20 #ifndef GvNAMELEN_get
21 #define GvNAMELEN_get GvNAMELEN
22 #endif
23
24 #ifndef mro_get_linear_isa
25 #define no_mro_get_linear_isa
26 #define mro_get_linear_isa(stash) mouse_mro_get_linear_isa(aTHX_ stash)
27 AV* mouse_mro_get_linear_isa(pTHX_ HV* const stash);
28 #endif /* !mro_get_linear_isa */
29
30 #ifndef mro_get_pkg_gen
31 #ifdef no_mro_get_linear_isa
32 #define mro_get_pkg_gen(stash) ((void)stash, PL_sub_generation)
33 #else
34 #define mro_get_pkg_gen(stash) (HvAUX(stash)->xhv_mro_meta ? HvAUX(stash)->xhv_mro_meta->pkg_gen : (U32)0)
35 #endif /* !no_mro_get_linear_isa */
36 #endif /* mro_get_package_gen */
37
38 #define MOUSE_CALL_BOOT(name) STMT_START {      \
39         EXTERN_C XS(CAT2(boot_, name));         \
40         PUSHMARK(SP);                           \
41         CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
42     } STMT_END
43
44 extern SV* mouse_package;
45 extern SV* mouse_namespace;
46 extern SV* mouse_methods;
47 extern SV* mouse_name;
48
49 void
50 mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...)
51 #ifdef __attribute__format__
52     __attribute__format__(__printf__, 3, 4);
53 #else
54     ;
55 #endif
56
57 #define is_class_loaded(sv) mouse_is_class_loaded(aTHX_ sv)
58 bool mouse_is_class_loaded(pTHX_ SV*);
59
60 #define is_instance_of(sv, klass) mouse_is_instance_of(aTHX_ sv, klass)
61 bool mouse_is_instance_of(pTHX_ SV* const sv, SV* const klass);
62
63 #define IsObject(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)))
64
65 #define mcall0(invocant, m)        mouse_call0(aTHX_ (invocant), (m))
66 #define mcall1(invocant, m, arg1)  mouse_call1(aTHX_ (invocant), (m), (arg1))
67 #define mcall0s(invocant, m)       mcall0((invocant), newSVpvs_flags(m, SVs_TEMP))
68 #define mcall1s(invocant, m, arg1) mcall1((invocant), newSVpvs_flags(m, SVs_TEMP), (arg1))
69
70 SV* mouse_call0(pTHX_ SV *const self, SV *const method);
71 SV* mouse_call1(pTHX_ SV *const self, SV *const method, SV* const arg1);
72
73 #define MOUSEf_DIE_ON_FAIL 0x01
74 MAGIC* mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags);
75
76 /* MOUSE_av_at(av, ix) is the safer version of AvARRAY(av)[ix] if perl is compiled with -DDEBUGGING */
77 #ifdef DEBUGGING
78 #define MOUSE_av_at(av, ix)  *mouse_av_at_safe(aTHX_ (av) , (ix))
79 SV** mouse_av_at_safe(pTHX_ AV* const mi, I32 const ix);
80 #else
81 #define MOUSE_av_at(av, ix)  AvARRAY(av)[ix]
82 #endif
83
84 #define dMOUSE_self  SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
85 SV* mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv);
86
87 #define MOUSE_mg_obj(mg)     ((mg)->mg_obj)
88 #define MOUSE_mg_ptr(mg)     ((mg)->mg_ptr)
89 #define MOUSE_mg_flags(mg)   ((mg)->mg_private)
90 #define MOUSE_mg_virtual(mg) ((mg)->mg_virtual)
91
92 #define MOUSE_mg_slot(mg)   MOUSE_mg_obj(mg)
93 #define MOUSE_mg_xa(mg)    ((AV*)MOUSE_mg_ptr(mg))
94
95
96 /* mouse_instance.xs stuff */
97 SV*  mouse_instance_create     (pTHX_ HV* const stash);
98 SV*  mouse_instance_clone      (pTHX_ SV* const instance);
99 bool mouse_instance_has_slot   (pTHX_ SV* const instance, SV* const slot);
100 SV*  mouse_instance_get_slot   (pTHX_ SV* const instance, SV* const slot);
101 SV*  mouse_instance_set_slot   (pTHX_ SV* const instance, SV* const slot, SV* const value);
102 SV*  mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot);
103 void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
104
105
106 /* mouse_simle_accessor.xs */
107 #define INSTALL_SIMPLE_READER(klass, name)                  INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
108 #define INSTALL_SIMPLE_READER_WITH_KEY(klass, name, key)    (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, mouse_xs_simple_reader)
109
110 #define INSTALL_SIMPLE_WRITER(klass, name)                  INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
111 #define INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, key)    (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, mouse_xs_simple_writer)
112
113 #define INSTALL_SIMPLE_PREDICATE(klass, name)                INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, name)
114 #define INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, key) (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, mouse_xs_simple_predicate)
115
116 CV* mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl);
117
118 XS(mouse_xs_simple_reader);
119 XS(mouse_xs_simple_writer);
120 XS(mouse_xs_simple_clearer);
121 XS(mouse_xs_simple_predicate);
122
123 CV* mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl);
124
125 XS(mouse_xs_accessor);
126 XS(mouse_xs_reader);
127 XS(mouse_xs_writer);
128
129 typedef enum mouse_tc{
130      MOUSE_TC_ANY,
131      MOUSE_TC_ITEM,
132      MOUSE_TC_UNDEF,
133      MOUSE_TC_DEFINED,
134      MOUSE_TC_BOOL,
135      MOUSE_TC_VALUE,
136      MOUSE_TC_REF,
137      MOUSE_TC_STR,
138      MOUSE_TC_NUM,
139      MOUSE_TC_INT,
140      MOUSE_TC_SCALAR_REF,
141      MOUSE_TC_ARRAY_REF,
142      MOUSE_TC_HASH_REF,
143      MOUSE_TC_CODE_REF,
144      MOUSE_TC_GLOB_REF,
145      MOUSE_TC_FILEHANDLE,
146      MOUSE_TC_REGEXP_REF,
147      MOUSE_TC_OBJECT,
148      MOUSE_TC_CLASS_NAME,
149      MOUSE_TC_ROLE_NAME,
150
151      MOUSE_TC_last
152 } mouse_tc;
153
154 /* type constraints */
155
156 int mouse_tc_check(pTHX_ SV* const tc, SV* const sv);
157 int mouse_builtin_tc_check(pTHX_ mouse_tc const tc, SV* const sv);
158
159 int mouse_tc_Any       (pTHX_ SV* const sv);
160 int mouse_tc_Bool      (pTHX_ SV* const sv);
161 int mouse_tc_Undef     (pTHX_ SV* const sv);
162 int mouse_tc_Defined   (pTHX_ SV* const sv);
163 int mouse_tc_Value     (pTHX_ SV* const sv);
164 int mouse_tc_Num       (pTHX_ SV* const sv);
165 int mouse_tc_Int       (pTHX_ SV* const sv);
166 int mouse_tc_Str       (pTHX_ SV* const sv);
167 int mouse_tc_ClassName (pTHX_ SV* const sv);
168 int mouse_tc_RoleName  (pTHX_ SV* const sv);
169 int mouse_tc_Ref       (pTHX_ SV* const sv);
170 int mouse_tc_ScalarRef (pTHX_ SV* const sv);
171 int mouse_tc_ArrayRef  (pTHX_ SV* const sv);
172 int mouse_tc_HashRef   (pTHX_ SV* const sv);
173 int mouse_tc_CodeRef   (pTHX_ SV* const sv);
174 int mouse_tc_RegexpRef (pTHX_ SV* const sv);
175 int mouse_tc_GlobRef   (pTHX_ SV* const sv);
176 int mouse_tc_FileHandle(pTHX_ SV* const sv);
177 int mouse_tc_Object    (pTHX_ SV* const sv);
178
179 const char* mouse_canonicalize_package_name(const char* name);
180
181 XS(XS_isa_check);
182 XS(XS_isa_check_for_universal);
183
184 #endif /* !MOUSE_H */
185