Add XS Optimized Type Constraints
[gitmo/Moose.git] / Moose.xs
CommitLineData
bc47531e 1
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5#include "ppport.h"
6
7static bool ck_sv_defined(SV*);
8static bool ck_sv_is_ref(SV*);
9static bool ck_sv_ref_type(SV*, int);
10
11static bool
12ck_sv_defined(SV* value){
13 return SvOK(value) ? 1 : 0;
14}
15
16static bool
17ck_sv_is_ref(SV* value){
18 bool retval = 0;
19 if( ck_sv_defined(value) && SvROK(value) ){
20 retval = 1;
21 }
22 return retval;
23}
24
25static bool
26ck_sv_ref_type(SV* value, int sv_type){
27 bool retval = 0;
28 if( ck_sv_is_ref(value) && SvTYPE( SvRV(value) ) == sv_type){
29 retval = 1;
30 }
31 return retval;
32}
33
34
35MODULE = Moose PACKAGE = Moose::Util::TypeConstraints::OptimizedConstraints
36PROTOTYPES: ENABLE
37
38#ifdef HEHEHOHOHAHA
39bool
40Undefined(value)
41 SV* value
42 CODE:
43 RETVAL = !ck_sv_defined(value);
44 OUTPUT:
45 RETVAL
46
47bool
48Defined(value)
49 SV* value
50 CODE:
51 RETVAL = ck_sv_defined(value);
52 OUTPUT:
53 RETVAL
54
55#endif
56
57bool
58Value(value)
59 SV* value
60 CODE:
61 RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
62 OUTPUT:
63 RETVAL
64
65bool
66Str(value)
67 SV* value
68 CODE:
69 RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
70 OUTPUT:
71 RETVAL
72
73bool
74Ref(value)
75 SV* value
76 CODE:
77 RETVAL = ck_sv_is_ref(value);
78 OUTPUT:
79 RETVAL
80
81bool
82ScalarRef(value)
83 SV* value
84 CODE:
85 RETVAL = 0;
86 if(
87 SvOK(value) && SvROK(value)
88 ){
89 int type = SvTYPE(SvRV(value));
90 if(
91 type == SVt_IV ||
92 type == SVt_NV ||
93 type == SVt_PV ||
94 type == SVt_NULL
95 ){
96 RETVAL = 1;
97 }
98 }
99 OUTPUT:
100 RETVAL
101
102bool
103ArrayRef(value)
104 SV* value
105 CODE:
106 RETVAL = ck_sv_ref_type(value, SVt_PVAV);
107 OUTPUT:
108 RETVAL
109
110bool
111HashRef(value)
112 SV* value
113 CODE:
114 RETVAL = (ck_sv_ref_type(value, SVt_PVHV) && !sv_isobject(value)) ? 1 : 0;
115 OUTPUT:
116 RETVAL
117
118bool
119CodeRef(value)
120 SV* value
121 CODE:
122 RETVAL = ck_sv_ref_type(value, SVt_PVCV);
123 OUTPUT:
124 RETVAL
125
126bool
127GlobRef(value)
128 SV* value
129 CODE:
130 RETVAL = ck_sv_ref_type(value, SVt_PVGV);
131 OUTPUT:
132 RETVAL
133
134bool
135Object(value)
136 SV* value
137 PREINIT:
138 char *regclass = "Regexp";
139 CODE:
140 RETVAL = 0;
141 if( ck_sv_is_ref(value)
142 && sv_isobject(value)
143 && !sv_isa(value, regclass)
144 ){
145 RETVAL = 1;
146 }
147 OUTPUT:
148 RETVAL
149
150bool
151RegexpRef(value)
152 SV* value
153 PREINIT:
154 char *regclass = "Regexp";
155 CODE:
156 RETVAL = 0;
157 if( ck_sv_is_ref(value)
158 && sv_isobject(value)
159 && sv_isa(value, regclass)
160 ){
161 RETVAL = 1;
162 }
163 OUTPUT:
164 RETVAL
165
166