3 static MGVTBL mouse_xa_vtbl; /* identity */
6 mouse_build_xa(pTHX_ SV* const attr) {
20 mg = sv_magicext(SvRV(attr), (SV*)xa, PERL_MAGIC_ext, &mouse_xa_vtbl, NULL, 0);
21 SvREFCNT_dec(xa); /* refcnt++ in sv_magicext */
23 av_extend(xa, MOUSE_XA_last - 1);
25 slot = mcall0(attr, mouse_name);
26 pv = SvPV_const(slot, len);
27 av_store(xa, MOUSE_XA_SLOT, newSVpvn_share(pv, len, 0U));
29 av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
31 av_store(xa, MOUSE_XA_INIT_ARG, newSVsv(mcall0s(attr, "init_arg")));
33 if(predicate_calls(attr, "has_type_constraint")){
35 flags |= MOUSEf_ATTR_HAS_TC;
37 tc = mcall0s(attr, "type_constraint");
38 av_store(xa, MOUSE_XA_TC, newSVsv(tc));
40 if(predicate_calls(attr, "should_auto_deref")){
41 SV* const is_a_type_of = sv_2mortal(newSVpvs_share("is_a_type_of"));
43 flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
44 if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
45 flags |= MOUSEf_TC_IS_ARRAYREF;
47 else if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("HashRef", SVs_TEMP))) ){
48 flags |= MOUSEf_TC_IS_HASHREF;
51 mouse_throw_error(attr, tc,
52 "Can not auto de-reference the type constraint '%"SVf"'",
53 mcall0(tc, mouse_name));
57 if(predicate_calls(attr, "should_coerce") && predicate_calls(tc, "has_coercion")){
58 flags |= MOUSEf_ATTR_SHOULD_COERCE;
63 if(predicate_calls(attr, "has_trigger")){
64 flags |= MOUSEf_ATTR_HAS_TRIGGER;
67 if(predicate_calls(attr, "is_lazy")){
68 flags |= MOUSEf_ATTR_IS_LAZY;
70 if(predicate_calls(attr, "has_builder")){
71 flags |= MOUSEf_ATTR_HAS_BUILDER;
73 else if(predicate_calls(attr, "has_default")){
74 flags |= MOUSEf_ATTR_HAS_DEFAULT;
77 if(predicate_calls(attr, "is_weak_ref")){
78 flags |= MOUSEf_ATTR_IS_WEAK_REF;
81 if(predicate_calls(attr, "is_required")){
82 flags |= MOUSEf_ATTR_IS_REQUIRED;
85 av_store(xa, MOUSE_XA_FLAGS, newSVuv(flags));
86 MOUSE_mg_flags(mg) = flags;
95 mouse_get_xa(pTHX_ SV* const attr) {
100 croak("Not a Mouse meta attribute");
103 mg = mouse_mg_find(aTHX_ SvRV(attr), &mouse_xa_vtbl, 0x00);
105 xa = mouse_build_xa(aTHX_ attr);
108 xa = (AV*)MOUSE_mg_obj(mg);
111 assert(SvTYPE(xa) == SVt_PVAV);
118 mouse_xa_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
119 SV* const tc = MOUSE_xa_tc(xa);
122 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
123 value = mcall1(tc, mouse_coerce, value);
126 if(!SvOK(MOUSE_xa_tc_code(xa))){
127 tc_code = mcall0s(tc, "_compiled_type_constraint");
128 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
130 if(!IsCodeRef(tc_code)){
131 mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
135 tc_code = MOUSE_xa_tc_code(xa);
138 if(!mouse_tc_check(aTHX_ tc_code, value)){
139 mouse_throw_error(MOUSE_xa_attribute(xa), value,
140 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
141 mcall0(MOUSE_xa_attribute(xa), mouse_name),
142 mcall1s(tc, "get_message", value));
150 mouse_xa_set_default(pTHX_ AV* const xa, SV* const object) {
151 U16 const flags = (U16)MOUSE_xa_flags(xa);
157 /* get default value by $attr->builder or $attr->default */
158 if(flags & MOUSEf_ATTR_HAS_BUILDER){
159 SV* const builder = mcall0s(MOUSE_xa_attribute(xa), "builder");
160 value = mcall0(object, builder); /* $object->$builder() */
163 value = mcall0s(MOUSE_xa_attribute(xa), "default");
165 if(IsCodeRef(value)){
166 value = mcall0(object, value);
170 /* apply coerce and type constraint */
171 if(flags & MOUSEf_ATTR_HAS_TC){
172 value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
175 /* store value to slot */
176 value = set_slot(object, MOUSE_xa_slot(xa), value);
177 if(flags & MOUSEf_ATTR_IS_WEAK_REF && SvROK(value)){
178 weaken_slot(object, MOUSE_xa_slot(xa));
187 MODULE = Mouse::Meta::Attribute PACKAGE = Mouse::Meta::Attribute
193 INSTALL_SIMPLE_READER(Attribute, name);
194 INSTALL_SIMPLE_READER(Attribute, associated_class);
195 INSTALL_SIMPLE_READER(Attribute, accessor);
196 INSTALL_SIMPLE_READER(Attribute, reader);
197 INSTALL_SIMPLE_READER(Attribute, writer);
198 INSTALL_SIMPLE_READER(Attribute, predicate);
199 INSTALL_SIMPLE_READER(Attribute, clearer);
200 INSTALL_SIMPLE_READER(Attribute, handles);
202 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, _is_metadata, is);
203 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_required, required);
204 INSTALL_SIMPLE_READER(Attribute, default);
205 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy, lazy);
206 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy_build, lazy_build);
207 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_weak_ref, weak_ref);
208 INSTALL_SIMPLE_READER(Attribute, init_arg);
209 INSTALL_SIMPLE_READER(Attribute, type_constraint);
210 INSTALL_SIMPLE_READER(Attribute, trigger);
211 INSTALL_SIMPLE_READER(Attribute, builder);
212 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_auto_deref, auto_deref);
213 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_coerce, coerce);
214 INSTALL_SIMPLE_READER(Attribute, documentation);
217 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_accessor, accessor);
218 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_reader, reader);
219 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_writer, writer);
220 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_predicate, predicate);
221 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_clearer, clearer);
222 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_handles, handles);
224 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_default, default);
225 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_type_constraint, type_constraint);
226 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_trigger, trigger);
227 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
228 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
230 INSTALL_CLASS_HOLDER(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
233 _process_options(SV* klass, SV* name, HV* args)
239 /* 'required' requires eigher 'init_arg', 'builder', or 'default' */
240 bool can_be_required = FALSE;
241 bool has_default = FALSE;
242 bool has_builder = FALSE;
244 /* taken from Class::MOP::Attribute::new */
247 mouse_throw_error(klass, NULL,
248 "You must provide a name for the attribute");
251 svp = hv_fetchs(args, "init_arg", FALSE);
253 (void)hv_stores(args, "init_arg", newSVsv(name));
254 can_be_required = TRUE;
257 can_be_required = SvOK(*svp) ? TRUE : FALSE;
260 svp = hv_fetchs(args, "builder", FALSE);
263 mouse_throw_error(klass, NULL,
264 "builder must be a defined scalar value which is a method name");
266 can_be_required = TRUE;
269 else if((svp = hv_fetchs(args, "default", FALSE))){
270 if(SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) {
271 mouse_throw_error(klass, NULL,
272 "References are not allowed as default values, you must "
273 "wrap the default of '%"SVf"' in a CODE reference "
274 "(ex: sub { [] } and not [])", name);
276 can_be_required = TRUE;
280 svp = hv_fetchs(args, "required", FALSE);
281 if( (svp && sv_true(*svp)) && !can_be_required){
282 mouse_throw_error(klass, NULL,
283 "You cannot have a required attribute (%"SVf") "
284 "without a default, builder, or an init_arg", name);
287 /* taken from Mouse::Meta::Attribute->new and ->_process_args */
289 svp = hv_fetchs(args, "is", FALSE);
291 const char* const is = SvOK(*svp) ? SvPV_nolen_const(*svp) : "undef";
293 svp = hv_fetchs(args, "reader", TRUE);
295 sv_setsv(*svp, name);
298 else if(strEQ(is, "rw")){
299 if(hv_fetchs(args, "writer", FALSE)){
300 svp = hv_fetchs(args, "reader", TRUE);
303 svp = hv_fetchs(args, "accessor", TRUE);
305 sv_setsv(*svp, name);
307 else if(strEQ(is, "bare")){
308 /* do nothing, but don't complain (later) about missing methods */
311 mouse_throw_error(klass, NULL,
312 "I do not understand this option (is => %s) on attribute (%"SVf")",
317 svp = hv_fetchs(args, "isa", FALSE);
324 call_pv("Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint",
330 else if((svp = hv_fetchs(args, "does", FALSE))){
336 call_pv("Mouse::Util::TypeConstraints::find_or_create_does_type_constraint",
343 (void)hv_stores(args, "type_constraint", tc);
346 svp = hv_fetchs(args, "coerce", FALSE);
349 mouse_throw_error(klass, NULL,
350 "You cannot have coercion without specifying a type constraint "
351 "on attribute (%"SVf")", name);
353 svp = hv_fetchs(args, "weak_ref", FALSE);
354 if(svp && sv_true(*svp)){
355 mouse_throw_error(klass, NULL,
356 "You cannot have a weak reference to a coerced value on "
357 "attribute (%"SVf")", name);
361 svp = hv_fetchs(args, "lazy_build", FALSE);
366 mouse_throw_error(klass, NULL,
367 "You can not use lazy_build and default for the same "
368 "attribute (%"SVf")", name);
371 svp = hv_fetchs(args, "lazy", TRUE);
372 sv_setiv(*svp, TRUE);
374 svp = hv_fetchs(args, "builder", TRUE);
376 sv_setpvf(*svp, "_build_%"SVf, name);
380 clearer = *hv_fetchs(args, "clearer", TRUE);
381 predicate = *hv_fetchs(args, "predicate", TRUE);
383 if(SvPV_nolen_const(name)[0] == '_'){
384 if(!sv_true(clearer)){
385 sv_setpvf(clearer, "_clear%"SVf, name);
387 if(!sv_true(predicate)){
388 sv_setpvf(predicate, "_has%"SVf, name);
392 if(!sv_true(clearer)){
393 sv_setpvf(clearer, "clear_%"SVf, name);
395 if(!sv_true(predicate)){
396 sv_setpvf(predicate, "has_%"SVf, name);
401 svp = hv_fetchs(args, "auto_deref", FALSE);
402 if(svp && sv_true(*svp)){
403 SV* const meth = sv_2mortal(newSVpvs_share("is_a_type_of"));
405 mouse_throw_error(klass, NULL,
406 "You cannot auto-dereference without specifying a type "
407 "constraint on attribute (%"SVf")", name);
410 if(!(sv_true(mcall1(tc, meth, newSVpvs_flags("ArrayRef", SVs_TEMP)))
411 || sv_true(mcall1(tc, meth, newSVpvs_flags("HashRef", SVs_TEMP))) )){
412 mouse_throw_error(klass, NULL,
413 "You cannot auto-dereference anything other than a ArrayRef "
414 "or HashRef on attribute (%"SVf")", name);
418 svp = hv_fetchs(args, "trigger", FALSE);
420 if(!IsCodeRef(*svp)){
421 mouse_throw_error(klass, NULL,
422 "Trigger must be a CODE ref on attribute (%"SVf")",
428 svp = hv_fetchs(args, "lazy", FALSE);
429 if(svp && sv_true(*svp)){
430 if(!(has_default || has_builder)){
431 mouse_throw_error(klass, NULL,
432 "You cannot have lazy attribute (%"SVf") without specifying "
433 "a default value for it", name);