Add ObjectOfType XS function for use with anon types constraints that
[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
67767270 34static const char *regclass = "Regexp";
bc47531e 35
36MODULE = Moose PACKAGE = Moose::Util::TypeConstraints::OptimizedConstraints
37PROTOTYPES: ENABLE
38
bc47531e 39bool
67767270 40Undef(value)
bc47531e 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
bc47531e 55bool
56Value(value)
57 SV* value
58 CODE:
59 RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
60 OUTPUT:
61 RETVAL
62
63bool
64Str(value)
65 SV* value
66 CODE:
67 RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
68 OUTPUT:
69 RETVAL
70
71bool
72Ref(value)
73 SV* value
74 CODE:
75 RETVAL = ck_sv_is_ref(value);
76 OUTPUT:
77 RETVAL
78
79bool
80ScalarRef(value)
81 SV* value
82 CODE:
83 RETVAL = 0;
84 if(
85 SvOK(value) && SvROK(value)
86 ){
87 int type = SvTYPE(SvRV(value));
88 if(
89 type == SVt_IV ||
90 type == SVt_NV ||
91 type == SVt_PV ||
92 type == SVt_NULL
93 ){
94 RETVAL = 1;
95 }
96 }
97 OUTPUT:
98 RETVAL
99
100bool
101ArrayRef(value)
102 SV* value
103 CODE:
104 RETVAL = ck_sv_ref_type(value, SVt_PVAV);
105 OUTPUT:
106 RETVAL
107
108bool
109HashRef(value)
110 SV* value
111 CODE:
112 RETVAL = (ck_sv_ref_type(value, SVt_PVHV) && !sv_isobject(value)) ? 1 : 0;
113 OUTPUT:
114 RETVAL
115
116bool
117CodeRef(value)
118 SV* value
119 CODE:
120 RETVAL = ck_sv_ref_type(value, SVt_PVCV);
121 OUTPUT:
122 RETVAL
123
124bool
125GlobRef(value)
126 SV* value
127 CODE:
128 RETVAL = ck_sv_ref_type(value, SVt_PVGV);
129 OUTPUT:
130 RETVAL
131
132bool
133Object(value)
134 SV* value
bc47531e 135 CODE:
136 RETVAL = 0;
137 if( ck_sv_is_ref(value)
138 && sv_isobject(value)
139 && !sv_isa(value, regclass)
140 ){
141 RETVAL = 1;
142 }
143 OUTPUT:
144 RETVAL
145
146bool
a4550ed1 147ObjectOfType(value, class)
148 SV* value
149 SV* class
150 PREINIT:
151 const char* classname;
152 CODE:
153 RETVAL = 0;
154
155 classname = SvPV_nolen(class);
156 if(!classname){
157 RETVAL = 0;
158 }
159
160 if( ck_sv_is_ref(value)
161 && sv_isobject(value)
162 && sv_derived_from(value, classname)
163 ){
164 RETVAL = 1;
165 }
166 OUTPUT:
167 RETVAL
168
169bool
bc47531e 170RegexpRef(value)
171 SV* value
bc47531e 172 CODE:
173 RETVAL = 0;
174 if( ck_sv_is_ref(value)
175 && sv_isobject(value)
176 && sv_isa(value, regclass)
177 ){
178 RETVAL = 1;
179 }
180 OUTPUT:
181 RETVAL
182
183