built-in type constraints in XS
[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 #ifndef newSVpvs_share
12 #define newSVpvs_share(s) Perl_newSVpvn_share(aTHX_ s, sizeof(s)-1, 0U)
13 #endif
14
15 #ifndef mro_get_linear_isa
16 #define no_mro_get_linear_isa
17 #define mro_get_linear_isa(stash) mouse_mro_get_linear_isa(aTHX_ stash)
18 AV* mouse_mro_get_linear_isa(pTHX_ HV* const stash)
19 #endif /* !mro_get_linear_isa */
20
21 #ifndef mro_get_pkg_gen
22 #ifdef no_mro_get_linear_isa
23 #define mro_get_pkg_gen(stash) ((void)stash, PL_sub_generation)
24 #else
25 #define mro_get_pkg_gen(stash) (HvAUX(stash)->xhv_mro_meta ? HvAUX(stash)->xhv_mro_meta->pkg_gen : (U32)0)
26 #endif /* !no_mro_get_linear_isa */
27 #endif /* mro_get_package_gen */
28
29 #define MOUSE_CALL_BOOT(name) STMT_START {        \
30         EXTERN_C XS(CAT2(boot_, name));         \
31         PUSHMARK(SP);                           \
32         CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
33     } STMT_END
34
35 extern SV* mouse_package;
36 extern SV* mouse_namespace;
37
38 #define is_class_loaded(sv) mouse_is_class_loaded(aTHX_ sv)
39 bool mouse_is_class_loaded(pTHX_ SV*);
40
41 #define is_instance_of(sv, klass) mouse_is_instance_of(aTHX_ sv, klass)
42 bool mouse_is_instance_of(pTHX_ SV* const sv, SV* const klass);
43
44 #define IsObject(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)))
45
46 #define mcall0(invocant, m)        mouse_call0(aTHX_ (invocant), (m))
47 #define mcall1(invocant, m, arg1)  mouse_call1(aTHX_ (invocant), (m), (arg1))
48 #define mcall0s(invocant, m)       mcall0((invocant), newSVpvs_flags(m, SVs_TEMP))
49 #define mcall1s(invocant, m, arg1) mcall1((invocant), newSVpvs_flags(m, SVs_TEMP), (arg1))
50
51 SV* mouse_call0(pTHX_ SV *const self, SV *const method);
52 SV* mouse_call1(pTHX_ SV *const self, SV *const method, SV* const arg1);
53
54 #define MOUSEf_DIE_ON_FAIL 0x01
55 MAGIC* mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags);
56
57 #define dMOUSE_self      SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
58
59 /* mouse_instance.xs stuff */
60 SV*  mouse_instance_create     (pTHX_ HV* const stash);
61 SV*  mouse_instance_clone      (pTHX_ SV* const instance);
62 bool mouse_instance_has_slot   (pTHX_ SV* const instance, SV* const slot);
63 SV*  mouse_instance_get_slot   (pTHX_ SV* const instance, SV* const slot);
64 SV*  mouse_instance_set_slot   (pTHX_ SV* const instance, SV* const slot, SV* const value);
65 SV*  mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot);
66 void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
67
68
69 /* mouse_simle_accessor.xs */
70 #define INSTALL_SIMPLE_READER(klass, name)                  INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
71 #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)
72
73 #define INSTALL_SIMPLE_WRITER(klass, name)                  INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
74 #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)
75
76 #define INSTALL_SIMPLE_PREDICATE(klass, name)                INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, name)
77 #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)
78
79 CV* mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl);
80
81 XS(mouse_xs_simple_reader);
82 XS(mouse_xs_simple_writer);
83 XS(mouse_xs_simple_predicate);
84
85 typedef enum mouse_tc{
86      MOUSE_TC_ANY,
87      MOUSE_TC_ITEM,
88      MOUSE_TC_UNDEF,
89      MOUSE_TC_DEFINED,
90      MOUSE_TC_BOOL,
91      MOUSE_TC_VALUE,
92      MOUSE_TC_REF,
93      MOUSE_TC_STR,
94      MOUSE_TC_NUM,
95      MOUSE_TC_INT,
96      MOUSE_TC_SCALAR_REF,
97      MOUSE_TC_ARRAY_REF,
98      MOUSE_TC_HASH_REF,
99      MOUSE_TC_CODE_REF,
100      MOUSE_TC_GLOB_REF,
101      MOUSE_TC_FILEHANDLE,
102      MOUSE_TC_REGEXP_REF,
103      MOUSE_TC_OBJECT,
104      MOUSE_TC_CLASS_NAME,
105      MOUSE_TC_ROLE_NAME,
106
107      MOUSE_TC_last
108 } mouse_tc;
109
110 /* type constraints */
111
112 int mouse_tc_check(pTHX_ mouse_tc const tc, SV* sv);
113
114 int mouse_tc_Any       (pTHX_ SV* const sv);
115 int mouse_tc_Bool      (pTHX_ SV* const sv);
116 int mouse_tc_Undef     (pTHX_ SV* const sv);
117 int mouse_tc_Defined   (pTHX_ SV* const sv);
118 int mouse_tc_Value     (pTHX_ SV* const sv);
119 int mouse_tc_Num       (pTHX_ SV* const sv);
120 int mouse_tc_Int       (pTHX_ SV* const sv);
121 int mouse_tc_Str       (pTHX_ SV* const sv);
122 int mouse_tc_ClassName (pTHX_ SV* const sv);
123 int mouse_tc_RoleName  (pTHX_ SV* const sv);
124 int mouse_tc_Ref       (pTHX_ SV* const sv);
125 int mouse_tc_ScalarRef (pTHX_ SV* const sv);
126 int mouse_tc_ArrayRef  (pTHX_ SV* const sv);
127 int mouse_tc_HashRef   (pTHX_ SV* const sv);
128 int mouse_tc_CodeRef   (pTHX_ SV* const sv);
129 int mouse_tc_RegexpRef (pTHX_ SV* const sv);
130 int mouse_tc_GlobRef   (pTHX_ SV* const sv);
131 int mouse_tc_FileHandle(pTHX_ SV* const sv);
132 int mouse_tc_Object    (pTHX_ SV* const sv);
133
134
135 #endif /* !MOUSE_H */
136