Add XS Optimized Type Constraints
[gitmo/Moose.git] / Moose.xs
1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6
7 static bool ck_sv_defined(SV*);
8 static bool ck_sv_is_ref(SV*);
9 static bool ck_sv_ref_type(SV*, int);
10
11 static bool
12 ck_sv_defined(SV* value){
13   return SvOK(value) ? 1 : 0; 
14 }
15
16 static bool
17 ck_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
25 static bool
26 ck_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
35 MODULE = Moose  PACKAGE = Moose::Util::TypeConstraints::OptimizedConstraints
36 PROTOTYPES: ENABLE
37
38 #ifdef HEHEHOHOHAHA
39 bool
40 Undefined(value)
41   SV* value
42   CODE:
43     RETVAL = !ck_sv_defined(value);
44   OUTPUT:
45     RETVAL
46
47 bool
48 Defined(value)
49   SV* value
50   CODE:
51     RETVAL = ck_sv_defined(value);
52   OUTPUT:
53     RETVAL
54
55 #endif
56
57 bool
58 Value(value)
59   SV* value
60   CODE:
61     RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
62   OUTPUT:
63     RETVAL
64
65 bool
66 Str(value)
67   SV* value
68   CODE:
69     RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
70   OUTPUT:
71     RETVAL
72
73 bool
74 Ref(value)
75   SV* value
76   CODE:
77     RETVAL = ck_sv_is_ref(value);
78   OUTPUT:
79     RETVAL
80
81 bool
82 ScalarRef(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
102 bool
103 ArrayRef(value)
104   SV* value
105   CODE:
106     RETVAL = ck_sv_ref_type(value, SVt_PVAV);
107   OUTPUT:
108     RETVAL
109
110 bool
111 HashRef(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
118 bool
119 CodeRef(value)
120   SV* value
121   CODE:
122     RETVAL = ck_sv_ref_type(value, SVt_PVCV);
123   OUTPUT:
124     RETVAL
125
126 bool
127 GlobRef(value)
128   SV* value
129   CODE:
130     RETVAL = ck_sv_ref_type(value, SVt_PVGV);
131   OUTPUT:
132     RETVAL
133
134 bool
135 Object(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
150 bool
151 RegexpRef(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