3 #define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object"); \
7 if(SvTIED_mg(SvRV(instance), PERL_MAGIC_tied)){ \
8 croak("MOP::Instance: tied HASH is not yet supported"); \
13 mop_instance_create_instance(pTHX_ HV* const stash) {
15 return sv_bless( newRV_noinc((SV*)newHV()), stash );
19 mop_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
22 CHECK_INSTANCE(instance);
23 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
27 mop_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
31 CHECK_INSTANCE(instance);
32 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
33 return he ? HeVAL(he) : NULL;
37 mop_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
43 CHECK_INSTANCE(instance);
44 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
46 sv_setsv_mg(sv, value);
51 mop_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
54 CHECK_INSTANCE(instance);
55 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
59 mop_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
63 CHECK_INSTANCE(instance);
64 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
66 sv_rvweaken(HeVAL(he));
70 static const mop_instance_vtbl mop_default_instance = {
71 mop_instance_create_instance,
72 mop_instance_has_slot,
73 mop_instance_get_slot,
74 mop_instance_set_slot,
75 mop_instance_delete_slot,
76 mop_instance_weaken_slot,
80 const mop_instance_vtbl*
81 mop_get_default_instance_vtbl(pTHX){
82 return &mop_default_instance;
86 MODULE = Class::MOP::Instance PACKAGE = Class::MOP::Instance
91 INSTALL_SIMPLE_READER(Instance, associated_metaclass);
96 CV* const default_method = get_cv("Class::MOP::Instance::get_slot_value", FALSE);
97 SV* const can = newSVpvs_flags("can", SVs_TEMP);
98 SV* const method = newSVpvs_flags("get_slot_value", SVs_TEMP);
101 /* $self->can("get_slot_value") == \&Class::MOP::Instance::get_slot_value */
102 code_ref = mop_call1(aTHX_ self, can, method);
103 if(SvROK(code_ref) && SvRV(code_ref) == (SV*)default_method){
104 RETVAL = (void*)&mop_default_instance;