5 mouse_get_xa(pTHX_ SV* const attr) {
6 static MGVTBL mouse_xa_vtbl; /* identity */
12 croak("Not a Mouse meta attribute");
15 mg = mouse_mg_find(aTHX_ SvRV(attr), &mouse_xa_vtbl, 0x00);
27 mg = sv_magicext(SvRV(attr), (SV*)xa, PERL_MAGIC_ext, &mouse_xa_vtbl,NULL, 0);
28 SvREFCNT_dec(xa); /* refcnt++ in sv_magicext */
30 av_extend(xa, MOUSE_XA_last - 1);
32 slot = mcall0(attr, mouse_name);
33 pv = SvPV_const(slot, len);
34 av_store(xa, MOUSE_XA_SLOT, newSVpvn_share(pv, len, 0U));
36 av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
38 av_store(xa, MOUSE_XA_INIT_ARG, newSVsv(mcall0s(attr, "init_arg")));
40 if(predicate_calls(attr, "has_type_constraint")){
42 flags |= MOUSEf_ATTR_HAS_TC;
44 tc = mcall0s(attr, "type_constraint");
45 av_store(xa, MOUSE_XA_TC, newSVsv(tc));
47 if(predicate_calls(attr, "should_auto_deref")){
48 SV* const is_a_type_of = sv_2mortal(newSVpvs_share("is_a_type_of"));
50 flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
51 if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
52 flags |= MOUSEf_TC_IS_ARRAYREF;
54 else if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("HashRef", SVs_TEMP))) ){
55 flags |= MOUSEf_TC_IS_HASHREF;
58 mouse_throw_error(attr, tc,
59 "Can not auto de-reference the type constraint '%"SVf"'",
60 mcall0(tc, mouse_name));
64 if(predicate_calls(attr, "should_coerce")){
65 flags |= MOUSEf_ATTR_SHOULD_COERCE;
70 if(predicate_calls(attr, "has_trigger")){
71 flags |= MOUSEf_ATTR_HAS_TRIGGER;
74 if(predicate_calls(attr, "is_lazy")){
75 flags |= MOUSEf_ATTR_IS_LAZY;
77 if(predicate_calls(attr, "has_builder")){
78 flags |= MOUSEf_ATTR_HAS_BUILDER;
80 else if(predicate_calls(attr, "has_default")){
81 flags |= MOUSEf_ATTR_HAS_DEFAULT;
84 if(predicate_calls(attr, "is_weak_ref")){
85 flags |= MOUSEf_ATTR_IS_WEAK_REF;
88 if(predicate_calls(attr, "is_required")){
89 flags |= MOUSEf_ATTR_IS_REQUIRED;
92 av_store(xa, MOUSE_XA_FLAGS, newSVuv(flags));
93 MOUSE_mg_flags(mg) = flags;
99 xa = (AV*)MOUSE_mg_obj(mg);
102 assert(SvTYPE(xa) == SVt_PVAV);
109 mouse_xa_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
110 SV* const tc = MOUSE_xa_tc(xa);
113 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
114 value = mcall1(tc, mouse_coerce, value);
117 if(!SvOK(MOUSE_xa_tc_code(xa))){
118 tc_code = mcall0s(tc, "_compiled_type_constraint");
119 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
121 if(!IsCodeRef(tc_code)){
122 mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
126 tc_code = MOUSE_xa_tc_code(xa);
129 if(!mouse_tc_check(aTHX_ tc_code, value)){
130 mouse_throw_error(MOUSE_xa_attribute(xa), value,
131 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
132 mcall0(MOUSE_xa_attribute(xa), mouse_name),
133 mcall1s(tc, "get_message", value));
141 mouse_xa_set_default(pTHX_ AV* const xa, SV* const object) {
142 U16 const flags = (U16)MOUSE_xa_flags(xa);
148 /* get default value by $attr->builder or $attr->default */
149 if(flags & MOUSEf_ATTR_HAS_BUILDER){
150 SV* const builder = mcall0s(MOUSE_xa_attribute(xa), "builder");
151 value = mcall0(object, builder); /* $object->$builder() */
154 value = mcall0s(MOUSE_xa_attribute(xa), "default");
156 if(IsCodeRef(value)){
157 value = mcall0(object, value);
161 /* apply coerce and type constraint */
162 if(flags & MOUSEf_ATTR_HAS_TC){
163 value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
166 /* store value to slot */
167 value = set_slot(object, MOUSE_xa_slot(xa), value);
168 if(flags & MOUSEf_ATTR_IS_WEAK_REF && SvROK(value)){
169 weaken_slot(object, MOUSE_xa_slot(xa));
178 MODULE = Mouse::Meta::Attribute PACKAGE = Mouse::Meta::Attribute
184 INSTALL_SIMPLE_READER(Attribute, name);
185 INSTALL_SIMPLE_READER(Attribute, associated_class);
186 INSTALL_SIMPLE_READER(Attribute, accessor);
187 INSTALL_SIMPLE_READER(Attribute, reader);
188 INSTALL_SIMPLE_READER(Attribute, writer);
189 INSTALL_SIMPLE_READER(Attribute, predicate);
190 INSTALL_SIMPLE_READER(Attribute, clearer);
191 INSTALL_SIMPLE_READER(Attribute, handles);
193 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, _is_metadata, is);
194 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_required, required);
195 INSTALL_SIMPLE_READER(Attribute, default);
196 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy, lazy);
197 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy_build, lazy_build);
198 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_weak_ref, weak_ref);
199 INSTALL_SIMPLE_READER(Attribute, init_arg);
200 INSTALL_SIMPLE_READER(Attribute, type_constraint);
201 INSTALL_SIMPLE_READER(Attribute, trigger);
202 INSTALL_SIMPLE_READER(Attribute, builder);
203 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_auto_deref, auto_deref);
204 INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_coerce, coerce);
205 INSTALL_SIMPLE_READER(Attribute, documentation);
208 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_accessor, accessor);
209 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_reader, reader);
210 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_writer, writer);
211 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_predicate, predicate);
212 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_clearer, clearer);
213 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_handles, handles);
215 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_default, default);
216 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_type_constraint, type_constraint);
217 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_trigger, trigger);
218 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
219 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
221 INSTALL_CLASS_HOLDER(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
224 _process_options(SV* klass, SV* name, HV* args)
230 /* 'required' requires eigher 'init_arg', 'builder', or 'default' */
231 bool can_be_required = FALSE;
232 bool has_default = FALSE;
233 bool has_builder = FALSE;
235 /* taken from Class::MOP::Attribute::new */
238 mouse_throw_error(klass, NULL,
239 "You must provide a name for the attribute");
242 svp = hv_fetchs(args, "init_arg", FALSE);
244 (void)hv_stores(args, "init_arg", newSVsv(name));
245 can_be_required = TRUE;
248 can_be_required = SvOK(*svp) ? TRUE : FALSE;
251 svp = hv_fetchs(args, "builder", FALSE);
254 mouse_throw_error(klass, NULL,
255 "builder must be a defined scalar value which is a method name");
257 can_be_required = TRUE;
260 else if((svp = hv_fetchs(args, "default", FALSE))){
261 if(SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) {
262 mouse_throw_error(klass, NULL,
263 "References are not allowed as default values, you must "
264 "wrap the default of '%"SVf"' in a CODE reference "
265 "(ex: sub { [] } and not [])", name);
267 can_be_required = TRUE;
271 svp = hv_fetchs(args, "required", FALSE);
272 if( (svp && sv_true(*svp)) && !can_be_required){
273 mouse_throw_error(klass, NULL,
274 "You cannot have a required attribute (%"SVf") "
275 "without a default, builder, or an init_arg", name);
278 /* taken from Mouse::Meta::Attribute->new and ->_process_args */
280 svp = hv_fetchs(args, "is", FALSE);
282 const char* const is = SvOK(*svp) ? SvPV_nolen_const(*svp) : "undef";
284 svp = hv_fetchs(args, "reader", TRUE);
286 sv_setsv(*svp, name);
289 else if(strEQ(is, "rw")){
290 if(hv_fetchs(args, "writer", FALSE)){
291 svp = hv_fetchs(args, "reader", TRUE);
294 svp = hv_fetchs(args, "accessor", TRUE);
296 sv_setsv(*svp, name);
298 else if(strEQ(is, "bare")){
299 /* do nothing, but don't complain (later) about missing methods */
302 mouse_throw_error(klass, NULL,
303 "I do not understand this option (is => %s) on attribute (%"SVf")",
308 svp = hv_fetchs(args, "isa", FALSE);
315 call_pv("Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint",
321 else if((svp = hv_fetchs(args, "does", FALSE))){
327 call_pv("Mouse::Util::TypeConstraints::find_or_create_does_type_constraint",
334 (void)hv_stores(args, "type_constraint", tc);
337 svp = hv_fetchs(args, "coerce", FALSE);
340 mouse_throw_error(klass, NULL,
341 "You cannot have coercion without specifying a type constraint "
342 "on attribute (%"SVf")", name);
344 svp = hv_fetchs(args, "weak_ref", FALSE);
345 if(svp && sv_true(*svp)){
346 mouse_throw_error(klass, NULL,
347 "You cannot have a weak reference to a coerced value on "
348 "attribute (%"SVf")", name);
352 svp = hv_fetchs(args, "lazy_build", FALSE);
357 mouse_throw_error(klass, NULL,
358 "You can not use lazy_build and default for the same "
359 "attribute (%"SVf")", name);
362 svp = hv_fetchs(args, "lazy", TRUE);
363 sv_setiv(*svp, TRUE);
365 svp = hv_fetchs(args, "builder", TRUE);
367 sv_setpvf(*svp, "_build_%"SVf, name);
371 clearer = *hv_fetchs(args, "clearer", TRUE);
372 predicate = *hv_fetchs(args, "predicate", TRUE);
374 if(SvPV_nolen_const(name)[0] == '_'){
375 if(!sv_true(clearer)){
376 sv_setpvf(clearer, "_clear%"SVf, name);
378 if(!sv_true(predicate)){
379 sv_setpvf(predicate, "_has%"SVf, name);
383 if(!sv_true(clearer)){
384 sv_setpvf(clearer, "clear_%"SVf, name);
386 if(!sv_true(predicate)){
387 sv_setpvf(predicate, "has_%"SVf, name);
392 svp = hv_fetchs(args, "auto_deref", FALSE);
393 if(svp && sv_true(*svp)){
394 SV* const meth = sv_2mortal(newSVpvs_share("is_a_type_of"));
396 mouse_throw_error(klass, NULL,
397 "You cannot auto-dereference without specifying a type "
398 "constraint on attribute (%"SVf")", name);
401 if(!(sv_true(mcall1(tc, meth, newSVpvs_flags("ArrayRef", SVs_TEMP)))
402 || sv_true(mcall1(tc, meth, newSVpvs_flags("HashRef", SVs_TEMP))) )){
403 mouse_throw_error(klass, NULL,
404 "You cannot auto-dereference anything other than a ArrayRef "
405 "or HashRef on attribute (%"SVf")", name);
409 svp = hv_fetchs(args, "trigger", FALSE);
411 if(!IsCodeRef(*svp)){
412 mouse_throw_error(klass, NULL,
413 "Trigger must be a CODE ref on attribute (%"SVf")",
419 svp = hv_fetchs(args, "lazy", FALSE);
420 if(svp && sv_true(*svp)){
421 if(!(has_default || has_builder)){
422 mouse_throw_error(klass, NULL,
423 "You cannot have lazy attribute (%"SVf") without specifying "
424 "a default value for it", name);