changes in Moose::Object
[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 static const char *regclass = "Regexp";
35
36 MODULE = Moose  PACKAGE = Moose::Util::TypeConstraints::OptimizedConstraints
37 PROTOTYPES: ENABLE
38
39 bool
40 Undef(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 bool
56 Value(value)
57   SV* value
58   CODE:
59     RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
60   OUTPUT:
61     RETVAL
62
63 bool
64 Str(value)
65   SV* value
66   CODE:
67     RETVAL = (ck_sv_defined(value) && !ck_sv_is_ref(value)) ? 1 : 0;
68   OUTPUT:
69     RETVAL
70
71 bool
72 Ref(value)
73   SV* value
74   CODE:
75     RETVAL = ck_sv_is_ref(value);
76   OUTPUT:
77     RETVAL
78
79 bool
80 ScalarRef(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
100 bool
101 ArrayRef(value)
102   SV* value
103   CODE:
104     RETVAL = ck_sv_ref_type(value, SVt_PVAV);
105   OUTPUT:
106     RETVAL
107
108 bool
109 HashRef(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
116 bool
117 CodeRef(value)
118   SV* value
119   CODE:
120     RETVAL = ck_sv_ref_type(value, SVt_PVCV);
121   OUTPUT:
122     RETVAL
123
124 bool
125 GlobRef(value)
126   SV* value
127   CODE:
128     RETVAL = ck_sv_ref_type(value, SVt_PVGV);
129   OUTPUT:
130     RETVAL
131
132 bool
133 Object(value)
134   SV* value
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
146 bool
147 ObjectOfType(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
169 bool
170 RegexpRef(value)
171   SV* value
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