[svn:parrot] r46992 - trunk/src/pmc

NotFound at svn.parrot.org NotFound at svn.parrot.org
Tue May 25 16:24:51 UTC 2010


Author: NotFound
Date: Tue May 25 16:24:50 2010
New Revision: 46992
URL: https://trac.parrot.org/parrot/changeset/46992

Log:
[cage] unify common parts of set/get attrs in Exception PMC

Modified:
   trunk/src/pmc/exception.pmc

Modified: trunk/src/pmc/exception.pmc
==============================================================================
--- trunk/src/pmc/exception.pmc	Tue May 25 15:29:18 2010	(r46991)
+++ trunk/src/pmc/exception.pmc	Tue May 25 16:24:50 2010	(r46992)
@@ -52,10 +52,38 @@
 #include "parrot/exceptions.h"
 #include "pmc/pmc_sub.h"
 
+typedef enum {
+    attr_id,
+    attr_birthime,
+    attr_message,
+    attr_payload,
+    attr_resume,
+    attr_severity,
+    attr_type,
+    attr_exit_code,
+    attr_backtrace,
+    attr_handled,
+    attr_handler_iter,
+    attr_handler_ctx,
+    attr_thrower,
+    attr_NONE = -1
+} AttrEnum;
+
 /* HEADERIZER HFILE: none */
 /* HEADERIZER BEGIN: static */
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
+
+static AttrEnum getAttrEnum(PARROT_INTERP, ARGIN(const STRING *name))
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2);
+
+#define ASSERT_ARGS_getAttrEnum __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(name))
+/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 /* HEADERIZER END: static */
 
+
 pmclass Exception auto_attrs {
 
     ATTR INTVAL          id;           /* The task ID in the scheduler. */
@@ -250,19 +278,20 @@
         STRING * const name = VTABLE_get_string(INTERP, key);
         INTVAL  result = 0;
 
-        if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "type"))) {
+        switch (getAttrEnum(INTERP, name)) {
+          case attr_type:
             GET_ATTR_type(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "severity"))) {
+            break;
+          case attr_severity:
             GET_ATTR_severity(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "exit_code"))) {
+            break;
+          case attr_exit_code:
             GET_ATTR_exit_code(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handled"))) {
+            break;
+          case attr_handled:
             GET_ATTR_handled(INTERP, SELF, result);
-        }
-        else {
+            break;
+          default:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such integer attribute '%S'", name);
@@ -283,20 +312,20 @@
 
     VTABLE INTVAL get_integer_keyed_str(STRING *key) {
         INTVAL result = 0;
-
-        if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "type"))) {
+        switch (getAttrEnum(INTERP, key)) {
+          case attr_type:
             GET_ATTR_type(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "severity"))) {
+            break;
+          case attr_severity:
             GET_ATTR_severity(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "exit_code"))) {
+            break;
+          case attr_exit_code:
             GET_ATTR_exit_code(INTERP, SELF, result);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "handled"))) {
+            break;
+          case attr_handled:
             GET_ATTR_handled(INTERP, SELF, result);
-        }
-        else {
+            break;
+          default:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such integer attribute '%S'", key);
@@ -400,20 +429,20 @@
 
     VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
         STRING * const name = VTABLE_get_string(INTERP, key);
-
-        if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "type"))) {
+        switch (getAttrEnum(INTERP, name)) {
+          case attr_type:
             SET_ATTR_type(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "severity"))) {
+            break;
+          case attr_severity:
             SET_ATTR_severity(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "exit_code"))) {
+            break;
+          case attr_exit_code:
             SET_ATTR_exit_code(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handled"))) {
+            break;
+          case attr_handled:
             SET_ATTR_handled(INTERP, SELF, value);
-        }
-        else {
+            break;
+          default:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such integer attribute '%S'", name);
@@ -431,19 +460,20 @@
 */
 
     VTABLE void set_integer_keyed_str(STRING *key, INTVAL value) {
-        if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "type"))) {
+        switch (getAttrEnum(INTERP, key)) {
+          case attr_type:
             SET_ATTR_type(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "severity"))) {
+            break;
+          case attr_severity:
             SET_ATTR_severity(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "exit_code"))) {
+            break;
+          case attr_exit_code:
             SET_ATTR_exit_code(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, key, CONST_STRING(INTERP, "handled"))) {
+            break;
+          case attr_handled:
             SET_ATTR_handled(INTERP, SELF, value);
-        }
-        else {
+            break;
+          default:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such integer attribute '%S'", key);
@@ -542,47 +572,60 @@
     VTABLE PMC *get_attr_str(STRING *name) {
         PMC *value = PMCNULL;
 
-        if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "type"))) {
-                INTVAL type;
-                GET_ATTR_type(INTERP, SELF, type);
-                value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, type);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "severity"))) {
-                INTVAL severity;
-                GET_ATTR_severity(INTERP, SELF, severity);
-                value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, severity);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "exit_code"))) {
-                INTVAL exit_code;
-                GET_ATTR_exit_code(INTERP, SELF, exit_code);
-                value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, exit_code);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handled"))) {
-                INTVAL handled;
-                GET_ATTR_handled(INTERP, SELF, handled);
-                value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, handled);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "message"))) {
-                STRING *message = SELF.get_string();
-                value = Parrot_pmc_new(INTERP, enum_class_String);
-                VTABLE_set_string_native(INTERP, value, message);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "payload"))) {
-                GET_ATTR_payload(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "resume"))) {
-                GET_ATTR_resume(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "backtrace"))) {
-                GET_ATTR_backtrace(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handler_iter"))) {
-                GET_ATTR_handler_iter(INTERP, SELF, value);
-        }
-        else {
+        switch (getAttrEnum(INTERP, name)) {
+          case attr_type:
+            {
+            INTVAL type;
+            GET_ATTR_type(INTERP, SELF, type);
+            value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, type);
+            }
+            break;
+          case attr_severity:
+            {
+            INTVAL severity;
+            GET_ATTR_severity(INTERP, SELF, severity);
+            value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, severity);
+            }
+            break;
+          case attr_exit_code:
+            {
+            INTVAL exit_code;
+            GET_ATTR_exit_code(INTERP, SELF, exit_code);
+            value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, exit_code);
+            }
+          case attr_handled:
+            {
+            INTVAL handled;
+            GET_ATTR_handled(INTERP, SELF, handled);
+            value = Parrot_pmc_new_init_int(INTERP, enum_class_Integer, handled);
+            }
+            break;
+          case attr_message:
+            {
+            STRING *message = SELF.get_string();
+            value = Parrot_pmc_new(INTERP, enum_class_String);
+            VTABLE_set_string_native(INTERP, value, message);
+            }
+            break;
+          case attr_payload:
+            GET_ATTR_payload(INTERP, SELF, value);
+            break;
+          case attr_resume:
+            GET_ATTR_resume(INTERP, SELF, value);
+            break;
+          case attr_backtrace:
+            GET_ATTR_backtrace(INTERP, SELF, value);
+            break;
+          case attr_handler_iter:
+            GET_ATTR_handler_iter(INTERP, SELF, value);
+            break;
+          case attr_NONE:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such attribute '%S'", name);
+          default:
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
+                "Can't get attribute '%S'", name);
         }
 
         return value;
