For Mouse::Util::MetaRole
gfx [Tue, 17 Nov 2009 06:42:40 +0000 (15:42 +0900)]
lib/Mouse/Meta/Module.pm
lib/Mouse/Util.pm
mouse.h
xs-src/Mouse.xs
xs-src/MouseAccessor.xs
xs-src/MouseAttribute.xs

index ab2fd6a..b9281a8 100755 (executable)
@@ -29,7 +29,17 @@ sub initialize {
         ||= $class->_construct_meta(package => $package_name, @args);
 }
 
-sub class_of{
+sub reinitialize {
+    my($class, $package_name, @args) = @_;
+
+    ($package_name && !ref($package_name))
+        || $class->throw_error("You must pass a package name and it cannot be blessed");
+
+    delete $METAS{$package_name};
+    return $class->initialize($package_name, @args);
+}
+
+sub _class_of{
     my($class_or_instance) = @_;
     return undef unless defined $class_or_instance;
     return $METAS{ ref($class_or_instance) || $class_or_instance };
@@ -37,14 +47,14 @@ sub class_of{
 
 # Means of accessing all the metaclasses that have
 # been initialized thus far
-#sub get_all_metaclasses         {        %METAS         }
-sub get_all_metaclass_instances { values %METAS         }
-sub get_all_metaclass_names     { keys   %METAS         }
-sub get_metaclass_by_name       { $METAS{$_[0]}         }
-#sub store_metaclass_by_name     { $METAS{$_[0]} = $_[1] }
-#sub weaken_metaclass            { weaken($METAS{$_[0]}) }
-#sub does_metaclass_exist        { defined $METAS{$_[0]} }
-#sub remove_metaclass_by_name    { delete $METAS{$_[0]}  }
+#sub _get_all_metaclasses         {        %METAS         }
+sub _get_all_metaclass_instances { values %METAS         }
+sub _get_all_metaclass_names     { keys   %METAS         }
+sub _get_metaclass_by_name       { $METAS{$_[0]}         }
+#sub _store_metaclass_by_name     { $METAS{$_[0]} = $_[1] }
+#sub _weaken_metaclass            { weaken($METAS{$_[0]}) }
+#sub _does_metaclass_exist        { defined $METAS{$_[0]} }
+#sub _remove_metaclass_by_name    { delete $METAS{$_[0]}  }
 
 sub name;
 
index 34f6eee..b99cd8f 100644 (file)
@@ -73,10 +73,10 @@ Mouse::Exporter->setup_import_methods(
 require Mouse::Meta::Module; # for the entities of metaclass cache utilities
 
 BEGIN {
-    *class_of                    = \&Mouse::Meta::Module::class_of;
-    *get_metaclass_by_name       = \&Mouse::Meta::Module::get_metaclass_by_name;
-    *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances;
-    *get_all_metaclass_names     = \&Mouse::Meta::Module::get_all_metaclass_names;
+    *class_of                    = \&Mouse::Meta::Module::_class_of;
+    *get_metaclass_by_name       = \&Mouse::Meta::Module::_get_metaclass_by_name;
+    *get_all_metaclass_instances = \&Mouse::Meta::Module::_get_all_metaclass_instances;
+    *get_all_metaclass_names     = \&Mouse::Meta::Module::_get_all_metaclass_names;
 
     # is-a predicates
     generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint');
diff --git a/mouse.h b/mouse.h
index 5fab08f..e6b8218 100644 (file)
--- a/mouse.h
+++ b/mouse.h
@@ -142,14 +142,15 @@ void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
 #define set_slots(self, key, value) set_slot(self, sv_2mortal(newSVpvs_share(key)), value)
 
 /* mouse_simle_accessor.xs */
+#define INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTSV(klass, name, dsv) \
+    (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #name, sizeof(#name)-1, XS_Mouse_simple_accessor, (dsv), HEf_SVKEY)
+#define INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(klass, name, ds) \
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTSV(klass, name, newSVpvs(ds))
+
 #define INSTALL_SIMPLE_READER(klass, name) \
     INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
 #define INSTALL_SIMPLE_READER_WITH_KEY(klass, name, key) \
     (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #key, sizeof(#key)-1, XS_Mouse_simple_reader, NULL, 0)
-#define INSTALL_SIMPLE_READER_WITH_DEFAULTSV(klass, name, dsv) \
-    (void)mouse_install_simple_accessor(aTHX_ "Mouse::Meta::" #klass "::" #name, #name, sizeof(#name)-1, XS_Mouse_simple_reader, (dsv), HEf_SVKEY)
-#define INSTALL_SIMPLE_READER_WITH_DEFAULTS(klass, name, ds) \
-    INSTALL_SIMPLE_READER_WITH_DEFAULTSV(klass, name, newSVpvs(ds))
 
 #define INSTALL_SIMPLE_WRITER(klass, name) \
     NSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
@@ -163,6 +164,7 @@ void mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot);
 
 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);
 
+XS(XS_Mouse_simple_accessor);
 XS(XS_Mouse_simple_reader);
 XS(XS_Mouse_simple_writer);
 XS(XS_Mouse_simple_clearer);
index f008333..7c27924 100644 (file)
@@ -437,10 +437,10 @@ BOOT:
     INSTALL_SIMPLE_READER(Class, roles);
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
 
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Class, method_metaclass,     "Mouse::Meta::Method");
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Class, attribute_metaclass,  "Mouse::Meta::Attribute");
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Class, constructor_class,    "Mouse::Meta::Method::Constructor::XS");
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Class, destructor_class,     "Mouse::Meta::Method::Destructor::XS");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Class, method_metaclass,     "Mouse::Meta::Method");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Class, attribute_metaclass,  "Mouse::Meta::Attribute");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Class, constructor_class,    "Mouse::Meta::Method::Constructor::XS");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Class, destructor_class,     "Mouse::Meta::Method::Destructor::XS");
 
     newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Constructor::XS", TRUE), "_generate_constructor",
         newRV_inc((SV*)get_cvs("Mouse::Object::new", TRUE)));
@@ -508,7 +508,7 @@ BOOT:
     INSTALL_SIMPLE_READER_WITH_KEY(Role, get_roles, roles);
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Role, is_anon_role, anon_serial_id);
 
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Role, method_metaclass,  "Mouse::Meta::Role::Method");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Role, method_metaclass,  "Mouse::Meta::Role::Method");
 
 MODULE = Mouse  PACKAGE = Mouse::Object
 
index fbe8ab8..ddd6333 100644 (file)
@@ -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);
 }
 
index 30163fb..f494f17 100644 (file)
@@ -218,5 +218,5 @@ BOOT:
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
 
-    INSTALL_SIMPLE_READER_WITH_DEFAULTS(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
+    INSTALL_SIMPLE_ACCESSOR_WITH_DEFAULTS(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");