X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=xs-src%2FMouseAccessor.xs;h=f93d25f8fc10e125cee029c3ccaf02a1a589fc35;hb=93fe8a5fa9c9db1d8901ad4c47a13de670673dfa;hp=fbe8ab803789694d5872356b3c3d064996997c87;hpb=e058b279f94cfd76ca59d7972209e1b346492db9;p=gitmo%2FMouse.git diff --git a/xs-src/MouseAccessor.xs b/xs-src/MouseAccessor.xs index fbe8ab8..f93d25f 100644 --- a/xs-src/MouseAccessor.xs +++ b/xs-src/MouseAccessor.xs @@ -28,7 +28,7 @@ mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) { CV* -mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){ +mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){ AV* const xa = mouse_get_xa(aTHX_ attr); CV* xsub; MAGIC* mg; @@ -221,7 +221,7 @@ mouse_accessor_get_mg(pTHX_ CV* const xsub){ */ CV* -mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen){ +mouse_simple_accessor_generate(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen){ CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__); SV* const slot = newSVpvn_share(key, keylen, 0U); MAGIC* mg; @@ -245,6 +245,37 @@ mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const return xsub; } +XS(XS_Mouse_simple_accessor) +{ + dVAR; dXSARGS; + dMOUSE_self; + MAGIC* const mg = (MAGIC*)XSANY.any_ptr; + SV* value; + + if(items == 1){ /* reader */ + value = get_slot(self, MOUSE_mg_slot(mg)); + if(!value) { + if(MOUSE_mg_ptr(mg)){ + /* the default value must be a SV */ + assert(MOUSE_mg_len(mg) == HEf_SVKEY); + value = (SV*)MOUSE_mg_ptr(mg); + } + else{ + value = &PL_sv_undef; + } + } + } + else if(items == 2){ /* writer */ + value = set_slot(self, MOUSE_mg_slot(mg), ST(1)); + } + else { + croak("Expected exactly one or two argument for an accessor for '%"SVf"'", MOUSE_mg_slot(mg)); + } + + ST(0) = value; + XSRETURN(1); +} + XS(XS_Mouse_simple_reader) { dVAR; dXSARGS; @@ -257,17 +288,7 @@ XS(XS_Mouse_simple_reader) } value = get_slot(self, MOUSE_mg_slot(mg)); - if(!value) { - if(MOUSE_mg_ptr(mg)){ - /* the default value must be a SV */ - assert(MOUSE_mg_len(mg) == HEf_SVKEY); - value = (SV*)MOUSE_mg_ptr(mg); - } - else{ - value = &PL_sv_undef; - } - } - ST(0) = value; + ST(0) = value ? value : &PL_sv_undef; XSRETURN(1); } @@ -396,7 +417,7 @@ CV* _generate_accessor(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_accessor); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor); } OUTPUT: RETVAL @@ -405,7 +426,7 @@ CV* _generate_reader(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_reader); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader); } OUTPUT: RETVAL @@ -414,7 +435,7 @@ CV* _generate_writer(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_writer); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer); } OUTPUT: RETVAL @@ -426,7 +447,7 @@ CODE: SV* const slot = mcall0s(attr, "name"); STRLEN len; const char* const pv = SvPV_const(slot, len); - RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0); + RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0); } OUTPUT: RETVAL @@ -438,7 +459,7 @@ CODE: SV* const slot = mcall0s(attr, "name"); STRLEN len; const char* const pv = SvPV_const(slot, len); - RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0); + RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0); } OUTPUT: RETVAL