@@ -599,39 +642,50 @@
 */
     VTABLE void set_attr_str(STRING *name, PMC *value) {
 
-        if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "type"))) {
+        switch (getAttrEnum(INTERP, name)) {
+          case attr_type:
+            {
             const INTVAL type = VTABLE_get_integer(INTERP, value);
             SET_ATTR_type(INTERP, SELF, type);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "severity"))) {
+            }
+            break;
+          case attr_severity:
+            {
             const INTVAL severity = VTABLE_get_integer(INTERP, value);
             SET_ATTR_severity(INTERP, SELF, severity);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "exit_code"))) {
+            }
+            break;
+          case attr_exit_code:
+            {
             const INTVAL exit_code = VTABLE_get_integer(INTERP, value);
             SET_ATTR_exit_code(INTERP, SELF, exit_code);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handled"))) {
+            }
+            break;
+          case attr_handled:
+            {
             const INTVAL handled = VTABLE_get_integer(INTERP, value);
             SET_ATTR_handled(INTERP, SELF, handled);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "message"))) {
+            }
+            break;
+          case attr_message:
+            {
             STRING * const message = VTABLE_get_string(INTERP, value);
             SELF.set_string_native(message);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "payload"))) {
+            }
+            break;
+          case attr_payload:
             SET_ATTR_payload(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "resume"))) {
+            break;
+          case attr_resume:
             SET_ATTR_resume(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "backtrace"))) {
+            break;
+          case attr_backtrace:
             SET_ATTR_backtrace(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "handler_iter"))) {
+            break;
+          case attr_handler_iter:
             SET_ATTR_handler_iter(INTERP, SELF, value);
-        }
-        else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "thrower"))) {
+            break;
+          case attr_thrower:
             /* Ensure it's a ret cont, and extract the from_ctx.
              * XXX TT #596 - when we have Context PMCs, just take and set that. */
             if (!PMC_IS_NULL(value) && VTABLE_isa(INTERP, value, CONST_STRING(INTERP, "Continuation"))) {
@@ -639,13 +693,15 @@
                 if (!PMC_IS_NULL(ctx))
                     SET_ATTR_thrower(INTERP, SELF, ctx);
             }
-        }
-        else {
+            break;
+          case attr_NONE:
             /* If unknown attribute name, throw an exception. */
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ATTRIB_NOT_FOUND,
                 "No such attribute '%S'", name);
+          default:
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
+                "Can't set attribute '%S'", name);
         }
-
     }
 
 /*
@@ -763,6 +819,63 @@
 }
 
 /*
+
+=head2 Auxliary functions
+
+=over 4
+
+=item C<static AttrEnum getAttrEnum(PARROT_INTERP, const STRING *name)>
+
+Gets an enumerated value corresponding to the attribute with that name.
+
+=cut
+
+*/
+
+static AttrEnum
+getAttrEnum(PARROT_INTERP, ARGIN(const STRING *name))
+{
+    ASSERT_ARGS(getAttrEnum)
+
+    AttrEnum r;
+    if (Parrot_str_equal(interp, name, CONST_STRING(interp, "birthime")))
+        r = attr_birthime;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "message")))
+        r = attr_message;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "payload")))
+        r = attr_payload;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "resume")))
+        r = attr_resume;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "severity")))
+        r = attr_severity;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "type")))
+        r = attr_type;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "exit_code")))
+        r = attr_exit_code;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "backtrace")))
+        r = attr_backtrace;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "handled")))
+        r = attr_handled;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "handler_iter")))
+        r = attr_handler_iter;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "handler_ctx")))
+        r = attr_handler_ctx;
+    else if (Parrot_str_equal(interp, name, CONST_STRING(interp, "thrower")))
+        r = attr_thrower;
+    else
+        r = attr_NONE;
+    return r;
+}
+
+/*
+
+=back
+
+=cut
+
+*/
+
+/*
  * Local variables:
  *   c-file-style: "parrot"
  * End:


More information about the parrot-commits mailing list